desc
stringlengths 3
26.7k
| decl
stringlengths 11
7.89k
| bodies
stringlengths 8
553k
|
---|---|---|
'Try and deny a record that already exists'
| def test_absent_record_does_not_exist(self):
| result = libcloud_dns.record_absent('mail', 'test.com', 'A', '127.0.0.1', 'test')
self.assertTrue(result)
|
'Assert that when you try and ensure present state for a record to a zone that doesn\'t exist
it fails gracefully'
| def test_present_zone_not_found(self):
| result = libcloud_dns.record_present('mail', 'notatest.com', 'A', '127.0.0.1', 'test')
self.assertFalse(result['result'])
|
'Assert that when you try and ensure absent state for a record to a zone that doesn\'t exist
it fails gracefully'
| def test_absent_zone_not_found(self):
| result = libcloud_dns.record_absent('mail', 'notatest.com', 'A', '127.0.0.1', 'test')
self.assertFalse(result['result'])
|
'Assert that a zone is present (that did not exist)'
| def test_zone_present(self):
| result = libcloud_dns.zone_present('testing.com', 'master', 'test1')
self.assertTrue(result)
|
'Assert that a zone is present (that did exist)'
| def test_zone_already_present(self):
| result = libcloud_dns.zone_present('test.com', 'master', 'test1')
self.assertTrue(result)
|
'Assert that a zone that did exist is absent'
| def test_zone_absent(self):
| result = libcloud_dns.zone_absent('test.com', 'test1')
self.assertTrue(result)
|
'Assert that a zone that did not exist is absent'
| def test_zone_already_absent(self):
| result = libcloud_dns.zone_absent('testing.com', 'test1')
self.assertTrue(result)
|
'Test to ensure a Linux ACL is present'
| def test_present(self):
| self.maxDiff = None
name = '/root'
acl_type = 'users'
acl_name = 'damian'
perms = 'rwx'
mock = MagicMock(side_effect=[{name: {acl_type: [{acl_name: {'octal': 'A'}}]}}, {name: {acl_type: [{acl_name: {'octal': 'A'}}]}}, {name: {acl_type: [{acl_name: {'octal': 'A'}}]}}, {name: {acl_type: [{}]}}, {name: {acl_type: [{}]}}, {name: {acl_type: [{}]}}, {name: {acl_type: ''}}])
mock_modfacl = MagicMock(return_value=True)
with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
with patch.dict(linux_acl.__opts__, {'test': True}):
comt = 'Updated permissions will be applied for {0}: A -> {1}'.format(acl_name, perms)
ret = {'name': name, 'comment': comt, 'changes': {}, 'pchanges': {'new': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': perms}, 'old': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': 'A'}}, 'result': None}
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, perms), ret)
with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
with patch.dict(linux_acl.__opts__, {'test': False}):
comt = 'Updated permissions for {0}'.format(acl_name)
ret = {'name': name, 'comment': comt, 'changes': {'new': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': perms}, 'old': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': 'A'}}, 'pchanges': {}, 'result': True}
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, perms), ret)
with patch.dict(linux_acl.__salt__, {'acl.modfacl': MagicMock(side_effect=CommandExecutionError('Custom err'))}):
with patch.dict(linux_acl.__opts__, {'test': False}):
comt = 'Error updating permissions for {0}: Custom err'.format(acl_name)
ret = {'name': name, 'comment': comt, 'changes': {}, 'pchanges': {}, 'result': False}
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, perms), ret)
with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
with patch.dict(linux_acl.__opts__, {'test': True}):
comt = 'New permissions will be applied for {0}: {1}'.format(acl_name, perms)
ret = {'name': name, 'comment': comt, 'changes': {}, 'pchanges': {'new': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': perms}}, 'result': None}
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, perms), ret)
with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
with patch.dict(linux_acl.__opts__, {'test': False}):
comt = 'Applied new permissions for {0}'.format(acl_name)
ret = {'name': name, 'comment': comt, 'changes': {'new': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': perms}}, 'pchanges': {}, 'result': True}
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, perms), ret)
with patch.dict(linux_acl.__salt__, {'acl.modfacl': MagicMock(side_effect=CommandExecutionError('Custom err'))}):
with patch.dict(linux_acl.__opts__, {'test': False}):
comt = 'Error updating permissions for {0}: Custom err'.format(acl_name)
ret = {'name': name, 'comment': comt, 'changes': {}, 'pchanges': {}, 'result': False}
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, perms), ret)
comt = 'ACL Type does not exist'
ret = {'name': name, 'comment': comt, 'result': False, 'changes': {}, 'pchanges': {}}
self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, perms), ret)
|
'Test to ensure a Linux ACL does not exist'
| def test_absent(self):
| name = '/root'
acl_type = 'users'
acl_name = 'damian'
perms = 'rwx'
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
mock = MagicMock(side_effect=[{name: {acl_type: [{acl_name: {'octal': 'A'}}]}}, {name: {acl_type: ''}}])
with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
with patch.dict(linux_acl.__opts__, {'test': True}):
comt = 'Removing permissions'
ret.update({'comment': comt})
self.assertDictEqual(linux_acl.absent(name, acl_type, acl_name, perms), ret)
comt = 'ACL Type does not exist'
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(linux_acl.absent(name, acl_type, acl_name, perms), ret)
|
'Test to return the current load average for the specified minion.'
| def test_loadavg(self):
| name = 'mymonitor'
ret = {'name': name, 'changes': {}, 'result': True, 'data': {}, 'comment': ''}
mock = MagicMock(return_value=[])
with patch.dict(status.__salt__, {'status.loadavg': mock}):
comt = 'Requested load average mymonitor not available '
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(status.loadavg(name), ret)
mock = MagicMock(return_value={name: 3})
with patch.dict(status.__salt__, {'status.loadavg': mock}):
comt = 'Min must be less than max'
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(status.loadavg(name, 1, 5), ret)
comt = 'Load avg is below minimum of 4 at 3.0'
ret.update({'comment': comt, 'data': 3})
self.assertDictEqual(status.loadavg(name, 5, 4), ret)
comt = 'Load avg above maximum of 2 at 3.0'
ret.update({'comment': comt, 'data': 3})
self.assertDictEqual(status.loadavg(name, 2, 1), ret)
comt = 'Load avg in acceptable range'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(status.loadavg(name, 3, 1), ret)
|
'Test to return whether the specified signature
is found in the process tree.'
| def test_process(self):
| name = 'mymonitor'
ret = {'name': name, 'changes': {}, 'result': True, 'data': {}, 'comment': ''}
mock = MagicMock(side_effect=[{}, {name: 1}])
with patch.dict(status.__salt__, {'status.pid': mock}):
comt = 'Process signature "mymonitor" not found '
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(status.process(name), ret)
comt = 'Process signature "mymonitor" was found '
ret.update({'comment': comt, 'result': True, 'data': {name: 1}})
self.assertDictEqual(status.process(name), ret)
|
'init method'
| def __init__(self, master_config):
| pass
|
'Mock cmd method'
| @staticmethod
def cmd(arg1, arg2):
| return []
|
'Test to refresh the winrepo.p file of the repository'
| def test_genrepo(self):
| ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': ''}
ret.update({'comment': '{0} is missing'.format(os.sep.join([BASE_FILE_ROOTS_DIR, 'win', 'repo']))})
self.assertDictEqual(winrepo.genrepo('salt'), ret)
mock = MagicMock(return_value={'winrepo_dir': 'salt', 'winrepo_cachefile': 'abc'})
with patch.object(salt.config, 'master_config', mock):
mock = MagicMock(return_value=[0, 1, 2, 3, 4, 5, 6, 7, 8])
with patch.object(os, 'stat', mock):
mock = MagicMock(return_value=[])
with patch.object(os, 'walk', mock):
with patch.dict(winrepo.__opts__, {'test': True}):
ret.update({'comment': '', 'result': None})
self.assertDictEqual(winrepo.genrepo('salt'), ret)
with patch.dict(winrepo.__opts__, {'test': False}):
ret.update({'result': True})
self.assertDictEqual(winrepo.genrepo('salt'), ret)
ret.update({'changes': {'winrepo': []}})
self.assertDictEqual(winrepo.genrepo('salt', True), ret)
|
'Test to verifies that the specified host is known by the specified user.'
| def test_present(self):
| name = 'github.com'
user = 'root'
key = '16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48'
fingerprint = [key]
ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}
with patch.dict(ssh_known_hosts.__opts__, {'test': True}):
with patch.object(os.path, 'isabs', MagicMock(return_value=False)):
comt = 'If not specifying a "user", specify an absolute "config".'
ret.update({'comment': comt})
self.assertDictEqual(ssh_known_hosts.present(name), ret)
comt = 'Specify either "key" or "fingerprint", not both.'
ret.update({'comment': comt})
self.assertDictEqual(ssh_known_hosts.present(name, user, key=key, fingerprint=[key]), ret)
comt = 'Required argument "enc" if using "key" argument.'
ret.update({'comment': comt})
self.assertDictEqual(ssh_known_hosts.present(name, user, key=key), ret)
mock = MagicMock(side_effect=['exists', 'add', 'update'])
with patch.dict(ssh_known_hosts.__salt__, {'ssh.check_known_host': mock}):
comt = 'Host github.com is already in .ssh/known_hosts'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
comt = 'Key for github.com is set to be added to .ssh/known_hosts'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
comt = 'Key for github.com is set to be updated in .ssh/known_hosts'
ret.update({'comment': comt})
self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
with patch.dict(ssh_known_hosts.__opts__, {'test': False}):
result = {'status': 'exists', 'error': ''}
mock = MagicMock(return_value=result)
with patch.dict(ssh_known_hosts.__salt__, {'ssh.set_known_host': mock}):
comt = 'github.com already exists in .ssh/known_hosts'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
result = {'status': 'error', 'error': ''}
mock = MagicMock(return_value=result)
with patch.dict(ssh_known_hosts.__salt__, {'ssh.set_known_host': mock}):
ret.update({'comment': '', 'result': False})
self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
result = {'status': 'updated', 'error': '', 'new': {'fingerprint': fingerprint, 'key': key}, 'old': ''}
mock = MagicMock(return_value=result)
with patch.dict(ssh_known_hosts.__salt__, {'ssh.set_known_host': mock}):
comt = "{0}'s key saved to .ssh/known_hosts (key: {1})".format(name, key)
ret.update({'comment': comt, 'result': True, 'changes': {'new': {'fingerprint': fingerprint, 'key': key}, 'old': ''}})
self.assertDictEqual(ssh_known_hosts.present(name, user, key=key), ret)
comt = "{0}'s key saved to .ssh/known_hosts (fingerprint: {1})".format(name, fingerprint)
ret.update({'comment': comt})
self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
|
'Test to verifies that the specified host is not known by the given user.'
| def test_absent(self):
| name = 'github.com'
user = 'root'
ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}
with patch.object(os.path, 'isabs', MagicMock(return_value=False)):
comt = 'If not specifying a "user", specify an absolute "config".'
ret.update({'comment': comt})
self.assertDictEqual(ssh_known_hosts.absent(name), ret)
mock = MagicMock(return_value=False)
with patch.dict(ssh_known_hosts.__salt__, {'ssh.get_known_host': mock}):
comt = 'Host is already absent'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ssh_known_hosts.absent(name, user), ret)
mock = MagicMock(return_value=True)
with patch.dict(ssh_known_hosts.__salt__, {'ssh.get_known_host': mock}):
with patch.dict(ssh_known_hosts.__opts__, {'test': True}):
comt = 'Key for github.com is set to be removed from .ssh/known_hosts'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(ssh_known_hosts.absent(name, user), ret)
with patch.dict(ssh_known_hosts.__opts__, {'test': False}):
result = {'status': 'error', 'error': ''}
mock = MagicMock(return_value=result)
with patch.dict(ssh_known_hosts.__salt__, {'ssh.rm_known_host': mock}):
ret.update({'comment': '', 'result': False})
self.assertDictEqual(ssh_known_hosts.absent(name, user), ret)
result = {'status': 'removed', 'error': '', 'comment': 'removed'}
mock = MagicMock(return_value=result)
with patch.dict(ssh_known_hosts.__salt__, {'ssh.rm_known_host': mock}):
ret.update({'comment': 'removed', 'result': True, 'changes': {'new': None, 'old': True}})
self.assertDictEqual(ssh_known_hosts.absent(name, user), ret)
|
'Mock SetCategories'
| @staticmethod
def SetCategories(arg):
| return arg
|
'Mock SetIncludes'
| @staticmethod
def SetIncludes(arg):
| return arg
|
'Mock GetInstallationResults'
| @staticmethod
def GetInstallationResults():
| return True
|
'Mock GetDownloadResults'
| @staticmethod
def GetDownloadResults():
| return True
|
'Test to install specified windows updates'
| def test_installed(self):
| ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': '', 'warnings': ["The 'win_update' module is deprecated, and will be removed in Salt Fluorine. Please use the 'win_wua' module instead."]}
mock = MagicMock(side_effect=[['Saltstack', False, 5], ['Saltstack', True, 5], ['Saltstack', True, 5], ['Saltstack', True, 5]])
with patch.object(win_update, '_search', mock):
ret.update({'comment': 'Saltstack'})
self.assertDictEqual(win_update.installed('salt'), ret)
mock = MagicMock(side_effect=[['dude', False, 5], ['dude', True, 5], ['dude', True, 5]])
with patch.object(win_update, '_download', mock):
ret.update({'comment': 'Saltstackdude'})
self.assertDictEqual(win_update.installed('salt'), ret)
mock = MagicMock(side_effect=[['@Me', False, 5], ['@Me', True, 5]])
with patch.object(win_update, '_install', mock):
ret.update({'comment': 'Saltstackdude@Me'})
self.assertDictEqual(win_update.installed('salt'), ret)
ret.update({'changes': True, 'result': True})
self.assertDictEqual(win_update.installed('salt'), ret)
|
'Test to cache updates for later install.'
| def test_downloaded(self):
| ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': '', 'warnings': ["The 'win_update' module is deprecated, and will be removed in Salt Fluorine. Please use the 'win_wua' module instead."]}
mock = MagicMock(side_effect=[['Saltstack', False, 5], ['Saltstack', True, 5], ['Saltstack', True, 5]])
with patch.object(win_update, '_search', mock):
ret.update({'comment': 'Saltstack'})
self.assertDictEqual(win_update.downloaded('salt'), ret)
mock = MagicMock(side_effect=[['dude', False, 5], ['dude', True, 5]])
with patch.object(win_update, '_download', mock):
ret.update({'comment': 'Saltstackdude'})
self.assertDictEqual(win_update.downloaded('salt'), ret)
ret.update({'changes': True, 'result': True})
self.assertDictEqual(win_update.downloaded('salt'), ret)
|
'Test if none list changes handled correctly'
| def test_change_non_list_changes(self):
| comt = "'changes' must be specified as a list"
self.ret.update({'comment': comt})
self.assertDictEqual(augeas.change(self.name), self.ret)
|
'Test if none list load_path is handled correctly'
| def test_change_non_list_load_path(self):
| comt = "'load_path' must be specified as a list"
self.ret.update({'comment': comt})
self.assertDictEqual(augeas.change(self.name, self.context, self.changes, load_path='x'), self.ret)
|
'Test test mode handling'
| def test_change_in_test_mode(self):
| comt = 'Executing commands in file "/files/etc/services":\nins service-name after service-name[last()]\nset service-name[last()] zabbix-agent'
self.ret.update({'comment': comt, 'result': True})
with patch.dict(augeas.__opts__, {'test': True}):
self.assertDictEqual(augeas.change(self.name, self.context, self.changes), self.ret)
|
'Test handling of no context without full path'
| def test_change_no_context_without_full_path(self):
| comt = 'Error: Changes should be prefixed with /files if no context is provided, change: {0}'.format(self.changes[0])
self.ret.update({'comment': comt, 'result': False})
with patch.dict(augeas.__opts__, {'test': False}):
mock_dict_ = {'augeas.method_map': self.mock_method_map}
with patch.dict(augeas.__salt__, mock_dict_):
self.assertDictEqual(augeas.change(self.name, changes=self.changes), self.ret)
|
'Test handling of no context with full path with execute fail'
| def test_change_no_context_with_full_path_fail(self):
| self.ret.update({'comment': 'Error: error', 'result': False})
with patch.dict(augeas.__opts__, {'test': False}):
mock_execute = MagicMock(return_value=dict(retval=False, error='error'))
mock_dict_ = {'augeas.execute': mock_execute, 'augeas.method_map': self.mock_method_map}
with patch.dict(augeas.__salt__, mock_dict_):
self.assertDictEqual(augeas.change(self.name, changes=self.fp_changes), self.ret)
|
'Test handling of no context with full path with execute pass'
| def test_change_no_context_with_full_path_pass(self):
| self.ret.update(dict(comment='Changes have been saved', result=True, changes={'diff': '+ zabbix-agent'}))
with patch.dict(augeas.__opts__, {'test': False}):
mock_execute = MagicMock(return_value=dict(retval=True))
mock_dict_ = {'augeas.execute': mock_execute, 'augeas.method_map': self.mock_method_map}
with patch.dict(augeas.__salt__, mock_dict_):
mock_filename = MagicMock(return_value='/etc/services')
with patch.object(augeas, '_workout_filename', mock_filename):
with patch('salt.utils.files.fopen', MagicMock(mock_open)):
mock_diff = MagicMock(return_value=['+ zabbix-agent'])
with patch('difflib.unified_diff', mock_diff):
self.assertDictEqual(augeas.change(self.name, changes=self.fp_changes), self.ret)
|
'Test handling of invalid commands when no context supplied'
| def test_change_no_context_without_full_path_invalid_cmd(self):
| self.ret.update(dict(comment='Error: Command det is not supported (yet)', result=False))
with patch.dict(augeas.__opts__, {'test': False}):
mock_execute = MagicMock(return_value=dict(retval=True))
mock_dict_ = {'augeas.execute': mock_execute, 'augeas.method_map': self.mock_method_map}
with patch.dict(augeas.__salt__, mock_dict_):
changes = ['det service-name[last()] zabbix-agent']
self.assertDictEqual(augeas.change(self.name, changes=changes), self.ret)
|
'Test handling of invalid change when no context supplied'
| def test_change_no_context_without_full_path_invalid_change(self):
| comt = 'Error: Invalid formatted command, see debug log for details: require'
self.ret.update(dict(comment=comt, result=False))
changes = ['require']
with patch.dict(augeas.__opts__, {'test': False}):
mock_execute = MagicMock(return_value=dict(retval=True))
mock_dict_ = {'augeas.execute': mock_execute, 'augeas.method_map': self.mock_method_map}
with patch.dict(augeas.__salt__, mock_dict_):
self.assertDictEqual(augeas.change(self.name, changes=changes), self.ret)
|
'Test handling of different paths with no context supplied'
| def test_change_no_context_with_full_path_multiple_files(self):
| changes = ['set /files/etc/hosts/service-name test', 'set /files/etc/services/service-name test']
filename = '/etc/hosts/service-name'
filename_ = '/etc/services/service-name'
comt = 'Error: Changes should be made to one file at a time, detected changes to {0} and {1}'.format(filename, filename_)
self.ret.update(dict(comment=comt, result=False))
with patch.dict(augeas.__opts__, {'test': False}):
mock_execute = MagicMock(return_value=dict(retval=True))
mock_dict_ = {'augeas.execute': mock_execute, 'augeas.method_map': self.mock_method_map}
with patch.dict(augeas.__salt__, mock_dict_):
self.assertDictEqual(augeas.change(self.name, changes=changes), self.ret)
|
'Test handling of context without full path fails'
| def test_change_with_context_without_full_path_fail(self):
| self.ret.update(dict(comment='Error: error', result=False))
with patch.dict(augeas.__opts__, {'test': False}):
mock_execute = MagicMock(return_value=dict(retval=False, error='error'))
mock_dict_ = {'augeas.execute': mock_execute, 'augeas.method_map': self.mock_method_map}
with patch.dict(augeas.__salt__, mock_dict_):
with patch('salt.utils.files.fopen', MagicMock(mock_open)):
self.assertDictEqual(augeas.change(self.name, context=self.context, changes=self.changes), self.ret)
|
'Test handling of context without oldfile pass'
| def test_change_with_context_without_old_file(self):
| self.ret.update(dict(comment='Changes have been saved', result=True, changes={'updates': self.changes}))
with patch.dict(augeas.__opts__, {'test': False}):
mock_execute = MagicMock(return_value=dict(retval=True))
mock_dict_ = {'augeas.execute': mock_execute, 'augeas.method_map': self.mock_method_map}
with patch.dict(augeas.__salt__, mock_dict_):
mock_isfile = MagicMock(return_value=False)
with patch.object(os.path, 'isfile', mock_isfile):
self.assertDictEqual(augeas.change(self.name, context=self.context, changes=self.changes), self.ret)
|
'Test to verifies the mode SELinux is running in,
can be set to enforcing or permissive.'
| def test_mode(self):
| ret = {'name': 'unknown', 'changes': {}, 'result': False, 'comment': 'unknown is not an accepted mode'}
self.assertDictEqual(selinux.mode('unknown'), ret)
mock_en = MagicMock(return_value='Enforcing')
mock_pr = MagicMock(side_effect=['Permissive', 'Enforcing'])
with patch.dict(selinux.__salt__, {'selinux.getenforce': mock_en, 'selinux.getconfig': mock_en, 'selinux.setenforce': mock_pr}):
comt = 'SELinux is already in Enforcing mode'
ret = {'name': 'Enforcing', 'comment': comt, 'result': True, 'changes': {}}
self.assertDictEqual(selinux.mode('Enforcing'), ret)
with patch.dict(selinux.__opts__, {'test': True}):
comt = 'SELinux mode is set to be changed to Permissive'
ret = {'name': 'Permissive', 'comment': comt, 'result': None, 'changes': {'new': 'Permissive', 'old': 'Enforcing'}}
self.assertDictEqual(selinux.mode('Permissive'), ret)
with patch.dict(selinux.__opts__, {'test': False}):
comt = 'SELinux has been set to Permissive mode'
ret = {'name': 'Permissive', 'comment': comt, 'result': True, 'changes': {'new': 'Permissive', 'old': 'Enforcing'}}
self.assertDictEqual(selinux.mode('Permissive'), ret)
comt = 'Failed to set SELinux to Permissive mode'
ret.update({'name': 'Permissive', 'comment': comt, 'result': False, 'changes': {}})
self.assertDictEqual(selinux.mode('Permissive'), ret)
|
'Test to set up an SELinux boolean.'
| def test_boolean(self):
| name = 'samba_create_home_dirs'
value = True
ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}
mock_en = MagicMock(return_value=[])
with patch.dict(selinux.__salt__, {'selinux.list_sebool': mock_en}):
comt = 'Boolean {0} is not available'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(selinux.boolean(name, value), ret)
mock_bools = MagicMock(return_value={name: {'State': 'on', 'Default': 'on'}})
with patch.dict(selinux.__salt__, {'selinux.list_sebool': mock_bools}):
comt = 'None is not a valid value for the boolean'
ret.update({'comment': comt})
self.assertDictEqual(selinux.boolean(name, None), ret)
comt = 'Boolean is in the correct state'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(selinux.boolean(name, value, True), ret)
comt = 'Boolean is in the correct state'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(selinux.boolean(name, value), ret)
mock_bools = MagicMock(return_value={name: {'State': 'off', 'Default': 'on'}})
mock = MagicMock(side_effect=[True, False])
with patch.dict(selinux.__salt__, {'selinux.list_sebool': mock_bools, 'selinux.setsebool': mock}):
with patch.dict(selinux.__opts__, {'test': True}):
comt = 'Boolean samba_create_home_dirs is set to be changed to on'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(selinux.boolean(name, value), ret)
with patch.dict(selinux.__opts__, {'test': False}):
comt = 'Boolean samba_create_home_dirs has been set to on'
ret.update({'comment': comt, 'result': True})
ret.update({'changes': {'State': {'old': 'off', 'new': 'on'}}})
self.assertDictEqual(selinux.boolean(name, value), ret)
comt = 'Failed to set the boolean samba_create_home_dirs to on'
ret.update({'comment': comt, 'result': False})
ret.update({'changes': {}})
self.assertDictEqual(selinux.boolean(name, value), ret)
|
'Test to allows for inputting a yaml dictionary into a file
for apache configuration files.'
| def test_configfile(self):
| with patch('os.path.exists', MagicMock(return_value=True)):
name = '/etc/distro/specific/apache.conf'
config = 'VirtualHost: this: "*:80"'
new_config = 'LiteralHost: that: "*:79"'
ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''}
with patch.object(salt.utils.files, 'fopen', mock_open(read_data=config)):
mock_config = MagicMock(return_value=config)
with patch.dict(apache.__salt__, {'apache.config': mock_config}):
ret.update({'comment': 'Configuration is up to date.'})
self.assertDictEqual(apache.configfile(name, config), ret)
with patch.object(salt.utils.files, 'fopen', mock_open(read_data=config)):
mock_config = MagicMock(return_value=new_config)
with patch.dict(apache.__salt__, {'apache.config': mock_config}):
ret.update({'comment': 'Configuration will update.', 'changes': {'new': new_config, 'old': config}, 'result': None})
with patch.dict(apache.__opts__, {'test': True}):
self.assertDictEqual(apache.configfile(name, new_config), ret)
with patch.object(salt.utils.files, 'fopen', mock_open(read_data=config)):
mock_config = MagicMock(return_value=new_config)
with patch.dict(apache.__salt__, {'apache.config': mock_config}):
ret.update({'comment': 'Successfully created configuration.', 'result': True})
with patch.dict(apache.__opts__, {'test': False}):
self.assertDictEqual(apache.configfile(name, config), ret)
|
'Test - Import the certificate file into the given certificate store.'
| def test_import_cert(self):
| kwargs = {'name': CERT_PATH}
ret = {'name': kwargs['name'], 'changes': {'old': None, 'new': THUMBPRINT}, 'comment': "Certificate '{0}' imported into store: {1}".format(THUMBPRINT, STORE_PATH), 'result': True}
mock_cache_file = MagicMock(return_value=CERT_PATH)
mock_certs = MagicMock(return_value={})
mock_cert_file = MagicMock(return_value=CERTS[THUMBPRINT])
mock_import_cert = MagicMock(return_value=True)
with patch.dict(win_pki.__salt__, {'cp.cache_file': mock_cache_file, 'win_pki.get_certs': mock_certs, 'win_pki.get_cert_file': mock_cert_file, 'win_pki.import_cert': mock_import_cert}):
with patch.dict(win_pki.__opts__, {'test': False}):
self.assertEqual(win_pki.import_cert(**kwargs), ret)
with patch.dict(win_pki.__opts__, {'test': True}):
ret['comment'] = "Certificate '{0}' will be imported into store: {1}".format(THUMBPRINT, STORE_PATH)
ret['result'] = None
self.assertEqual(win_pki.import_cert(**kwargs), ret)
|
'Test - Remove the certificate from the given certificate store.'
| def test_remove_cert(self):
| kwargs = {'name': 'remove-cert', 'thumbprint': THUMBPRINT}
ret = {'name': kwargs['name'], 'changes': {'old': kwargs['thumbprint'], 'new': None}, 'comment': "Certificate '{0}' removed from store: {1}".format(kwargs['thumbprint'], STORE_PATH), 'result': True}
mock_certs = MagicMock(return_value=CERTS)
mock_remove_cert = MagicMock(return_value=True)
with patch.dict(win_pki.__salt__, {'win_pki.get_certs': mock_certs, 'win_pki.remove_cert': mock_remove_cert}):
with patch.dict(win_pki.__opts__, {'test': False}):
self.assertEqual(win_pki.remove_cert(**kwargs), ret)
with patch.dict(win_pki.__opts__, {'test': True}):
ret['comment'] = "Certificate '{0}' will be removed from store: {1}".format(kwargs['thumbprint'], STORE_PATH)
ret['result'] = None
self.assertEqual(win_pki.remove_cert(**kwargs), ret)
|
'Test to device is monitored with Server Density.'
| def test_monitored(self):
| name = 'my_special_server'
ret = {'name': name, 'changes': {}, 'result': True, 'comment': ''}
mock_dict = MagicMock(return_value={'id': name})
mock_t = MagicMock(side_effect=[True, {'agentKey': True}, [{'agentKey': True}]])
mock_sd = MagicMock(side_effect=[['sd-agent'], [], True])
with patch.dict(serverdensity_device.__salt__, {'status.all_status': mock_dict, 'grains.items': mock_dict, 'serverdensity_device.ls': mock_t, 'pkg.list_pkgs': mock_sd, 'serverdensity_device.install_agent': mock_sd}):
comt = 'Such server name already exists in this Server Density account. And sd-agent is installed'
ret.update({'comment': comt})
self.assertDictEqual(serverdensity_device.monitored(name), ret)
comt = 'Successfully installed agent and created device in Server Density db.'
ret.update({'comment': comt, 'changes': {'created_device': {'agentKey': True}, 'installed_agent': True}})
self.assertDictEqual(serverdensity_device.monitored(name), ret)
|
'Test to verify options present in file.'
| def test_options_present(self):
| name = 'salt'
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
with patch.dict(ini_manage.__opts__, {'test': True}):
comt = 'No changes detected.'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ini_manage.options_present(name), ret)
changes = {'first': 'who is on', 'second': 'what is on', 'third': "I don't know"}
with patch.dict(ini_manage.__salt__, {'ini.set_option': MagicMock(return_value=changes)}):
with patch.dict(ini_manage.__opts__, {'test': False}):
comt = 'Changes take effect'
ret.update({'comment': comt, 'result': True, 'changes': changes})
self.assertDictEqual(ini_manage.options_present(name), ret)
original = {'mysection': {'first': 'who is on', 'second': 'what is on', 'third': "I don't know"}}
desired = {'mysection': {'first': 'who is on', 'second': 'what is on'}}
changes = {'mysection': {'first': 'who is on', 'second': 'what is on', 'third': {'after': None, 'before': "I don't know"}}}
with patch.dict(ini_manage.__salt__, {'ini.get_section': MagicMock(return_value=original['mysection'])}):
with patch.dict(ini_manage.__salt__, {'ini.remove_option': MagicMock(return_value='third')}):
with patch.dict(ini_manage.__salt__, {'ini.get_option': MagicMock(return_value="I don't know")}):
with patch.dict(ini_manage.__salt__, {'ini.set_option': MagicMock(return_value=desired)}):
with patch.dict(ini_manage.__opts__, {'test': False}):
comt = 'Changes take effect'
ret.update({'comment': comt, 'result': True, 'changes': changes})
self.assertDictEqual(ini_manage.options_present(name, desired, strict=True), ret)
|
'Test to verify options absent in file.'
| def test_options_absent(self):
| name = 'salt'
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
with patch.dict(ini_manage.__opts__, {'test': True}):
comt = 'No changes detected.'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ini_manage.options_absent(name), ret)
with patch.dict(ini_manage.__opts__, {'test': False}):
comt = 'No anomaly detected'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ini_manage.options_absent(name), ret)
|
'Test to verify sections present in file.'
| def test_sections_present(self):
| name = 'salt'
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
with patch.dict(ini_manage.__opts__, {'test': True}):
comt = 'No changes detected.'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ini_manage.sections_present(name), ret)
changes = {'first': 'who is on', 'second': 'what is on', 'third': "I don't know"}
with patch.dict(ini_manage.__salt__, {'ini.set_option': MagicMock(return_value=changes)}):
with patch.dict(ini_manage.__opts__, {'test': False}):
comt = 'Changes take effect'
ret.update({'comment': comt, 'result': True, 'changes': changes})
self.assertDictEqual(ini_manage.sections_present(name), ret)
|
'Test to verify sections absent in file.'
| def test_sections_absent(self):
| name = 'salt'
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
with patch.dict(ini_manage.__opts__, {'test': True}):
comt = 'No changes detected.'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ini_manage.sections_absent(name), ret)
with patch.dict(ini_manage.__opts__, {'test': False}):
comt = 'No anomaly detected'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(ini_manage.sections_absent(name), ret)
|
'Test capability installed state'
| def test_capability_installed(self):
| expected = {'comment': 'Installed Capa2', 'changes': {'capability': {'new': 'Capa2'}, 'retcode': 0}, 'name': 'Capa2', 'result': True}
mock_installed = MagicMock(side_effect=[['Capa1'], ['Capa1', 'Capa2']])
mock_add = MagicMock(return_value={'retcode': 0})
with patch.dict(dism.__salt__, {'dism.installed_capabilities': mock_installed, 'dism.add_capability': mock_add}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.capability_installed('Capa2', 'somewhere', True)
mock_installed.assert_called_with()
mock_add.assert_called_once_with('Capa2', 'somewhere', True, None, False)
self.assertEqual(out, expected)
|
'Test installing a capability which fails with DISM'
| def test_capability_installed_failure(self):
| expected = {'comment': 'Failed to install Capa2: Failed', 'changes': {}, 'name': 'Capa2', 'result': False}
mock_installed = MagicMock(side_effect=[['Capa1'], ['Capa1']])
mock_add = MagicMock(return_value={'retcode': 67, 'stdout': 'Failed'})
with patch.dict(dism.__salt__, {'dism.installed_capabilities': mock_installed, 'dism.add_capability': mock_add}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.capability_installed('Capa2', 'somewhere', True)
mock_installed.assert_called_with()
mock_add.assert_called_once_with('Capa2', 'somewhere', True, None, False)
self.assertEqual(out, expected)
|
'Test installing a capability already installed'
| def test_capability_installed_installed(self):
| expected = {'comment': 'The capability Capa2 is already installed', 'changes': {}, 'name': 'Capa2', 'result': True}
mock_installed = MagicMock(return_value=['Capa1', 'Capa2'])
mock_add = MagicMock()
with patch.dict(dism.__salt__, {'dism.installed_capabilities': mock_installed, 'dism.add_capability': mock_add}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.capability_installed('Capa2', 'somewhere', True)
mock_installed.assert_called_once_with()
assert (not mock_add.called)
self.assertEqual(out, expected)
|
'Test capability removed state'
| def test_capability_removed(self):
| expected = {'comment': 'Removed Capa2', 'changes': {'capability': {'old': 'Capa2'}, 'retcode': 0}, 'name': 'Capa2', 'result': True}
mock_removed = MagicMock(side_effect=[['Capa1', 'Capa2'], ['Capa1']])
mock_remove = MagicMock(return_value={'retcode': 0})
with patch.dict(dism.__salt__, {'dism.installed_capabilities': mock_removed, 'dism.remove_capability': mock_remove}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.capability_removed('Capa2')
mock_removed.assert_called_with()
mock_remove.assert_called_once_with('Capa2', None, False)
self.assertEqual(out, expected)
|
'Test removing a capability which fails with DISM'
| def test_capability_removed_failure(self):
| expected = {'comment': 'Failed to remove Capa2: Failed', 'changes': {}, 'name': 'Capa2', 'result': False}
mock_removed = MagicMock(side_effect=[['Capa1', 'Capa2'], ['Capa1', 'Capa2']])
mock_remove = MagicMock(return_value={'retcode': 67, 'stdout': 'Failed'})
with patch.dict(dism.__salt__, {'dism.installed_capabilities': mock_removed, 'dism.remove_capability': mock_remove}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.capability_removed('Capa2')
mock_removed.assert_called_with()
mock_remove.assert_called_once_with('Capa2', None, False)
self.assertEqual(out, expected)
|
'Test removing a capability already removed'
| def test_capability_removed_removed(self):
| expected = {'comment': 'The capability Capa2 is already removed', 'changes': {}, 'name': 'Capa2', 'result': True}
mock_removed = MagicMock(return_value=['Capa1'])
mock_remove = MagicMock()
with patch.dict(dism.__salt__, {'dism.installed_capabilities': mock_removed, 'dism.add_capability': mock_remove}):
out = dism.capability_removed('Capa2', 'somewhere', True)
mock_removed.assert_called_once_with()
assert (not mock_remove.called)
self.assertEqual(out, expected)
|
'Test installing a feature with DISM'
| def test_feature_installed(self):
| expected = {'comment': 'Installed Feat2', 'changes': {'feature': {'new': 'Feat2'}, 'retcode': 0}, 'name': 'Feat2', 'result': True}
mock_installed = MagicMock(side_effect=[['Feat1'], ['Feat1', 'Feat2']])
mock_add = MagicMock(return_value={'retcode': 0})
with patch.dict(dism.__salt__, {'dism.installed_features': mock_installed, 'dism.add_feature': mock_add}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.feature_installed('Feat2')
mock_installed.assert_called_with()
mock_add.assert_called_once_with('Feat2', None, None, False, False, None, False)
self.assertEqual(out, expected)
|
'Test installing a feature which fails with DISM'
| def test_feature_installed_failure(self):
| expected = {'comment': 'Failed to install Feat2: Failed', 'changes': {}, 'name': 'Feat2', 'result': False}
mock_installed = MagicMock(side_effect=[['Feat1'], ['Feat1']])
mock_add = MagicMock(return_value={'retcode': 67, 'stdout': 'Failed'})
with patch.dict(dism.__salt__, {'dism.installed_features': mock_installed, 'dism.add_feature': mock_add}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.feature_installed('Feat2')
mock_installed.assert_called_with()
mock_add.assert_called_once_with('Feat2', None, None, False, False, None, False)
self.assertEqual(out, expected)
|
'Test installing a feature already installed'
| def test_feature_installed_installed(self):
| expected = {'comment': 'The feature Feat1 is already installed', 'changes': {}, 'name': 'Feat1', 'result': True}
mock_installed = MagicMock(side_effect=[['Feat1', 'Feat2'], ['Feat1', 'Feat2']])
mock_add = MagicMock()
with patch.dict(dism.__salt__, {'dism.installed_features': mock_installed, 'dism.add_feature': mock_add}):
out = dism.feature_installed('Feat1')
mock_installed.assert_called_once_with()
assert (not mock_add.called)
self.assertEqual(out, expected)
|
'Test removing a feature with DISM'
| def test_feature_removed(self):
| expected = {'comment': 'Removed Feat2', 'changes': {'feature': {'old': 'Feat2'}, 'retcode': 0}, 'name': 'Feat2', 'result': True}
mock_removed = MagicMock(side_effect=[['Feat1', 'Feat2'], ['Feat1']])
mock_remove = MagicMock(return_value={'retcode': 0})
with patch.dict(dism.__salt__, {'dism.installed_features': mock_removed, 'dism.remove_feature': mock_remove}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.feature_removed('Feat2')
mock_removed.assert_called_with()
mock_remove.assert_called_once_with('Feat2', False, None, False)
self.assertEqual(out, expected)
|
'Test removing a feature which fails with DISM'
| def test_feature_removed_failure(self):
| expected = {'comment': 'Failed to remove Feat2: Failed', 'changes': {}, 'name': 'Feat2', 'result': False}
mock_removed = MagicMock(side_effect=[['Feat1', 'Feat2'], ['Feat1', 'Feat2']])
mock_remove = MagicMock(return_value={'retcode': 67, 'stdout': 'Failed'})
with patch.dict(dism.__salt__, {'dism.installed_features': mock_removed, 'dism.remove_feature': mock_remove}):
with patch.dict(dism.__opts__, {'test': False}):
out = dism.feature_removed('Feat2')
mock_removed.assert_called_with()
mock_remove.assert_called_once_with('Feat2', False, None, False)
self.assertEqual(out, expected)
|
'Test removing a feature already removed'
| def test_feature_removed_removed(self):
| expected = {'comment': 'The feature Feat2 is already removed', 'changes': {}, 'name': 'Feat2', 'result': True}
mock_removed = MagicMock(side_effect=[['Feat1'], ['Feat1']])
mock_remove = MagicMock()
with patch.dict(dism.__salt__, {'dism.installed_features': mock_removed, 'dism.remove_feature': mock_remove}):
out = dism.feature_removed('Feat2')
mock_removed.assert_called_once_with()
assert (not mock_remove.called)
self.assertEqual(out, expected)
|
'Test installing a package with DISM'
| def test_package_installed(self):
| expected = {'comment': 'Installed Pack2', 'changes': {'package': {'new': 'Pack2'}, 'retcode': 0}, 'name': 'Pack2', 'result': True}
mock_installed = MagicMock(side_effect=[['Pack1'], ['Pack1', 'Pack2']])
mock_add = MagicMock(return_value={'retcode': 0})
mock_info = MagicMock(return_value={'Package Identity': 'Pack2'})
with patch.dict(dism.__salt__, {'dism.installed_packages': mock_installed, 'dism.add_package': mock_add, 'dism.package_info': mock_info}):
with patch.dict(dism.__opts__, {'test': False}):
with patch('os.path.exists'):
out = dism.package_installed('Pack2')
mock_installed.assert_called_with()
mock_add.assert_called_once_with('Pack2', False, False, None, False)
self.assertEqual(out, expected)
|
'Test installing a package which fails with DISM'
| def test_package_installed_failure(self):
| expected = {'comment': 'Failed to install Pack2: Failed', 'changes': {}, 'name': 'Pack2', 'result': False}
mock_installed = MagicMock(side_effect=[['Pack1'], ['Pack1']])
mock_add = MagicMock(return_value={'retcode': 67, 'stdout': 'Failed'})
mock_info = MagicMock(return_value={'Package Identity': 'Pack2'})
with patch.dict(dism.__salt__, {'dism.installed_packages': mock_installed, 'dism.add_package': mock_add, 'dism.package_info': mock_info}):
with patch.dict(dism.__opts__, {'test': False}):
with patch('os.path.exists'):
out = dism.package_installed('Pack2')
mock_installed.assert_called_with()
mock_add.assert_called_once_with('Pack2', False, False, None, False)
self.assertEqual(out, expected)
|
'Test installing a package already installed'
| def test_package_installed_installed(self):
| expected = {'comment': 'The package Pack2 is already installed: Pack2', 'changes': {}, 'name': 'Pack2', 'result': True}
mock_installed = MagicMock(side_effect=[['Pack1', 'Pack2'], ['Pack1', 'Pack2']])
mock_add = MagicMock()
mock_info = MagicMock(return_value={'Package Identity': 'Pack2'})
with patch.dict(dism.__salt__, {'dism.installed_packages': mock_installed, 'dism.add_package': mock_add, 'dism.package_info': mock_info}):
with patch.dict(dism.__opts__, {'test': False}):
with patch('os.path.exists'):
out = dism.package_installed('Pack2')
mock_installed.assert_called_once_with()
assert (not mock_add.called)
self.assertEqual(out, expected)
|
'Test removing a package with DISM'
| def test_package_removed(self):
| expected = {'comment': 'Removed Pack2', 'changes': {'package': {'old': 'Pack2'}, 'retcode': 0}, 'name': 'Pack2', 'result': True}
mock_removed = MagicMock(side_effect=[['Pack1', 'Pack2'], ['Pack1']])
mock_remove = MagicMock(return_value={'retcode': 0})
mock_info = MagicMock(return_value={'Package Identity': 'Pack2'})
with patch.dict(dism.__salt__, {'dism.installed_packages': mock_removed, 'dism.remove_package': mock_remove, 'dism.package_info': mock_info}):
with patch.dict(dism.__opts__, {'test': False}):
with patch('os.path.exists'):
out = dism.package_removed('Pack2')
mock_removed.assert_called_with()
mock_remove.assert_called_once_with('Pack2', None, False)
self.assertEqual(out, expected)
|
'Test removing a package which fails with DISM'
| def test_package_removed_failure(self):
| expected = {'comment': 'Failed to remove Pack2: Failed', 'changes': {}, 'name': 'Pack2', 'result': False}
mock_removed = MagicMock(side_effect=[['Pack1', 'Pack2'], ['Pack1', 'Pack2']])
mock_remove = MagicMock(return_value={'retcode': 67, 'stdout': 'Failed'})
mock_info = MagicMock(return_value={'Package Identity': 'Pack2'})
with patch.dict(dism.__salt__, {'dism.installed_packages': mock_removed, 'dism.remove_package': mock_remove, 'dism.package_info': mock_info}):
with patch.dict(dism.__opts__, {'test': False}):
with patch('os.path.exists'):
out = dism.package_removed('Pack2')
mock_removed.assert_called_with()
mock_remove.assert_called_once_with('Pack2', None, False)
self.assertEqual(out, expected)
|
'Test removing a package already removed'
| def test_package_removed_removed(self):
| expected = {'comment': 'The package Pack2 is already removed', 'changes': {}, 'name': 'Pack2', 'result': True}
mock_removed = MagicMock(side_effect=[['Pack1'], ['Pack1']])
mock_remove = MagicMock()
mock_info = MagicMock(return_value={'Package Identity': 'Pack2'})
with patch.dict(dism.__salt__, {'dism.installed_packages': mock_removed, 'dism.remove_package': mock_remove, 'dism.package_info': mock_info}):
with patch.dict(dism.__opts__, {'test': False}):
with patch('os.path.exists'):
out = dism.package_removed('Pack2')
mock_removed.assert_called_once_with()
assert (not mock_remove.called)
self.assertEqual(out, expected)
|
'Test to ensure the user exists on the Dell DRAC'
| def test_present(self):
| name = 'damian'
password = 'secret'
permission = 'login,test_alerts,clear_logs'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock = MagicMock(return_value=[name])
with patch.dict(drac.__salt__, {'drac.list_users': mock}):
with patch.dict(drac.__opts__, {'test': True}):
comt = '`{0}` already exists'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(drac.present(name, password, permission), ret)
with patch.dict(drac.__opts__, {'test': False}):
comt = '`{0}` already exists'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(drac.present(name, password, permission), ret)
|
'Test to ensure a user does not exist on the Dell DRAC'
| def test_absent(self):
| name = 'damian'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock = MagicMock(return_value=[])
with patch.dict(drac.__salt__, {'drac.list_users': mock}):
with patch.dict(drac.__opts__, {'test': True}):
comt = '`{0}` does not exist'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(drac.absent(name), ret)
with patch.dict(drac.__opts__, {'test': False}):
comt = '`{0}` does not exist'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(drac.absent(name), ret)
|
'Test to ensure the DRAC network settings are consistent'
| def test_network(self):
| ip_ = '10.225.108.29'
netmask = '255.255.255.224'
gateway = '10.225.108.1'
ret = {'name': ip_, 'result': None, 'comment': '', 'changes': {}}
net_info = {'IPv4 settings': {'IP Address': ip_, 'Subnet Mask': netmask, 'Gateway': gateway}}
mock_info = MagicMock(return_value=net_info)
mock_bool = MagicMock(side_effect=[True, False])
with patch.dict(drac.__salt__, {'drac.network_info': mock_info, 'drac.set_network': mock_bool}):
with patch.dict(drac.__opts__, {'test': True}):
self.assertDictEqual(drac.network(ip_, netmask, gateway), ret)
with patch.dict(drac.__opts__, {'test': False}):
comt = 'Network is in the desired state'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(drac.network(ip_, netmask, gateway), ret)
comt = 'unable to configure network'
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(drac.network(ip_, netmask, gateway), ret)
|
'Test to ensure that the specified kernel module is loaded.'
| def test_present(self):
| name = 'cheese'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock_mod_list = MagicMock(return_value=[name])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
comment = 'Kernel module {0} is already present'.format(name)
ret.update({'comment': comment})
self.assertDictEqual(kmod.present(name), ret)
mock_mod_list = MagicMock(return_value=[])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
with patch.dict(kmod.__opts__, {'test': True}):
comment = 'Kernel module {0} is set to be loaded'.format(name)
ret.update({'comment': comment, 'result': None})
self.assertDictEqual(kmod.present(name), ret)
mock_mod_list = MagicMock(return_value=[])
mock_available = MagicMock(return_value=[name])
mock_load = MagicMock(return_value=[name])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list, 'kmod.available': mock_available, 'kmod.load': mock_load}):
with patch.dict(kmod.__opts__, {'test': False}):
comment = 'Loaded kernel module {0}'.format(name)
ret.update({'comment': comment, 'result': True, 'changes': {name: 'loaded'}})
self.assertDictEqual(kmod.present(name), ret)
|
'Test to ensure that multiple kernel modules are loaded.'
| def test_present_multi(self):
| name = 'salted kernel'
mods = ['cheese', 'crackers']
ret = {'name': name, 'result': True, 'changes': {}}
mock_mod_list = MagicMock(return_value=mods)
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
call_ret = kmod.present(name, mods=mods)
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('are already present', comment)
self.assertDictEqual(ret, call_ret)
mock_mod_list = MagicMock(return_value=[])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
with patch.dict(kmod.__opts__, {'test': True}):
call_ret = kmod.present(name, mods=mods)
ret.update({'result': None})
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('are set to be loaded', comment)
self.assertDictEqual(ret, call_ret)
mock_mod_list = MagicMock(return_value=[])
mock_available = MagicMock(return_value=mods)
mock_load = MagicMock(return_value=mods)
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list, 'kmod.available': mock_available, 'kmod.load': mock_load}):
with patch.dict(kmod.__opts__, {'test': False}):
call_ret = kmod.present(name, mods=mods)
ret.update({'result': True, 'changes': {mods[0]: 'loaded', mods[1]: 'loaded'}})
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('Loaded kernel modules', comment)
self.assertDictEqual(ret, call_ret)
|
'Test to verify that the named kernel module is not loaded.'
| def test_absent(self):
| name = 'cheese'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock_mod_list = MagicMock(return_value=[name])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
with patch.dict(kmod.__opts__, {'test': True}):
comment = 'Kernel module {0} is set to be removed'.format(name)
ret.update({'comment': comment, 'result': None})
self.assertDictEqual(kmod.absent(name), ret)
mock_mod_list = MagicMock(return_value=[name])
mock_remove = MagicMock(return_value=[name])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list, 'kmod.remove': mock_remove}):
with patch.dict(kmod.__opts__, {'test': False}):
comment = 'Removed kernel module {0}'.format(name)
ret.update({'comment': comment, 'result': True, 'changes': {name: 'removed'}})
self.assertDictEqual(kmod.absent(name), ret)
mock_mod_list = MagicMock(return_value=[])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
with patch.dict(kmod.__opts__, {'test': True}):
comment = 'Kernel module {0} is already removed'.format(name)
ret.update({'comment': comment, 'result': True, 'changes': {}})
self.assertDictEqual(kmod.absent(name), ret)
|
'Test to verify that multiple kernel modules are not loaded.'
| def test_absent_multi(self):
| name = 'salted kernel'
mods = ['cheese', 'crackers']
ret = {'name': name, 'result': True, 'changes': {}}
mock_mod_list = MagicMock(return_value=mods)
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
with patch.dict(kmod.__opts__, {'test': True}):
ret.update({'result': None})
call_ret = kmod.absent(name, mods=mods)
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('are set to be removed', comment)
self.assertDictEqual(ret, call_ret)
mock_mod_list = MagicMock(return_value=mods)
mock_remove = MagicMock(return_value=mods)
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list, 'kmod.remove': mock_remove}):
with patch.dict(kmod.__opts__, {'test': False}):
call_ret = kmod.absent(name, mods=mods)
ret.update({'result': True, 'changes': {mods[0]: 'removed', mods[1]: 'removed'}})
comment = call_ret.pop('comment')
self.assertIn('cheese', comment)
self.assertIn('crackers', comment)
self.assertIn('Removed kernel modules', comment)
self.assertDictEqual(ret, call_ret)
mock_mod_list = MagicMock(return_value=[])
with patch.dict(kmod.__salt__, {'kmod.mod_list': mock_mod_list}):
with patch.dict(kmod.__opts__, {'test': True}):
comment = 'Kernel modules {0} are already removed'.format(', '.join(mods))
ret.update({'comment': comment, 'result': True, 'changes': {}})
self.assertDictEqual(kmod.absent(name, mods=mods), ret)
|
'Test to verify the chain is exist.'
| def test_chain_present(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
mock = MagicMock(side_effect=[True, False, False, False])
with patch.dict(iptables.__salt__, {'iptables.check_chain': mock}):
ret.update({'comment': 'iptables salt chain is already exist in filter table for ipv4'})
self.assertDictEqual(iptables.chain_present('salt'), ret)
with patch.dict(iptables.__opts__, {'test': True}):
ret.update({'comment': 'iptables salt chain in filter table needs to be set for ipv4', 'result': None})
self.assertDictEqual(iptables.chain_present('salt'), ret)
with patch.dict(iptables.__opts__, {'test': False}):
mock = MagicMock(side_effect=[True, ''])
with patch.dict(iptables.__salt__, {'iptables.new_chain': mock}):
ret.update({'result': True, 'comment': 'iptables salt chain in filter table create success for ipv4', 'changes': {'locale': 'salt'}})
self.assertDictEqual(iptables.chain_present('salt'), ret)
ret.update({'changes': {}, 'result': False, 'comment': 'Failed to create salt chain in filter table: for ipv4'})
self.assertDictEqual(iptables.chain_present('salt'), ret)
|
'Test to verify the chain is absent.'
| def test_chain_absent(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
mock = MagicMock(side_effect=[False, True, True, True])
with patch.dict(iptables.__salt__, {'iptables.check_chain': mock}):
ret.update({'comment': 'iptables salt chain is already absent in filter table for ipv4'})
self.assertDictEqual(iptables.chain_absent('salt'), ret)
with patch.dict(iptables.__opts__, {'test': True}):
ret.update({'comment': 'iptables salt chain in filter table needs to be removed ipv4', 'result': None})
self.assertDictEqual(iptables.chain_absent('salt'), ret)
with patch.dict(iptables.__opts__, {'test': False}):
mock = MagicMock(side_effect=[False, 'a'])
with patch.dict(iptables.__salt__, {'iptables.flush': mock}):
mock = MagicMock(return_value=True)
with patch.dict(iptables.__salt__, {'iptables.delete_chain': mock}):
ret.update({'changes': {'locale': 'salt'}, 'comment': 'iptables salt chain in filter table delete success for ipv4', 'result': True})
self.assertDictEqual(iptables.chain_absent('salt'), ret)
ret.update({'changes': {}, 'result': False, 'comment': 'Failed to flush salt chain in filter table: a for ipv4'})
self.assertDictEqual(iptables.chain_absent('salt'), ret)
|
'Test to append a rule to a chain'
| def test_append(self):
| ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''}
self.assertDictEqual(iptables.append('salt', rules=[]), ret)
mock = MagicMock(return_value=[])
with patch.object(iptables, '_STATE_INTERNAL_KEYWORDS', mock):
mock = MagicMock(return_value='a')
with patch.dict(iptables.__salt__, {'iptables.build_rule': mock}):
mock = MagicMock(side_effect=[True, False, False, False])
with patch.dict(iptables.__salt__, {'iptables.check': mock}):
ret.update({'comment': 'iptables rule for salt already set (a) for ipv4', 'result': True})
self.assertDictEqual(iptables.append('salt', table='', chain=''), ret)
with patch.dict(iptables.__opts__, {'test': True}):
ret.update({'result': None, 'comment': 'iptables rule for salt needs to be set (a) for ipv4'})
self.assertDictEqual(iptables.append('salt', table='', chain=''), ret)
with patch.dict(iptables.__opts__, {'test': False}):
mock = MagicMock(side_effect=[True, False])
with patch.dict(iptables.__salt__, {'iptables.append': mock}):
ret.update({'changes': {'locale': 'salt'}, 'result': True, 'comment': 'Set iptables rule for salt to: a for ipv4'})
self.assertDictEqual(iptables.append('salt', table='', chain=''), ret)
ret.update({'changes': {}, 'result': False, 'comment': 'Failed to set iptables rule for salt.\nAttempted rule was a for ipv4'})
self.assertDictEqual(iptables.append('salt', table='', chain=''), ret)
|
'Test to insert a rule into a chain'
| def test_insert(self):
| ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''}
self.assertDictEqual(iptables.insert('salt', rules=[]), ret)
mock = MagicMock(return_value=[])
with patch.object(iptables, '_STATE_INTERNAL_KEYWORDS', mock):
mock = MagicMock(return_value='a')
with patch.dict(iptables.__salt__, {'iptables.build_rule': mock}):
mock = MagicMock(side_effect=[True, False, False, False])
with patch.dict(iptables.__salt__, {'iptables.check': mock}):
ret.update({'comment': 'iptables rule for salt already set for ipv4 (a)', 'result': True})
self.assertDictEqual(iptables.insert('salt', table='', chain=''), ret)
with patch.dict(iptables.__opts__, {'test': True}):
ret.update({'result': None, 'comment': 'iptables rule for salt needs to be set for ipv4 (a)'})
self.assertDictEqual(iptables.insert('salt', table='', chain=''), ret)
with patch.dict(iptables.__opts__, {'test': False}):
mock = MagicMock(side_effect=[False, True])
with patch.dict(iptables.__salt__, {'iptables.insert': mock}):
ret.update({'changes': {'locale': 'salt'}, 'result': True, 'comment': 'Set iptables rule for salt to: a for ipv4'})
self.assertDictEqual(iptables.insert('salt', table='', chain='', position=''), ret)
ret.update({'changes': {}, 'result': False, 'comment': 'Failed to set iptables rule for salt.\nAttempted rule was a'})
self.assertDictEqual(iptables.insert('salt', table='', chain='', position=''), ret)
|
'Test to delete a rule to a chain'
| def test_delete(self):
| ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''}
self.assertDictEqual(iptables.delete('salt', rules=[]), ret)
mock = MagicMock(return_value=[])
with patch.object(iptables, '_STATE_INTERNAL_KEYWORDS', mock):
mock = MagicMock(return_value='a')
with patch.dict(iptables.__salt__, {'iptables.build_rule': mock}):
mock = MagicMock(side_effect=[False, True, True, True])
with patch.dict(iptables.__salt__, {'iptables.check': mock}):
ret.update({'comment': 'iptables rule for salt already absent for ipv4 (a)', 'result': True})
self.assertDictEqual(iptables.delete('salt', table='', chain=''), ret)
with patch.dict(iptables.__opts__, {'test': True}):
ret.update({'result': None, 'comment': 'iptables rule for salt needs to be deleted for ipv4 (a)'})
self.assertDictEqual(iptables.delete('salt', table='', chain=''), ret)
with patch.dict(iptables.__opts__, {'test': False}):
mock = MagicMock(side_effect=[False, True])
with patch.dict(iptables.__salt__, {'iptables.delete': mock}):
ret.update({'result': True, 'changes': {'locale': 'salt'}, 'comment': 'Delete iptables rule for salt a'})
self.assertDictEqual(iptables.delete('salt', table='', chain='', position=''), ret)
ret.update({'result': False, 'changes': {}, 'comment': 'Failed to delete iptables rule for salt.\nAttempted rule was a'})
self.assertDictEqual(iptables.delete('salt', table='', chain='', position=''), ret)
|
'Test to sets the default policy for iptables firewall tables'
| def test_set_policy(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
mock = MagicMock(return_value=[])
with patch.object(iptables, '_STATE_INTERNAL_KEYWORDS', mock):
mock = MagicMock(return_value='stack')
with patch.dict(iptables.__salt__, {'iptables.get_policy': mock}):
ret.update({'comment': 'iptables default policy for chain on table for ipv4 already set to stack'})
self.assertDictEqual(iptables.set_policy('salt', table='', chain='', policy='stack'), ret)
with patch.dict(iptables.__opts__, {'test': True}):
ret.update({'comment': 'iptables default policy for chain on table for ipv4 needs to be set to sal', 'result': None})
self.assertDictEqual(iptables.set_policy('salt', table='', chain='', policy='sal'), ret)
with patch.dict(iptables.__opts__, {'test': False}):
mock = MagicMock(side_effect=[False, True])
with patch.dict(iptables.__salt__, {'iptables.set_policy': mock}):
ret.update({'changes': {'locale': 'salt'}, 'comment': 'Set default policy for to sal family ipv4', 'result': True})
self.assertDictEqual(iptables.set_policy('salt', table='', chain='', policy='sal'), ret)
ret.update({'comment': 'Failed to set iptables default policy', 'result': False, 'changes': {}})
self.assertDictEqual(iptables.set_policy('salt', table='', chain='', policy='sal'), ret)
|
'Test to flush current iptables state'
| def test_flush(self):
| ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''}
mock = MagicMock(return_value=[])
with patch.object(iptables, '_STATE_INTERNAL_KEYWORDS', mock):
with patch.dict(iptables.__opts__, {'test': True}):
ret.update({'comment': 'iptables rules in salt table filter chain ipv4 family needs to be flushed'})
self.assertDictEqual(iptables.flush('salt'), ret)
with patch.dict(iptables.__opts__, {'test': False}):
mock = MagicMock(side_effect=[False, True])
with patch.dict(iptables.__salt__, {'iptables.flush': mock}):
ret.update({'changes': {'locale': 'salt'}, 'comment': 'Flush iptables rules in table chain ipv4 family', 'result': True})
self.assertDictEqual(iptables.flush('salt', table='', chain=''), ret)
ret.update({'changes': {}, 'comment': 'Failed to flush iptables rules', 'result': False})
self.assertDictEqual(iptables.flush('salt', table='', chain=''), ret)
|
'Test to mod_aggregate function'
| def test_mod_aggregate(self):
| self.assertDictEqual(iptables.mod_aggregate({'fun': 'salt'}, [], []), {'fun': 'salt'})
self.assertDictEqual(iptables.mod_aggregate({'fun': 'append'}, [], []), {'fun': 'append'})
|
'Test to verify that the overlay is present.'
| def test_present(self):
| name = 'sunrise'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock = MagicMock(side_effect=[[name], []])
with patch.dict(layman.__salt__, {'layman.list_local': mock}):
comt = 'Overlay {0} already present'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(layman.present(name), ret)
with patch.dict(layman.__opts__, {'test': True}):
comt = 'Overlay {0} is set to be added'.format(name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(layman.present(name), ret)
|
'Test to verify that the overlay is absent.'
| def test_absent(self):
| name = 'sunrise'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock = MagicMock(side_effect=[[], [name]])
with patch.dict(layman.__salt__, {'layman.list_local': mock}):
comt = 'Overlay {0} already absent'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(layman.absent(name), ret)
with patch.dict(layman.__opts__, {'test': True}):
comt = 'Overlay {0} is set to be deleted'.format(name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(layman.absent(name), ret)
|
'Test to make sure we can set the proxy settings on macOS'
| def test_set_proxy_macos(self):
| with patch.dict(proxy.__grains__, {'os': 'Darwin'}):
expected = {'changes': {'new': [{'port': '3128', 'server': '192.168.0.1', 'service': 'http', 'user': 'frank'}, {'port': '3128', 'server': '192.168.0.1', 'service': 'https', 'user': 'frank'}, {'port': '3128', 'server': '192.168.0.1', 'service': 'ftp', 'user': 'frank'}, {'bypass_domains': ['salt.com', 'test.com']}]}, 'comment': 'http proxy settings updated correctly\nhttps proxy settings updated correctly\nftp proxy settings updated correctly\nProxy bypass domains updated correctly\n', 'name': '192.168.0.1', 'result': True}
set_proxy_mock = MagicMock(return_value=True)
patches = {'proxy.get_http_proxy': MagicMock(return_value={}), 'proxy.get_https_proxy': MagicMock(return_value={}), 'proxy.get_ftp_proxy': MagicMock(return_value={}), 'proxy.get_proxy_bypass': MagicMock(return_value=[]), 'proxy.set_http_proxy': set_proxy_mock, 'proxy.set_https_proxy': set_proxy_mock, 'proxy.set_ftp_proxy': set_proxy_mock, 'proxy.set_proxy_bypass': set_proxy_mock}
with patch.dict(proxy.__salt__, patches):
out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd', bypass_domains=['salt.com', 'test.com'])
if six.PY3:
out['changes']['new'][(-1)]['bypass_domains'] = sorted(out['changes']['new'][(-1)]['bypass_domains'])
calls = [call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'), call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'), call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'), call(['salt.com', 'test.com'], 'Ethernet')]
set_proxy_mock.assert_has_calls(calls)
self.assertEqual(out, expected)
|
'Test to make sure we can set the proxy settings on macOS'
| def test_set_proxy_macos_same(self):
| with patch.dict(proxy.__grains__, {'os': 'Darwin'}):
expected = {'changes': {}, 'comment': 'http proxy settings already set.\nhttps proxy settings already set.\nftp proxy settings already set.\nProxy bypass domains are already set correctly.\n', 'name': '192.168.0.1', 'result': True}
proxy_val = {'enabled': True, 'server': '192.168.0.1', 'port': '3128'}
set_proxy_mock = MagicMock()
patches = {'proxy.get_http_proxy': MagicMock(return_value=proxy_val), 'proxy.get_https_proxy': MagicMock(return_value=proxy_val), 'proxy.get_ftp_proxy': MagicMock(return_value=proxy_val), 'proxy.get_proxy_bypass': MagicMock(return_value=['test.com', 'salt.com']), 'proxy.set_http_proxy': set_proxy_mock, 'proxy.set_https_proxy': set_proxy_mock, 'proxy.set_ftp_proxy': set_proxy_mock, 'proxy.set_proxy_bypass': set_proxy_mock}
with patch.dict(proxy.__salt__, patches):
out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd', bypass_domains=['salt.com', 'test.com'])
assert (not set_proxy_mock.called)
self.assertEqual(out, expected)
|
'Test to make sure we can set the proxy settings on Windows'
| def test_set_proxy_windows(self):
| with patch.dict(proxy.__grains__, {'os': 'Windows'}):
expected = {'changes': {}, 'comment': 'Proxy settings updated correctly', 'name': '192.168.0.1', 'result': True}
set_proxy_mock = MagicMock(return_value=True)
patches = {'proxy.get_proxy_win': MagicMock(return_value={}), 'proxy.get_proxy_bypass': MagicMock(return_value=[]), 'proxy.set_proxy_win': set_proxy_mock}
with patch.dict(proxy.__salt__, patches):
out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd', bypass_domains=['salt.com', 'test.com'])
set_proxy_mock.assert_called_once_with('192.168.0.1', '3128', ['http', 'https', 'ftp'], ['salt.com', 'test.com'])
self.assertEqual(out, expected)
|
'Test to make sure we can set the proxy settings on Windows'
| def test_set_proxy_windows_same(self):
| with patch.dict(proxy.__grains__, {'os': 'Windows'}):
expected = {'changes': {}, 'comment': 'Proxy settings already correct.', 'name': '192.168.0.1', 'result': True}
proxy_val = {'enabled': True, 'http': {'enabled': True, 'server': '192.168.0.1', 'port': '3128'}, 'https': {'enabled': True, 'server': '192.168.0.1', 'port': '3128'}, 'ftp': {'enabled': True, 'server': '192.168.0.1', 'port': '3128'}}
set_proxy_mock = MagicMock(return_value=True)
patches = {'proxy.get_proxy_win': MagicMock(return_value=proxy_val), 'proxy.get_proxy_bypass': MagicMock(return_value=['salt.com', 'test.com']), 'proxy.set_proxy_win': set_proxy_mock}
with patch.dict(proxy.__salt__, patches):
out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd', bypass_domains=['salt.com', 'test.com'])
assert (not set_proxy_mock.called)
self.assertEqual(out, expected)
|
'Tests present on a function that does not exist.'
| def test_present_when_function_does_not_exist(self):
| self.conn.list_functions.side_effect = [{'Functions': []}, {'Functions': [function_ret]}]
self.conn.create_function.return_value = function_ret
with patch.dict(self.funcs, {'boto_iam.get_account_id': MagicMock(return_value='1234')}):
with TempZipFile() as zipfile:
result = self.salt_states['boto_lambda.function_present']('function present', FunctionName=function_ret['FunctionName'], Runtime=function_ret['Runtime'], Role=function_ret['Role'], Handler=function_ret['Handler'], ZipFile=zipfile)
self.assertTrue(result['result'])
self.assertEqual(result['changes']['new']['function']['FunctionName'], function_ret['FunctionName'])
|
'Tests absent on a function that does not exist.'
| def test_absent_when_function_does_not_exist(self):
| self.conn.list_functions.return_value = {'Functions': [function_ret]}
result = self.salt_states['boto_lambda.function_absent']('test', 'myfunc')
self.assertTrue(result['result'])
self.assertEqual(result['changes'], {})
|
'Tests present on a alias that does not exist.'
| def test_present_when_alias_does_not_exist(self):
| self.conn.list_aliases.side_effect = [{'Aliases': []}, {'Aliases': [alias_ret]}]
self.conn.create_alias.return_value = alias_ret
result = self.salt_states['boto_lambda.alias_present']('alias present', FunctionName='testfunc', Name=alias_ret['Name'], FunctionVersion=alias_ret['FunctionVersion'])
self.assertTrue(result['result'])
self.assertEqual(result['changes']['new']['alias']['Name'], alias_ret['Name'])
|
'Tests absent on a alias that does not exist.'
| def test_absent_when_alias_does_not_exist(self):
| self.conn.list_aliases.return_value = {'Aliases': [alias_ret]}
result = self.salt_states['boto_lambda.alias_absent']('alias absent', FunctionName='testfunc', Name='myalias')
self.assertTrue(result['result'])
self.assertEqual(result['changes'], {})
|
'Tests present on a event_source_mapping that does not exist.'
| def test_present_when_event_source_mapping_does_not_exist(self):
| self.conn.list_event_source_mappings.side_effect = [{'EventSourceMappings': []}, {'EventSourceMappings': [event_source_mapping_ret]}]
self.conn.get_event_source_mapping.return_value = event_source_mapping_ret
self.conn.create_event_source_mapping.return_value = event_source_mapping_ret
result = self.salt_states['boto_lambda.event_source_mapping_present']('event source mapping present', EventSourceArn=event_source_mapping_ret['EventSourceArn'], FunctionName='myfunc', StartingPosition='LATEST')
self.assertTrue(result['result'])
self.assertEqual(result['changes']['new']['event_source_mapping']['UUID'], event_source_mapping_ret['UUID'])
|
'Tests absent on a event_source_mapping that does not exist.'
| def test_absent_when_event_source_mapping_does_not_exist(self):
| self.conn.list_event_source_mappings.return_value = {'EventSourceMappings': []}
result = self.salt_states['boto_lambda.event_source_mapping_absent']('event source mapping absent', EventSourceArn=event_source_mapping_ret['EventSourceArn'], FunctionName='myfunc')
self.assertTrue(result['result'])
self.assertEqual(result['changes'], {})
|
'Test docker_volume.present'
| def test_present(self):
| volumes = []
default_driver = 'dummy_default'
def create_volume(name, driver=None, driver_opts=None):
for v in volumes:
self.assertNotEqual(v['Name'], name)
if (driver is None):
driver = default_driver
new = {'Name': name, 'Driver': driver}
volumes.append(new)
return new
def remove_volume(name):
old_len = len(volumes)
removed = [v for v in volumes if (v['Name'] == name)]
self.assertEqual(1, len(removed))
volumes.remove(removed[0])
return removed[0]
docker_create_volume = Mock(side_effect=create_volume)
__salt__ = {'docker.create_volume': docker_create_volume, 'docker.volumes': Mock(return_value={'Volumes': volumes}), 'docker.remove_volume': Mock(side_effect=remove_volume)}
with patch.dict(docker_state.__dict__, {'__salt__': __salt__}):
ret = docker_state.present('volume_foo')
docker_create_volume.assert_called_with('volume_foo', driver=None, driver_opts=None)
self.assertEqual({'name': 'volume_foo', 'comment': '', 'changes': {'created': {'Driver': default_driver, 'Name': 'volume_foo'}}, 'result': True}, ret)
self.assertEqual(len(volumes), 1)
self.assertEqual(volumes[0]['Name'], 'volume_foo')
self.assertIs(volumes[0]['Driver'], default_driver)
orig_volumes = [volumes[0].copy()]
ret = docker_state.present('volume_foo')
self.assertEqual({'name': 'volume_foo', 'comment': "Volume 'volume_foo' already exists.", 'changes': {}, 'result': True}, ret)
self.assertEqual(orig_volumes, volumes)
ret = docker_state.present('volume_foo', driver='local')
self.assertEqual({'name': 'volume_foo', 'comment': "Driver for existing volume 'volume_foo' ('dummy_default') does not match specified driver ('local') and force is False", 'changes': {}, 'result': False}, ret)
self.assertEqual(orig_volumes, volumes)
ret = docker_state.present('volume_foo', driver='local', force=True)
self.assertEqual({'name': 'volume_foo', 'comment': '', 'changes': {'removed': {'Driver': default_driver, 'Name': 'volume_foo'}, 'created': {'Driver': 'local', 'Name': 'volume_foo'}}, 'result': True}, ret)
mod_orig_volumes = [orig_volumes[0].copy()]
mod_orig_volumes[0]['Driver'] = 'local'
self.assertEqual(mod_orig_volumes, volumes)
|
'Test docker_volume.present'
| def test_present_with_another_driver(self):
| docker_create_volume = Mock(return_value='created')
docker_remove_volume = Mock(return_value='removed')
__salt__ = {'docker.create_volume': docker_create_volume, 'docker.remove_volume': docker_remove_volume, 'docker.volumes': Mock(return_value={'Volumes': [{'Name': 'volume_foo', 'Driver': 'foo'}]})}
with patch.dict(docker_state.__dict__, {'__salt__': __salt__}):
ret = docker_state.present('volume_foo', driver='bar', force=True)
docker_remove_volume.assert_called_with('volume_foo')
docker_create_volume.assert_called_with('volume_foo', driver='bar', driver_opts=None)
self.assertEqual(ret, {'name': 'volume_foo', 'comment': '', 'changes': {'created': 'created', 'removed': 'removed'}, 'result': True})
|
'Test docker_volume.present without existing volumes.'
| def test_present_wo_existing_volumes(self):
| docker_create_volume = Mock(return_value='created')
docker_remove_volume = Mock(return_value='removed')
__salt__ = {'docker.create_volume': docker_create_volume, 'docker.remove_volume': docker_remove_volume, 'docker.volumes': Mock(return_value={'Volumes': None})}
with patch.dict(docker_state.__dict__, {'__salt__': __salt__}):
ret = docker_state.present('volume_foo', driver='bar', force=True)
docker_create_volume.assert_called_with('volume_foo', driver='bar', driver_opts=None)
self.assertEqual(ret, {'name': 'volume_foo', 'comment': '', 'changes': {'created': 'created'}, 'result': True})
|
'Test docker_volume.absent'
| def test_absent(self):
| docker_remove_volume = Mock(return_value='removed')
__salt__ = {'docker.remove_volume': docker_remove_volume, 'docker.volumes': Mock(return_value={'Volumes': [{'Name': 'volume_foo'}]})}
with patch.dict(docker_state.__dict__, {'__salt__': __salt__}):
ret = docker_state.absent('volume_foo')
docker_remove_volume.assert_called_with('volume_foo')
self.assertEqual(ret, {'name': 'volume_foo', 'comment': '', 'changes': {'removed': 'removed'}, 'result': True})
|
'Test to ensure the current node joined
to a cluster with node user@host'
| def test_joined(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
mock = MagicMock(side_effect=[['rahulha@salt'], [''], ['']])
with patch.dict(rabbitmq_cluster.__salt__, {'rabbitmq.cluster_status': mock}):
ret.update({'comment': 'Already in cluster'})
self.assertDictEqual(rabbitmq_cluster.joined('salt', 'salt', 'rahulha'), ret)
with patch.dict(rabbitmq_cluster.__opts__, {'test': True}):
ret.update({'result': None, 'comment': 'Node is set to join cluster rahulha@salt', 'changes': {'new': 'rahulha@salt', 'old': ''}})
self.assertDictEqual(rabbitmq_cluster.joined('salt', 'salt', 'rahulha'), ret)
with patch.dict(rabbitmq_cluster.__opts__, {'test': False}):
mock = MagicMock(return_value={'Error': 'ERR'})
with patch.dict(rabbitmq_cluster.__salt__, {'rabbitmq.join_cluster': mock}):
ret.update({'result': False, 'comment': 'ERR', 'changes': {}})
self.assertDictEqual(rabbitmq_cluster.joined('salt', 'salt', 'rahulha'), ret)
|
'Test to set a registry entry.'
| def test_present(self):
| name = 'HKEY_CURRENT_USER\\SOFTWARE\\Salt'
vname = 'version'
vdata = '0.15.3'
ret = {'name': name, 'changes': {}, 'result': True, 'comment': '{0} in {1} is already configured'.format(vname, name)}
mock_read = MagicMock(side_effect=[{'vdata': vdata, 'success': True}, {'vdata': 'a', 'success': True}, {'vdata': 'a', 'success': True}])
mock_t = MagicMock(return_value=True)
with patch.dict(reg.__salt__, {'reg.read_value': mock_read, 'reg.set_value': mock_t}):
self.assertDictEqual(reg.present(name, vname=vname, vdata=vdata), ret)
with patch.dict(reg.__opts__, {'test': True}):
ret.update({'comment': '', 'result': None, 'changes': {'reg': {'Will add': {'Key': name, 'Entry': vname, 'Value': vdata}}}})
self.assertDictEqual(reg.present(name, vname=vname, vdata=vdata), ret)
with patch.dict(reg.__opts__, {'test': False}):
ret.update({'comment': 'Added {0} to {0}'.format(name), 'result': True, 'changes': {'reg': {'Added': {'Key': name, 'Entry': vname, 'Value': vdata}}}})
self.assertDictEqual(reg.present(name, vname=vname, vdata=vdata), ret)
|
'Test to remove a registry entry.'
| def test_absent(self):
| hive = 'HKEY_CURRENT_USER'
key = 'SOFTWARE\\Salt'
name = ((hive + '\\') + key)
vname = 'version'
vdata = '0.15.3'
ret = {'name': name, 'changes': {}, 'result': True, 'comment': '{0} is already absent'.format(name)}
mock_read_true = MagicMock(return_value={'success': True, 'vdata': vdata})
mock_read_false = MagicMock(return_value={'success': False, 'vdata': False})
mock_t = MagicMock(return_value=True)
with patch.dict(reg.__salt__, {'reg.read_value': mock_read_false, 'reg.delete_value': mock_t}):
self.assertDictEqual(reg.absent(name, vname), ret)
with patch.dict(reg.__salt__, {'reg.read_value': mock_read_true}):
with patch.dict(reg.__opts__, {'test': True}):
ret.update({'comment': '', 'result': None, 'changes': {'reg': {'Will remove': {'Entry': vname, 'Key': name}}}})
self.assertDictEqual(reg.absent(name, vname), ret)
with patch.dict(reg.__salt__, {'reg.read_value': mock_read_true, 'reg.delete_value': mock_t}):
with patch.dict(reg.__opts__, {'test': False}):
ret.update({'result': True, 'changes': {'reg': {'Removed': {'Entry': vname, 'Key': name}}}, 'comment': 'Removed {0} from {1}'.format(key, hive)})
self.assertDictEqual(reg.absent(name, vname), ret)
|
'Test to stop the named worker from the lbn load balancers
at the targeted minions.'
| def test_stop(self):
| name = "{{ grains['id'] }}"
lbn = 'application'
target = 'roles:balancer'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
comt = 'no servers answered the published command modjk.worker_status'
mock = MagicMock(return_value=False)
with patch.dict(modjk_worker.__salt__, {'publish.publish': mock}):
ret.update({'comment': comt})
self.assertDictEqual(modjk_worker.stop(name, lbn, target), ret)
|
'Test to activate the named worker from the lbn load balancers
at the targeted minions.'
| def test_activate(self):
| name = "{{ grains['id'] }}"
lbn = 'application'
target = 'roles:balancer'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
comt = 'no servers answered the published command modjk.worker_status'
mock = MagicMock(return_value=False)
with patch.dict(modjk_worker.__salt__, {'publish.publish': mock}):
ret.update({'comment': comt})
self.assertDictEqual(modjk_worker.activate(name, lbn, target), ret)
|
'Test to disable the named worker from the lbn load balancers
at the targeted minions.'
| def test_disable(self):
| name = "{{ grains['id'] }}"
lbn = 'application'
target = 'roles:balancer'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
comt = 'no servers answered the published command modjk.worker_status'
mock = MagicMock(return_value=False)
with patch.dict(modjk_worker.__salt__, {'publish.publish': mock}):
ret.update({'comment': comt})
self.assertDictEqual(modjk_worker.disable(name, lbn, target), ret)
|
'Test to ensure that the named database is present
with the specified properties.'
| def test_present(self):
| name = 'main'
version = '9.4'
ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}
mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False)
infos = {'{0}/{1}'.format(version, name): {}}
mock = MagicMock(return_value=infos)
with patch.dict(postgres_cluster.__salt__, {'postgres.cluster_list': mock, 'postgres.cluster_exists': mock_t, 'postgres.cluster_create': mock_t}):
comt = 'Cluster {0}/{1} is already present'.format(version, name)
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(postgres_cluster.present(version, name), ret)
infos['{0}/{1}'.format(version, name)]['port'] = 5433
comt = "Cluster {0}/{1} has wrong parameters which couldn't be changed on fly.".format(version, name)
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(postgres_cluster.present(version, name, port=5434), ret)
infos['{0}/{1}'.format(version, name)]['datadir'] = '/tmp/'
comt = "Cluster {0}/{1} has wrong parameters which couldn't be changed on fly.".format(version, name)
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(postgres_cluster.present(version, name, port=5434), ret)
with patch.dict(postgres_cluster.__salt__, {'postgres.cluster_list': mock, 'postgres.cluster_exists': mock_f, 'postgres.cluster_create': mock_t}):
comt = 'The cluster {0}/{1} has been created'.format(version, name)
ret.update({'comment': comt, 'result': True, 'changes': {'{0}/{1}'.format(version, name): 'Present'}})
self.assertDictEqual(postgres_cluster.present(version, name), ret)
with patch.dict(postgres_cluster.__opts__, {'test': True}):
comt = 'Cluster {0}/{1} is set to be created'.format(version, name)
ret.update({'comment': comt, 'result': None, 'changes': {}})
self.assertDictEqual(postgres_cluster.present(version, name), ret)
with patch.dict(postgres_cluster.__salt__, {'postgres.cluster_list': mock, 'postgres.cluster_exists': mock_f, 'postgres.cluster_create': mock_f}):
comt = 'Failed to create cluster {0}/{1}'.format(version, name)
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(postgres_cluster.present(version, name), ret)
|
'Test to ensure that the named database is absent.'
| def test_absent(self):
| name = 'main'
version = '9.4'
ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}
mock_t = MagicMock(return_value=True)
mock = MagicMock(side_effect=[True, True, False])
with patch.dict(postgres_cluster.__salt__, {'postgres.cluster_exists': mock, 'postgres.cluster_remove': mock_t}):
with patch.dict(postgres_cluster.__opts__, {'test': True}):
comt = 'Cluster {0}/{1} is set to be removed'.format(version, name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(postgres_cluster.absent(version, name), ret)
with patch.dict(postgres_cluster.__opts__, {'test': False}):
comt = 'Cluster {0}/{1} has been removed'.format(version, name)
ret.update({'comment': comt, 'result': True, 'changes': {name: 'Absent'}})
self.assertDictEqual(postgres_cluster.absent(version, name), ret)
comt = 'Cluster {0}/{1} is not present, so it cannot be removed'.format(version, name)
ret.update({'comment': comt, 'result': True, 'changes': {}})
self.assertDictEqual(postgres_cluster.absent(version, name), ret)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.