salt/tests/unit/states/test_cmd.py

308 lines
11 KiB
Python
Raw Normal View History

2015-04-29 12:01:23 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import
import os.path
# Import Salt Testing Libs
2017-03-22 16:42:17 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
from tests.support.mock import (
2015-04-29 12:01:23 +00:00
NO_MOCK,
NO_MOCK_REASON,
MagicMock,
patch)
from salt.exceptions import CommandExecutionError
# Import Salt Libs
import salt.states.cmd as cmd
2015-04-29 12:01:23 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-03-22 16:42:17 +00:00
class CmdTestCase(TestCase, LoaderModuleMockMixin):
2015-04-29 12:01:23 +00:00
'''
Test cases for salt.states.cmd
'''
2017-03-22 16:42:17 +00:00
def setup_loader_modules(self):
return {cmd: {'__env__': 'base'}}
2015-04-29 12:01:23 +00:00
# 'mod_run_check' function tests: 1
def test_mod_run_check(self):
'''
Test to execute the onlyif and unless logic.
'''
cmd_kwargs = {}
creates = '/tmp'
mock = MagicMock(return_value=1)
with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
with patch.dict(cmd.__opts__, {'test': True}):
ret = {'comment': 'onlyif execution failed', 'result': True,
'skip_watch': True}
[develop] Merge forward from 2016.3 to develop (#32636) * Ensure rh_service not used on CloudLinux 7 Add CloudLinux to RHEL-derived distros excluded from rh_service use in osrelease >= 7 * Fix binary search and replace (#32542) * Don't return None from eval_master (#32555) Raise an exception instead. Because eval master if returns should return a tuple. * redact passwords and hashes from user.present updates Fixes #32381 * Better log message on minion restart if master couldn't be reached. (#32576) * Revert PR #32480 and apply #32314 with fixes / documentation (#32558) * Revert "Fix loop in maint.flo" This reverts commit 5196cd6a6e5db3c7b1a47b1740881bbd3e87ea3d. * Revert "Clear VCS fsbackend and git_pillar locks on master start" This reverts commit 7e3caa9bae1ac4de62db9924374e35a8b826937e. * Revert "Add functions to remove VCS fsbackend update locks and git_pillar update/checkout locks" This reverts commit 4c2db32419022501eae2a695ec488693e043d189. * prevent eternal gitfs lock due to process crash * Use salt.utils.fopen() instead of open() * Make pid locking work for more than just gitfs Also, make logging more descriptive, to aid in troubleshooting. * Add git_pillar_global_lock config option default value * Document proper usage of {gitfs,git_pillar}_global_lock * Fix comments value in salt.states.pkgrepo example (#32604) 'comments' option adds '#' automatically. Example contains `#http://mirror.centos.org/centos/$releasever/os/$basearch/` string which becomes prefixed with '##' in generated file. * alphabetize directories for dynamic modules (#32599) Also add engines and proxy minions to the list. * Expand on the open-source vs open-core FAQ * Language clarification. * Fix some mistakes in the salt-ssh thin shell script (#32583) * [[ is bash, not compatible with /bin/sh * check if python command exists before calling it * Deprecate 'user' and 'group' in state cmd (#32613) * Remove unused 'group' argument * Fix unit testing of cmd.mod_run_check without group arg * Deprecate 'user/group' in cmd.run * Deprecate 'user'/'group' in cmd.script * Deprecate 'user' in cmd.wait * Deprecate 'user'/'group' in cmd.wait_script * Fix mod_run_check without 'group' * Push deprecation back one release * Fix mac_service and mac_system modules (#32587) * Fix mac_service module * Add integration tests for new functions * Start will not enable the service beforehand * Remove unused variables
2016-04-18 14:40:20 +00:00
self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, '', '', creates), ret)
2015-04-29 12:01:23 +00:00
[develop] Merge forward from 2016.3 to develop (#32636) * Ensure rh_service not used on CloudLinux 7 Add CloudLinux to RHEL-derived distros excluded from rh_service use in osrelease >= 7 * Fix binary search and replace (#32542) * Don't return None from eval_master (#32555) Raise an exception instead. Because eval master if returns should return a tuple. * redact passwords and hashes from user.present updates Fixes #32381 * Better log message on minion restart if master couldn't be reached. (#32576) * Revert PR #32480 and apply #32314 with fixes / documentation (#32558) * Revert "Fix loop in maint.flo" This reverts commit 5196cd6a6e5db3c7b1a47b1740881bbd3e87ea3d. * Revert "Clear VCS fsbackend and git_pillar locks on master start" This reverts commit 7e3caa9bae1ac4de62db9924374e35a8b826937e. * Revert "Add functions to remove VCS fsbackend update locks and git_pillar update/checkout locks" This reverts commit 4c2db32419022501eae2a695ec488693e043d189. * prevent eternal gitfs lock due to process crash * Use salt.utils.fopen() instead of open() * Make pid locking work for more than just gitfs Also, make logging more descriptive, to aid in troubleshooting. * Add git_pillar_global_lock config option default value * Document proper usage of {gitfs,git_pillar}_global_lock * Fix comments value in salt.states.pkgrepo example (#32604) 'comments' option adds '#' automatically. Example contains `#http://mirror.centos.org/centos/$releasever/os/$basearch/` string which becomes prefixed with '##' in generated file. * alphabetize directories for dynamic modules (#32599) Also add engines and proxy minions to the list. * Expand on the open-source vs open-core FAQ * Language clarification. * Fix some mistakes in the salt-ssh thin shell script (#32583) * [[ is bash, not compatible with /bin/sh * check if python command exists before calling it * Deprecate 'user' and 'group' in state cmd (#32613) * Remove unused 'group' argument * Fix unit testing of cmd.mod_run_check without group arg * Deprecate 'user/group' in cmd.run * Deprecate 'user'/'group' in cmd.script * Deprecate 'user' in cmd.wait * Deprecate 'user'/'group' in cmd.wait_script * Fix mod_run_check without 'group' * Push deprecation back one release * Fix mac_service and mac_system modules (#32587) * Fix mac_service module * Add integration tests for new functions * Start will not enable the service beforehand * Remove unused variables
2016-04-18 14:40:20 +00:00
self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, {}, '', creates), ret)
2015-04-29 12:01:23 +00:00
mock = MagicMock(return_value=1)
with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
with patch.dict(cmd.__opts__, {'test': True}):
ret = {'comment': 'onlyif execution failed: ', 'result': True,
'skip_watch': True}
self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, [''], '', creates), ret)
2015-04-29 12:01:23 +00:00
mock = MagicMock(return_value=0)
with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
ret = {'comment': 'unless execution succeeded', 'result': True,
'skip_watch': True}
[develop] Merge forward from 2016.3 to develop (#32636) * Ensure rh_service not used on CloudLinux 7 Add CloudLinux to RHEL-derived distros excluded from rh_service use in osrelease >= 7 * Fix binary search and replace (#32542) * Don't return None from eval_master (#32555) Raise an exception instead. Because eval master if returns should return a tuple. * redact passwords and hashes from user.present updates Fixes #32381 * Better log message on minion restart if master couldn't be reached. (#32576) * Revert PR #32480 and apply #32314 with fixes / documentation (#32558) * Revert "Fix loop in maint.flo" This reverts commit 5196cd6a6e5db3c7b1a47b1740881bbd3e87ea3d. * Revert "Clear VCS fsbackend and git_pillar locks on master start" This reverts commit 7e3caa9bae1ac4de62db9924374e35a8b826937e. * Revert "Add functions to remove VCS fsbackend update locks and git_pillar update/checkout locks" This reverts commit 4c2db32419022501eae2a695ec488693e043d189. * prevent eternal gitfs lock due to process crash * Use salt.utils.fopen() instead of open() * Make pid locking work for more than just gitfs Also, make logging more descriptive, to aid in troubleshooting. * Add git_pillar_global_lock config option default value * Document proper usage of {gitfs,git_pillar}_global_lock * Fix comments value in salt.states.pkgrepo example (#32604) 'comments' option adds '#' automatically. Example contains `#http://mirror.centos.org/centos/$releasever/os/$basearch/` string which becomes prefixed with '##' in generated file. * alphabetize directories for dynamic modules (#32599) Also add engines and proxy minions to the list. * Expand on the open-source vs open-core FAQ * Language clarification. * Fix some mistakes in the salt-ssh thin shell script (#32583) * [[ is bash, not compatible with /bin/sh * check if python command exists before calling it * Deprecate 'user' and 'group' in state cmd (#32613) * Remove unused 'group' argument * Fix unit testing of cmd.mod_run_check without group arg * Deprecate 'user/group' in cmd.run * Deprecate 'user'/'group' in cmd.script * Deprecate 'user' in cmd.wait * Deprecate 'user'/'group' in cmd.wait_script * Fix mod_run_check without 'group' * Push deprecation back one release * Fix mac_service and mac_system modules (#32587) * Fix mac_service module * Add integration tests for new functions * Start will not enable the service beforehand * Remove unused variables
2016-04-18 14:40:20 +00:00
self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, '', creates), ret)
2015-04-29 12:01:23 +00:00
[develop] Merge forward from 2016.3 to develop (#32636) * Ensure rh_service not used on CloudLinux 7 Add CloudLinux to RHEL-derived distros excluded from rh_service use in osrelease >= 7 * Fix binary search and replace (#32542) * Don't return None from eval_master (#32555) Raise an exception instead. Because eval master if returns should return a tuple. * redact passwords and hashes from user.present updates Fixes #32381 * Better log message on minion restart if master couldn't be reached. (#32576) * Revert PR #32480 and apply #32314 with fixes / documentation (#32558) * Revert "Fix loop in maint.flo" This reverts commit 5196cd6a6e5db3c7b1a47b1740881bbd3e87ea3d. * Revert "Clear VCS fsbackend and git_pillar locks on master start" This reverts commit 7e3caa9bae1ac4de62db9924374e35a8b826937e. * Revert "Add functions to remove VCS fsbackend update locks and git_pillar update/checkout locks" This reverts commit 4c2db32419022501eae2a695ec488693e043d189. * prevent eternal gitfs lock due to process crash * Use salt.utils.fopen() instead of open() * Make pid locking work for more than just gitfs Also, make logging more descriptive, to aid in troubleshooting. * Add git_pillar_global_lock config option default value * Document proper usage of {gitfs,git_pillar}_global_lock * Fix comments value in salt.states.pkgrepo example (#32604) 'comments' option adds '#' automatically. Example contains `#http://mirror.centos.org/centos/$releasever/os/$basearch/` string which becomes prefixed with '##' in generated file. * alphabetize directories for dynamic modules (#32599) Also add engines and proxy minions to the list. * Expand on the open-source vs open-core FAQ * Language clarification. * Fix some mistakes in the salt-ssh thin shell script (#32583) * [[ is bash, not compatible with /bin/sh * check if python command exists before calling it * Deprecate 'user' and 'group' in state cmd (#32613) * Remove unused 'group' argument * Fix unit testing of cmd.mod_run_check without group arg * Deprecate 'user/group' in cmd.run * Deprecate 'user'/'group' in cmd.script * Deprecate 'user' in cmd.wait * Deprecate 'user'/'group' in cmd.wait_script * Fix mod_run_check without 'group' * Push deprecation back one release * Fix mac_service and mac_system modules (#32587) * Fix mac_service module * Add integration tests for new functions * Start will not enable the service beforehand * Remove unused variables
2016-04-18 14:40:20 +00:00
self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, [''], creates), ret)
2015-04-29 12:01:23 +00:00
[develop] Merge forward from 2016.3 to develop (#32636) * Ensure rh_service not used on CloudLinux 7 Add CloudLinux to RHEL-derived distros excluded from rh_service use in osrelease >= 7 * Fix binary search and replace (#32542) * Don't return None from eval_master (#32555) Raise an exception instead. Because eval master if returns should return a tuple. * redact passwords and hashes from user.present updates Fixes #32381 * Better log message on minion restart if master couldn't be reached. (#32576) * Revert PR #32480 and apply #32314 with fixes / documentation (#32558) * Revert "Fix loop in maint.flo" This reverts commit 5196cd6a6e5db3c7b1a47b1740881bbd3e87ea3d. * Revert "Clear VCS fsbackend and git_pillar locks on master start" This reverts commit 7e3caa9bae1ac4de62db9924374e35a8b826937e. * Revert "Add functions to remove VCS fsbackend update locks and git_pillar update/checkout locks" This reverts commit 4c2db32419022501eae2a695ec488693e043d189. * prevent eternal gitfs lock due to process crash * Use salt.utils.fopen() instead of open() * Make pid locking work for more than just gitfs Also, make logging more descriptive, to aid in troubleshooting. * Add git_pillar_global_lock config option default value * Document proper usage of {gitfs,git_pillar}_global_lock * Fix comments value in salt.states.pkgrepo example (#32604) 'comments' option adds '#' automatically. Example contains `#http://mirror.centos.org/centos/$releasever/os/$basearch/` string which becomes prefixed with '##' in generated file. * alphabetize directories for dynamic modules (#32599) Also add engines and proxy minions to the list. * Expand on the open-source vs open-core FAQ * Language clarification. * Fix some mistakes in the salt-ssh thin shell script (#32583) * [[ is bash, not compatible with /bin/sh * check if python command exists before calling it * Deprecate 'user' and 'group' in state cmd (#32613) * Remove unused 'group' argument * Fix unit testing of cmd.mod_run_check without group arg * Deprecate 'user/group' in cmd.run * Deprecate 'user'/'group' in cmd.script * Deprecate 'user' in cmd.wait * Deprecate 'user'/'group' in cmd.wait_script * Fix mod_run_check without 'group' * Push deprecation back one release * Fix mac_service and mac_system modules (#32587) * Fix mac_service module * Add integration tests for new functions * Start will not enable the service beforehand * Remove unused variables
2016-04-18 14:40:20 +00:00
self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, True, creates), ret)
2015-04-29 12:01:23 +00:00
with patch.object(os.path, 'exists',
MagicMock(sid_effect=[True, True, False])):
ret = {'comment': '/tmp exists', 'result': True}
[develop] Merge forward from 2016.3 to develop (#32636) * Ensure rh_service not used on CloudLinux 7 Add CloudLinux to RHEL-derived distros excluded from rh_service use in osrelease >= 7 * Fix binary search and replace (#32542) * Don't return None from eval_master (#32555) Raise an exception instead. Because eval master if returns should return a tuple. * redact passwords and hashes from user.present updates Fixes #32381 * Better log message on minion restart if master couldn't be reached. (#32576) * Revert PR #32480 and apply #32314 with fixes / documentation (#32558) * Revert "Fix loop in maint.flo" This reverts commit 5196cd6a6e5db3c7b1a47b1740881bbd3e87ea3d. * Revert "Clear VCS fsbackend and git_pillar locks on master start" This reverts commit 7e3caa9bae1ac4de62db9924374e35a8b826937e. * Revert "Add functions to remove VCS fsbackend update locks and git_pillar update/checkout locks" This reverts commit 4c2db32419022501eae2a695ec488693e043d189. * prevent eternal gitfs lock due to process crash * Use salt.utils.fopen() instead of open() * Make pid locking work for more than just gitfs Also, make logging more descriptive, to aid in troubleshooting. * Add git_pillar_global_lock config option default value * Document proper usage of {gitfs,git_pillar}_global_lock * Fix comments value in salt.states.pkgrepo example (#32604) 'comments' option adds '#' automatically. Example contains `#http://mirror.centos.org/centos/$releasever/os/$basearch/` string which becomes prefixed with '##' in generated file. * alphabetize directories for dynamic modules (#32599) Also add engines and proxy minions to the list. * Expand on the open-source vs open-core FAQ * Language clarification. * Fix some mistakes in the salt-ssh thin shell script (#32583) * [[ is bash, not compatible with /bin/sh * check if python command exists before calling it * Deprecate 'user' and 'group' in state cmd (#32613) * Remove unused 'group' argument * Fix unit testing of cmd.mod_run_check without group arg * Deprecate 'user/group' in cmd.run * Deprecate 'user'/'group' in cmd.script * Deprecate 'user' in cmd.wait * Deprecate 'user'/'group' in cmd.wait_script * Fix mod_run_check without 'group' * Push deprecation back one release * Fix mac_service and mac_system modules (#32587) * Fix mac_service module * Add integration tests for new functions * Start will not enable the service beforehand * Remove unused variables
2016-04-18 14:40:20 +00:00
self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, None, creates), ret)
2015-04-29 12:01:23 +00:00
ret = {'comment': 'All files in creates exist', 'result': True}
[develop] Merge forward from 2016.3 to develop (#32636) * Ensure rh_service not used on CloudLinux 7 Add CloudLinux to RHEL-derived distros excluded from rh_service use in osrelease >= 7 * Fix binary search and replace (#32542) * Don't return None from eval_master (#32555) Raise an exception instead. Because eval master if returns should return a tuple. * redact passwords and hashes from user.present updates Fixes #32381 * Better log message on minion restart if master couldn't be reached. (#32576) * Revert PR #32480 and apply #32314 with fixes / documentation (#32558) * Revert "Fix loop in maint.flo" This reverts commit 5196cd6a6e5db3c7b1a47b1740881bbd3e87ea3d. * Revert "Clear VCS fsbackend and git_pillar locks on master start" This reverts commit 7e3caa9bae1ac4de62db9924374e35a8b826937e. * Revert "Add functions to remove VCS fsbackend update locks and git_pillar update/checkout locks" This reverts commit 4c2db32419022501eae2a695ec488693e043d189. * prevent eternal gitfs lock due to process crash * Use salt.utils.fopen() instead of open() * Make pid locking work for more than just gitfs Also, make logging more descriptive, to aid in troubleshooting. * Add git_pillar_global_lock config option default value * Document proper usage of {gitfs,git_pillar}_global_lock * Fix comments value in salt.states.pkgrepo example (#32604) 'comments' option adds '#' automatically. Example contains `#http://mirror.centos.org/centos/$releasever/os/$basearch/` string which becomes prefixed with '##' in generated file. * alphabetize directories for dynamic modules (#32599) Also add engines and proxy minions to the list. * Expand on the open-source vs open-core FAQ * Language clarification. * Fix some mistakes in the salt-ssh thin shell script (#32583) * [[ is bash, not compatible with /bin/sh * check if python command exists before calling it * Deprecate 'user' and 'group' in state cmd (#32613) * Remove unused 'group' argument * Fix unit testing of cmd.mod_run_check without group arg * Deprecate 'user/group' in cmd.run * Deprecate 'user'/'group' in cmd.script * Deprecate 'user' in cmd.wait * Deprecate 'user'/'group' in cmd.wait_script * Fix mod_run_check without 'group' * Push deprecation back one release * Fix mac_service and mac_system modules (#32587) * Fix mac_service module * Add integration tests for new functions * Start will not enable the service beforehand * Remove unused variables
2016-04-18 14:40:20 +00:00
self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, None, [creates]), ret)
2015-04-29 12:01:23 +00:00
[develop] Merge forward from 2016.3 to develop (#32636) * Ensure rh_service not used on CloudLinux 7 Add CloudLinux to RHEL-derived distros excluded from rh_service use in osrelease >= 7 * Fix binary search and replace (#32542) * Don't return None from eval_master (#32555) Raise an exception instead. Because eval master if returns should return a tuple. * redact passwords and hashes from user.present updates Fixes #32381 * Better log message on minion restart if master couldn't be reached. (#32576) * Revert PR #32480 and apply #32314 with fixes / documentation (#32558) * Revert "Fix loop in maint.flo" This reverts commit 5196cd6a6e5db3c7b1a47b1740881bbd3e87ea3d. * Revert "Clear VCS fsbackend and git_pillar locks on master start" This reverts commit 7e3caa9bae1ac4de62db9924374e35a8b826937e. * Revert "Add functions to remove VCS fsbackend update locks and git_pillar update/checkout locks" This reverts commit 4c2db32419022501eae2a695ec488693e043d189. * prevent eternal gitfs lock due to process crash * Use salt.utils.fopen() instead of open() * Make pid locking work for more than just gitfs Also, make logging more descriptive, to aid in troubleshooting. * Add git_pillar_global_lock config option default value * Document proper usage of {gitfs,git_pillar}_global_lock * Fix comments value in salt.states.pkgrepo example (#32604) 'comments' option adds '#' automatically. Example contains `#http://mirror.centos.org/centos/$releasever/os/$basearch/` string which becomes prefixed with '##' in generated file. * alphabetize directories for dynamic modules (#32599) Also add engines and proxy minions to the list. * Expand on the open-source vs open-core FAQ * Language clarification. * Fix some mistakes in the salt-ssh thin shell script (#32583) * [[ is bash, not compatible with /bin/sh * check if python command exists before calling it * Deprecate 'user' and 'group' in state cmd (#32613) * Remove unused 'group' argument * Fix unit testing of cmd.mod_run_check without group arg * Deprecate 'user/group' in cmd.run * Deprecate 'user'/'group' in cmd.script * Deprecate 'user' in cmd.wait * Deprecate 'user'/'group' in cmd.wait_script * Fix mod_run_check without 'group' * Push deprecation back one release * Fix mac_service and mac_system modules (#32587) * Fix mac_service module * Add integration tests for new functions * Start will not enable the service beforehand * Remove unused variables
2016-04-18 14:40:20 +00:00
self.assertTrue(cmd.mod_run_check(cmd_kwargs, None, None, {}))
2015-04-29 12:01:23 +00:00
# 'wait' function tests: 1
def test_wait(self):
'''
Test to run the given command only if the watch statement calls it.
'''
name = 'cmd.script'
ret = {'name': name,
'result': True,
'changes': {},
'comment': ''}
self.assertDictEqual(cmd.wait(name), ret)
# 'wait_script' function tests: 1
def test_wait_script(self):
'''
Test to download a script from a remote source and execute it
only if a watch statement calls it.
'''
name = 'cmd.script'
ret = {'name': name,
'result': True,
'changes': {},
'comment': ''}
self.assertDictEqual(cmd.wait_script(name), ret)
# 'run' function tests: 1
def test_run(self):
'''
Test to run a command if certain circumstances are met.
'''
name = 'cmd.script'
ret = {'name': name,
'result': False,
'changes': {},
'comment': ''}
2015-04-29 21:00:56 +00:00
with patch.dict(cmd.__opts__, {'test': False}):
comt = ("Invalidly-formatted 'env' parameter. See documentation.")
ret.update({'comment': comt})
self.assertDictEqual(cmd.run(name, env='salt'), ret)
2015-04-29 12:01:23 +00:00
with patch.dict(cmd.__grains__, {'shell': 'shell'}):
with patch.dict(cmd.__opts__, {'test': False}):
mock = MagicMock(side_effect=[CommandExecutionError,
{'retcode': 1}])
with patch.dict(cmd.__salt__, {'cmd.run_all': mock}):
ret.update({'comment': '', 'result': False})
self.assertDictEqual(cmd.run(name), ret)
ret.update({'comment': 'Command "cmd.script" run',
'result': False, 'changes': {'retcode': 1}})
self.assertDictEqual(cmd.run(name), ret)
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.run(name), ret)
mock = MagicMock(return_value=1)
with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
2015-04-29 21:00:56 +00:00
with patch.dict(cmd.__opts__, {'test': False}):
comt = ('onlyif execution failed')
ret.update({'comment': comt, 'result': True,
'skip_watch': True})
self.assertDictEqual(cmd.run(name, onlyif=''), ret)
2015-04-29 12:01:23 +00:00
# 'script' function tests: 1
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': ''}
2015-04-29 21:00:56 +00:00
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)
2015-04-29 12:01:23 +00:00
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}):
2015-04-29 21:00:56 +00:00
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)
2015-04-29 12:01:23 +00:00
# 'call' function tests: 1
def test_call(self):
'''
Test to invoke a pre-defined Python function with arguments
specified in the state declaration.
'''
name = 'cmd.script'
# func = 'myfunc'
ret = {'name': name,
'result': False,
'changes': {},
'comment': ''}
flag = None
def func():
'''
Mock func method
'''
if flag:
return {}
else:
return []
with patch.dict(cmd.__grains__, {'shell': 'shell'}):
flag = True
self.assertDictEqual(cmd.call(name, func), ret)
flag = False
comt = ('onlyif execution failed')
ret.update({'comment': '', 'result': False,
'changes': {'retval': []}})
self.assertDictEqual(cmd.call(name, func), ret)
mock = MagicMock(return_value=1)
with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
with patch.dict(cmd.__opts__, {'test': True}):
comt = ('onlyif execution failed')
ret.update({'comment': comt, 'skip_watch': True,
'result': True, 'changes': {}})
self.assertDictEqual(cmd.call(name, func, onlyif=''), ret)
# 'wait_call' function tests: 1
def test_wait_call(self):
'''
Test to run wait_call.
'''
name = 'cmd.script'
func = 'myfunc'
ret = {'name': name,
'result': True,
'changes': {},
'comment': ''}
self.assertDictEqual(cmd.wait_call(name, func), ret)
# 'mod_watch' function tests: 1
def test_mod_watch(self):
'''
Test to execute a cmd function based on a watch call
'''
name = 'cmd.script'
ret = {'name': name,
'result': False,
'changes': {},
'comment': ''}
def func():
'''
Mock func method
'''
return {}
with patch.dict(cmd.__grains__, {'shell': 'shell'}):
with patch.dict(cmd.__opts__, {'test': False}):
mock = MagicMock(return_value={'retcode': 1})
with patch.dict(cmd.__salt__, {'cmd.run_all': mock}):
self.assertDictEqual(cmd.mod_watch(name, sfun='wait',
stateful=True), ret)
comt = ('Command "cmd.script" run')
ret.update({'comment': comt, 'changes': {'retcode': 1}})
self.assertDictEqual(cmd.mod_watch(name, sfun='wait',
stateful=False), ret)
with patch.dict(cmd.__salt__, {'cmd.script': mock}):
ret.update({'comment': '', 'changes': {}})
self.assertDictEqual(cmd.mod_watch(name, sfun='script',
stateful=True), ret)
comt = ("Command 'cmd.script' run")
ret.update({'comment': comt, 'changes': {'retcode': 1}})
self.assertDictEqual(cmd.mod_watch(name, sfun='script',
stateful=False), ret)
with patch.dict(cmd.__salt__, {'cmd.script': mock}):
ret.update({'comment': '', 'changes': {}})
self.assertDictEqual(cmd.mod_watch(name, sfun='call',
func=func), ret)
comt = ('cmd.call needs a named parameter func')
ret.update({'comment': comt})
self.assertDictEqual(cmd.mod_watch(name, sfun='call'), ret)
comt = ('cmd.salt does not work with the watch requisite,'
' please use cmd.wait or cmd.wait_script')
ret.update({'comment': comt})
self.assertDictEqual(cmd.mod_watch(name, sfun='salt'), ret)