def test_script(self): """ Test to download a script and execute it with specified arguments. """ name = "cmd.script" ret = {"name": name, "result": False, "changes": {}, "comment": ""} with patch.dict(cmd.__opts__, {"test": False}): comt = "Invalidly-formatted 'env' parameter. See documentation." ret.update({"comment": comt}) self.assertDictEqual(cmd.script(name, env="salt"), ret) with patch.dict(cmd.__grains__, {"shell": "shell"}): with patch.dict(cmd.__opts__, {"test": True}): comt = "Command 'cmd.script' would have been executed" ret.update({"comment": comt, "result": None, "changes": {}}) self.assertDictEqual(cmd.script(name), ret) with patch.dict(cmd.__opts__, {"test": False}): mock = MagicMock(side_effect=[CommandExecutionError, {"retcode": 1}]) with patch.dict(cmd.__salt__, {"cmd.script": mock}): ret.update({"comment": "", "result": False}) self.assertDictEqual(cmd.script(name), ret) ret.update( { "comment": "Command 'cmd.script' run", "result": False, "changes": {"retcode": 1}, } ) self.assertDictEqual(cmd.script(name), ret)
def test_script(self): ''' Test to download a script and execute it with specified arguments. ''' name = 'cmd.script' ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} with patch.dict(cmd.__opts__, {'test': False}): comt = ("Invalidly-formatted 'env' parameter. See documentation.") ret.update({'comment': comt}) self.assertDictEqual(cmd.script(name, env='salt'), ret) with patch.dict(cmd.__grains__, {'shell': 'shell'}): with patch.dict(cmd.__opts__, {'test': True}): comt = ("Command 'cmd.script' would have been executed") ret.update({'comment': comt, 'result': None, 'changes': {}}) self.assertDictEqual(cmd.script(name), ret) with patch.dict(cmd.__opts__, {'test': False}): mock = MagicMock( side_effect=[CommandExecutionError, { 'retcode': 1 }]) with patch.dict(cmd.__salt__, {'cmd.script': mock}): ret.update({'comment': '', 'result': False}) self.assertDictEqual(cmd.script(name), ret) ret.update({ 'comment': "Command 'cmd.script' run", 'result': False, 'changes': { 'retcode': 1 } }) self.assertDictEqual(cmd.script(name), ret) mock = MagicMock(return_value=1) with patch.dict(cmd.__salt__, {'cmd.retcode': mock}): with patch.dict(cmd.__opts__, {'test': False}): comt = ('onlyif condition is false') ret.update({ 'comment': comt, 'result': True, 'skip_watch': True, 'changes': {} }) self.assertDictEqual(cmd.script(name, onlyif=''), ret)
def test_script(self): ''' Test to download a script and execute it with specified arguments. ''' name = 'cmd.script' ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} with patch.dict(cmd.__opts__, {'test': False}): comt = ("Invalidly-formatted 'env' parameter. See documentation.") ret.update({'comment': comt}) self.assertDictEqual(cmd.script(name, env='salt'), ret) with patch.dict(cmd.__grains__, {'shell': 'shell'}): with patch.dict(cmd.__opts__, {'test': True}): comt = ("Command 'cmd.script' would have been executed") ret.update({'comment': comt, 'result': None, 'changes': {}}) self.assertDictEqual(cmd.script(name), ret) with patch.dict(cmd.__opts__, {'test': False}): mock = MagicMock(side_effect=[CommandExecutionError, {'retcode': 1}]) with patch.dict(cmd.__salt__, {'cmd.script': mock}): ret.update({'comment': '', 'result': False}) self.assertDictEqual(cmd.script(name), ret) ret.update({'comment': "Command 'cmd.script' run", 'result': False, 'changes': {'retcode': 1}}) self.assertDictEqual(cmd.script(name), ret) mock = MagicMock(return_value=1) with patch.dict(cmd.__salt__, {'cmd.retcode': mock}): with patch.dict(cmd.__opts__, {'test': False}): comt = ('onlyif execution failed') ret.update({'comment': comt, 'result': True, 'skip_watch': True, 'changes': {}}) self.assertDictEqual(cmd.script(name, onlyif=''), ret)
def test_script_runas_password(): """ Test to download a script and execute it, specifically on windows. Make sure that when we do supply a password, we get into the function call. """ name = "cmd.script" ret = {"changes": {}, "comment": "", "name": "cmd.script", "result": False} patch_opts = patch.dict(cmd.__opts__, {"test": False}) patch_shell = patch.dict(cmd.__grains__, {"shell": "shell"}) # Fake the execution module call mock = MagicMock(side_effect=[CommandExecutionError, {"retcode": 1}]) patch_call = patch.dict(cmd.__salt__, {"cmd.script": mock}) with patch_opts, patch_shell, patch_call: assert cmd.script(name, runas="User", password="******") == ret
def test_script_runas_no_password(): """ Test to download a script and execute it, specifically on windows. Make sure that when using runas, and a password is not supplied we check. """ name = "cmd.script" ret = { "name": "cmd.script", "changes": {}, "result": False, "comment": "Must supply a password if runas argument is used on Windows.", } patch_opts = patch.dict(cmd.__opts__, {"test": False}) patch_shell = patch.dict(cmd.__grains__, {"shell": "shell"}) # Fake the execution module call mock = MagicMock(side_effect=[CommandExecutionError, {"retcode": 1}]) patch_call = patch.dict(cmd.__salt__, {"cmd.script": mock}) with patch_opts, patch_shell, patch_call: assert cmd.script(name, runas="User") == ret