# Buggy code
def _run_check_unless(self, low_data, cmd_opts):
    """
    Check that unless doesn't return 0, and that onlyif returns a 0.
    """
    ret = {"result": False}

    if not isinstance(low_data["unless"], list):
        low_data_unless = [low_data["unless"]]
    else:
        low_data_unless = low_data["unless"]

    def _check_cmd(cmd):
        if cmd == 0 and ret["result"] is False:
            ret.update(
                {
                    "comment": "unless condition is true",
                    "skip_watch": True,
                    "result": True,
                }
            )
        elif cmd != 0:
            ret.update({"comment": "unless condition is false", "result": False})

    for entry in low_data_unless:
        if isinstance(entry, six.string_types):
            cmd = self.functions["cmd.retcode"](
                entry, ignore_retcode=True, python_shell=True, **cmd_opts
            )
            log.debug("Last command return code: %s", cmd)
            _check_cmd(cmd)
        elif isinstance(entry, dict):
            if "fun" not in entry:
                ret["comment"] = "no `fun` argument in unless: {0}".format(entry)
                log.warning(ret["comment"])
                return ret

            result = self._run_check_function(entry)
            if self.state_con.get("retcode", 0):
                _check_cmd(self.state_con["retcode"])
            elif result:
                ret.update(
                    {
                        "comment": "unless condition is true",
                        "skip_watch": True,
                        "result": True,
                    }
                )
            else:
                ret.update({"comment": "unless condition is false", "result": False})
        else:
            ret.update(
                {
                    "comment": "unless condition is false, bad type passed",
                    "result": False,
                }
            )

    # No reason to stop, return ret
    return ret


# Traceback (KeyError)
----------
ID: zsh_fail_1
Function: cmd.run
Name: echo hey
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/modules/cmdmod.py", line 442, in _run
pwd.getpwnam(runas)
KeyError: "getpwnam(): name not found: 'cristi'"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/state.py", line 1981, in call
**cdata['kwargs'])
File "/usr/lib/python3/dist-packages/salt/loader.py", line 1977, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/salt/states/cmd.py", line 892, in run
cret = mod_run_check(cmd_kwargs, onlyif, unless, creates)
File "/usr/lib/python3/dist-packages/salt/states/cmd.py", line 366, in mod_run_check
cmd = __salt__['cmd.retcode'](unless, ignore_retcode=True, python_shell=True, **cmd_kwargs)
File "/usr/lib/python3/dist-packages/salt/modules/cmdmod.py", line 2277, in retcode
**kwargs)
File "/usr/lib/python3/dist-packages/salt/modules/cmdmod.py", line 445, in _run
'User \'{0}\' is not available'.format(runas)
salt.exceptions.CommandExecutionError: User 'cristi' is not available
Started: 11:52:32.097022
Duration: 9.035 ms
Changes:
----------
ID: zsh_test_1
Function: cmd.run
Name: echo "t1"
Result: True
Comment: Command "echo "t1"" run
Started: 11:52:32.106490
Duration: 37.838 ms
Changes:
----------
pid:
30057
retcode:
0
stderr:
stdout:
t1

# Fixed code
def _run_check_unless(self, low_data, cmd_opts):
    """
    Check that unless doesn't return 0, and that onlyif returns a 0.
    """
    ret = {"result": False}

    if not isinstance(low_data["unless"], list):
        low_data_unless = [low_data["unless"]]
    else:
        low_data_unless = low_data["unless"]

    def _check_cmd(cmd):
        if cmd == 0 and ret["result"] is False:
            ret.update(
                {
                    "comment": "unless condition is true",
                    "skip_watch": True,
                    "result": True,
                }
            )
        elif cmd != 0:
            ret.update({"comment": "unless condition is false", "result": False})

    for entry in low_data_unless:
        if isinstance(entry, six.string_types):
            try:
                cmd = self.functions["cmd.retcode"](
                    entry, ignore_retcode=True, python_shell=True, **cmd_opts
                )
                log.debug("Last command return code: %s", cmd)
            except CommandExecutionError:
                # Command failed, so notify unless to skip the item
                cmd = 0
            _check_cmd(cmd)
        elif isinstance(entry, dict):
            if "fun" not in entry:
                ret["comment"] = "no `fun` argument in unless: {0}".format(entry)
                log.warning(ret["comment"])
                return ret

            result = self._run_check_function(entry)
            if self.state_con.get("retcode", 0):
                _check_cmd(self.state_con["retcode"])
            elif result:
                ret.update(
                    {
                        "comment": "unless condition is true",
                        "skip_watch": True,
                        "result": True,
                    }
                )
            else:
                ret.update({"comment": "unless condition is false", "result": False})
        else:
            ret.update(
                {
                    "comment": "unless condition is false, bad type passed",
                    "result": False,
                }
            )

    # No reason to stop, return ret
    return ret