123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- # -*- coding: utf-8 -*-
- '''
- :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
- '''
- # Import Python Libs
- from __future__ import absolute_import, unicode_literals, print_function
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.unit import TestCase
- from tests.support.mock import (
- patch,
- )
- # Import Salt Libs
- import salt.modules.sysmod as sysmod
- class MockDocstringable(object):
- def __init__(self, docstr):
- self.__doc__ = docstr
- def set_module_docstring(self, docstr):
- self.__globals__ = {'__doc__': docstr}
- class Mockstate(object):
- """
- Mock of State
- """
- class State(object):
- """
- Mock state functions
- """
- states = {}
- def __init__(self, opts):
- pass
- class Mockrunner(object):
- """
- Mock of runner
- """
- class Runner(object):
- """
- Mock runner functions
- """
- def __init__(self, opts):
- pass
- @property
- def functions(self):
- return sysmod.__salt__
- class Mockloader(object):
- """
- Mock of loader
- """
- functions = [] # ? does not have any effect on existing tests
- def __init__(self):
- pass
- def returners(self, opts, lst):
- """
- Mock returner functions
- """
- return sysmod.__salt__
- def render(self, opts, lst):
- """
- Mock renderers
- """
- return sysmod.__salt__ # renderers do not have '.'s; but whatever. This is for convenience
- class SysmodTestCase(TestCase, LoaderModuleMockMixin):
- '''
- Test cases for salt.modules.sysmod
- '''
- def setup_loader_modules(self):
- return {sysmod: {'__salt__': self.salt_dunder}}
- @classmethod
- def setUpClass(cls):
- cls._modules = set()
- cls._functions = [
- 'exist.exist',
- 'sys.doc', 'sys.list_functions', 'sys.list_modules',
- 'sysctl.get', 'sysctl.show',
- 'system.halt', 'system.reboot',
- 'udev.name', 'udev.path',
- 'user.add', 'user.info', 'user.rename',
- ]
- cls._docstrings = {}
- cls._statedocstrings = {}
- cls.salt_dunder = {}
- for func in cls._functions:
- docstring = 'docstring for {0}'.format(func)
- cls.salt_dunder[func] = MockDocstringable(docstring)
- cls._docstrings[func] = docstring
- module = func.split('.')[0]
- cls._statedocstrings[func] = docstring
- cls._statedocstrings[module] = 'docstring for {0}'.format(module)
- cls._modules.add(func.split('.')[0])
- docstring = 'docstring for {0}'.format(func)
- mock = MockDocstringable(docstring)
- mock.set_module_docstring('docstring for {0}'.format(func.split('.')[0]))
- Mockstate.State.states[func] = mock
- cls._modules = sorted(list(cls._modules))
- cls.state_patcher = patch('salt.state', Mockstate())
- cls.state_patcher.start()
- cls.runner_patcher = patch('salt.runner', Mockrunner())
- cls.runner_patcher.start()
- cls.loader_patcher = patch('salt.loader', Mockloader())
- cls.loader_patcher.start()
- # 'doc' function tests: 2
- @classmethod
- def tearDownClass(cls):
- cls.runner_patcher.stop()
- cls.state_patcher.stop()
- cls.loader_patcher.stop()
- for attrname in ('_modules', '_functions', '_docstrings', '_statedocstrings', 'salt_dunder',
- 'runner_patcher', 'state_patcher', 'loader_patcher'):
- try:
- delattr(cls, attrname)
- except AttributeError:
- continue
- def test_doc(self):
- '''
- Test if it returns the docstrings for all modules.
- '''
- self.assertDictEqual(sysmod.doc(), self._docstrings)
- self.assertDictEqual(sysmod.doc('sys.doc'), {'sys.doc': 'docstring for sys.doc'})
- # 'state_doc' function tests: 2
- def test_state_doc(self):
- '''
- Test if it returns the docstrings for all states.
- '''
- self.assertDictEqual(sysmod.state_doc(), self._statedocstrings)
- self.assertDictEqual(sysmod.state_doc('sys.doc'), {'sys': 'docstring for sys', 'sys.doc': 'docstring for sys.doc'})
- # 'runner_doc' function tests: 2
- def test_runner_doc(self):
- '''
- Test if it returns the docstrings for all runners.
- '''
- self.assertDictEqual(sysmod.runner_doc(), self._docstrings)
- self.assertDictEqual(sysmod.runner_doc('sys.doc'), {'sys.doc': 'docstring for sys.doc'})
- # 'returner_doc' function tests: 2
- def test_returner_doc(self):
- '''
- Test if it returns the docstrings for all returners.
- '''
- self.assertDictEqual(sysmod.returner_doc(), self._docstrings)
- self.assertDictEqual(sysmod.returner_doc('sys.doc'), {'sys.doc': 'docstring for sys.doc'})
- # 'renderer_doc' function tests: 2
- def test_renderer_doc(self):
- '''
- Test if it returns the docstrings for all renderers.
- '''
- self.assertDictEqual(sysmod.renderer_doc(), self._docstrings)
- self.assertDictEqual(sysmod.renderer_doc('sys.doc'), {'sys.doc': 'docstring for sys.doc'})
- # 'list_functions' function tests: 7
- def test_list_functions(self):
- '''
- Test if it lists the functions for all modules.
- '''
- self.assertListEqual(sysmod.list_functions(), self._functions)
- self.assertListEqual(sysmod.list_functions('nonexist'), [])
- # list all functions in/given a specific module
- self.assertListEqual(sysmod.list_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules'])
- # globs can be used for both module names and function names:
- self.assertListEqual(sysmod.list_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot'])
- self.assertListEqual(sysmod.list_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules'])
- # "list", or check for a specific function:
- self.assertListEqual(sysmod.list_functions('sys.list'), [])
- self.assertListEqual(sysmod.list_functions('exist.exist'), ['exist.exist'])
- # 'list_modules' function tests: 4
- def test_list_modules(self):
- '''
- Test if it lists the modules loaded on the minion
- '''
- self.assertListEqual(sysmod.list_modules(), self._modules)
- self.assertListEqual(sysmod.list_modules('nonexist'), [])
- self.assertListEqual(sysmod.list_modules('user'), ['user'])
- self.assertListEqual(sysmod.list_modules('s*'), ['sys', 'sysctl', 'system'])
- # 'reload_modules' function tests: 1
- def test_reload_modules(self):
- '''
- Test if it tell the minion to reload the execution modules
- '''
- self.assertTrue(sysmod.reload_modules())
- # 'argspec' function tests: 1
- def test_argspec(self):
- '''
- Test if it return the argument specification
- of functions in Salt execution modules.
- '''
- self.assertDictEqual(sysmod.argspec(), {})
- # 'state_argspec' function tests: 1
- def test_state_argspec(self):
- '''
- Test if it return the argument specification
- of functions in Salt state modules.
- '''
- self.assertDictEqual(sysmod.state_argspec(), {})
- # 'returner_argspec' function tests: 1
- def test_returner_argspec(self):
- '''
- Test if it return the argument specification
- of functions in Salt returner modules.
- '''
- self.assertDictEqual(sysmod.returner_argspec(), {})
- # 'runner_argspec' function tests: 1
- def test_runner_argspec(self):
- '''
- Test if it return the argument specification of functions in Salt runner
- modules.
- '''
- self.assertDictEqual(sysmod.runner_argspec(), {})
- # 'list_state_functions' function tests: 7
- def test_list_state_functions(self):
- '''
- Test if it lists the functions for all state modules.
- '''
- self.assertListEqual(sysmod.list_state_functions(), self._functions)
- self.assertListEqual(sysmod.list_state_functions('nonexist'), [])
- # list all functions in/given a specific module
- self.assertListEqual(sysmod.list_state_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules'])
- # globs can be used for both module names and function names:
- self.assertListEqual(sysmod.list_state_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot'])
- self.assertListEqual(sysmod.list_state_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules'])
- # "list", or check for a specific function:
- self.assertListEqual(sysmod.list_state_functions('sys.list'), [])
- self.assertListEqual(sysmod.list_state_functions('exist.exist'), ['exist.exist'])
- # 'list_state_modules' function tests: 4
- def test_list_state_modules(self):
- '''
- Test if it lists the modules loaded on the minion.
- '''
- self.assertListEqual(sysmod.list_state_modules(), self._modules)
- self.assertListEqual(sysmod.list_state_modules('nonexist'), [])
- self.assertListEqual(sysmod.list_state_modules('user'), ['user'])
- self.assertListEqual(sysmod.list_state_modules('s*'), ['sys', 'sysctl', 'system'])
- # 'list_runners' function tests: 4
- def test_list_runners(self):
- '''
- Test if it list the runners loaded on the minion.
- '''
- self.assertListEqual(sysmod.list_runners(), self._modules)
- self.assertListEqual(sysmod.list_runners('nonexist'), [])
- self.assertListEqual(sysmod.list_runners('user'), ['user'])
- self.assertListEqual(sysmod.list_runners('s*'), ['sys', 'sysctl', 'system'])
- # 'list_runner_functions' function tests: 7
- def test_list_runner_functions(self):
- '''
- Test if it lists the functions for all runner modules.
- '''
- self.assertListEqual(sysmod.list_runner_functions(), self._functions)
- self.assertListEqual(sysmod.list_runner_functions('nonexist'), [])
- # list all functions in/given a specific module
- self.assertListEqual(sysmod.list_runner_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules'])
- # globs can be used for both module names and function names:
- self.assertListEqual(sysmod.list_runner_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot'])
- self.assertListEqual(sysmod.list_runner_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules'])
- # "list", or check for a specific function:
- self.assertListEqual(sysmod.list_runner_functions('sys.list'), [])
- self.assertListEqual(sysmod.list_runner_functions('exist.exist'), ['exist.exist'])
- # 'list_returners' function tests: 4
- def test_list_returners(self):
- '''
- Test if it lists the returners loaded on the minion
- '''
- self.assertListEqual(sysmod.list_returners(), self._modules)
- self.assertListEqual(sysmod.list_returners('nonexist'), [])
- self.assertListEqual(sysmod.list_returners('user'), ['user'])
- self.assertListEqual(sysmod.list_returners('s*'), ['sys', 'sysctl', 'system'])
- # 'list_returner_functions' function tests: 7
- def test_list_returner_functions(self):
- '''
- Test if it lists the functions for all returner modules.
- '''
- self.assertListEqual(sysmod.list_returner_functions(), self._functions)
- self.assertListEqual(sysmod.list_returner_functions('nonexist'), [])
- # list all functions in/given a specific module
- self.assertListEqual(sysmod.list_returner_functions('sys'), ['sys.doc', 'sys.list_functions', 'sys.list_modules'])
- # globs can be used for both module names and function names:
- self.assertListEqual(sysmod.list_returner_functions('sys*'), ['sys.doc', 'sys.list_functions', 'sys.list_modules', 'sysctl.get', 'sysctl.show', 'system.halt', 'system.reboot'])
- self.assertListEqual(sysmod.list_returner_functions('sys.list*'), ['sys.list_functions', 'sys.list_modules'])
- # "list", or check for a specific function:
- self.assertListEqual(sysmod.list_returner_functions('sys.list'), [])
- self.assertListEqual(sysmod.list_returner_functions('exist.exist'), ['exist.exist'])
- # 'list_renderers' function tests: 4
- def test_list_renderers(self):
- '''
- Test if it list the renderers loaded on the minion.
- '''
- self.assertListEqual(sysmod.list_renderers(), self._functions)
- self.assertListEqual(sysmod.list_renderers('nonexist'), [])
- self.assertListEqual(sysmod.list_renderers('user.info'), ['user.info'])
- self.assertListEqual(sysmod.list_renderers('syst*'), ['system.halt', 'system.reboot'])
|