# Buggy code
def __virtual__():
    """
    Only work on select distros which still use Red Hat's /usr/bin/service for
    management of either sysvinit or a hybrid sysvinit/upstart init system.
    """
    # Enable on these platforms only.
    enable = set(
        (
            "XenServer",
            "RedHat",
            "CentOS",
            "ScientificLinux",
            "CloudLinux",
            "Amazon",
            "Fedora",
            "ALT",
            "OEL",
            "SUSE  Enterprise Server",
            "SUSE",
            "McAfee  OS Server",
        )
    )
    if __grains__["os"] in enable:
        if __grains__["os"] == "XenServer":
            return __virtualname__
        try:
            osrelease = float(__grains__.get("osrelease", 0))
        except ValueError:
            return (
                False,
                "Cannot load rh_service module: "
                "osrelease grain, {0}, not a float,".format(osrelease),
            )
        if __grains__["os"] == "SUSE":
            if osrelease >= 12:
                return (False, "Cannot load rh_service module on SUSE >= 12")
        if __grains__["os"] == "Fedora":
            if osrelease > 15:
                return (False, "Cannot load rh_service module on Fedora >= 15")
        if __grains__["os"] in ("RedHat", "CentOS", "ScientificLinux", "OEL"):
            if osrelease >= 7:
                return (False, "Cannot load rh_service module on RedHat >= 7")
        return __virtualname__
    return (False, "Cannot load rh_service module: OS not in {0}".format(enable))


# Traceback (KeyError)
2016-03-02 16:15:03,851 [salt.loader                                            ][ERROR   ][15417] Exception raised when processing __virtual__ function for rh_service. Module will not be loaded local variable 'osrelease' referenced before assignment
2016-03-02 16:15:03,851 [salt.loader                                            ][WARNING ][15417] salt.loaded.int.module.rh_service.__virtual__() is wrongly returning `None`. It should either return `True`, `False` or a new name. If you're the developer of the module 'rh_service', please fix this.
2016-03-02 16:15:04,708 [salt.state                                                  ][ERROR   ][15417] An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib64/python2.6/site-packages/salt/state.py", line 1624, in call
**cdata['kwargs'])
File "/usr/lib64/python2.6/site-packages/salt/loader.py", line 1491, in wrapper
return f(*args, **kwargs)
File "/usr/lib64/python2.6/site-packages/salt/states/service.py", line 296, in running
before_toggle_enable_status = __salt__['service.enabled'](name)
File "/usr/lib64/python2.6/site-packages/salt/loader.py", line 900, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib64/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'service.enabled'

2016-03-02 16:15:05,430 [salt.state                                                  ][ERROR   ][15417] An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib64/python2.6/site-packages/salt/state.py", line 1624, in call
**cdata['kwargs'])
File "/usr/lib64/python2.6/site-packages/salt/loader.py", line 1491, in wrapper
return f(*args, **kwargs)
File "/usr/lib64/python2.6/site-packages/salt/states/service.py", line 296, in running
before_toggle_enable_status = __salt__['service.enabled'](name)
File "/usr/lib64/python2.6/site-packages/salt/loader.py", line 900, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/usr/lib64/python2.6/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'service.enabled'

# Fixed code
def __virtual__():
    """
    Only work on select distros which still use Red Hat's /usr/bin/service for
    management of either sysvinit or a hybrid sysvinit/upstart init system.
    """
    # Enable on these platforms only.
    enable = set(
        (
            "XenServer",
            "RedHat",
            "CentOS",
            "ScientificLinux",
            "CloudLinux",
            "Amazon",
            "Fedora",
            "ALT",
            "OEL",
            "SUSE  Enterprise Server",
            "SUSE",
            "McAfee  OS Server",
        )
    )
    if __grains__["os"] in enable:
        if __grains__["os"] == "XenServer":
            return __virtualname__

        if __grains__["os"] == "SUSE":
            if str(__grains__["osrelease"]).startswith("11"):
                return __virtualname__
            else:
                return (False, "Cannot load rh_service module on SUSE > 11")

        try:
            osrelease = float(__grains__.get("osrelease", 0))
        except ValueError:
            return (
                False,
                "Cannot load rh_service module: "
                "osrelease grain, {0}, not a float,".format(osrelease),
            )

        if __grains__["os"] == "Fedora":
            if osrelease > 15:
                return (False, "Cannot load rh_service module on Fedora >= 15")
        if __grains__["os"] in ("RedHat", "CentOS", "ScientificLinux", "OEL"):
            if osrelease >= 7:
                return (False, "Cannot load rh_service module on RedHat >= 7")
        return __virtualname__
    return (False, "Cannot load rh_service module: OS not in {0}".format(enable))