2014-10-24 14:56:40 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Nicole Thomas (nicole@saltstack.com)`
|
|
|
|
'''
|
|
|
|
|
2015-03-18 01:44:30 +00:00
|
|
|
# Import Python Libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2014-10-24 21:28:03 +00:00
|
|
|
from inspect import ArgSpec
|
2014-10-24 14:56:40 +00:00
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.states.module as module
|
2014-10-24 14:56:40 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import (
|
2014-10-24 14:56:40 +00:00
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON,
|
|
|
|
MagicMock,
|
|
|
|
patch
|
|
|
|
)
|
|
|
|
|
|
|
|
CMD = 'foo.bar'
|
|
|
|
|
|
|
|
|
2017-03-08 11:02:27 +00:00
|
|
|
def _mocked_func_named(name, names=('Fred', 'Swen',)):
|
|
|
|
'''
|
|
|
|
Mocked function with named defaults.
|
|
|
|
|
|
|
|
:param name:
|
|
|
|
:param names:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
return {'name': name, 'names': names}
|
|
|
|
|
2017-03-08 12:03:08 +00:00
|
|
|
|
|
|
|
def _mocked_func_args(*args):
|
|
|
|
'''
|
|
|
|
Mocked function with args.
|
|
|
|
|
|
|
|
:param args:
|
|
|
|
:return:
|
|
|
|
'''
|
2017-03-08 14:10:00 +00:00
|
|
|
assert args == ('foo', 'bar')
|
2017-03-08 12:03:08 +00:00
|
|
|
return {'args': args}
|
|
|
|
|
|
|
|
|
2017-03-08 14:34:56 +00:00
|
|
|
def _mocked_none_return(ret=None):
|
2017-03-08 14:25:34 +00:00
|
|
|
'''
|
|
|
|
Mocked function returns None
|
|
|
|
:return:
|
|
|
|
'''
|
2017-03-08 14:34:56 +00:00
|
|
|
return ret
|
2017-03-08 14:25:34 +00:00
|
|
|
|
|
|
|
|
2014-10-24 14:56:40 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class ModuleStateTest(TestCase, LoaderModuleMockMixin):
|
2014-10-24 14:56:40 +00:00
|
|
|
'''
|
|
|
|
Tests module state (salt/states/module.py)
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
2017-02-19 15:35:30 +00:00
|
|
|
return {
|
2017-03-22 12:12:36 +00:00
|
|
|
module: {
|
|
|
|
'__opts__': {'test': False},
|
|
|
|
'__salt__': {CMD: MagicMock()}
|
|
|
|
}
|
2017-02-19 15:35:30 +00:00
|
|
|
}
|
2014-10-24 14:56:40 +00:00
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.aspec = ArgSpec(args=['hello', 'world'],
|
|
|
|
varargs=None,
|
|
|
|
keywords=None,
|
|
|
|
defaults=False)
|
|
|
|
|
2017-06-21 08:27:28 +00:00
|
|
|
cls.bspec = ArgSpec(args=[],
|
|
|
|
varargs='names',
|
|
|
|
keywords='kwargs',
|
|
|
|
defaults=None)
|
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
del cls.aspec
|
2017-06-21 08:27:28 +00:00
|
|
|
del cls.bspec
|
2014-10-24 21:28:03 +00:00
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_module_not_available(self):
|
2017-03-08 10:49:48 +00:00
|
|
|
'''
|
2017-03-13 12:51:34 +00:00
|
|
|
Tests the return of module.run state when the module function is not available.
|
2017-03-08 10:49:48 +00:00
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__salt__, {}, clear=True):
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'use_superseded': ['module.run']}):
|
|
|
|
ret = module.run(**{CMD: None})
|
|
|
|
assert ret['comment'] == "Unavailable function: {0}.".format(CMD)
|
|
|
|
assert not ret['result']
|
2017-03-08 10:49:48 +00:00
|
|
|
|
2017-03-01 14:37:04 +00:00
|
|
|
def test_module_run_hidden_varargs(self):
|
|
|
|
'''
|
|
|
|
Tests the return of module.run state when hidden varargs are used with
|
|
|
|
wrong type.
|
|
|
|
'''
|
2017-06-21 08:27:28 +00:00
|
|
|
with patch('salt.utils.args.get_function_argspec', MagicMock(return_value=self.bspec)):
|
|
|
|
ret = module._run(CMD, m_names='anyname')
|
|
|
|
comment = "'names' must be a list."
|
|
|
|
self.assertEqual(ret['comment'], comment)
|
2017-03-01 14:37:04 +00:00
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_testmode(self):
|
2017-03-08 10:51:38 +00:00
|
|
|
'''
|
2017-03-13 12:51:34 +00:00
|
|
|
Tests the return of the module.run state when test=True is passed.
|
2017-03-08 10:51:38 +00:00
|
|
|
:return:
|
|
|
|
'''
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'test': True, 'use_superseded': ['module.run']}):
|
|
|
|
ret = module.run(**{CMD: None})
|
|
|
|
assert ret['comment'] == "Function {0} to be executed.".format(CMD)
|
2017-03-08 10:51:38 +00:00
|
|
|
assert ret['result']
|
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_missing_arg(self):
|
2017-03-08 11:02:27 +00:00
|
|
|
'''
|
2017-03-13 12:51:34 +00:00
|
|
|
Tests the return of module.run state when arguments are missing
|
2017-03-08 11:02:27 +00:00
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__salt__, {CMD: _mocked_func_named}):
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'use_superseded': ['module.run']}):
|
|
|
|
ret = module.run(**{CMD: None})
|
|
|
|
assert ret['comment'] == "'{0}' failed: Missing arguments: name".format(CMD)
|
2017-03-08 11:02:27 +00:00
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_correct_arg(self):
|
2017-03-08 12:02:46 +00:00
|
|
|
'''
|
2017-03-13 12:51:34 +00:00
|
|
|
Tests the return of module.run state when arguments are correct
|
2017-03-08 12:02:46 +00:00
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__salt__, {CMD: _mocked_func_named}):
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'use_superseded': ['module.run']}):
|
|
|
|
ret = module.run(**{CMD: [{'name': 'Fred'}]})
|
|
|
|
assert ret['comment'] == '{0}: Success'.format(CMD)
|
|
|
|
assert ret['result']
|
2017-03-08 12:02:46 +00:00
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_unexpected_keywords(self):
|
2017-03-08 12:03:08 +00:00
|
|
|
with patch.dict(module.__salt__, {CMD: _mocked_func_args}):
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'use_superseded': ['module.run']}):
|
|
|
|
ret = module.run(**{CMD: [{'foo': 'bar'}]})
|
|
|
|
assert ret['comment'] == "'{0}' failed: {1}() got an unexpected keyword argument " \
|
2017-03-20 14:31:41 +00:00
|
|
|
"'foo'".format(CMD, module.__salt__[CMD].__name__)
|
2017-03-13 12:51:34 +00:00
|
|
|
assert not ret['result']
|
2017-03-08 12:03:08 +00:00
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_args(self):
|
2017-03-08 14:10:25 +00:00
|
|
|
'''
|
|
|
|
Test unnamed args.
|
|
|
|
:return:
|
|
|
|
'''
|
2017-03-08 14:10:00 +00:00
|
|
|
with patch.dict(module.__salt__, {CMD: _mocked_func_args}):
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'use_superseded': ['module.run']}):
|
|
|
|
assert module.run(**{CMD: ['foo', 'bar']})['result']
|
2017-03-08 11:02:27 +00:00
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_none_return(self):
|
2017-03-08 14:25:34 +00:00
|
|
|
'''
|
|
|
|
Test handling of a broken function that returns None.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__salt__, {CMD: _mocked_none_return}):
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'use_superseded': ['module.run']}):
|
|
|
|
assert module.run(**{CMD: None})['result']
|
2017-03-08 14:25:34 +00:00
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_typed_return(self):
|
2017-03-08 14:34:56 +00:00
|
|
|
'''
|
|
|
|
Test handling of a broken function that returns any type.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
for val in [1, 0, 'a', '', (1, 2,), (), [1, 2], [], {'a': 'b'}, {}, True, False]:
|
|
|
|
with patch.dict(module.__salt__, {CMD: _mocked_none_return}):
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'use_superseded': ['module.run']}):
|
|
|
|
assert module.run(**{CMD: [{'ret': val}]})['result']
|
2017-03-08 14:34:56 +00:00
|
|
|
|
2017-03-13 12:51:34 +00:00
|
|
|
def test_run_batch_call(self):
|
2017-03-08 14:40:59 +00:00
|
|
|
'''
|
|
|
|
Test batch call
|
|
|
|
:return:
|
|
|
|
'''
|
2017-03-13 12:51:34 +00:00
|
|
|
with patch.dict(module.__opts__, {'use_superseded': ['module.run']}):
|
|
|
|
with patch.dict(module.__salt__,
|
|
|
|
{'first': _mocked_none_return,
|
|
|
|
'second': _mocked_none_return,
|
|
|
|
'third': _mocked_none_return}, clear=True):
|
|
|
|
for f_name in module.__salt__:
|
|
|
|
assert module.run(**{f_name: None})['result']
|
2017-03-08 14:40:59 +00:00
|
|
|
|
2014-10-24 14:56:40 +00:00
|
|
|
def test_module_run_module_not_available(self):
|
|
|
|
'''
|
|
|
|
Tests the return of module.run state when the module function
|
|
|
|
name isn't available
|
|
|
|
'''
|
2017-03-08 10:49:25 +00:00
|
|
|
with patch.dict(module.__salt__, {}, clear=True):
|
2017-03-13 12:51:34 +00:00
|
|
|
ret = module._run(CMD)
|
2017-03-08 10:49:25 +00:00
|
|
|
comment = 'Module function {0} is not available'.format(CMD)
|
2014-10-24 14:56:40 +00:00
|
|
|
self.assertEqual(ret['comment'], comment)
|
|
|
|
self.assertFalse(ret['result'])
|
|
|
|
|
|
|
|
def test_module_run_test_true(self):
|
|
|
|
'''
|
|
|
|
Tests the return of module.run state when test=True is passed in
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__opts__, {'test': True}):
|
2017-03-13 12:51:34 +00:00
|
|
|
ret = module._run(CMD)
|
2014-10-24 14:56:40 +00:00
|
|
|
comment = 'Module function {0} is set to execute'.format(CMD)
|
|
|
|
self.assertEqual(ret['comment'], comment)
|
|
|
|
|
2014-10-24 21:28:03 +00:00
|
|
|
def test_module_run_missing_arg(self):
|
|
|
|
'''
|
|
|
|
Tests the return of module.run state when arguments are missing
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
with patch('salt.utils.args.get_function_argspec', MagicMock(return_value=self.aspec)):
|
|
|
|
ret = module._run(CMD)
|
|
|
|
comment = 'The following arguments are missing:'
|
|
|
|
self.assertIn(comment, ret['comment'])
|
|
|
|
self.assertIn('world', ret['comment'])
|
|
|
|
self.assertIn('hello', ret['comment'])
|