test_module.py (salt-3002.1) | : | test_module.py (salt-3002.2) | ||
---|---|---|---|---|
# -*- coding: utf-8 -*- | ||||
""" | """ | |||
:codeauthor: Nicole Thomas (nicole@saltstack.com) | :codeauthor: Nicole Thomas (nicole@saltstack.com) | |||
""" | """ | |||
# Import Python Libs | ||||
from __future__ import absolute_import, print_function, unicode_literals | ||||
import logging | import logging | |||
from inspect import ArgSpec | from inspect import ArgSpec | |||
# Import Salt Libs | ||||
import salt.states.module as module | import salt.states.module as module | |||
# Import Salt Testing Libs | ||||
from tests.support.mixins import LoaderModuleMockMixin | from tests.support.mixins import LoaderModuleMockMixin | |||
from tests.support.mock import MagicMock, patch | from tests.support.mock import MagicMock, patch | |||
from tests.support.unit import TestCase | from tests.support.unit import TestCase | |||
log = logging.getLogger(__name__) | log = logging.getLogger(__name__) | |||
CMD = "foo.bar" | CMD = "foo.bar" | |||
STATE_APPLY_RET = { | STATE_APPLY_RET = { | |||
"module_|-test2_|-state.apply_|-run": { | "module_|-test2_|-state.apply_|-run": { | |||
skipping to change at line 130 | skipping to change at line 123 | |||
def test_run_module_not_available(self): | def test_run_module_not_available(self): | |||
""" | """ | |||
Tests the return of module.run state when the module function is not ava ilable. | Tests the return of module.run state when the module function is not ava ilable. | |||
:return: | :return: | |||
""" | """ | |||
with patch.dict(module.__salt__, {}, clear=True), patch.dict( | with patch.dict(module.__salt__, {}, clear=True), patch.dict( | |||
module.__opts__, {"use_superseded": ["module.run"]} | module.__opts__, {"use_superseded": ["module.run"]} | |||
): | ): | |||
ret = module.run(**{CMD: None}) | ret = module.run(**{CMD: None}) | |||
if ret["comment"] != "Unavailable function: {0}.".format(CMD) or ret["re | if ret["comment"] != "Unavailable function: {}.".format(CMD) or ret["res | |||
sult"]: | ult"]: | |||
self.fail("module.run did not fail as expected: {0}".format(ret)) | self.fail("module.run did not fail as expected: {}".format(ret)) | |||
def test_module_run_hidden_varargs(self): | def test_module_run_hidden_varargs(self): | |||
""" | """ | |||
Tests the return of module.run state when hidden varargs are used with | Tests the return of module.run state when hidden varargs are used with | |||
wrong type. | wrong type. | |||
""" | """ | |||
with patch( | with patch( | |||
"salt.utils.args.get_function_argspec", MagicMock(return_value=self. bspec) | "salt.utils.args.get_function_argspec", MagicMock(return_value=self. bspec) | |||
): | ): | |||
ret = module._run(CMD, m_names="anyname") | ret = module._run(CMD, m_names="anyname") | |||
skipping to change at line 154 | skipping to change at line 147 | |||
def test_run_testmode(self): | def test_run_testmode(self): | |||
""" | """ | |||
Tests the return of the module.run state when test=True is passed. | Tests the return of the module.run state when test=True is passed. | |||
:return: | :return: | |||
""" | """ | |||
with patch.dict( | with patch.dict( | |||
module.__opts__, {"test": True, "use_superseded": ["module.run"]} | module.__opts__, {"test": True, "use_superseded": ["module.run"]} | |||
): | ): | |||
ret = module.run(**{CMD: None}) | ret = module.run(**{CMD: None}) | |||
if ( | if ( | |||
ret["comment"] != "Function {0} to be executed.".format(CMD) | ret["comment"] != "Function {} to be executed.".format(CMD) | |||
or not ret["result"] | or not ret["result"] | |||
): | ): | |||
self.fail("module.run failed: {0}".format(ret)) | self.fail("module.run failed: {}".format(ret)) | |||
def test_run_missing_arg(self): | def test_run_missing_arg(self): | |||
""" | """ | |||
Tests the return of module.run state when arguments are missing | Tests the return of module.run state when arguments are missing | |||
:return: | :return: | |||
""" | """ | |||
with patch.dict(module.__salt__, {CMD: _mocked_func_named}), patch.dict( | with patch.dict(module.__salt__, {CMD: _mocked_func_named}), patch.dict( | |||
module.__opts__, {"use_superseded": ["module.run"]} | module.__opts__, {"use_superseded": ["module.run"]} | |||
): | ): | |||
ret = module.run(**{CMD: None}) | ret = module.run(**{CMD: None}) | |||
skipping to change at line 181 | skipping to change at line 174 | |||
def test_run_correct_arg(self): | def test_run_correct_arg(self): | |||
""" | """ | |||
Tests the return of module.run state when arguments are correct | Tests the return of module.run state when arguments are correct | |||
:return: | :return: | |||
""" | """ | |||
with patch.dict(module.__salt__, {CMD: _mocked_func_named}), patch.dict( | with patch.dict(module.__salt__, {CMD: _mocked_func_named}), patch.dict( | |||
module.__opts__, {"use_superseded": ["module.run"]} | module.__opts__, {"use_superseded": ["module.run"]} | |||
): | ): | |||
ret = module.run(**{CMD: ["Fred"]}) | ret = module.run(**{CMD: ["Fred"]}) | |||
if ret["comment"] != "{0}: Success".format(CMD) or not ret["result"]: | if ret["comment"] != "{}: Success".format(CMD) or not ret["result"]: | |||
self.fail("module.run failed: {0}".format(ret)) | self.fail("module.run failed: {}".format(ret)) | |||
def test_run_state_apply_result_false(self): | def test_run_state_apply_result_false(self): | |||
""" | """ | |||
Tests the 'result' of module.run that calls state.apply execution module | Tests the 'result' of module.run that calls state.apply execution module | |||
:return: | :return: | |||
""" | """ | |||
with patch.dict( | with patch.dict( | |||
module.__salt__, {"state.apply": MagicMock(return_value=STATE_APPLY_ RET)} | module.__salt__, {"state.apply": MagicMock(return_value=STATE_APPLY_ RET)} | |||
), patch.dict(module.__opts__, {"use_deprecated": ["module.run"]}): | ), patch.dict(module.__opts__, {"use_deprecated": ["module.run"]}): | |||
ret = module.run(**{"name": "state.apply", "mods": "test2"}) | ret = module.run(**{"name": "state.apply", "mods": "test2"}) | |||
self.assertFalse(ret["result"]) | self.assertFalse(ret["result"]) | |||
def test_run_service_status_dead(self): | ||||
""" | ||||
Tests the 'result' of module.run that calls service.status when | ||||
service is dead or does not exist | ||||
""" | ||||
func = "service.status" | ||||
with patch.dict( | ||||
module.__salt__, {func: MagicMock(return_value=False)} | ||||
), patch.dict(module.__opts__, {"use_superseded": ["module.run"]}): | ||||
ret = module.run( | ||||
**{ | ||||
"name": "test_service_state", | ||||
"service.status": {"name": "doesnotexist"}, | ||||
} | ||||
) | ||||
self.assertEqual( | ||||
ret, | ||||
{ | ||||
"name": ["service.status"], | ||||
"changes": {}, | ||||
"comment": "'service.status': False", | ||||
"result": False, | ||||
}, | ||||
) | ||||
def test_run_unexpected_keywords(self): | def test_run_unexpected_keywords(self): | |||
with patch.dict(module.__salt__, {CMD: _mocked_func_args}), patch.dict( | with patch.dict(module.__salt__, {CMD: _mocked_func_args}), patch.dict( | |||
module.__opts__, {"use_superseded": ["module.run"]} | module.__opts__, {"use_superseded": ["module.run"]} | |||
): | ): | |||
ret = module.run(**{CMD: [{"foo": "bar"}]}) | ret = module.run(**{CMD: [{"foo": "bar"}]}) | |||
module_function = module.__salt__[CMD].__name__ | module_function = module.__salt__[CMD].__name__ | |||
self.assertEqual( | self.assertEqual( | |||
ret["comment"], | ret["comment"], | |||
( | ( | |||
"'{0}' failed: {1}() got an unexpected keyword argument " | "'{}' failed: {}() got an unexpected keyword argument " | |||
"'foo'".format(CMD, module_function) | "'foo'".format(CMD, module_function) | |||
), | ), | |||
) | ) | |||
self.assertFalse(ret["result"]) | self.assertFalse(ret["result"]) | |||
def test_run_args(self): | def test_run_args(self): | |||
""" | """ | |||
Test unnamed args. | Test unnamed args. | |||
:return: | :return: | |||
""" | """ | |||
skipping to change at line 293 | skipping to change at line 311 | |||
{"a": "b"}, | {"a": "b"}, | |||
{}, | {}, | |||
True, | True, | |||
False, | False, | |||
]: | ]: | |||
with patch.dict(module.__salt__, {CMD: _mocked_none_return}), patch. dict( | with patch.dict(module.__salt__, {CMD: _mocked_none_return}), patch. dict( | |||
module.__opts__, {"use_superseded": ["module.run"]} | module.__opts__, {"use_superseded": ["module.run"]} | |||
): | ): | |||
log.debug("test_run_typed_return: trying %s", val) | log.debug("test_run_typed_return: trying %s", val) | |||
ret = module.run(**{CMD: [{"ret": val}]}) | ret = module.run(**{CMD: [{"ret": val}]}) | |||
self.assertTrue(ret["result"]) | if val is False: | |||
self.assertFalse(ret["result"]) | ||||
self.assertEqual(ret["comment"], "'foo.bar': False") | ||||
else: | ||||
self.assertTrue(ret["result"]) | ||||
def test_run_batch_call(self): | def test_run_batch_call(self): | |||
""" | """ | |||
Test batch call | Test batch call | |||
:return: | :return: | |||
""" | """ | |||
with patch.dict( | with patch.dict( | |||
module.__opts__, {"use_superseded": ["module.run"]} | module.__opts__, {"use_superseded": ["module.run"]} | |||
), patch.dict( | ), patch.dict( | |||
module.__salt__, | module.__salt__, | |||
skipping to change at line 325 | skipping to change at line 347 | |||
def test_module_run_module_not_available(self): | def test_module_run_module_not_available(self): | |||
""" | """ | |||
Tests the return of module.run state when the module function | Tests the return of module.run state when the module function | |||
name isn't available | name isn't available | |||
""" | """ | |||
with patch.dict(module.__salt__, {}, clear=True): | with patch.dict(module.__salt__, {}, clear=True): | |||
ret = module._run(CMD) | ret = module._run(CMD) | |||
self.assertFalse(ret["result"]) | self.assertFalse(ret["result"]) | |||
self.assertEqual( | self.assertEqual( | |||
ret["comment"], "Module function {0} is not available".format(CMD) | ret["comment"], "Module function {} is not available".format(CMD) | |||
) | ) | |||
def test_module_run_test_true(self): | def test_module_run_test_true(self): | |||
""" | """ | |||
Tests the return of module.run state when test=True is passed in | Tests the return of module.run state when test=True is passed in | |||
""" | """ | |||
with patch.dict(module.__opts__, {"test": True}): | with patch.dict(module.__opts__, {"test": True}): | |||
ret = module._run(CMD) | ret = module._run(CMD) | |||
self.assertEqual( | self.assertEqual( | |||
ret["comment"], "Module function {0} is set to execute".format(CMD) | ret["comment"], "Module function {} is set to execute".format(CMD) | |||
) | ) | |||
def test_module_run_missing_arg(self): | def test_module_run_missing_arg(self): | |||
""" | """ | |||
Tests the return of module.run state when arguments are missing | Tests the return of module.run state when arguments are missing | |||
""" | """ | |||
with patch( | with patch( | |||
"salt.utils.args.get_function_argspec", MagicMock(return_value=self. aspec) | "salt.utils.args.get_function_argspec", MagicMock(return_value=self. aspec) | |||
): | ): | |||
ret = module._run(CMD) | ret = module._run(CMD) | |||
End of changes. 13 change blocks. | ||||
18 lines changed or deleted | 40 lines changed or added |