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
|
|
|
|
from salt.states import module
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
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'
|
|
|
|
MOCK = MagicMock()
|
|
|
|
module.__salt__ = {CMD: MOCK}
|
|
|
|
module.__opts__ = {'test': False}
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
2014-10-24 14:56:40 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
class ModuleStateTest(TestCase):
|
|
|
|
'''
|
|
|
|
Tests module state (salt/states/module.py)
|
|
|
|
'''
|
|
|
|
|
2014-10-24 21:28:03 +00:00
|
|
|
aspec = ArgSpec(args=['hello', 'world'],
|
|
|
|
varargs=None,
|
|
|
|
keywords=None,
|
|
|
|
defaults=False)
|
|
|
|
|
2017-03-08 10:49:48 +00:00
|
|
|
def test_xrun_module_not_available(self):
|
|
|
|
'''
|
|
|
|
Tests the return of module.xrun state when the module function is not available.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__salt__, {}, clear=True):
|
2017-03-08 11:02:58 +00:00
|
|
|
ret = module.xrun(**{CMD: None})
|
|
|
|
assert ret['comment'] == "Module function '{0}' is not available".format(CMD)
|
2017-03-08 10:49:48 +00:00
|
|
|
assert not ret['result']
|
|
|
|
|
2017-03-08 10:51:38 +00:00
|
|
|
def test_xrun_testmode(self):
|
|
|
|
'''
|
|
|
|
Tests the return of the module.xrun state when test=True is passed.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__opts__, {'test': True}):
|
2017-03-08 11:02:58 +00:00
|
|
|
ret = module.xrun(**{CMD: None})
|
2017-03-08 10:51:38 +00:00
|
|
|
assert ret['comment'] == "Module function '{0}' is set to execute".format(CMD)
|
|
|
|
assert ret['result']
|
|
|
|
|
2017-03-08 11:02:27 +00:00
|
|
|
def test_xrun_missing_arg(self):
|
|
|
|
'''
|
|
|
|
Tests the return of module.xrun state when arguments are missing
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__salt__, {CMD: _mocked_func_named}):
|
|
|
|
ret = module.xrun(**{CMD: None})
|
|
|
|
assert ret['comment'] == "'{0}' failed: Missing arguments: name".format(CMD)
|
|
|
|
|
2017-03-08 12:02:46 +00:00
|
|
|
def test_xrun_correct_arg(self):
|
|
|
|
'''
|
|
|
|
Tests the return of module.xrun state when arguments are correct
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch.dict(module.__salt__, {CMD: _mocked_func_named}):
|
|
|
|
ret = module.xrun(**{CMD: [{'name': 'Fred'}]})
|
|
|
|
assert ret['comment'] == '{0}: Success'.format(CMD)
|
|
|
|
assert ret['result']
|
|
|
|
|
2017-03-08 11:02:27 +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):
|
|
|
|
ret = module.run(CMD)
|
|
|
|
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}):
|
|
|
|
ret = module.run(CMD)
|
|
|
|
comment = 'Module function {0} is set to execute'.format(CMD)
|
|
|
|
self.assertEqual(ret['comment'], comment)
|
|
|
|
|
2014-12-10 16:33:54 +00:00
|
|
|
@patch('salt.utils.args.get_function_argspec', MagicMock(return_value=aspec))
|
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
|
|
|
|
'''
|
|
|
|
ret = module.run(CMD)
|
2017-01-12 21:19:31 +00:00
|
|
|
comment = 'The following arguments are missing:'
|
|
|
|
self.assertIn(comment, ret['comment'])
|
|
|
|
self.assertIn('world', ret['comment'])
|
|
|
|
self.assertIn('hello', ret['comment'])
|