desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Test that renderers have: - __salt__ # Execution functions (i.e. __salt__[\'test.echo\'](\'foo\')) - __grains__ # Grains (i.e. __grains__[\'os\']) - __pillar__ # Pillar data (i.e. __pillar__[\'foo\']) - __opts__ # Minion configuration options - __context__ # Context dict shared amongst all modules of the same type'
def test_renderers(self):
self._verify_globals(salt.loader.render(self.master_opts, {}))
'Test to verifies that the specified SSH key is present for the specified user.'
def test_present(self):
name = 'sshkeys' user = 'root' source = 'salt://ssh_keys/id_rsa.pub' ret = {'name': name, 'changes': {}, 'result': True, 'comment': ''} mock = MagicMock(return_value='exists') mock_data = MagicMock(side_effect=['replace', 'new']) with patch.dict(ssh_auth.__salt__, {'ssh.check_key': mock, 'ssh.set_auth_key': mock_data}): with patch.dict(ssh_auth.__opts__, {'test': True}): comt = 'The authorized host key sshkeys is already present for user root' ret.update({'comment': comt}) self.assertDictEqual(ssh_auth.present(name, user, source), ret) with patch.dict(ssh_auth.__opts__, {'test': False}): comt = 'The authorized host key sshkeys for user root was updated' ret.update({'comment': comt, 'changes': {name: 'Updated'}}) self.assertDictEqual(ssh_auth.present(name, user, source), ret) comt = 'The authorized host key sshkeys for user root was added' ret.update({'comment': comt, 'changes': {name: 'New'}}) self.assertDictEqual(ssh_auth.present(name, user, source), ret)
'Test to verifies that the specified SSH key is absent.'
def test_absent(self):
name = 'sshkeys' user = 'root' source = 'salt://ssh_keys/id_rsa.pub' ret = {'name': name, 'changes': {}, 'result': None, 'comment': ''} mock = MagicMock(side_effect=['User authorized keys file not present', 'Key removed']) mock_up = MagicMock(side_effect=['update', 'updated']) with patch.dict(ssh_auth.__salt__, {'ssh.rm_auth_key': mock, 'ssh.check_key': mock_up}): with patch.dict(ssh_auth.__opts__, {'test': True}): comt = 'Key sshkeys for user root is set for removal' ret.update({'comment': comt}) self.assertDictEqual(ssh_auth.absent(name, user, source), ret) comt = 'Key is already absent' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(ssh_auth.absent(name, user, source), ret) with patch.dict(ssh_auth.__opts__, {'test': False}): comt = 'User authorized keys file not present' ret.update({'comment': comt, 'result': False}) self.assertDictEqual(ssh_auth.absent(name, user, source), ret) comt = 'Key removed' ret.update({'comment': comt, 'result': True, 'changes': {name: 'Removed'}}) self.assertDictEqual(ssh_auth.absent(name, user, source), ret)
'Test to spin up a single instance on a cloud provider, using salt-cloud.'
def test_present(self):
name = 'mycloud' cloud_provider = 'my_cloud_provider' ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, False]) mock_bool = MagicMock(side_effect=[True, False, False]) mock_dict = MagicMock(return_value={'cloud': 'saltcloud'}) with patch.dict(cloud.__salt__, {'cmd.retcode': mock, 'cloud.has_instance': mock_bool, 'cloud.create': mock_dict}): comt = 'onlyif execution failed' ret.update({'comment': comt}) self.assertDictEqual(cloud.present(name, cloud_provider, onlyif=False), ret) self.assertDictEqual(cloud.present(name, cloud_provider, onlyif=''), ret) comt = 'unless execution succeeded' ret.update({'comment': comt}) self.assertDictEqual(cloud.present(name, cloud_provider, unless=True), ret) self.assertDictEqual(cloud.present(name, cloud_provider, unless=''), ret) comt = 'Already present instance {0}'.format(name) ret.update({'comment': comt}) self.assertDictEqual(cloud.present(name, cloud_provider), ret) with patch.dict(cloud.__opts__, {'test': True}): comt = 'Instance {0} needs to be created'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.present(name, cloud_provider), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = 'Created instance mycloud using provider my_cloud_provider and the following options: {}' ret.update({'comment': comt, 'result': True, 'changes': {'cloud': 'saltcloud'}}) self.assertDictEqual(cloud.present(name, cloud_provider), ret)
'Test to ensure that no instances with the specified names exist.'
def test_absent(self):
name = 'mycloud' ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, False]) mock_bool = MagicMock(side_effect=[False, True, True]) mock_dict = MagicMock(return_value={'cloud': 'saltcloud'}) with patch.dict(cloud.__salt__, {'cmd.retcode': mock, 'cloud.has_instance': mock_bool, 'cloud.destroy': mock_dict}): comt = 'onlyif execution failed' ret.update({'comment': comt}) self.assertDictEqual(cloud.absent(name, onlyif=False), ret) self.assertDictEqual(cloud.absent(name, onlyif=''), ret) comt = 'unless execution succeeded' ret.update({'comment': comt}) self.assertDictEqual(cloud.absent(name, unless=True), ret) self.assertDictEqual(cloud.absent(name, unless=''), ret) comt = 'Already absent instance {0}'.format(name) ret.update({'comment': comt}) self.assertDictEqual(cloud.absent(name), ret) with patch.dict(cloud.__opts__, {'test': True}): comt = 'Instance {0} needs to be destroyed'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.absent(name), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = 'Destroyed instance {0}'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {'cloud': 'saltcloud'}}) self.assertDictEqual(cloud.absent(name), ret)
'Test to create a single instance on a cloud provider, using a salt-cloud profile.'
def test_profile(self):
name = 'mycloud' profile = 'myprofile' ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, False]) mock_dict = MagicMock(side_effect=[{'cloud': 'saltcloud'}, {'Not Actioned': True}, {'Not Actioned': True}, {'Not Found': True, 'Not Actioned/Not Running': True}]) mock_d = MagicMock(return_value={}) with patch.dict(cloud.__salt__, {'cmd.retcode': mock, 'cloud.profile': mock_d, 'cloud.action': mock_dict}): comt = 'onlyif execution failed' ret.update({'comment': comt}) self.assertDictEqual(cloud.profile(name, profile, onlyif=False), ret) self.assertDictEqual(cloud.profile(name, profile, onlyif=''), ret) comt = 'unless execution succeeded' ret.update({'comment': comt}) self.assertDictEqual(cloud.profile(name, profile, unless=True), ret) self.assertDictEqual(cloud.profile(name, profile, unless=''), ret) comt = 'Already present instance {0}'.format(name) ret.update({'comment': comt}) self.assertDictEqual(cloud.profile(name, profile), ret) with patch.dict(cloud.__opts__, {'test': True}): comt = 'Instance {0} needs to be created'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.profile(name, profile), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = 'Failed to create instance {0}using profile {1}'.format(name, profile) ret.update({'comment': comt, 'result': False}) self.assertDictEqual(cloud.profile(name, profile), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = 'Failed to create instance {0}using profile {1}'.format(name, profile) ret.update({'comment': comt, 'result': False}) self.assertDictEqual(cloud.profile(name, profile), ret)
'Test to check that a block volume exists.'
def test_volume_present(self):
name = 'mycloud' ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} mock = MagicMock(return_value=name) mock_lst = MagicMock(side_effect=[[name], [], []]) with patch.dict(cloud.__salt__, {'cloud.volume_list': mock_lst, 'cloud.volume_create': mock}): with patch.object(salt.utils.cloud, 'check_name', MagicMock(return_value=True)): comt = 'Invalid characters in name.' ret.update({'comment': comt}) self.assertDictEqual(cloud.volume_present(name), ret) comt = 'Volume exists: {0}'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(cloud.volume_present(name), ret) with patch.dict(cloud.__opts__, {'test': True}): comt = 'Volume {0} will be created.'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.volume_present(name), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = 'Volume {0} was created'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {'old': None, 'new': name}}) self.assertDictEqual(cloud.volume_present(name), ret)
'Test to check that a block volume exists.'
def test_volume_absent(self):
name = 'mycloud' ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} mock = MagicMock(return_value=False) mock_lst = MagicMock(side_effect=[[], [name], [name]]) with patch.dict(cloud.__salt__, {'cloud.volume_list': mock_lst, 'cloud.volume_delete': mock}): with patch.object(salt.utils.cloud, 'check_name', MagicMock(return_value=True)): comt = 'Invalid characters in name.' ret.update({'comment': comt}) self.assertDictEqual(cloud.volume_absent(name), ret) comt = 'Volume is absent.' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(cloud.volume_absent(name), ret) with patch.dict(cloud.__opts__, {'test': True}): comt = 'Volume {0} will be deleted.'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.volume_absent(name), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = 'Volume {0} failed to delete.'.format(name) ret.update({'comment': comt, 'result': False}) self.assertDictEqual(cloud.volume_absent(name), ret)
'Test to check if a block volume is attached.'
def test_volume_attached(self):
name = 'mycloud' server_name = 'mycloud_server' disk_name = 'trogdor' ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} mock = MagicMock(return_value=False) mock_dict = MagicMock(side_effect=[{name: {'name': disk_name, 'attachments': True}}, {}, {name: {'name': disk_name, 'attachments': False}}, {name: {'name': disk_name, 'attachments': False}}, {name: {'name': disk_name, 'attachments': False}}]) with patch.dict(cloud.__salt__, {'cloud.volume_list': mock_dict, 'cloud.action': mock}): with patch.object(salt.utils.cloud, 'check_name', MagicMock(side_effect=[True, False, True])): comt = 'Invalid characters in name.' ret.update({'comment': comt}) self.assertDictEqual(cloud.volume_attached(name, server_name), ret) ret.update({'name': server_name}) self.assertDictEqual(cloud.volume_attached(name, server_name), ret) comt = 'Volume {0} is already attached: True'.format(disk_name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(cloud.volume_attached(name, server_name), ret) comt = 'Volume {0} does not exist'.format(name) ret.update({'comment': comt, 'result': False}) self.assertDictEqual(cloud.volume_attached(name, server_name), ret) comt = 'Server {0} does not exist'.format(server_name) ret.update({'comment': comt, 'result': False}) self.assertDictEqual(cloud.volume_attached(name, server_name), ret) mock = MagicMock(return_value=True) with patch.dict(cloud.__salt__, {'cloud.action': mock, 'cloud.volume_attach': mock}): with patch.dict(cloud.__opts__, {'test': True}): comt = 'Volume {0} will be will be attached.'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.volume_attached(name, server_name), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = 'Volume {0} was created'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {'new': True, 'old': {'name': disk_name, 'attachments': False}}}) self.assertDictEqual(cloud.volume_attached(name, server_name), ret)
'Test to check if a block volume is detached.'
def test_volume_detached(self):
name = 'mycloud' server_name = 'mycloud_server' disk_name = 'trogdor' ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} mock = MagicMock(return_value=False) mock_dict = MagicMock(side_effect=[{name: {'name': disk_name, 'attachments': False}}, {}, {name: {'name': disk_name, 'attachments': True}}, {name: {'name': disk_name, 'attachments': True}}, {name: {'name': disk_name, 'attachments': True}}]) with patch.dict(cloud.__salt__, {'cloud.volume_list': mock_dict, 'cloud.action': mock}): with patch.object(salt.utils.cloud, 'check_name', MagicMock(side_effect=[True, False, True])): comt = 'Invalid characters in name.' ret.update({'comment': comt}) self.assertDictEqual(cloud.volume_detached(name, server_name), ret) ret.update({'name': server_name}) self.assertDictEqual(cloud.volume_detached(name, server_name), ret) comt = 'Volume {0} is not currently attached to anything.'.format(disk_name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(cloud.volume_detached(name, server_name), ret) comt = 'Volume {0} does not exist'.format(name) ret.update({'comment': comt}) self.assertDictEqual(cloud.volume_detached(name, server_name), ret) comt = 'Server {0} does not exist'.format(server_name) ret.update({'comment': comt}) self.assertDictEqual(cloud.volume_detached(name, server_name), ret) mock = MagicMock(return_value=True) with patch.dict(cloud.__salt__, {'cloud.action': mock, 'cloud.volume_detach': mock}): with patch.dict(cloud.__opts__, {'test': True}): comt = 'Volume {0} will be will be detached.'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.volume_detached(name, server_name), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = 'Volume {0} was created'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {'new': True, 'old': {'name': disk_name, 'attachments': True}}}) self.assertDictEqual(cloud.volume_detached(name, server_name), ret)
'tests present on a thing type that does not exist.'
def test_present_when_thing_type_does_not_exist(self):
self.conn.describe_thing_type.side_effect = [not_found_error, thing_type_ret] self.conn.create_thing_type.return_value = create_thing_type_ret result = self.salt_states['boto_iot.thing_type_present']('thing type present', thingTypeName=thing_type_name, thingTypeDescription=thing_type_desc, searchableAttributesList=[thing_type_attr_1], **conn_parameters) self.assertTrue(result['result']) self.assertEqual(result['changes']['new']['thing_type']['thingTypeName'], thing_type_name)
'Tests absent on a thing type does not exist'
def test_absent_when_thing_type_does_not_exist(self):
self.conn.describe_thing_type.side_effect = not_found_error result = self.salt_states['boto_iot.thing_type_absent']('test', 'mythingtype', **conn_parameters) self.assertTrue(result['result']) self.assertEqual(result['changes'], {})
'Tests absent on a thing type'
def test_absent_when_thing_type_exists(self):
self.conn.describe_thing_type.return_value = deprecated_thing_type_ret result = self.salt_states['boto_iot.thing_type_absent']('test', thing_type_name, **conn_parameters) self.assertTrue(result['result']) self.assertEqual(result['changes']['new']['thing_type'], None) self.assertTrue((self.conn.deprecate_thing_type.call_count == 0))
'Tests present on a policy that does not exist.'
def test_present_when_policy_does_not_exist(self):
self.conn.get_policy.side_effect = [not_found_error, policy_ret] self.conn.create_policy.return_value = policy_ret result = self.salt_states['boto_iot.policy_present']('policy present', policyName=policy_ret['policyName'], policyDocument=policy_ret['policyDocument']) self.assertTrue(result['result']) self.assertEqual(result['changes']['new']['policy']['policyName'], policy_ret['policyName'])
'Tests absent on a policy that does not exist.'
def test_absent_when_policy_does_not_exist(self):
self.conn.get_policy.side_effect = not_found_error result = self.salt_states['boto_iot.policy_absent']('test', 'mypolicy') self.assertTrue(result['result']) self.assertEqual(result['changes'], {})
'Tests attached on a policy that is not attached.'
def test_attached_when_policy_not_attached(self):
self.conn.list_principal_policies.return_value = {'policies': []} result = self.salt_states['boto_iot.policy_attached']('test', 'myfunc', principal) self.assertTrue(result['result']) self.assertTrue(result['changes']['new']['attached'])
'Tests attached on a policy that is attached.'
def test_attached_when_policy_attached(self):
self.conn.list_principal_policies.return_value = {'policies': [policy_ret]} result = self.salt_states['boto_iot.policy_attached']('test', policy_ret['policyName'], principal) self.assertTrue(result['result']) self.assertEqual(result['changes'], {})
'Tests attached on a policy that is attached.'
def test_attached_with_failure(self):
self.conn.list_principal_policies.return_value = {'policies': []} self.conn.attach_principal_policy.side_effect = ClientError(error_content, 'attach_principal_policy') result = self.salt_states['boto_iot.policy_attached']('test', policy_ret['policyName'], principal) self.assertFalse(result['result']) self.assertEqual(result['changes'], {})
'Tests detached on a policy that is not detached.'
def test_detached_when_policy_not_detached(self):
self.conn.list_principal_policies.return_value = {'policies': [policy_ret]} result = self.salt_states['boto_iot.policy_detached']('test', policy_ret['policyName'], principal) self.assertTrue(result['result']) log.warning(result) self.assertFalse(result['changes']['new']['attached'])
'Tests detached on a policy that is detached.'
def test_detached_when_policy_detached(self):
self.conn.list_principal_policies.return_value = {'policies': []} result = self.salt_states['boto_iot.policy_detached']('test', policy_ret['policyName'], principal) self.assertTrue(result['result']) self.assertEqual(result['changes'], {})
'Tests detached on a policy that is detached.'
def test_detached_with_failure(self):
self.conn.list_principal_policies.return_value = {'policies': [policy_ret]} self.conn.detach_principal_policy.side_effect = ClientError(error_content, 'detach_principal_policy') result = self.salt_states['boto_iot.policy_detached']('test', policy_ret['policyName'], principal) self.assertFalse(result['result']) self.assertEqual(result['changes'], {})
'Tests present on a topic_rule that does not exist.'
def test_present_when_topic_rule_does_not_exist(self):
self.conn.get_topic_rule.side_effect = [topic_rule_not_found_error, {'rule': topic_rule_ret}] self.conn.create_topic_rule.return_value = {'created': True} result = self.salt_states['boto_iot.topic_rule_present']('topic rule present', ruleName=topic_rule_ret['ruleName'], sql=topic_rule_ret['sql'], description=topic_rule_ret['description'], actions=topic_rule_ret['actions'], ruleDisabled=topic_rule_ret['ruleDisabled']) self.assertTrue(result['result']) self.assertEqual(result['changes']['new']['rule']['ruleName'], topic_rule_ret['ruleName'])
'Tests absent on a topic rule that does not exist.'
def test_absent_when_topic_rule_does_not_exist(self):
self.conn.get_topic_rule.side_effect = topic_rule_not_found_error result = self.salt_states['boto_iot.topic_rule_absent']('test', 'myrule') self.assertTrue(result['result']) self.assertEqual(result['changes'], {})
'Test to ensure that the cluster admin or database user is present.'
def test_present(self):
name = 'salt' passwd = 'salt' ret = {'name': name, 'result': False, 'comment': '', 'changes': {}} mock = MagicMock(side_effect=[False, False, False, True]) mock_t = MagicMock(side_effect=[True, False]) mock_f = MagicMock(return_value=False) with patch.dict(influxdb08_user.__salt__, {'influxdb08.db_exists': mock_f, 'influxdb08.user_exists': mock, 'influxdb08.user_create': mock_t}): comt = 'Database mydb does not exist' ret.update({'comment': comt}) self.assertDictEqual(influxdb08_user.present(name, passwd, database='mydb'), ret) with patch.dict(influxdb08_user.__opts__, {'test': True}): comt = 'User {0} is not present and needs to be created'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(influxdb08_user.present(name, passwd), ret) with patch.dict(influxdb08_user.__opts__, {'test': False}): comt = 'User {0} has been created'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {'salt': 'Present'}}) self.assertDictEqual(influxdb08_user.present(name, passwd), ret) comt = 'Failed to create user {0}'.format(name) ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(influxdb08_user.present(name, passwd), ret) comt = 'User {0} is already present'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(influxdb08_user.present(name, passwd), ret)
'Test to ensure that the named cluster admin or database user is absent.'
def test_absent(self):
name = 'salt' ret = {'name': name, 'result': None, 'comment': '', 'changes': {}} mock = MagicMock(side_effect=[True, True, True, False]) mock_t = MagicMock(side_effect=[True, False]) with patch.dict(influxdb08_user.__salt__, {'influxdb08.user_exists': mock, 'influxdb08.user_remove': mock_t}): with patch.dict(influxdb08_user.__opts__, {'test': True}): comt = 'User {0} is present and needs to be removed'.format(name) ret.update({'comment': comt}) self.assertDictEqual(influxdb08_user.absent(name), ret) with patch.dict(influxdb08_user.__opts__, {'test': False}): comt = 'User {0} has been removed'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {'salt': 'Absent'}}) self.assertDictEqual(influxdb08_user.absent(name), ret) comt = 'Failed to remove user {0}'.format(name) ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(influxdb08_user.absent(name), ret) comt = 'User {0} is not present, so it cannot be removed'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(influxdb08_user.absent(name), ret)
'Checkout or update the working directory to the latest revision from the remote repository.'
def test_latest(self):
mock = MagicMock(return_value=True) with patch.object(svn, '_fail', mock): self.assertTrue(svn.latest('salt')) mock = MagicMock(side_effect=[True, False, False, False]) with patch.object(os.path, 'exists', mock): mock = MagicMock(return_value=False) with patch.object(os.path, 'isdir', mock): with patch.object(svn, '_fail', mock): self.assertFalse(svn.latest('salt', 'c://salt')) with patch.dict(svn.__opts__, {'test': True}): mock = MagicMock(return_value=['salt']) with patch.object(svn, '_neutral_test', mock): self.assertListEqual(svn.latest('salt', 'c://salt'), ['salt']) mock = MagicMock(side_effect=[False, True]) with patch.object(os.path, 'exists', mock): mock = MagicMock(return_value=True) info_mock = MagicMock(return_value=[{'Revision': 'mocked'}]) with patch.dict(svn.__salt__, {'svn.diff': mock, 'svn.info': info_mock}): mock = MagicMock(return_value=['Dude']) with patch.object(svn, '_neutral_test', mock): self.assertListEqual(svn.latest('salt', 'c://salt'), ['Dude']) with patch.dict(svn.__opts__, {'test': False}): mock = MagicMock(return_value=[{'Revision': 'a'}]) with patch.dict(svn.__salt__, {'svn.info': mock}): mock = MagicMock(return_value=True) with patch.dict(svn.__salt__, {'svn.update': mock}): self.assertDictEqual(svn.latest('salt', 'c://salt'), {'changes': {}, 'comment': True, 'name': 'salt', 'result': True})
'Test to export a file or directory from an SVN repository'
def test_export(self):
mock = MagicMock(return_value=True) with patch.object(svn, '_fail', mock): self.assertTrue(svn.export('salt')) mock = MagicMock(side_effect=[True, False, False, False]) with patch.object(os.path, 'exists', mock): mock = MagicMock(return_value=False) with patch.object(os.path, 'isdir', mock): with patch.object(svn, '_fail', mock): self.assertFalse(svn.export('salt', 'c://salt')) with patch.dict(svn.__opts__, {'test': True}): mock = MagicMock(return_value=['salt']) with patch.object(svn, '_neutral_test', mock): self.assertListEqual(svn.export('salt', 'c://salt'), ['salt']) mock = MagicMock(side_effect=[False, True]) with patch.object(os.path, 'exists', mock): mock = MagicMock(return_value=True) with patch.dict(svn.__salt__, {'svn.list': mock}): mock = MagicMock(return_value=['Dude']) with patch.object(svn, '_neutral_test', mock): self.assertListEqual(svn.export('salt', 'c://salt'), ['Dude']) with patch.dict(svn.__opts__, {'test': False}): mock = MagicMock(return_value=True) with patch.dict(svn.__salt__, {'svn.export': mock}): self.assertDictEqual(svn.export('salt', 'c://salt'), {'changes': 'salt was Exported to c://salt', 'comment': '', 'name': 'salt', 'result': True})
'Test to determine if the working directory has been changed.'
def test_dirty(self):
mock = MagicMock(return_value=True) with patch.object(svn, '_fail', mock): self.assertTrue(svn.dirty('salt', 'c://salt'))
'Test to ensure that the named database is present with the specified properties.'
def test_present(self):
name = 'frank' ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''} mock_t = MagicMock(return_value=True) mock = MagicMock(return_value={name: {}}) with patch.dict(postgres_database.__salt__, {'postgres.db_list': mock, 'postgres.db_alter': mock_t}): comt = 'Database {0} is already present'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(postgres_database.present(name), ret) comt = "Database frank has wrong parameters which couldn't be changed on fly." ret.update({'comment': comt, 'result': False}) self.assertDictEqual(postgres_database.present(name, tablespace='A', lc_collate=True), ret) with patch.dict(postgres_database.__opts__, {'test': True}): comt = 'Database frank exists, but parameters need to be changed' ret.update({'comment': comt, 'result': None}) self.assertDictEqual(postgres_database.present(name, tablespace='A'), ret) with patch.dict(postgres_database.__opts__, {'test': False}): comt = 'Parameters for database frank have been changed' ret.update({'comment': comt, 'result': True, 'changes': {name: 'Parameters changed'}}) self.assertDictEqual(postgres_database.present(name, tablespace='A'), ret)
'Test to ensure that the named database is absent.'
def test_absent(self):
name = 'frank' ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''} mock_t = MagicMock(return_value=True) mock = MagicMock(side_effect=[True, True, False]) with patch.dict(postgres_database.__salt__, {'postgres.db_exists': mock, 'postgres.db_remove': mock_t}): with patch.dict(postgres_database.__opts__, {'test': True}): comt = 'Database {0} is set to be removed'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(postgres_database.absent(name), ret) with patch.dict(postgres_database.__opts__, {'test': False}): comt = 'Database {0} has been removed'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {name: 'Absent'}}) self.assertDictEqual(postgres_database.absent(name), ret) comt = 'Database {0} is not present, so it cannot be removed'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {}}) self.assertDictEqual(postgres_database.absent(name), ret)
'Test to ensure the SQS queue exists.'
def test_exists(self):
name = 'myqueue' region = 'eu-west-1' ret = {'name': name, 'result': None, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[False, True]) with patch.dict(aws_sqs.__salt__, {'aws_sqs.queue_exists': mock}): comt = 'AWS SQS queue {0} is set to be created'.format(name) ret.update({'comment': comt}) with patch.dict(aws_sqs.__opts__, {'test': True}): self.assertDictEqual(aws_sqs.exists(name, region), ret) comt = u'{0} exists in {1}'.format(name, region) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(aws_sqs.exists(name, region), ret)
'Test to remove the named SQS queue if it exists.'
def test_absent(self):
name = 'myqueue' region = 'eu-west-1' ret = {'name': name, 'result': None, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, False]) with patch.dict(aws_sqs.__salt__, {'aws_sqs.queue_exists': mock}): comt = 'AWS SQS queue {0} is set to be removed'.format(name) ret.update({'comment': comt}) with patch.dict(aws_sqs.__opts__, {'test': True}): self.assertDictEqual(aws_sqs.absent(name, region), ret) comt = u'{0} does not exist in {1}'.format(name, region) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(aws_sqs.absent(name, region), ret)
'Test to set package in \'hold\' state, meaning it will not be upgraded.'
def test_held(self):
name = 'tmux' ret = {'name': name, 'result': False, 'changes': {}, 'comment': 'Package {0} does not have a state'.format(name)} mock = MagicMock(return_value=False) with patch.dict(aptpkg.__salt__, {'pkg.get_selections': mock}): self.assertDictEqual(aptpkg.held(name), ret)
'Test to create an event on the PagerDuty service.'
def test_create_event(self):
name = 'This is a server warning message' details = 'This is a much more detailed message' service_key = '9abcd123456789efabcde362783cdbaf' profile = 'my-pagerduty-account' ret = {'name': name, 'result': None, 'comment': '', 'changes': {}} with patch.dict(pagerduty.__opts__, {'test': True}): comt = 'Need to create event: {0}'.format(name) ret.update({'comment': comt}) self.assertDictEqual(pagerduty.create_event(name, details, service_key, profile), ret) with patch.dict(pagerduty.__opts__, {'test': False}): mock_t = MagicMock(return_value=True) with patch.dict(pagerduty.__salt__, {'pagerduty.create_event': mock_t}): comt = 'Created event: {0}'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(pagerduty.create_event(name, details, service_key, profile), ret)
'Test to ensure that the named sysctl value is set in memory and persisted to the named configuration file.'
def test_present(self):
name = 'net.ipv4.conf.all.rp_filter' value = '1' config = '/etc/sysctl.conf' comment = 'Sysctl option {0} might be changed, we failed to check config file at {1}. The file is either unreadable, or missing.'.format(name, config) ret = {'name': name, 'result': None, 'changes': {}, 'comment': comment} comment1 = 'Sysctl option {0} set to be changed to {1}'.format(name, value) comment2 = 'Sysctl value is currently set on the running system but not in a config file. Sysctl option {0} set to be changed to 2 in config file.'.format(name) comt3 = 'Sysctl value {0} is present in configuration file but is not present in the running config. The value {0} is set to be changed to {1}'.format(name, value) comt4 = 'Sysctl value {0} = {1} is already set'.format(name, value) comt5 = 'Sysctl option {0} would be changed to {1}'.format(name, value) comt6 = 'Failed to set {0} to {1}: '.format(name, value) comt7 = 'Sysctl value {0} = {1} is already set'.format(name, value) def mock_current(config_file=None): '\n Mock return value for __salt__.\n ' if (config_file is None): return {name: '2'} return [''] def mock_config(config_file=None): '\n Mock return value for __salt__.\n ' if (config_file is None): return {'salt': '2'} return [name] def mock_both(config_file=None): '\n Mock return value for __salt__.\n ' if (config_file is None): return {name: value} return [name] with patch.dict(sysctl.__opts__, {'test': True}): mock = MagicMock(return_value=False) with patch.dict(sysctl.__salt__, {'sysctl.show': mock}): self.assertDictEqual(sysctl.present(name, value), ret) with patch.dict(sysctl.__salt__, {'sysctl.show': mock_current}): ret.update({'comment': comment1}) self.assertDictEqual(sysctl.present(name, value), ret) ret.update({'comment': comment2}) self.assertDictEqual(sysctl.present(name, '2'), ret) with patch.dict(sysctl.__salt__, {'sysctl.show': mock_config}): ret.update({'comment': comt3}) self.assertDictEqual(sysctl.present(name, value), ret) mock = MagicMock(return_value=value) with patch.dict(sysctl.__salt__, {'sysctl.show': mock_both, 'sysctl.get': mock}): ret.update({'comment': comt4, 'result': True}) self.assertDictEqual(sysctl.present(name, value), ret) mock = MagicMock(return_value=[True]) with patch.dict(sysctl.__salt__, {'sysctl.show': mock}): ret.update({'comment': comt5, 'result': None}) self.assertDictEqual(sysctl.present(name, value), ret) with patch.dict(sysctl.__opts__, {'test': False}): mock = MagicMock(side_effect=CommandExecutionError) with patch.dict(sysctl.__salt__, {'sysctl.persist': mock}): ret.update({'comment': comt6, 'result': False}) self.assertDictEqual(sysctl.present(name, value), ret) mock = MagicMock(return_value='Already set') with patch.dict(sysctl.__salt__, {'sysctl.persist': mock}): ret.update({'comment': comt7, 'result': True}) self.assertDictEqual(sysctl.present(name, value), ret)
'Test if it returns True when specified package is not installed'
def test_removed_not_installed(self):
mock = MagicMock(return_value={'underscore': {}}) with patch.dict(bower.__salt__, {'bower.list': mock}): ret = bower.removed('jquery', '/path/to/project') expected = {'name': 'jquery', 'result': True, 'comment': "Package 'jquery' is not installed", 'changes': {}} self.assertEqual(ret, expected)
'Test if returns False when list packages fails'
def test_removed_with_error(self):
mock = MagicMock(side_effect=CommandExecutionError) with patch.dict(bower.__salt__, {'bower.list': mock}): ret = bower.removed('underscore', '/path/to/project') expected = {'name': 'underscore', 'result': False, 'comment': "Error removing 'underscore': ", 'changes': {}} self.assertEqual(ret, expected)
'Test if it returns True when specified package is installed and uninstall succeeds'
def test_removed_existing(self):
mock_list = MagicMock(return_value={'underscore': {}}) mock_uninstall = MagicMock(return_value=True) with patch.dict(bower.__salt__, {'bower.list': mock_list, 'bower.uninstall': mock_uninstall}): ret = bower.removed('underscore', '/path/to/project') expected = {'name': 'underscore', 'result': True, 'comment': "Package 'underscore' was successfully removed", 'changes': {'underscore': 'Removed'}} self.assertEqual(ret, expected)
'Test if it returns False when specified package is installed and uninstall fails'
def test_removed_existing_with_error(self):
mock_list = MagicMock(return_value={'underscore': {}}) mock_uninstall = MagicMock(side_effect=CommandExecutionError) with patch.dict(bower.__salt__, {'bower.list': mock_list, 'bower.uninstall': mock_uninstall}): ret = bower.removed('underscore', '/path/to/project') expected = {'name': 'underscore', 'result': False, 'comment': "Error removing 'underscore': ", 'changes': {}} self.assertEqual(ret, expected)
'Test if it return False when install packages fails'
def test_bootstrap_with_error(self):
mock = MagicMock(side_effect=CommandExecutionError) with patch.dict(bower.__salt__, {'bower.install': mock}): ret = bower.bootstrap('/path/to/project') expected = {'name': '/path/to/project', 'result': False, 'comment': "Error bootstrapping '/path/to/project': ", 'changes': {}} self.assertEqual(ret, expected)
'Test if it returns True when there is nothing to install'
def test_bootstrap_not_needed(self):
mock = MagicMock(return_value=False) with patch.dict(bower.__salt__, {'bower.install': mock}): ret = bower.bootstrap('/path/to/project') expected = {'name': '/path/to/project', 'result': True, 'comment': 'Directory is already bootstrapped', 'changes': {}} self.assertEqual(ret, expected)
'Test if it returns True when install packages succeeds'
def test_bootstrap_success(self):
mock = MagicMock(return_value=True) with patch.dict(bower.__salt__, {'bower.install': mock}): ret = bower.bootstrap('/path/to/project') expected = {'name': '/path/to/project', 'result': True, 'comment': 'Directory was successfully bootstrapped', 'changes': {'/path/to/project': 'Bootstrapped'}} self.assertEqual(ret, expected)
'Test if it returns False when list packages fails'
def test_installed_with_error(self):
mock = MagicMock(side_effect=CommandExecutionError) with patch.dict(bower.__salt__, {'bower.list': mock}): ret = bower.installed('underscore', '/path/to/project') expected = {'name': 'underscore', 'result': False, 'comment': "Error looking up 'underscore': ", 'changes': {}} self.assertEqual(ret, expected)
'Test if it returns True when there is nothing to install'
def test_installed_not_needed(self):
mock = MagicMock(return_value={'underscore': {'pkgMeta': {'version': '1.7.0'}}, 'jquery': {'pkgMeta': {'version': '2.0.0'}}}) with patch.dict(bower.__salt__, {'bower.list': mock}): ret = bower.installed('test', '/path/to/project', ['underscore', 'jquery#2.0.0']) expected = {'name': 'test', 'result': True, 'comment': "Package(s) 'underscore, jquery#2.0.0' satisfied by underscore#1.7.0, jquery#2.0.0", 'changes': {}} self.assertEqual(ret, expected)
'Test if it returns False when install packages fails (exception)'
def test_installed_new_with_exc(self):
mock_list = MagicMock(return_value={}) mock_install = MagicMock(side_effect=CommandExecutionError) with patch.dict(bower.__salt__, {'bower.list': mock_list, 'bower.install': mock_install}): ret = bower.installed('underscore', '/path/to/project') expected = {'name': 'underscore', 'result': False, 'comment': "Error installing 'underscore': ", 'changes': {}} self.assertEqual(ret, expected)
'Test if returns False when install packages fails (bower error)'
def test_installed_new_with_error(self):
mock_list = MagicMock(return_value={}) mock_install = MagicMock(return_value=False) with patch.dict(bower.__salt__, {'bower.list': mock_list, 'bower.install': mock_install}): ret = bower.installed('underscore', '/path/to/project') expected = {'name': 'underscore', 'result': False, 'comment': "Could not install package(s) 'underscore'", 'changes': {}} self.assertEqual(ret, expected)
'Test if it returns True when install succeeds'
def test_installed_success(self):
mock_list = MagicMock(return_value={}) mock_install = MagicMock(return_value=True) with patch.dict(bower.__salt__, {'bower.list': mock_list, 'bower.install': mock_install}): ret = bower.installed('underscore', '/path/to/project') expected = {'name': 'underscore', 'result': True, 'comment': "Package(s) 'underscore' successfully installed", 'changes': {'new': ['underscore'], 'old': []}} self.assertEqual(ret, expected)
'Test to verify that the correct versions of composer dependencies are present.'
def test_installed(self):
name = 'CURL' ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} mock = MagicMock(return_value=True) with patch.dict(composer.__salt__, {'composer.did_composer_install': mock}): comt = 'Composer already installed this directory' ret.update({'comment': comt}) self.assertDictEqual(composer.installed(name, always_check=False), ret) with patch.dict(composer.__opts__, {'test': True}): comt = 'The state of "CURL" will be changed.' changes = {'new': 'composer install will be run in CURL', 'old': 'composer install has been run in CURL'} ret.update({'comment': comt, 'result': None, 'changes': changes}) self.assertDictEqual(composer.installed(name), ret) with patch.dict(composer.__opts__, {'test': False}): mock = MagicMock(side_effect=[SaltException, {}]) with patch.dict(composer.__salt__, {'composer.install': mock}): comt = "Error executing composer in 'CURL': " ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(composer.installed(name), ret) comt = 'Composer install completed successfully, output silenced by quiet flag' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(composer.installed(name, quiet=True), ret)
'Test to composer update the directory to ensure we have the latest versions of all project dependencies.'
def test_update(self):
name = 'CURL' ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} changes = {'new': 'composer install/update will be run in CURL', 'old': 'composer install has not yet been run in CURL'} mock = MagicMock(return_value=True) with patch.dict(composer.__salt__, {'composer.did_composer_install': mock}): with patch.dict(composer.__opts__, {'test': True}): comt = 'The state of "CURL" will be changed.' ret.update({'comment': comt, 'result': None, 'changes': changes}) self.assertDictEqual(composer.update(name), ret) with patch.dict(composer.__opts__, {'test': False}): mock = MagicMock(side_effect=[SaltException, {}]) with patch.dict(composer.__salt__, {'composer.update': mock}): comt = "Error executing composer in 'CURL': " ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(composer.update(name), ret) comt = 'Composer update completed successfully, output silenced by quiet flag' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(composer.update(name, quiet=True), ret)
'Test to send an event to the Salt Master'
def test_send(self):
with patch.dict(event.__opts__, {'test': True}): self.assertDictEqual(event.send('salt'), {'changes': {'data': None, 'tag': 'salt'}, 'comment': 'Event would have been fired', 'name': 'salt', 'result': None}) with patch.dict(event.__opts__, {'test': False}): mock = MagicMock(return_value=True) with patch.dict(event.__salt__, {'event.send': mock}): self.assertDictEqual(event.send('salt'), {'changes': {'data': None, 'tag': 'salt'}, 'comment': 'Event fired', 'name': 'salt', 'result': True})
'Test to fire an event on the Salt master'
def test_wait(self):
self.assertDictEqual(event.wait('salt'), {'changes': {}, 'comment': '', 'name': 'salt', 'result': True})
'Test to ensures that the named host is present with the given ip'
def test_present(self):
ret = {'changes': {}, 'comment': 'Host salt (127.0.0.1) already present', 'name': 'salt', 'result': True} mock = MagicMock(return_value=True) with patch.dict(host.__salt__, {'hosts.has_pair': mock}): self.assertDictEqual(host.present('salt', '127.0.0.1'), ret)
'Test to ensure that the named host is absent'
def test_absent(self):
ret = {'changes': {}, 'comment': 'Host salt (127.0.0.1) already absent', 'name': 'salt', 'result': True} mock = MagicMock(return_value=False) with patch.dict(host.__salt__, {'hosts.has_pair': mock}): self.assertDictEqual(host.absent('salt', '127.0.0.1'), ret)
'Test only() when the state hasn\'t changed'
def test_only_already(self):
expected = {'name': '127.0.1.1', 'changes': {}, 'result': True, 'comment': 'IP address 127.0.1.1 already set to "foo.bar"'} mock1 = MagicMock(return_value=['foo.bar']) with patch.dict(host.__salt__, {'hosts.get_alias': mock1}): mock2 = MagicMock(return_value=True) with patch.dict(host.__salt__, {'hosts.set_host': mock2}): with patch.dict(host.__opts__, {'test': False}): self.assertDictEqual(expected, host.only('127.0.1.1', 'foo.bar'))
'Test only() when state would change, but it\'s a dry run'
def test_only_dryrun(self):
expected = {'name': '127.0.1.1', 'changes': {}, 'result': None, 'comment': 'Would change 127.0.1.1 from "foo.bar" to "foo.bar foo"'} mock1 = MagicMock(return_value=['foo.bar']) with patch.dict(host.__salt__, {'hosts.get_alias': mock1}): mock2 = MagicMock(return_value=True) with patch.dict(host.__salt__, {'hosts.set_host': mock2}): with patch.dict(host.__opts__, {'test': True}): self.assertDictEqual(expected, host.only('127.0.1.1', ['foo.bar', 'foo']))
'Test only() when state change fails'
def test_only_fail(self):
expected = {'name': '127.0.1.1', 'changes': {}, 'result': False, 'comment': ('hosts.set_host failed to change 127.0.1.1' + ' from "foo.bar" to "foo.bar foo"')} mock = MagicMock(return_value=['foo.bar']) with patch.dict(host.__salt__, {'hosts.get_alias': mock}): mock = MagicMock(return_value=False) with patch.dict(host.__salt__, {'hosts.set_host': mock}): with patch.dict(host.__opts__, {'test': False}): self.assertDictEqual(expected, host.only('127.0.1.1', ['foo.bar', 'foo']))
'Test only() when state successfully changes'
def test_only_success(self):
expected = {'name': '127.0.1.1', 'changes': {'127.0.1.1': {'old': 'foo.bar', 'new': 'foo.bar foo'}}, 'result': True, 'comment': ('successfully changed 127.0.1.1' + ' from "foo.bar" to "foo.bar foo"')} mock = MagicMock(return_value=['foo.bar']) with patch.dict(host.__salt__, {'hosts.get_alias': mock}): mock = MagicMock(return_value=True) with patch.dict(host.__salt__, {'hosts.set_host': mock}): with patch.dict(host.__opts__, {'test': False}): self.assertDictEqual(expected, host.only('127.0.1.1', ['foo.bar', 'foo']))
'Test to enforce that the WAR will be deployed and started in the context path it will make use of WAR versions'
def test_war_deployed(self):
ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''} mock_start = MagicMock(return_value='saltstack') mock_undeploy = MagicMock(side_effect=['FAIL', 'saltstack']) mock_deploy = MagicMock(return_value='deploy') mock_ls = MagicMock(side_effect=[{'salt': {'version': 'jenkins-1.20.4', 'mode': 'running'}}, {'salt': {'version': '1'}}, {'salt': {'version': 'jenkins-1.2.4', 'mode': 'run'}}, {'salt': {'version': '1'}}, {'salt': {'version': '1'}}, {'salt': {'version': '1'}}]) with patch.dict(tomcat.__salt__, {'tomcat.ls': mock_ls, 'tomcat.extract_war_version': tomcatmod.extract_war_version, 'tomcat.start': mock_start, 'tomcat.undeploy': mock_undeploy, 'tomcat.deploy_war': mock_deploy}): ret.update({'comment': 'salt with version 1.20.4 is already deployed'}) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins-1.20.4.war'), ret) with patch.dict(tomcat.__opts__, {'test': True}): ret.update({'changes': {'deploy': 'will deploy salt with version 1.2.4', 'undeploy': 'undeployed salt with version 1'}, 'result': None, 'comment': ''}) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins-1.2.4.war'), ret) with patch.dict(tomcat.__opts__, {'test': False}): ret.update({'changes': {'start': 'starting salt'}, 'comment': 'saltstack', 'result': False}) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins-1.2.4.war'), ret) ret.update({'changes': {'deploy': 'will deploy salt with version 1.2.4', 'undeploy': 'undeployed salt with version 1'}, 'comment': 'FAIL'}) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins-1.2.4.war'), ret) ret.update({'changes': {'undeploy': 'undeployed salt with version 1'}, 'comment': 'deploy'}) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins-1.2.4.war'), ret)
'Tests that going from versions to no versions and back work, as well as not overwriting a WAR without version with another without version.'
def test_war_deployed_no_version(self):
ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''} mock_deploy = MagicMock(return_value='deploy') mock_undeploy = MagicMock(return_value='SUCCESS') mock_ls_version = MagicMock(return_value={'salt': {'version': '1.2.4', 'mode': 'running'}}) mock_ls_no_version = MagicMock(return_value={'salt': {'version': '', 'mode': 'running'}}) with patch.dict(tomcat.__opts__, {'test': True}): with patch.dict(tomcat.__salt__, {'tomcat.ls': mock_ls_version, 'tomcat.extract_war_version': tomcatmod.extract_war_version, 'tomcat.deploy_war': mock_deploy, 'tomcat.undeploy': mock_undeploy}): ret.update({'changes': {'deploy': 'will deploy salt with no version', 'undeploy': 'undeployed salt with version 1.2.4'}}) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins.war'), ret) with patch.dict(tomcat.__salt__, {'tomcat.ls': mock_ls_no_version, 'tomcat.extract_war_version': tomcatmod.extract_war_version, 'tomcat.deploy_war': mock_deploy, 'tomcat.undeploy': mock_undeploy}): ret.update({'changes': {'deploy': 'will deploy salt with version 1.2.4', 'undeploy': 'undeployed salt with no version'}}) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins.war', version='1.2.4'), ret) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins-1.2.4.war'), ret) ret.update({'changes': {}, 'comment': 'salt with no version is already deployed', 'result': True}) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins.war'), ret) self.assertDictEqual(tomcat.war_deployed('salt', 'salt://jenkins-1.2.4.war', version=False), ret)
'Test to wait for the tomcat manager to load'
def test_wait(self):
ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': 'tomcat manager is ready'} mock = MagicMock(return_value=True) with patch.dict(tomcat.__salt__, {'tomcat.status': mock, 'tomcat.extract_war_version': tomcatmod.extract_war_version}): self.assertDictEqual(tomcat.wait('salt'), ret)
'Test to the tomcat watcher function.'
def test_mod_watch(self):
ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': 'True'} mock = MagicMock(return_value='True') with patch.dict(tomcat.__salt__, {'tomcat.reload': mock, 'tomcat.extract_war_version': tomcatmod.extract_war_version}): ret.update({'changes': {'salt': False}}) self.assertDictEqual(tomcat.mod_watch('salt'), ret)
'Test to enforce that the WAR will be un-deployed from the server'
def test_undeployed(self):
ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': 'True'} mock = MagicMock(side_effect=[False, True, True, True, True]) mock1 = MagicMock(side_effect=[{'salt': {'a': 1}}, {'salt': {'version': 1}}, {'salt': {'version': 1}}, {'salt': {'version': 1}}]) mock2 = MagicMock(side_effect=['FAIL', 'saltstack']) with patch.dict(tomcat.__salt__, {'tomcat.status': mock, 'tomcat.extract_war_version': tomcatmod.extract_war_version, 'tomcat.ls': mock1, 'tomcat.undeploy': mock2}): ret.update({'comment': 'Tomcat Manager does not respond'}) self.assertDictEqual(tomcat.undeployed('salt'), ret) ret.update({'comment': '', 'result': True}) self.assertDictEqual(tomcat.undeployed('salt'), ret) with patch.dict(tomcat.__opts__, {'test': True}): ret.update({'changes': {'undeploy': 1}, 'result': None}) self.assertDictEqual(tomcat.undeployed('salt'), ret) with patch.dict(tomcat.__opts__, {'test': False}): ret.update({'changes': {'undeploy': 1}, 'comment': 'FAIL', 'result': False}) self.assertDictEqual(tomcat.undeployed('salt'), ret) ret.update({'changes': {'undeploy': 1}, 'comment': '', 'result': True}) self.assertDictEqual(tomcat.undeployed('salt'), ret)
'Test to ensure the SNS topic exists.'
def test_present(self):
name = 'test.example.com.' ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, False, False]) mock_bool = MagicMock(return_value=False) with patch.dict(boto_sns.__salt__, {'boto_sns.exists': mock, 'boto_sns.create': mock_bool}): comt = 'AWS SNS topic {0} present.'.format(name) ret.update({'comment': comt}) self.assertDictEqual(boto_sns.present(name), ret) with patch.dict(boto_sns.__opts__, {'test': True}): comt = 'AWS SNS topic {0} is set to be created.'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(boto_sns.present(name), ret) with patch.dict(boto_sns.__opts__, {'test': False}): comt = 'Failed to create {0} AWS SNS topic'.format(name) ret.update({'comment': comt, 'result': False}) self.assertDictEqual(boto_sns.present(name), ret)
'Test to ensure the named sns topic is deleted.'
def test_absent(self):
name = 'test.example.com.' ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''} self.maxDiff = None exists_mock = MagicMock(side_effect=[False, True, True, True, True, True, True]) with patch.dict(boto_sns.__salt__, {'boto_sns.exists': exists_mock}): comt = 'AWS SNS topic {0} does not exist.'.format(name) ret.update({'comment': comt}) self.assertDictEqual(boto_sns.absent(name), ret) with patch.dict(boto_sns.__opts__, {'test': True}): comt = 'AWS SNS topic {0} is set to be removed. 0 subscription(s) will be removed.'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(boto_sns.absent(name), ret) subscriptions = [dict(Endpoint='arn:aws:lambda:us-west-2:123456789:function:test', Owner=123456789, Protocol='Lambda', TopicArn='arn:aws:sns:us-west-2:123456789:test', SubscriptionArn='arn:aws:sns:us-west-2:123456789:test:some_uuid')] with patch.dict(boto_sns.__opts__, {'test': True}): subs_mock = MagicMock(return_value=subscriptions) with patch.dict(boto_sns.__salt__, {'boto_sns.get_all_subscriptions_by_topic': subs_mock}): comt = 'AWS SNS topic {0} is set to be removed. 1 subscription(s) will be removed.'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(boto_sns.absent(name, unsubscribe=True), ret) subs_mock = MagicMock(return_value=subscriptions) unsubscribe_mock = MagicMock(side_effect=[True, False]) with patch.dict(boto_sns.__salt__, {'boto_sns.unsubscribe': unsubscribe_mock}): with patch.dict(boto_sns.__salt__, {'boto_sns.get_all_subscriptions_by_topic': subs_mock}): delete_mock = MagicMock(side_effect=[True, True, True, False]) with patch.dict(boto_sns.__salt__, {'boto_sns.delete': delete_mock}): comt = 'AWS SNS topic {0} deleted.'.format(name) ret.update({'changes': {'new': None, 'old': {'topic': name, 'subscriptions': subscriptions}}, 'result': True, 'comment': comt}) self.assertDictEqual(boto_sns.absent(name, unsubscribe=True), ret) ret.update({'changes': {'new': {'subscriptions': subscriptions}, 'old': {'topic': name, 'subscriptions': subscriptions}}, 'result': True, 'comment': comt}) self.assertDictEqual(boto_sns.absent(name, unsubscribe=True), ret) ret.update({'changes': {'new': None, 'old': {'topic': name}}, 'result': True, 'comment': comt}) self.assertDictEqual(boto_sns.absent(name), ret) comt = 'Failed to delete {0} AWS SNS topic.'.format(name) ret.update({'changes': {}, 'result': False, 'comment': comt}) self.assertDictEqual(boto_sns.absent(name), ret)
'Test to ensure that the named service is present.'
def test_present(self):
name = 'lvsrs' ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} mock_check = MagicMock(side_effect=[True, True, True, False, True, False, True, False, False, False, False]) mock_edit = MagicMock(side_effect=[True, False]) mock_add = MagicMock(side_effect=[True, False]) with patch.dict(lvs_server.__salt__, {'lvs.check_server': mock_check, 'lvs.edit_server': mock_edit, 'lvs.add_server': mock_add}): with patch.dict(lvs_server.__opts__, {'test': True}): comt = 'LVS Server lvsrs in service None(None) is present' ret.update({'comment': comt}) self.assertDictEqual(lvs_server.present(name), ret) comt = 'LVS Server lvsrs in service None(None) is present but some options should update' ret.update({'comment': comt, 'result': None}) self.assertDictEqual(lvs_server.present(name), ret) with patch.dict(lvs_server.__opts__, {'test': False}): comt = 'LVS Server lvsrs in service None(None) has been updated' ret.update({'comment': comt, 'result': True, 'changes': {'lvsrs': 'Update'}}) self.assertDictEqual(lvs_server.present(name), ret) comt = 'LVS Server lvsrs in service None(None) update failed(False)' ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(lvs_server.present(name), ret) with patch.dict(lvs_server.__opts__, {'test': True}): comt = 'LVS Server lvsrs in service None(None) is not present and needs to be created' ret.update({'comment': comt, 'result': None}) self.assertDictEqual(lvs_server.present(name), ret) with patch.dict(lvs_server.__opts__, {'test': False}): comt = 'LVS Server lvsrs in service None(None) has been created' ret.update({'comment': comt, 'result': True, 'changes': {'lvsrs': 'Present'}}) self.assertDictEqual(lvs_server.present(name), ret) comt = 'LVS Service lvsrs in service None(None) create failed(False)' ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(lvs_server.present(name), ret)
'Test to ensure the LVS Real Server in specified service is absent.'
def test_absent(self):
name = 'lvsrs' ret = {'name': name, 'result': None, 'comment': '', 'changes': {}} mock_check = MagicMock(side_effect=[True, True, True, False]) mock_delete = MagicMock(side_effect=[True, False]) with patch.dict(lvs_server.__salt__, {'lvs.check_server': mock_check, 'lvs.delete_server': mock_delete}): with patch.dict(lvs_server.__opts__, {'test': True}): comt = 'LVS Server lvsrs in service None(None) is present and needs to be removed' ret.update({'comment': comt}) self.assertDictEqual(lvs_server.absent(name), ret) with patch.dict(lvs_server.__opts__, {'test': False}): comt = 'LVS Server lvsrs in service None(None) has been removed' ret.update({'comment': comt, 'result': True, 'changes': {'lvsrs': 'Absent'}}) self.assertDictEqual(lvs_server.absent(name), ret) comt = 'LVS Server lvsrs in service None(None) removed failed(False)' ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(lvs_server.absent(name), ret) comt = 'LVS Server lvsrs in service None(None) is not present, so it cannot be removed' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(lvs_server.absent(name), ret)
'Test to send a message to a Slack channel.'
def test_post_message(self):
name = 'slack-message' channel = '#general' from_name = 'SuperAdmin' message = 'This state was executed successfully.' ret = {'name': name, 'changes': {}, 'result': None, 'comment': ''} with patch.dict(slack.__opts__, {'test': True}): comt = 'The following message is to be sent to Slack: {0}'.format(message) ret.update({'comment': comt}) self.assertDictEqual(slack.post_message(name, channel, from_name, message), ret) with patch.dict(slack.__opts__, {'test': False}): comt = 'Slack channel is missing: None' ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, None, from_name, message), ret) comt = 'Slack from name is missing: None' ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, channel, None, message), ret) comt = 'Slack message is missing: None' ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, channel, from_name, None), ret) mock = MagicMock(return_value=True) with patch.dict(slack.__salt__, {'slack.post_message': mock}): comt = 'Sent message: slack-message' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(slack.post_message(name, channel, from_name, message), ret)
'Test to make sure that a pecl extension is installed.'
def test_installed(self):
name = 'mongo' ver = '1.0.1' ret = {'name': name, 'result': None, 'comment': '', 'changes': {}} mock_lst = MagicMock(return_value={name: 'stable'}) mock_t = MagicMock(return_value=True) with patch.dict(pecl.__salt__, {'pecl.list': mock_lst, 'pecl.install': mock_t}): comt = 'Pecl extension {0} is already installed.'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(pecl.installed(name), ret) with patch.dict(pecl.__opts__, {'test': True}): comt = 'Pecl extension mongo-1.0.1 would have been installed' ret.update({'comment': comt, 'result': None}) self.assertDictEqual(pecl.installed(name, version=ver), ret) with patch.dict(pecl.__opts__, {'test': False}): comt = 'Pecl extension mongo-1.0.1 was successfully installed' ret.update({'comment': comt, 'result': True, 'changes': {'mongo-1.0.1': 'Installed'}}) self.assertDictEqual(pecl.installed(name, version=ver), ret)
'Test to make sure that a pecl extension is not installed.'
def test_removed(self):
name = 'mongo' ret = {'name': name, 'result': None, 'comment': '', 'changes': {}} mock_lst = MagicMock(side_effect=[{}, {name: 'stable'}, {name: 'stable'}]) mock_t = MagicMock(return_value=True) with patch.dict(pecl.__salt__, {'pecl.list': mock_lst, 'pecl.uninstall': mock_t}): comt = 'Pecl extension {0} is not installed.'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(pecl.removed(name), ret) with patch.dict(pecl.__opts__, {'test': True}): comt = 'Pecl extension mongo would have been removed' ret.update({'comment': comt, 'result': None}) self.assertDictEqual(pecl.removed(name), ret) with patch.dict(pecl.__opts__, {'test': False}): comt = 'Pecl extension mongo was successfully removed.' ret.update({'comment': comt, 'result': True, 'changes': {name: 'Removed'}}) self.assertDictEqual(pecl.removed(name), ret)
'Test to ensure an Apache site is enabled.'
def test_enabled(self):
name = 'saltstack.com' ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, False, False]) mock_str = MagicMock(return_value={'Status': ['enabled']}) with patch.dict(apache_site.__salt__, {'apache.check_site_enabled': mock, 'apache.a2ensite': mock_str}): comt = '{0} already enabled.'.format(name) ret.update({'comment': comt}) self.assertDictEqual(apache_site.enabled(name), ret) comt = 'Apache site {0} is set to be enabled.'.format(name) ret.update({'comment': comt, 'result': None, 'changes': {'new': name, 'old': None}}) with patch.dict(apache_site.__opts__, {'test': True}): self.assertDictEqual(apache_site.enabled(name), ret) comt = 'Failed to enable {0} Apache site'.format(name) ret.update({'comment': comt, 'result': False, 'changes': {}}) with patch.dict(apache_site.__opts__, {'test': False}): self.assertDictEqual(apache_site.enabled(name), ret)
'Test to ensure an Apache site is disabled.'
def test_disabled(self):
name = 'saltstack.com' ret = {'name': name, 'result': None, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, True, False]) mock_str = MagicMock(return_value={'Status': ['disabled']}) with patch.dict(apache_site.__salt__, {'apache.check_site_enabled': mock, 'apache.a2dissite': mock_str}): comt = 'Apache site {0} is set to be disabled.'.format(name) ret.update({'comment': comt, 'changes': {'new': None, 'old': name}}) with patch.dict(apache_site.__opts__, {'test': True}): self.assertDictEqual(apache_site.disabled(name), ret) comt = 'Failed to disable {0} Apache site'.format(name) ret.update({'comment': comt, 'result': False, 'changes': {}}) with patch.dict(apache_site.__opts__, {'test': False}): self.assertDictEqual(apache_site.disabled(name), ret) comt = '{0} already disabled.'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(apache_site.disabled(name), ret)
'Test to ensures that the artifact from artifactory exists at given location.'
def test_downloaded(self):
name = 'jboss' arti_url = 'http://artifactory.intranet.example.com/artifactory' artifact = {'artifactory_url': arti_url, 'artifact_id': 'module', 'repository': 'libs-release-local', 'packaging': 'jar', 'group_id': 'com.company.module', 'classifier': 'sources', 'version': '1.0'} ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} mck = MagicMock(return_value={'status': False, 'changes': {}, 'comment': ''}) with patch.dict(artifactory.__salt__, {'artifactory.get_release': mck}): self.assertDictEqual(artifactory.downloaded(name, artifact), ret) with patch.object(artifactory, '__fetch_from_artifactory', MagicMock(side_effect=Exception('error'))): ret = artifactory.downloaded(name, artifact) self.assertEqual(ret['result'], False) self.assertEqual(ret['comment'], 'error')
'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]) with patch.dict(nftables.__salt__, {'nftables.check_chain': mock}): ret.update({'comment': 'nftables salt chain is already exist in filter table for ipv4'}) self.assertDictEqual(nftables.chain_present('salt'), ret) mock = MagicMock(side_effect=[True, '']) with patch.dict(nftables.__salt__, {'nftables.new_chain': mock}): ret.update({'changes': {'locale': 'salt'}, 'comment': 'nftables salt chain in filter table create success for ipv4'}) self.assertDictEqual(nftables.chain_present('salt'), ret) ret.update({'changes': {}, 'comment': 'Failed to create salt chain in filter table: for ipv4', 'result': False}) self.assertDictEqual(nftables.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]) with patch.dict(nftables.__salt__, {'nftables.check_chain': mock}): ret.update({'comment': 'nftables salt chain is already absent in filter table for ipv4'}) self.assertDictEqual(nftables.chain_absent('salt'), ret) mock = MagicMock(return_value='') with patch.dict(nftables.__salt__, {'nftables.flush': mock}): ret.update({'result': False, 'comment': 'Failed to flush salt chain in filter table: for ipv4'}) self.assertDictEqual(nftables.chain_absent('salt'), ret)
'Test to append a rule to a chain'
def test_append(self):
ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''} mock = MagicMock(return_value=[]) with patch.object(nftables, '_STATE_INTERNAL_KEYWORDS', mock): mock = MagicMock(return_value='a') with patch.dict(nftables.__salt__, {'nftables.build_rule': mock}): mock = MagicMock(side_effect=[True, False, False, False]) with patch.dict(nftables.__salt__, {'nftables.check': mock}): ret.update({'comment': 'nftables rule for salt already set (a) for ipv4'}) self.assertDictEqual(nftables.append('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {'test': True}): ret.update({'result': None, 'comment': 'nftables rule for salt needs to be set (a) for ipv4'}) self.assertDictEqual(nftables.append('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {'test': False}): mock = MagicMock(side_effect=[True, False]) with patch.dict(nftables.__salt__, {'nftables.append': mock}): ret.update({'changes': {'locale': 'salt'}, 'comment': 'Set nftables rule for salt to: a for ipv4', 'result': True}) self.assertDictEqual(nftables.append('salt', table='', chain=''), ret) ret.update({'changes': {}, 'comment': 'Failed to set nftables rule for salt.\nAttempted rule was a for ipv4', 'result': False}) self.assertDictEqual(nftables.append('salt', table='', chain=''), ret)
'Test to insert a rule into a chain'
def test_insert(self):
ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''} mock = MagicMock(return_value=[]) with patch.object(nftables, '_STATE_INTERNAL_KEYWORDS', mock): mock = MagicMock(return_value='a') with patch.dict(nftables.__salt__, {'nftables.build_rule': mock}): mock = MagicMock(side_effect=[True, False, False, False]) with patch.dict(nftables.__salt__, {'nftables.check': mock}): ret.update({'comment': 'nftables rule for salt already set for ipv4 (a)'}) self.assertDictEqual(nftables.insert('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {'test': True}): ret.update({'result': None, 'comment': 'nftables rule for salt needs to be set for ipv4 (a)'}) self.assertDictEqual(nftables.insert('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {'test': False}): mock = MagicMock(side_effect=[True, False]) with patch.dict(nftables.__salt__, {'nftables.insert': mock}): ret.update({'changes': {'locale': 'salt'}, 'comment': 'Set nftables rule for salt to: a for ipv4', 'result': True}) self.assertDictEqual(nftables.insert('salt', table='', chain='', position=''), ret) ret.update({'changes': {}, 'comment': 'Failed to set nftables rule for salt.\nAttempted rule was a', 'result': False}) self.assertDictEqual(nftables.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': ''} mock = MagicMock(return_value=[]) with patch.object(nftables, '_STATE_INTERNAL_KEYWORDS', mock): mock = MagicMock(return_value='a') with patch.dict(nftables.__salt__, {'nftables.build_rule': mock}): mock = MagicMock(side_effect=[False, True, True, True]) with patch.dict(nftables.__salt__, {'nftables.check': mock}): ret.update({'comment': 'nftables rule for salt already absent for ipv4 (a)', 'result': True}) self.assertDictEqual(nftables.delete('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {'test': True}): ret.update({'result': None, 'comment': 'nftables rule for salt needs to be deleted for ipv4 (a)'}) self.assertDictEqual(nftables.delete('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {'test': False}): mock = MagicMock(side_effect=[True, False]) with patch.dict(nftables.__salt__, {'nftables.delete': mock}): ret.update({'result': True, 'changes': {'locale': 'salt'}, 'comment': 'Delete nftables rule for salt a'}) self.assertDictEqual(nftables.delete('salt', table='', chain='', position=''), ret) ret.update({'result': False, 'changes': {}, 'comment': 'Failed to delete nftables rule for salt.\nAttempted rule was a'}) self.assertDictEqual(nftables.delete('salt', table='', chain='', position=''), ret)
'Test to flush current nftables state'
def test_flush(self):
ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''} mock = MagicMock(return_value=[]) with patch.object(nftables, '_STATE_INTERNAL_KEYWORDS', mock): mock = MagicMock(side_effect=[False, True, True, True]) with patch.dict(nftables.__salt__, {'nftables.check_table': mock}): ret.update({'comment': 'Failed to flush table in family ipv4, table does not exist.', 'result': False}) self.assertDictEqual(nftables.flush('salt', table='', chain=''), ret) mock = MagicMock(side_effect=[False, True, True]) with patch.dict(nftables.__salt__, {'nftables.check_chain': mock}): ret.update({'comment': 'Failed to flush chain in table in family ipv4, chain does not exist.'}) self.assertDictEqual(nftables.flush('salt', table='', chain=''), ret) mock = MagicMock(side_effect=[True, False]) with patch.dict(nftables.__salt__, {'nftables.flush': mock}): ret.update({'changes': {'locale': 'salt'}, 'comment': 'Flush nftables rules in table chain ipv4 family', 'result': True}) self.assertDictEqual(nftables.flush('salt', table='', chain=''), ret) ret.update({'changes': {}, 'comment': 'Failed to flush nftables rules', 'result': False}) self.assertDictEqual(nftables.flush('salt', table='', chain=''), ret)
'Test to ensure that the named user is present with the specified properties'
def test_present(self):
ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': ''} mock_false = MagicMock(return_value=False) mock_empty_list = MagicMock(return_value=[]) with patch.dict(user.__grains__, {'kernel': 'Linux'}): with patch.dict(user.__salt__, {'group.info': mock_false, 'user.info': mock_empty_list, 'user.chkey': mock_empty_list, 'user.add': mock_false}): ret.update({'comment': 'The following group(s) are not present: salt'}) self.assertDictEqual(user.present('salt', groups=['salt']), ret) mock_false = MagicMock(side_effect=[{'key': 'value'}, {'key': 'value'}, {'key': 'value'}, False, False]) with patch.object(user, '_changes', mock_false): with patch.dict(user.__opts__, {'test': True}): ret.update({'comment': 'The following user attributes are set to be changed:\nkey: value\n', 'result': None}) self.assertDictEqual(user.present('salt'), ret) with patch.dict(user.__opts__, {'test': False}): ret.update({'comment': "These values could not be changed: {'key': 'value'}", 'result': False}) self.assertDictEqual(user.present('salt'), ret) with patch.dict(user.__opts__, {'test': True}): ret.update({'comment': 'User salt set to be added', 'result': None}) self.assertDictEqual(user.present('salt'), ret) with patch.dict(user.__opts__, {'test': False}): ret.update({'comment': 'Failed to create new user salt', 'result': False}) self.assertDictEqual(user.present('salt'), ret)
'Test to ensure that the named user is absent'
def test_absent(self):
ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''} mock = MagicMock(side_effect=[True, True, False]) mock1 = MagicMock(return_value=False) with patch.dict(user.__salt__, {'user.info': mock, 'user.delete': mock1, 'group.info': mock1}): with patch.dict(user.__opts__, {'test': True}): ret.update({'comment': 'User salt set for removal'}) self.assertDictEqual(user.absent('salt'), ret) with patch.dict(user.__opts__, {'test': False}): ret.update({'comment': 'Failed to remove user salt', 'result': False}) self.assertDictEqual(user.absent('salt'), ret) ret.update({'comment': 'User salt is not present', 'result': True}) self.assertDictEqual(user.absent('salt'), ret)
'test alias.present has target already'
def test_present_has_target(self):
name = 'saltdude' target = '[email protected]' ret = {'comment': 'Alias {0} already present'.format(name), 'changes': {}, 'name': name, 'result': True} has_target = MagicMock(return_value=True) with patch.dict(alias.__salt__, {'aliases.has_target': has_target}): self.assertEqual(alias.present(name, target), ret)
'test alias.present has\'t got target yet test mode'
def test_present_has_not_target_test(self):
name = 'saltdude' target = '[email protected]' ret = {'comment': 'Alias {0} -> {1} is set to be added'.format(name, target), 'changes': {}, 'name': name, 'result': None} has_target = MagicMock(return_value=False) with patch.dict(alias.__salt__, {'aliases.has_target': has_target}): with patch.dict(alias.__opts__, {'test': True}): self.assertEqual(alias.present(name, target), ret)
'test alias.present set target'
def test_present_set_target(self):
name = 'saltdude' target = '[email protected]' ret = {'comment': 'Set email alias {0} -> {1}'.format(name, target), 'changes': {'alias': name}, 'name': name, 'result': True} has_target = MagicMock(return_value=False) set_target = MagicMock(return_value=True) with patch.dict(alias.__salt__, {'aliases.has_target': has_target}): with patch.dict(alias.__opts__, {'test': False}): with patch.dict(alias.__salt__, {'aliases.set_target': set_target}): self.assertEqual(alias.present(name, target), ret)
'test alias.present set target failure'
def test_present_set_target_failed(self):
name = 'saltdude' target = '[email protected]' ret = {'comment': 'Failed to set alias {0} -> {1}'.format(name, target), 'changes': {}, 'name': name, 'result': False} has_target = MagicMock(return_value=False) set_target = MagicMock(return_value=False) with patch.dict(alias.__salt__, {'aliases.has_target': has_target}): with patch.dict(alias.__opts__, {'test': False}): with patch.dict(alias.__salt__, {'aliases.set_target': set_target}): self.assertEqual(alias.present(name, target), ret)
'test alias.absent already gone'
def test_absent_already_gone(self):
name = 'saltdude' target = '[email protected]' ret = {'comment': 'Alias {0} already absent'.format(name), 'changes': {}, 'name': name, 'result': True} get_target = MagicMock(return_value=False) with patch.dict(alias.__salt__, {'aliases.get_target': get_target}): self.assertEqual(alias.absent(name), ret)
'test alias.absent already gone test mode'
def test_absent_not_gone_test(self):
name = 'saltdude' target = '[email protected]' ret = {'comment': 'Alias {0} is set to be removed'.format(name), 'changes': {}, 'name': name, 'result': None} get_target = MagicMock(return_value=True) with patch.dict(alias.__salt__, {'aliases.get_target': get_target}): with patch.dict(alias.__opts__, {'test': True}): self.assertEqual(alias.absent(name), ret)
'test alias.absent remove alias'
def test_absent_rm_alias(self):
name = 'saltdude' target = '[email protected]' ret = {'comment': 'Removed alias {0}'.format(name), 'changes': {'alias': name}, 'name': name, 'result': True} get_target = MagicMock(return_value=True) rm_alias = MagicMock(return_value=True) with patch.dict(alias.__salt__, {'aliases.get_target': get_target}): with patch.dict(alias.__opts__, {'test': False}): with patch.dict(alias.__salt__, {'aliases.rm_alias': rm_alias}): self.assertEqual(alias.absent(name), ret)
'test alias.absent remove alias failure'
def test_absent_rm_alias_failed(self):
name = 'saltdude' target = '[email protected]' ret = {'comment': 'Failed to remove alias {0}'.format(name), 'changes': {}, 'name': name, 'result': False} get_target = MagicMock(return_value=True) rm_alias = MagicMock(return_value=False) with patch.dict(alias.__salt__, {'aliases.get_target': get_target}): with patch.dict(alias.__opts__, {'test': False}): with patch.dict(alias.__salt__, {'aliases.rm_alias': rm_alias}): self.assertEqual(alias.absent(name), ret)
'Test to configure the DNS server list in the specified interface'
def test_dns_exists(self):
ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': ''} with patch.dict(win_dns_client.__opts__, {'test': False}): ret.update({'changes': {'Servers Added': [], 'Servers Removed': [], 'Servers Reordered': []}, 'comment': 'servers entry is not a list !'}) self.assertDictEqual(win_dns_client.dns_exists('salt'), ret) mock = MagicMock(return_value=[2, 'salt']) with patch.dict(win_dns_client.__salt__, {'win_dns_client.get_dns_servers': mock}): ret.update({'changes': {}, 'comment': "[2, 'salt'] are already configured", 'result': True}) self.assertDictEqual(win_dns_client.dns_exists('salt', [2, 'salt']), ret) mock = MagicMock(side_effect=[False, True, True]) with patch.dict(win_dns_client.__salt__, {'win_dns_client.add_dns': mock}): ret.update({'comment': 'Failed to add 1 as DNS server number 1', 'result': False}) self.assertDictEqual(win_dns_client.dns_exists('salt', [1, 'salt']), ret) mock = MagicMock(return_value=False) with patch.dict(win_dns_client.__salt__, {'win_dns_client.rm_dns': mock}): ret.update({'changes': {'Servers Added': ['a'], 'Servers Removed': [], 'Servers Reordered': []}, 'comment': 'Failed to remove 2 from DNS server list'}) self.assertDictEqual(win_dns_client.dns_exists('salt', ['a'], 'a', 1), ret) ret.update({'comment': 'DNS Servers have been updated', 'result': True}) self.assertDictEqual(win_dns_client.dns_exists('salt', ['a']), ret)
'Test to configure the DNS server list from DHCP Server'
def test_dns_dhcp(self):
ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''} mock = MagicMock(side_effect=['dhcp', 'salt', 'salt']) with patch.dict(win_dns_client.__salt__, {'win_dns_client.get_dns_config': mock}): ret.update({'comment': 'Local Area Connection already configured with DNS from DHCP'}) self.assertDictEqual(win_dns_client.dns_dhcp('salt'), ret) with patch.dict(win_dns_client.__opts__, {'test': True}): ret.update({'comment': '', 'result': None, 'changes': {'dns': 'configured from DHCP'}}) self.assertDictEqual(win_dns_client.dns_dhcp('salt'), ret) with patch.dict(win_dns_client.__opts__, {'test': False}): mock = MagicMock(return_value=True) with patch.dict(win_dns_client.__salt__, {'win_dns_client.dns_dhcp': mock}): ret.update({'result': True}) self.assertDictEqual(win_dns_client.dns_dhcp('salt'), ret)
'Test to configure the global primary DNS suffix of a DHCP client.'
def test_primary_suffix(self):
ret = {'name': 'salt', 'changes': {}, 'result': False, 'comment': ''} ret.update({'comment': "'updates' must be a boolean value"}) self.assertDictEqual(win_dns_client.primary_suffix('salt', updates='a'), ret) mock = MagicMock(side_effect=[{'vdata': 'a'}, {'vdata': False}, {'vdata': 'b'}, {'vdata': False}]) with patch.dict(win_dns_client.__salt__, {'reg.read_value': mock}): ret.update({'comment': 'No changes needed', 'result': True}) self.assertDictEqual(win_dns_client.primary_suffix('salt', 'a'), ret) mock = MagicMock(return_value=True) with patch.dict(win_dns_client.__salt__, {'reg.set_value': mock}): ret.update({'changes': {'new': {'suffix': 'a'}, 'old': {'suffix': 'b'}}, 'comment': 'Updated primary DNS suffix (a)'}) self.assertDictEqual(win_dns_client.primary_suffix('salt', 'a'), ret)
'Test to ensure the IAM role exists.'
def test_present(self):
name = 'myrole' ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} _desc_role = {'create_date': '2015-02-11T19:47:14Z', 'role_id': 'HIUHBIUBIBNKJNBKJ', 'assume_role_policy_document': {'Version': '2008-10-17', 'Statement': [{'Action': 'sts:AssumeRole', 'Principal': {'Service': 'ec2.amazonaws.com'}, 'Effect': 'Allow'}]}, 'role_name': 'myfakerole', 'path': '/', 'arn': 'arn:aws:iam::12345:role/myfakerole'} _desc_role2 = {'create_date': '2015-02-11T19:47:14Z', 'role_id': 'HIUHBIUBIBNKJNBKJ', 'assume_role_policy_document': {'Version': '2008-10-17', 'Statement': [{'Action': 'sts:AssumeRole', 'Principal': {'Service': ['ec2.amazonaws.com', 'datapipeline.amazonaws.com']}, 'Effect': 'Allow'}]}, 'role_name': 'myfakerole', 'path': '/', 'arn': 'arn:aws:iam::12345:role/myfakerole'} mock_desc = MagicMock(side_effect=[False, _desc_role, _desc_role, _desc_role2, _desc_role]) _build_policy = {'Version': '2008-10-17', 'Statement': [{'Action': 'sts:AssumeRole', 'Effect': 'Allow', 'Principal': {'Service': 'ec2.amazonaws.com'}}]} mock_policy = MagicMock(return_value=_build_policy) mock_ipe = MagicMock(side_effect=[False, True, True, True]) mock_pa = MagicMock(side_effect=[False, True, True, True]) mock_bool = MagicMock(return_value=False) mock_lst = MagicMock(return_value=[]) with patch.dict(boto_iam_role.__salt__, {'boto_iam.describe_role': mock_desc, 'boto_iam.create_role': mock_bool, 'boto_iam.build_policy': mock_policy, 'boto_iam.update_assume_role_policy': mock_bool, 'boto_iam.instance_profile_exists': mock_ipe, 'boto_iam.list_attached_role_policies': mock_lst, 'boto_iam.create_instance_profile': mock_bool, 'boto_iam.profile_associated': mock_pa, 'boto_iam.associate_profile_to_role': mock_bool, 'boto_iam.list_role_policies': mock_lst}): with patch.dict(boto_iam_role.__opts__, {'test': False}): comt = ' Failed to create {0} IAM role.'.format(name) ret.update({'comment': comt}) self.assertDictEqual(boto_iam_role.present(name), ret) comt = ' myrole role present. Failed to create myrole instance profile.' ret.update({'comment': comt}) self.assertDictEqual(boto_iam_role.present(name), ret) comt = ' myrole role present. Failed to associate myrole instance profile with myrole role.' ret.update({'comment': comt}) self.assertDictEqual(boto_iam_role.present(name), ret) comt = ' myrole role present. Failed to update assume role policy.' ret.update({'comment': comt}) self.assertDictEqual(boto_iam_role.present(name), ret) comt = ' myrole role present. ' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(boto_iam_role.present(name), ret)
'Test to ensure the IAM role is deleted.'
def test_absent(self):
name = 'myrole' ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[['mypolicy'], ['mypolicy'], False, True, False, False, True, False, False, False, True]) mock_bool = MagicMock(return_value=False) mock_lst = MagicMock(return_value=[]) with patch.dict(boto_iam_role.__salt__, {'boto_iam.list_role_policies': mock, 'boto_iam.delete_role_policy': mock_bool, 'boto_iam.profile_associated': mock, 'boto_iam.disassociate_profile_from_role': mock_bool, 'boto_iam.instance_profile_exists': mock, 'boto_iam.list_attached_role_policies': mock_lst, 'boto_iam.delete_instance_profile': mock_bool, 'boto_iam.role_exists': mock, 'boto_iam.delete_role': mock_bool}): with patch.dict(boto_iam_role.__opts__, {'test': False}): comt = ' Failed to add policy mypolicy to role myrole' ret.update({'comment': comt, 'changes': {'new': {'policies': ['mypolicy']}, 'old': {'policies': ['mypolicy']}}}) self.assertDictEqual(boto_iam_role.absent(name), ret) comt = ' No policies in role myrole. No attached policies in role myrole. Failed to disassociate myrole instance profile from myrole role.' ret.update({'comment': comt, 'changes': {}}) self.assertDictEqual(boto_iam_role.absent(name), ret) comt = ' No policies in role myrole. No attached policies in role myrole. Failed to delete myrole instance profile.' ret.update({'comment': comt, 'changes': {}}) self.assertDictEqual(boto_iam_role.absent(name), ret) comt = ' No policies in role myrole. No attached policies in role myrole. myrole instance profile does not exist. Failed to delete myrole iam role.' ret.update({'comment': comt, 'changes': {}}) self.assertDictEqual(boto_iam_role.absent(name), ret)
'Test to verify that a device is mounted.'
def test_mounted(self):
name = '/mnt/sdb' device = '/dev/sdb5' fstype = 'xfs' name2 = '/mnt/cifs' device2 = '//SERVER/SHARE/' fstype2 = 'cifs' opts2 = ['noowners'] superopts2 = ['uid=510', 'gid=100', 'username=cifsuser', 'domain=cifsdomain'] ret = {'name': name, 'result': False, 'comment': '', 'changes': {}} mock = MagicMock(side_effect=['new', 'present', 'new', 'change', 'bad config', 'salt', 'present']) mock_t = MagicMock(return_value=True) mock_f = MagicMock(return_value=False) mock_ret = MagicMock(return_value={'retcode': 1}) mock_mnt = MagicMock(return_value={name: {'device': device, 'opts': [], 'superopts': []}, name2: {'device': device2, 'opts': opts2, 'superopts': superopts2}}) mock_emt = MagicMock(return_value={}) mock_str = MagicMock(return_value='salt') mock_user = MagicMock(return_value={'uid': 510}) mock_group = MagicMock(return_value={'gid': 100}) umount1 = "Forced unmount because devices don't match. Wanted: /dev/sdb6, current: /dev/sdb5, /dev/sdb5" with patch.dict(mount.__grains__, {'os': 'Darwin'}): with patch.dict(mount.__salt__, {'mount.active': mock_mnt, 'cmd.run_all': mock_ret, 'mount.umount': mock_f}): comt = 'Unable to find device with label /dev/sdb5.' ret.update({'comment': comt}) self.assertDictEqual(mount.mounted(name, 'LABEL=/dev/sdb5', fstype), ret) with patch.dict(mount.__opts__, {'test': True}): comt = 'Remount would be forced because options (noowners) changed' ret.update({'comment': comt, 'result': None}) self.assertDictEqual(mount.mounted(name, device, fstype), ret) with patch.dict(mount.__opts__, {'test': False}): comt = 'Unable to unmount /mnt/sdb: False.' umount = 'Forced unmount and mount because options (noowners) changed' ret.update({'comment': comt, 'result': False, 'changes': {'umount': umount}}) self.assertDictEqual(mount.mounted(name, device, 'nfs'), ret) comt = 'Unable to unmount' ret.update({'comment': comt, 'result': None, 'changes': {'umount': umount1}}) self.assertDictEqual(mount.mounted(name, '/dev/sdb6', fstype, opts=[]), ret) with patch.dict(mount.__salt__, {'mount.active': mock_emt, 'mount.mount': mock_str, 'mount.set_automaster': mock}): with patch.dict(mount.__opts__, {'test': True}): comt = '{0} does not exist and would not be created'.format(name) ret.update({'comment': comt, 'changes': {}}) self.assertDictEqual(mount.mounted(name, device, fstype), ret) with patch.dict(mount.__opts__, {'test': False}): with patch.object(os.path, 'exists', mock_f): comt = 'Mount directory is not present' ret.update({'comment': comt, 'result': False}) self.assertDictEqual(mount.mounted(name, device, fstype), ret) with patch.object(os.path, 'exists', mock_t): comt = 'Mount directory is not present' ret.update({'comment': 'salt', 'result': False}) self.assertDictEqual(mount.mounted(name, device, fstype), ret) with patch.dict(mount.__opts__, {'test': True}): comt = '{0} does not exist and would neither be created nor mounted. {0} needs to be written to the fstab in order to be made persistent.'.format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(mount.mounted(name, device, fstype, mount=False), ret) with patch.dict(mount.__opts__, {'test': False}): comt = '{0} not present and not mounted. Entry already exists in the fstab.'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(mount.mounted(name, device, fstype, mount=False), ret) comt = '{0} not present and not mounted. Added new entry to the fstab.'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {'persist': 'new'}}) self.assertDictEqual(mount.mounted(name, device, fstype, mount=False), ret) comt = '{0} not present and not mounted. Updated the entry in the fstab.'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {'persist': 'update'}}) self.assertDictEqual(mount.mounted(name, device, fstype, mount=False), ret) comt = '{0} not present and not mounted. However, the fstab was not found.'.format(name) ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(mount.mounted(name, device, fstype, mount=False), ret) comt = '{0} not present and not mounted'.format(name) ret.update({'comment': comt, 'result': True, 'changes': {}}) self.assertDictEqual(mount.mounted(name, device, fstype, mount=False), ret) with patch.dict(mount.__grains__, {'os': 'CentOS'}): with patch.dict(mount.__salt__, {'mount.active': mock_mnt, 'mount.mount': mock_str, 'mount.umount': mock_f, 'mount.set_fstab': mock, 'user.info': mock_user, 'group.info': mock_group}): with patch.dict(mount.__opts__, {'test': True}): with patch.object(os.path, 'exists', mock_t): comt = ('Target was already mounted. ' + 'Entry already exists in the fstab.') ret.update({'name': name2, 'result': True}) ret.update({'comment': comt, 'changes': {}}) self.assertDictEqual(mount.mounted(name2, device2, fstype2, opts=['uid=user1', 'gid=group1']), ret)
'Test to activates a swap device.'
def test_swap(self):
name = '/mnt/sdb' ret = {'name': name, 'result': None, 'comment': '', 'changes': {}} mock = MagicMock(side_effect=['present', 'new', 'change', 'bad config']) mock_f = MagicMock(return_value=False) mock_swp = MagicMock(return_value=[name]) mock_fs = MagicMock(return_value={'none': {'device': name, 'fstype': 'xfs'}}) mock_emt = MagicMock(return_value={}) with patch.dict(mount.__salt__, {'mount.swaps': mock_swp, 'mount.fstab': mock_fs, 'file.is_link': mock_f}): with patch.dict(mount.__opts__, {'test': True}): comt = 'Swap {0} is set to be added to the fstab and to be activated'.format(name) ret.update({'comment': comt}) self.assertDictEqual(mount.swap(name), ret) with patch.dict(mount.__opts__, {'test': False}): comt = 'Swap {0} already active'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(mount.swap(name), ret) with patch.dict(mount.__salt__, {'mount.fstab': mock_emt, 'mount.set_fstab': mock}): comt = 'Swap {0} already active'.format(name) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(mount.swap(name), ret) comt = 'Swap /mnt/sdb already active. Added new entry to the fstab.' ret.update({'comment': comt, 'result': True, 'changes': {'persist': 'new'}}) self.assertDictEqual(mount.swap(name), ret) comt = 'Swap /mnt/sdb already active. Updated the entry in the fstab.' ret.update({'comment': comt, 'result': True, 'changes': {'persist': 'update'}}) self.assertDictEqual(mount.swap(name), ret) comt = 'Swap /mnt/sdb already active. However, the fstab was not found.' ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(mount.swap(name), ret)
'Test to verify that a device is not mounted'
def test_unmounted(self):
name = '/mnt/sdb' device = '/dev/sdb5' ret = {'name': name, 'result': None, 'comment': '', 'changes': {}} mock_f = MagicMock(return_value=False) mock_dev = MagicMock(return_value={name: {'device': device}}) mock_fs = MagicMock(return_value={name: {'device': name}}) mock_mnt = MagicMock(side_effect=[{name: {}}, {}, {}, {}]) comt3 = 'Mount point /mnt/sdb is unmounted but needs to be purged from /etc/auto_salt to be made persistent' with patch.dict(mount.__grains__, {'os': 'Darwin'}): with patch.dict(mount.__salt__, {'mount.active': mock_mnt, 'mount.automaster': mock_fs, 'file.is_link': mock_f}): with patch.dict(mount.__opts__, {'test': True}): comt = 'Mount point {0} is mounted but should not be'.format(name) ret.update({'comment': comt}) self.assertDictEqual(mount.unmounted(name, device), ret) comt = 'Target was already unmounted. fstab entry for device /dev/sdb5 not found' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(mount.unmounted(name, device, persist=True), ret) with patch.dict(mount.__salt__, {'mount.automaster': mock_dev}): ret.update({'comment': comt3, 'result': None}) self.assertDictEqual(mount.unmounted(name, device, persist=True), ret) comt = 'Target was already unmounted' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(mount.unmounted(name, device), ret)
'Test the mounted watcher, called to invoke the watch command.'
def test_mod_watch(self):
name = '/mnt/sdb' ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} comt = 'Watch not supported in unmount at this time' ret.update({'comment': comt}) self.assertDictEqual(mount.mod_watch(name, sfun='unmount'), ret)
'Try and create a record that already exists'
def test_present_record_exists(self):
result = libcloud_dns.record_present('www', 'test.com', 'A', '127.0.0.1', 'test') self.assertTrue(result)
'Try and create a record that already exists'
def test_present_record_does_not_exist(self):
result = libcloud_dns.record_present('mail', 'test.com', 'A', '127.0.0.1', 'test') self.assertTrue(result)
'Try and deny a record that already exists'
def test_absent_record_exists(self):
result = libcloud_dns.record_absent('www', 'test.com', 'A', '127.0.0.1', 'test') self.assertTrue(result)