desc
stringlengths 3
26.7k
| decl
stringlengths 11
7.89k
| bodies
stringlengths 8
553k
|
---|---|---|
'Test to ensure the named elasticache cluster is deleted.'
| def test_absent(self):
| name = 'new_table'
ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''}
mock = MagicMock(side_effect=[False, True])
with patch.dict(boto_elasticache.__salt__, {'boto_elasticache.exists': mock}):
comt = '{0} does not exist in None.'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(boto_elasticache.absent(name), ret)
with patch.dict(boto_elasticache.__opts__, {'test': True}):
comt = 'Cache cluster {0} is set to be removed.'.format(name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(boto_elasticache.absent(name), ret)
|
'Test to ensure the a replication group is create.'
| def test_creategroup(self):
| name = 'new_table'
primary_cluster_id = 'A'
replication_group_description = 'my description'
ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''}
mock = MagicMock(return_value=True)
with patch.dict(boto_elasticache.__salt__, {'boto_elasticache.group_exists': mock}):
comt = '{0} replication group exists .'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(boto_elasticache.creategroup(name, primary_cluster_id, replication_group_description), ret)
|
'Test to ensure an Apache conf 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_conf.__salt__, {'apache.check_conf_enabled': mock, 'apache.a2enconf': mock_str}):
comt = '{0} already enabled.'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(apache_conf.enabled(name), ret)
comt = 'Apache conf {0} is set to be enabled.'.format(name)
ret.update({'comment': comt, 'result': None, 'changes': {'new': name, 'old': None}})
with patch.dict(apache_conf.__opts__, {'test': True}):
self.assertDictEqual(apache_conf.enabled(name), ret)
comt = 'Failed to enable {0} Apache conf'.format(name)
ret.update({'comment': comt, 'result': False, 'changes': {}})
with patch.dict(apache_conf.__opts__, {'test': False}):
self.assertDictEqual(apache_conf.enabled(name), ret)
|
'Test to ensure an Apache conf 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_conf.__salt__, {'apache.check_conf_enabled': mock, 'apache.a2disconf': mock_str}):
comt = 'Apache conf {0} is set to be disabled.'.format(name)
ret.update({'comment': comt, 'changes': {'new': None, 'old': name}})
with patch.dict(apache_conf.__opts__, {'test': True}):
self.assertDictEqual(apache_conf.disabled(name), ret)
comt = 'Failed to disable {0} Apache conf'.format(name)
ret.update({'comment': comt, 'result': False, 'changes': {}})
with patch.dict(apache_conf.__opts__, {'test': False}):
self.assertDictEqual(apache_conf.disabled(name), ret)
comt = '{0} already disabled.'.format(name)
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(apache_conf.disabled(name), ret)
|
'Test to stop all the workers in the modjk load balancer'
| def test_worker_stopped(self):
| name = 'loadbalancer'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
ret.update({'comment': LIST_NOT_STR})
self.assertDictEqual(modjk.worker_stopped(name, 'app1'), ret)
|
'Test to activate all the workers in the modjk load balancer'
| def test_worker_activated(self):
| name = 'loadbalancer'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
ret.update({'comment': LIST_NOT_STR})
self.assertDictEqual(modjk.worker_activated(name, 'app1'), ret)
|
'Test to disable all the workers in the modjk load balancer'
| def test_worker_disabled(self):
| name = 'loadbalancer'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
ret.update({'comment': LIST_NOT_STR})
self.assertDictEqual(modjk.worker_disabled(name, 'app1'), ret)
|
'Test to recover all the workers in the modjk load balancer'
| def test_worker_recover(self):
| name = 'loadbalancer'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
ret.update({'comment': LIST_NOT_STR})
self.assertDictEqual(modjk.worker_recover(name, 'app1'), ret)
|
'Tests present on a VPC that does not exist.'
| @mock_ec2
def test_present_when_vpc_does_not_exist(self):
| with patch.dict(salt.utils.boto.__salt__, self.funcs):
vpc_present_result = self.salt_states['boto_vpc.present']('test', cidr_block)
self.assertTrue(vpc_present_result['result'])
self.assertEqual(vpc_present_result['changes']['new']['vpc']['state'], 'available')
|
'Tests absent on a VPC that does not exist.'
| @mock_ec2
def test_absent_when_vpc_does_not_exist(self):
| with patch.dict(salt.utils.boto.__salt__, self.funcs):
vpc_absent_result = self.salt_states['boto_vpc.absent']('test')
self.assertTrue(vpc_absent_result['result'])
self.assertEqual(vpc_absent_result['changes'], {})
|
'Tests present on a resource that does not exist.'
| @mock_ec2
def test_present_when_resource_does_not_exist(self):
| vpc = self._create_vpc(name='test')
with patch.dict(salt.utils.boto.__salt__, self.funcs):
resource_present_result = self.salt_states['boto_vpc.{0}_present'.format(self.resource_type)](name='test', vpc_name='test', **self.extra_kwargs)
self.assertTrue(resource_present_result['result'])
exists = self.funcs['boto_vpc.resource_exists'](self.resource_type, 'test').get('exists')
self.assertTrue(exists)
|
'Tests absent on a resource that does not exist.'
| @mock_ec2
def test_absent_when_resource_does_not_exist(self):
| with patch.dict(salt.utils.boto.__salt__, self.funcs):
resource_absent_result = self.salt_states['boto_vpc.{0}_absent'.format(self.resource_type)]('test')
self.assertTrue(resource_absent_result['result'])
self.assertEqual(resource_absent_result['changes'], {})
|
'Test to verify that the named port exists on bridge, eventually creates it.'
| def test_present(self):
| name = 'salt'
bridge = 'br-salt'
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
mock = MagicMock(return_value=True)
mock_l = MagicMock(return_value=['salt'])
mock_n = MagicMock(return_value=[])
with patch.dict(openvswitch_port.__salt__, {'openvswitch.bridge_exists': mock, 'openvswitch.port_list': mock_l}):
comt = 'Port salt already exists.'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(openvswitch_port.present(name, bridge), ret)
with patch.dict(openvswitch_port.__salt__, {'openvswitch.bridge_exists': mock, 'openvswitch.port_list': mock_n, 'openvswitch.port_add': mock}):
comt = 'Port salt created on bridge br-salt.'
ret.update({'comment': comt, 'result': True, 'changes': {'salt': {'new': 'Created port salt on bridge br-salt.', 'old': 'No port named salt present.'}}})
self.assertDictEqual(openvswitch_port.present(name, bridge), ret)
with patch.dict(openvswitch_port.__salt__, {'openvswitch.bridge_exists': mock, 'openvswitch.port_list': mock_n, 'openvswitch.port_add': mock, 'openvswitch.interface_get_options': mock_n, 'openvswitch.interface_get_type': MagicMock(return_value=''), 'openvswitch.port_create_gre': mock, 'dig.check_ip': mock}):
comt = 'Port salt created on bridge br-salt.'
self.maxDiff = None
ret.update({'result': True, 'comment': 'Created GRE tunnel interface salt with remote ip 10.0.0.1 and key 1 on bridge br-salt.', 'changes': {'salt': {'new': 'Created GRE tunnel interface salt with remote ip 10.0.0.1 and key 1 on bridge br-salt.', 'old': 'No GRE tunnel interface salt with remote ip 10.0.0.1 and key 1 on bridge br-salt present.'}}})
self.assertDictEqual(openvswitch_port.present(name, bridge, tunnel_type='gre', id=1, remote='10.0.0.1'), ret)
|
'Test to set the quota for the system.'
| def test_mode(self):
| name = '/'
mode = True
quotatype = 'user'
ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}
mock_bool = MagicMock(side_effect=[True, False])
mock = MagicMock(return_value={name: {quotatype: 'on'}})
with patch.dict(quota.__salt__, {'quota.get_mode': mock}):
comt = 'Quota for / already set to on'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(quota.mode(name, mode, quotatype), ret)
mock = MagicMock(return_value={name: {quotatype: 'off'}})
with patch.dict(quota.__salt__, {'quota.get_mode': mock, 'quota.on': mock_bool}):
with patch.dict(quota.__opts__, {'test': True}):
comt = 'Quota for / needs to be set to on'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(quota.mode(name, mode, quotatype), ret)
with patch.dict(quota.__opts__, {'test': False}):
comt = 'Set quota for / to on'
ret.update({'comment': comt, 'result': True, 'changes': {'quota': name}})
self.assertDictEqual(quota.mode(name, mode, quotatype), ret)
comt = 'Failed to set quota for / to on'
ret.update({'comment': comt, 'result': False, 'changes': {}})
self.assertDictEqual(quota.mode(name, mode, quotatype), ret)
|
'Test to install the windows feature'
| def test_installed(self):
| mock_list = MagicMock(side_effect=[{'spongebob': 'squarepants'}, {'squidward': 'patrick'}, {'spongebob': 'squarepants'}, {'spongebob': 'squarepants', 'squidward': 'patrick'}])
mock_install = MagicMock(return_value={'Success': True, 'Restarted': False, 'RestartNeeded': False, 'ExitCode': 1234, 'Features': {'squidward': {'DisplayName': 'Squidward', 'Message': '', 'RestartNeeded': True, 'SkipReason': 0, 'Success': True}}})
with patch.dict(win_servermanager.__salt__, {'win_servermanager.list_installed': mock_list, 'win_servermanager.install': mock_install}):
ret = {'name': 'spongebob', 'changes': {}, 'result': True, 'comment': 'The following features are already installed:\n- spongebob'}
self.assertDictEqual(win_servermanager.installed('spongebob'), ret)
with patch.dict(win_servermanager.__opts__, {'test': True}):
ret = {'name': 'spongebob', 'result': None, 'comment': '', 'changes': {'spongebob': 'Will be installed recurse=False'}}
self.assertDictEqual(win_servermanager.installed('spongebob'), ret)
with patch.dict(win_servermanager.__opts__, {'test': False}):
ret = {'name': 'squidward', 'result': True, 'comment': 'Installed the following:\n- squidward', 'changes': {'squidward': {'new': 'patrick', 'old': ''}}}
self.assertDictEqual(win_servermanager.installed('squidward'), ret)
|
'Test to remove the windows feature'
| def test_removed(self):
| mock_list = MagicMock(side_effect=[{'spongebob': 'squarepants'}, {'squidward': 'patrick'}, {'spongebob': 'squarepants', 'squidward': 'patrick'}, {'spongebob': 'squarepants'}])
mock_remove = MagicMock(return_value={'Success': True, 'RestartNeeded': False, 'Restarted': False, 'ExitCode': 1234, 'Features': {'squidward': {'DisplayName': 'Squidward', 'Message': '', 'RestartNeeded': True, 'SkipReason': 0, 'Success': True}}})
with patch.dict(win_servermanager.__salt__, {'win_servermanager.list_installed': mock_list, 'win_servermanager.remove': mock_remove}):
ret = {'name': 'squidward', 'changes': {}, 'result': True, 'comment': 'The following features are not installed:\n- squidward'}
self.assertDictEqual(win_servermanager.removed('squidward'), ret)
with patch.dict(win_servermanager.__opts__, {'test': True}):
ret = {'name': 'squidward', 'result': None, 'comment': '', 'changes': {'squidward': 'Will be removed'}}
self.assertDictEqual(win_servermanager.removed('squidward'), ret)
with patch.dict(win_servermanager.__opts__, {'test': False}):
ret = {'name': 'squidward', 'result': True, 'comment': 'Removed the following:\n- squidward', 'changes': {'squidward': {'new': '', 'old': 'patrick'}}}
self.assertDictEqual(win_servermanager.removed('squidward'), ret)
|
'Test to verify that the service is running'
| def test_running(self):
| ret = [{'comment': '', 'changes': {}, 'name': 'salt', 'result': True}, {'changes': {}, 'comment': 'The service salt is already running', 'name': 'salt', 'result': True}, {'changes': 'saltstack', 'comment': 'The service salt is already running', 'name': 'salt', 'result': True}, {'changes': {}, 'comment': 'Service salt is set to start', 'name': 'salt', 'result': None}, {'changes': 'saltstack', 'comment': 'Started Service salt', 'name': 'salt', 'result': True}, {'changes': {}, 'comment': 'The service salt is already running', 'name': 'salt', 'result': True}, {'changes': 'saltstack', 'comment': 'Service salt failed to start', 'name': 'salt', 'result': False}]
tmock = MagicMock(return_value=True)
fmock = MagicMock(return_value=False)
vmock = MagicMock(return_value='salt')
with patch.object(service, '_enabled_used_error', vmock):
self.assertEqual(service.running('salt', enabled=1), 'salt')
with patch.object(service, '_available', fmock):
self.assertDictEqual(service.running('salt'), ret[0])
with patch.object(service, '_available', tmock):
with patch.dict(service.__opts__, {'test': False}):
with patch.dict(service.__salt__, {'service.enabled': tmock, 'service.status': tmock}):
self.assertDictEqual(service.running('salt'), ret[1])
mock = MagicMock(return_value={'changes': 'saltstack'})
with patch.dict(service.__salt__, {'service.enabled': MagicMock(side_effect=[False, True]), 'service.status': tmock}):
with patch.object(service, '_enable', mock):
self.assertDictEqual(service.running('salt', True), ret[2])
with patch.dict(service.__salt__, {'service.enabled': MagicMock(side_effect=[True, False]), 'service.status': tmock}):
with patch.object(service, '_disable', mock):
self.assertDictEqual(service.running('salt', False), ret[2])
with patch.dict(service.__salt__, {'service.status': MagicMock(side_effect=[False, True]), 'service.enabled': MagicMock(side_effect=[False, True]), 'service.start': MagicMock(return_value='stack')}):
with patch.object(service, '_enable', MagicMock(return_value={'changes': 'saltstack'})):
self.assertDictEqual(service.running('salt', True), ret[4])
with patch.dict(service.__opts__, {'test': True}):
with patch.dict(service.__salt__, {'service.status': tmock}):
self.assertDictEqual(service.running('salt'), ret[5])
with patch.dict(service.__salt__, {'service.status': fmock}):
self.assertDictEqual(service.running('salt'), ret[3])
with patch.dict(service.__opts__, {'test': False}):
with patch.dict(service.__salt__, {'service.status': MagicMock(side_effect=[False, False]), 'service.enabled': MagicMock(side_effecct=[True, True]), 'service.start': MagicMock(return_value='stack')}):
with patch.object(service, '_enable', MagicMock(return_value={'changes': 'saltstack'})):
self.assertDictEqual(service.running('salt', True), ret[6])
|
'Test to ensure that the named service is dead'
| def test_dead(self):
| ret = [{'changes': {}, 'comment': '', 'name': 'salt', 'result': True}, {'changes': 'saltstack', 'comment': 'The service salt is already dead', 'name': 'salt', 'result': True}, {'changes': {}, 'comment': 'Service salt is set to be killed', 'name': 'salt', 'result': None}, {'changes': 'saltstack', 'comment': 'Service salt was killed', 'name': 'salt', 'result': True}, {'changes': {}, 'comment': 'Service salt failed to die', 'name': 'salt', 'result': False}, {'changes': 'saltstack', 'comment': 'The service salt is already dead', 'name': 'salt', 'result': True}]
mock = MagicMock(return_value='salt')
with patch.object(service, '_enabled_used_error', mock):
self.assertEqual(service.dead('salt', enabled=1), 'salt')
tmock = MagicMock(return_value=True)
fmock = MagicMock(return_value=False)
with patch.object(service, '_available', fmock):
self.assertDictEqual(service.dead('salt'), ret[0])
with patch.object(service, '_available', tmock):
mock = MagicMock(return_value={'changes': 'saltstack'})
with patch.dict(service.__opts__, {'test': True}):
with patch.dict(service.__salt__, {'service.enabled': fmock, 'service.stop': tmock, 'service.status': fmock}):
with patch.object(service, '_enable', mock):
self.assertDictEqual(service.dead('salt', True), ret[5])
with patch.dict(service.__salt__, {'service.enabled': tmock, 'service.status': tmock}):
self.assertDictEqual(service.dead('salt'), ret[2])
with patch.dict(service.__opts__, {'test': False}):
with patch.dict(service.__salt__, {'service.enabled': fmock, 'service.stop': tmock, 'service.status': fmock}):
with patch.object(service, '_enable', mock):
self.assertDictEqual(service.dead('salt', True), ret[1])
with patch.dict(service.__salt__, {'service.enabled': MagicMock(side_effect=[True, True, False]), 'service.status': MagicMock(side_effect=[True, False, False]), 'service.stop': MagicMock(return_value='stack')}):
with patch.object(service, '_enable', MagicMock(return_value={'changes': 'saltstack'})):
self.assertDictEqual(service.dead('salt', True), ret[3])
with patch.dict(service.__salt__, {'service.enabled': MagicMock(side_effect=[False, False, False]), 'service.status': MagicMock(side_effect=[True, True, True]), 'service.stop': MagicMock(return_value='stack')}):
with patch.object(service, '_disable', MagicMock(return_value={})):
self.assertDictEqual(service.dead('salt', False), ret[4])
|
'Tests the case in which a service.dead state is executed on a state
which does not exist.
See https://github.com/saltstack/salt/issues/37511'
| def test_dead_with_missing_service(self):
| name = 'thisisnotarealservice'
with patch.dict(service.__salt__, {'service.available': MagicMock(return_value=False)}):
ret = service.dead(name=name)
self.assertDictEqual(ret, {'changes': {}, 'comment': 'The named service {0} is not available'.format(name), 'result': True, 'name': name})
|
'Test to verify that the service is enabled'
| def test_enabled(self):
| ret = {'changes': 'saltstack', 'comment': '', 'name': 'salt', 'result': True}
mock = MagicMock(return_value={'changes': 'saltstack'})
with patch.object(service, '_enable', mock):
self.assertDictEqual(service.enabled('salt'), ret)
|
'Test to verify that the service is disabled'
| def test_disabled(self):
| ret = {'changes': 'saltstack', 'comment': '', 'name': 'salt', 'result': True}
mock = MagicMock(return_value={'changes': 'saltstack'})
with patch.object(service, '_disable', mock):
self.assertDictEqual(service.disabled('salt'), ret)
|
'Test to the service watcher, called to invoke the watch command.'
| def test_mod_watch(self):
| ret = [{'changes': {}, 'comment': 'Service is already stopped', 'name': 'salt', 'result': True}, {'changes': {}, 'comment': 'Unable to trigger watch for service.stack', 'name': 'salt', 'result': False}, {'changes': {}, 'comment': 'Service is set to be started', 'name': 'salt', 'result': None}, {'changes': {'salt': 'salt'}, 'comment': 'Service started', 'name': 'salt', 'result': 'salt'}]
mock = MagicMock(return_value=False)
with patch.dict(service.__salt__, {'service.status': mock}):
self.assertDictEqual(service.mod_watch('salt', 'dead'), ret[0])
with patch.dict(service.__salt__, {'service.start': func}):
with patch.dict(service.__opts__, {'test': True}):
self.assertDictEqual(service.mod_watch('salt', 'running'), ret[2])
with patch.dict(service.__opts__, {'test': False}):
self.assertDictEqual(service.mod_watch('salt', 'running'), ret[3])
self.assertDictEqual(service.mod_watch('salt', 'stack'), ret[1])
|
'Test to send a message to an XMPP user'
| def test_send_msg(self):
| ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''}
with patch.dict(xmpp.__opts__, {'test': True}):
ret.update({'comment': 'Need to send message to myaccount: salt'})
self.assertDictEqual(xmpp.send_msg('salt', 'myaccount', '[email protected]'), ret)
with patch.dict(xmpp.__opts__, {'test': False}):
mock = MagicMock(return_value=True)
with patch.dict(xmpp.__salt__, {'xmpp.send_msg': mock, 'xmpp.send_msg_multi': mock}):
ret.update({'result': True, 'comment': 'Sent message to myaccount: salt'})
self.assertDictEqual(xmpp.send_msg('salt', 'myaccount', '[email protected]'), ret)
|
'Test to ensure the RackSpace queue exists.'
| def test_present(self):
| name = 'myqueue'
provider = 'my-pyrax'
ret = {'name': name, 'changes': {}, 'result': True, 'comment': ''}
mock_dct = MagicMock(side_effect=[{provider: {'salt': True}}, {provider: {'salt': False}}, {provider: {'salt': False}}, False])
with patch.dict(pyrax_queues.__salt__, {'cloud.action': mock_dct}):
comt = '{0} present.'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(pyrax_queues.present(name, provider), ret)
with patch.dict(pyrax_queues.__opts__, {'test': True}):
comt = 'Rackspace queue myqueue is set to be created.'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(pyrax_queues.present(name, provider), ret)
with patch.dict(pyrax_queues.__opts__, {'test': False}):
comt = 'Failed to create myqueue Rackspace queue.'
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(pyrax_queues.present(name, provider), ret)
|
'Test to ensure the named Rackspace queue is deleted.'
| def test_absent(self):
| name = 'myqueue'
provider = 'my-pyrax'
ret = {'name': name, 'changes': {}, 'result': True, 'comment': ''}
mock_dct = MagicMock(side_effect=[{provider: {'salt': False}}, {provider: {'salt': True}}])
with patch.dict(pyrax_queues.__salt__, {'cloud.action': mock_dct}):
comt = 'myqueue does not exist.'
ret.update({'comment': comt})
self.assertDictEqual(pyrax_queues.absent(name, provider), ret)
with patch.dict(pyrax_queues.__opts__, {'test': True}):
comt = 'Rackspace queue myqueue is set to be removed.'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(pyrax_queues.absent(name, provider), ret)
|
'Test installing a PKG file'
| def test_installed_pkg(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
expected = {'changes': {'installed': ['some.other.id']}, 'comment': '/path/to/file.pkg installed', 'name': '/path/to/file.pkg', 'result': True}
installed_mock = MagicMock(return_value=['com.apple.id'])
get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
install_mock = MagicMock(return_value={'retcode': 0})
_mod_run_check_mock.return_value = True
with patch.dict(macpackage.__salt__, {'macpackage.installed_pkgs': installed_mock, 'macpackage.get_pkg_id': get_pkg_id_mock, 'macpackage.install': install_mock}):
out = macpackage.installed('/path/to/file.pkg')
installed_mock.assert_called_once_with()
get_pkg_id_mock.assert_called_once_with('/path/to/file.pkg')
install_mock.assert_called_once_with('/path/to/file.pkg', 'LocalSystem', False, False)
self.assertEqual(out, expected)
|
'Test installing a PKG file where it\'s already installed'
| def test_installed_pkg_exists(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
expected = {'changes': {}, 'comment': '', 'name': '/path/to/file.pkg', 'result': True}
installed_mock = MagicMock(return_value=['com.apple.id', 'some.other.id'])
get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
install_mock = MagicMock(return_value={'retcode': 0})
_mod_run_check_mock.return_value = True
with patch.dict(macpackage.__salt__, {'macpackage.installed_pkgs': installed_mock, 'macpackage.get_pkg_id': get_pkg_id_mock, 'macpackage.install': install_mock}):
out = macpackage.installed('/path/to/file.pkg')
installed_mock.assert_called_once_with()
get_pkg_id_mock.assert_called_once_with('/path/to/file.pkg')
assert (not install_mock.called)
self.assertEqual(out, expected)
|
'Test installing a PKG file where the version number matches the current installed version'
| def test_installed_pkg_version_succeeds(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
expected = {'changes': {}, 'comment': 'Version already matches .*5\\.1\\.[0-9]', 'name': '/path/to/file.pkg', 'result': True}
installed_mock = MagicMock(return_value=['com.apple.id', 'some.other.id'])
get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
install_mock = MagicMock(return_value={'retcode': 0})
cmd_mock = MagicMock(return_value='Version of this: 5.1.9')
_mod_run_check_mock.return_value = True
with patch.dict(macpackage.__salt__, {'macpackage.installed_pkgs': installed_mock, 'macpackage.get_pkg_id': get_pkg_id_mock, 'macpackage.install': install_mock, 'cmd.run': cmd_mock}):
out = macpackage.installed('/path/to/file.pkg', version_check='/usr/bin/runme --version=.*5\\.1\\.[0-9]')
cmd_mock.assert_called_once_with('/usr/bin/runme --version', output_loglevel='quiet', ignore_retcode=True)
assert (not installed_mock.called)
assert (not get_pkg_id_mock.called)
assert (not install_mock.called)
self.assertEqual(out, expected)
|
'Test installing a PKG file where the version number if different from the expected one'
| def test_installed_pkg_version_fails(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
expected = {'changes': {'installed': ['some.other.id']}, 'comment': "Version Version of this: 1.8.9 doesn't match .*5\\.1\\.[0-9]. /path/to/file.pkg installed", 'name': '/path/to/file.pkg', 'result': True}
installed_mock = MagicMock(return_value=['com.apple.id'])
get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
install_mock = MagicMock(return_value={'retcode': 0})
cmd_mock = MagicMock(return_value='Version of this: 1.8.9')
_mod_run_check_mock.return_value = True
with patch.dict(macpackage.__salt__, {'macpackage.installed_pkgs': installed_mock, 'macpackage.get_pkg_id': get_pkg_id_mock, 'macpackage.install': install_mock, 'cmd.run': cmd_mock}):
out = macpackage.installed('/path/to/file.pkg', version_check='/usr/bin/runme --version=.*5\\.1\\.[0-9]')
cmd_mock.assert_called_once_with('/usr/bin/runme --version', output_loglevel='quiet', ignore_retcode=True)
installed_mock.assert_called_once_with()
get_pkg_id_mock.assert_called_once_with('/path/to/file.pkg')
install_mock.assert_called_once_with('/path/to/file.pkg', 'LocalSystem', False, False)
self.assertEqual(out, expected)
|
'Test installing a DMG file'
| def test_installed_dmg(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
expected = {'changes': {'installed': ['some.other.id']}, 'comment': '/path/to/file.dmg installed', 'name': '/path/to/file.dmg', 'result': True}
mount_mock = MagicMock(return_value=['success', '/tmp/dmg-X'])
unmount_mock = MagicMock()
installed_mock = MagicMock(return_value=['com.apple.id'])
get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
install_mock = MagicMock(return_value={'retcode': 0})
_mod_run_check_mock.return_value = True
with patch.dict(macpackage.__salt__, {'macpackage.mount': mount_mock, 'macpackage.unmount': unmount_mock, 'macpackage.installed_pkgs': installed_mock, 'macpackage.get_pkg_id': get_pkg_id_mock, 'macpackage.install': install_mock}):
out = macpackage.installed('/path/to/file.dmg', dmg=True)
mount_mock.assert_called_once_with('/path/to/file.dmg')
unmount_mock.assert_called_once_with('/tmp/dmg-X')
installed_mock.assert_called_once_with()
get_pkg_id_mock.assert_called_once_with('/tmp/dmg-X/*.pkg')
install_mock.assert_called_once_with('/tmp/dmg-X/*.pkg', 'LocalSystem', False, False)
self.assertEqual(out, expected)
|
'Test installing a DMG file when the package already exists'
| def test_installed_dmg_exists(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
expected = {'changes': {}, 'comment': '', 'name': '/path/to/file.dmg', 'result': True}
mount_mock = MagicMock(return_value=['success', '/tmp/dmg-X'])
unmount_mock = MagicMock()
installed_mock = MagicMock(return_value=['com.apple.id', 'some.other.id'])
get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
install_mock = MagicMock(return_value={'retcode': 0})
_mod_run_check_mock.return_value = True
with patch.dict(macpackage.__salt__, {'macpackage.mount': mount_mock, 'macpackage.unmount': unmount_mock, 'macpackage.installed_pkgs': installed_mock, 'macpackage.get_pkg_id': get_pkg_id_mock, 'macpackage.install': install_mock}):
out = macpackage.installed('/path/to/file.dmg', dmg=True)
mount_mock.assert_called_once_with('/path/to/file.dmg')
unmount_mock.assert_called_once_with('/tmp/dmg-X')
installed_mock.assert_called_once_with()
get_pkg_id_mock.assert_called_once_with('/tmp/dmg-X/*.pkg')
assert (not install_mock.called)
self.assertEqual(out, expected)
|
'Test installing an APP file'
| def test_installed_app(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
with patch('os.path.exists') as exists_mock:
expected = {'changes': {'installed': ['file.app']}, 'comment': 'file.app installed', 'name': '/path/to/file.app', 'result': True}
install_mock = MagicMock()
_mod_run_check_mock.return_value = True
exists_mock.return_value = False
with patch.dict(macpackage.__salt__, {'macpackage.install_app': install_mock}):
out = macpackage.installed('/path/to/file.app', app=True)
install_mock.assert_called_once_with('/path/to/file.app', '/Applications/')
self.assertEqual(out, expected)
|
'Test installing an APP file that already exists'
| def test_installed_app_exists(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
with patch('os.path.exists') as exists_mock:
expected = {'changes': {}, 'comment': '', 'name': '/path/to/file.app', 'result': True}
install_mock = MagicMock()
_mod_run_check_mock.return_value = True
exists_mock.return_value = True
with patch.dict(macpackage.__salt__, {'macpackage.install_app': install_mock}):
out = macpackage.installed('/path/to/file.app', app=True)
assert (not install_mock.called)
self.assertEqual(out, expected)
|
'Test installing an APP file contained in a DMG file'
| def test_installed_app_dmg(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
with patch('os.path.exists') as exists_mock:
expected = {'changes': {'installed': ['file.app']}, 'comment': 'file.app installed', 'name': '/path/to/file.dmg', 'result': True}
install_mock = MagicMock()
mount_mock = MagicMock(return_value=['success', '/tmp/dmg-X'])
unmount_mock = MagicMock()
cmd_mock = MagicMock(return_value='file.app')
_mod_run_check_mock.return_value = True
exists_mock.return_value = False
with patch.dict(macpackage.__salt__, {'macpackage.install_app': install_mock, 'macpackage.mount': mount_mock, 'macpackage.unmount': unmount_mock, 'cmd.run': cmd_mock}):
out = macpackage.installed('/path/to/file.dmg', app=True, dmg=True)
mount_mock.assert_called_once_with('/path/to/file.dmg')
unmount_mock.assert_called_once_with('/tmp/dmg-X')
cmd_mock.assert_called_once_with('ls -d *.app', python_shell=True, cwd='/tmp/dmg-X')
install_mock.assert_called_once_with('/tmp/dmg-X/file.app', '/Applications/')
self.assertEqual(out, expected)
|
'Test installing an APP file contained in a DMG file where the file exists'
| def test_installed_app_dmg_exists(self):
| with patch('salt.states.mac_package._mod_run_check') as _mod_run_check_mock:
with patch('os.path.exists') as exists_mock:
expected = {'changes': {}, 'comment': '', 'name': '/path/to/file.dmg', 'result': True}
install_mock = MagicMock()
mount_mock = MagicMock(return_value=['success', '/tmp/dmg-X'])
unmount_mock = MagicMock()
cmd_mock = MagicMock(return_value='file.app')
_mod_run_check_mock.return_value = True
exists_mock.return_value = True
with patch.dict(macpackage.__salt__, {'macpackage.install_app': install_mock, 'macpackage.mount': mount_mock, 'macpackage.unmount': unmount_mock, 'cmd.run': cmd_mock}):
out = macpackage.installed('/path/to/file.dmg', app=True, dmg=True)
mount_mock.assert_called_once_with('/path/to/file.dmg')
unmount_mock.assert_called_once_with('/tmp/dmg-X')
cmd_mock.assert_called_once_with('ls -d *.app', python_shell=True, cwd='/tmp/dmg-X')
assert (not install_mock.called)
self.assertEqual(out, expected)
|
'Test installing a PKG file where the only if call passes'
| def test_installed_pkg_only_if_pass(self):
| expected = {'changes': {'installed': ['some.other.id']}, 'comment': '/path/to/file.pkg installed', 'name': '/path/to/file.pkg', 'result': True}
installed_mock = MagicMock(return_value=['com.apple.id'])
get_pkg_id_mock = MagicMock(return_value=['some.other.id'])
install_mock = MagicMock(return_value={'retcode': 0})
cmd_mock = MagicMock(return_value=0)
with patch.dict(macpackage.__salt__, {'macpackage.installed_pkgs': installed_mock, 'macpackage.get_pkg_id': get_pkg_id_mock, 'macpackage.install': install_mock, 'cmd.retcode': cmd_mock}):
out = macpackage.installed('/path/to/file.pkg')
installed_mock.assert_called_once_with()
get_pkg_id_mock.assert_called_once_with('/path/to/file.pkg')
install_mock.assert_called_once_with('/path/to/file.pkg', 'LocalSystem', False, False)
self.assertEqual(out, expected)
|
'Test installing a PKG file where the onlyif call fails'
| def test_installed_pkg_onlyif_fail(self):
| expected = {'changes': {}, 'comment': 'onlyif execution failed', 'skip_watch': True, 'result': True, 'name': '/path/to/file.pkg'}
mock = MagicMock(return_value=1)
with patch.dict(macpackage.__salt__, {'cmd.retcode': mock}):
out = macpackage.installed('/path/to/file.pkg', onlyif='some command')
self.assertEqual(out, expected)
|
'Test installing a PKG file where the unless run fails'
| def test_installed_pkg_unless_fail(self):
| expected = {'changes': {}, 'comment': 'unless execution succeeded', 'skip_watch': True, 'result': True, 'name': '/path/to/file.pkg'}
mock = MagicMock(return_value=0)
with patch.dict(macpackage.__salt__, {'cmd.retcode': mock}):
out = macpackage.installed('/path/to/file.pkg', unless='some command')
self.assertEqual(out, expected)
|
'Test disk.status when name not found'
| def test_status_missing(self):
| mock_fs = '/mnt/cheese'
mock_ret = {'name': mock_fs, 'result': False, 'comment': 'Named disk mount not present ', 'changes': {}, 'data': {}}
ret = disk.status(mock_fs)
self.assertEqual(ret, mock_ret)
|
'Test disk.status with incorrectly formatted arguments'
| def test_status_type_error(self):
| mock_fs = '/'
mock_ret = {'name': mock_fs, 'result': False, 'comment': '', 'changes': {}, 'data': {}}
mock_ret['comment'] = 'maximum must be an integer '
ret = disk.status(mock_fs, maximum='e^{i\\pi}')
self.assertEqual(ret, mock_ret)
mock_ret['comment'] = 'minimum must be an integer '
ret = disk.status(mock_fs, minimum='\\cos\\pi + i\\sin\\pi')
self.assertEqual(ret, mock_ret)
|
'Test disk.status with excessive extrema'
| def test_status_range_error(self):
| mock_fs = '/'
mock_ret = {'name': mock_fs, 'result': False, 'comment': '', 'changes': {}, 'data': {}}
mock_ret['comment'] = 'maximum must be in the range [0, 100] '
ret = disk.status(mock_fs, maximum='-1')
self.assertEqual(ret, mock_ret)
mock_ret['comment'] = 'minimum must be in the range [0, 100] '
ret = disk.status(mock_fs, minimum='101')
self.assertEqual(ret, mock_ret)
|
'Test disk.status when minimum > maximum'
| def test_status_inverted_range(self):
| mock_fs = '/'
mock_ret = {'name': mock_fs, 'result': False, 'comment': 'minimum must be less than maximum ', 'changes': {}, 'data': {}}
ret = disk.status(mock_fs, maximum='0', minimum='1')
self.assertEqual(ret, mock_ret)
|
'Test disk.status when filesystem triggers thresholds'
| def test_status_threshold(self):
| mock_min = 100
mock_max = 0
mock_fs = '/'
mock_used = int(self.mock_data[mock_fs]['capacity'].strip('%'))
mock_ret = {'name': mock_fs, 'result': False, 'comment': '', 'changes': {}, 'data': self.mock_data[mock_fs]}
mock_ret['comment'] = 'Disk used space is below minimum of {0} % at {1} %'.format(mock_min, mock_used)
ret = disk.status(mock_fs, minimum=mock_min)
self.assertEqual(ret, mock_ret)
mock_ret['comment'] = 'Disk used space is above maximum of {0} % at {1} %'.format(mock_max, mock_used)
ret = disk.status(mock_fs, maximum=mock_max)
self.assertEqual(ret, mock_ret)
|
'Test disk.status appropriately strips unit info from numbers'
| def test_status_strip(self):
| mock_fs = '/'
mock_ret = {'name': mock_fs, 'result': True, 'comment': 'Disk used space in acceptable range', 'changes': {}, 'data': self.mock_data[mock_fs]}
ret = disk.status(mock_fs, minimum='0%')
self.assertEqual(ret, mock_ret)
ret = disk.status(mock_fs, minimum='0 %')
self.assertEqual(ret, mock_ret)
ret = disk.status(mock_fs, maximum='100%')
self.assertEqual(ret, mock_ret)
ret = disk.status(mock_fs, minimum='1024K', absolute=True)
self.assertEqual(ret, mock_ret)
ret = disk.status(mock_fs, minimum='1024KB', absolute=True)
self.assertEqual(ret, mock_ret)
ret = disk.status(mock_fs, maximum='4194304 KB', absolute=True)
self.assertEqual(ret, mock_ret)
|
'Test disk.status when filesystem meets thresholds'
| def test_status(self):
| mock_min = 0
mock_max = 100
mock_fs = '/'
mock_ret = {'name': mock_fs, 'result': True, 'comment': 'Disk used space in acceptable range', 'changes': {}, 'data': self.mock_data[mock_fs]}
ret = disk.status(mock_fs, minimum=mock_min)
self.assertEqual(ret, mock_ret)
ret = disk.status(mock_fs, maximum=mock_max)
self.assertEqual(ret, mock_ret)
ret = {'name': mock_fs, 'result': False, 'comment': '', 'changes': {}, 'data': {}}
mock = MagicMock(side_effect=[[], [mock_fs], {mock_fs: {'capacity': '8 %', 'used': '8'}}, {mock_fs: {'capacity': '22 %', 'used': '22'}}, {mock_fs: {'capacity': '15 %', 'used': '15'}}])
with patch.dict(disk.__salt__, {'disk.usage': mock}):
comt = 'Named disk mount not present '
ret.update({'comment': comt})
self.assertDictEqual(disk.status(mock_fs), ret)
comt = 'minimum must be less than maximum '
ret.update({'comment': comt})
self.assertDictEqual(disk.status(mock_fs, '10', '20', absolute=True), ret)
comt = 'Disk used space is below minimum of 10 KB at 8 KB'
ret.update({'comment': comt, 'data': {'capacity': '8 %', 'used': '8'}})
self.assertDictEqual(disk.status(mock_fs, '20', '10', absolute=True), ret)
comt = 'Disk used space is above maximum of 20 KB at 22 KB'
ret.update({'comment': comt, 'data': {'capacity': '22 %', 'used': '22'}})
self.assertDictEqual(disk.status(mock_fs, '20', '10', absolute=True), ret)
comt = 'Disk used space in acceptable range'
ret.update({'comment': comt, 'result': True, 'data': {'capacity': '15 %', 'used': '15'}})
self.assertDictEqual(disk.status(mock_fs, '20', '10', absolute=True), ret)
|
'Test to manage options of block device'
| def test_tuned(self):
| name = '/dev/vg/master-data'
ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''}
comt = 'Changes to {0} cannot be applied. Not a block device. '.format(name)
with patch.dict(blockdev.__salt__, {'file.is_blkdev': False}):
ret.update({'comment': comt})
self.assertDictEqual(blockdev.tuned(name), ret)
comt = 'Changes to {0} will be applied '.format(name)
with patch.dict(blockdev.__salt__, {'file.is_blkdev': True}):
ret.update({'comment': comt, 'result': None})
with patch.dict(blockdev.__opts__, {'test': True}):
self.assertDictEqual(blockdev.tuned(name), ret)
|
'Test to manage filesystems of partitions.'
| def test_formatted(self):
| name = '/dev/vg/master-data'
ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''}
with patch.object(os.path, 'exists', MagicMock(side_effect=[False, True, True, True, True])):
comt = '{0} does not exist'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(blockdev.formatted(name), ret)
mock_ext4 = MagicMock(return_value='ext4')
with patch.dict(blockdev.__salt__, {'cmd.run': mock_ext4}):
comt = '{0} already formatted with ext4'.format(name)
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(blockdev.formatted(name), ret)
with patch.dict(blockdev.__salt__, {'cmd.run': MagicMock(return_value='')}):
ret.update({'comment': 'Invalid fs_type: foo-bar', 'result': False})
with patch.object(salt.utils.path, 'which', MagicMock(return_value=False)):
self.assertDictEqual(blockdev.formatted(name, fs_type='foo-bar'), ret)
with patch.dict(blockdev.__salt__, {'cmd.run': MagicMock(return_value='new-thing')}):
comt = 'Changes to {0} will be applied '.format(name)
ret.update({'comment': comt, 'result': None})
with patch.object(salt.utils.path, 'which', MagicMock(return_value=True)):
with patch.dict(blockdev.__opts__, {'test': True}):
self.assertDictEqual(blockdev.formatted(name), ret)
with patch.dict(blockdev.__salt__, {'cmd.run': MagicMock(return_value=mock_ext4), 'disk.format_': MagicMock(return_value=True)}):
comt = 'Failed to format {0}'.format(name)
ret.update({'comment': comt, 'result': False})
with patch.object(salt.utils.path, 'which', MagicMock(return_value=True)):
with patch.dict(blockdev.__opts__, {'test': False}):
self.assertDictEqual(blockdev.formatted(name), ret)
|
'Test to ensure that the named user is present with
the specified properties.'
| def test_present(self):
| name = 'frank'
password = 'bob@cat'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
mock = MagicMock(side_effect=[True, False, True, False, False, True, False, False, False, False, False, True])
mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False)
mock_str = MagicMock(return_value='salt')
mock_none = MagicMock(return_value=None)
mock_sn = MagicMock(side_effect=[None, 'salt', None, None, None])
with patch.object(salt.utils, 'is_true', mock_f):
comt = 'Either password or password_hash must be specified, unless allow_passwordless is True'
ret.update({'comment': comt})
self.assertDictEqual(mysql_user.present(name), ret)
with patch.dict(mysql_user.__salt__, {'mysql.user_exists': mock, 'mysql.user_chpass': mock_t}):
with patch.object(salt.utils, 'is_true', mock_t):
comt = 'User frank@localhost is already present with passwordless login'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mysql_user.present(name), ret)
with patch.object(mysql_user, '_get_mysql_error', mock_str):
ret.update({'comment': 'salt', 'result': False})
self.assertDictEqual(mysql_user.present(name), ret)
with patch.object(mysql_user, '_get_mysql_error', mock_str):
comt = 'User frank@localhost is already present with the desired password'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mysql_user.present(name, password=password), ret)
with patch.object(mysql_user, '_get_mysql_error', mock_str):
ret.update({'comment': 'salt', 'result': False})
self.assertDictEqual(mysql_user.present(name, password=password), ret)
with patch.object(mysql_user, '_get_mysql_error', mock_none):
with patch.dict(mysql_user.__opts__, {'test': True}):
comt = 'Password for user frank@localhost is set to be changed'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(mysql_user.present(name, password=password), ret)
with patch.object(mysql_user, '_get_mysql_error', mock_sn):
with patch.dict(mysql_user.__opts__, {'test': False}):
ret.update({'comment': 'salt', 'result': False})
self.assertDictEqual(mysql_user.present(name, password=password), ret)
with patch.dict(mysql_user.__opts__, {'test': True}):
comt = 'User frank@localhost is set to be added'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(mysql_user.present(name, password=password), ret)
with patch.dict(mysql_user.__opts__, {'test': False}):
comt = 'Password for user frank@localhost has been changed'
ret.update({'comment': comt, 'result': True, 'changes': {name: 'Updated'}})
self.assertDictEqual(mysql_user.present(name, password=password), ret)
|
'Test to ensure that the named user is absent.'
| def test_absent(self):
| name = 'frank_exampledb'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock = MagicMock(side_effect=[True, True, True, False, False, False])
mock_t = MagicMock(side_effect=[True, False])
mock_str = MagicMock(return_value='salt')
mock_none = MagicMock(return_value=None)
with patch.dict(mysql_user.__salt__, {'mysql.user_exists': mock, 'mysql.user_remove': mock_t}):
with patch.dict(mysql_user.__opts__, {'test': True}):
comt = 'User frank_exampledb@localhost is set to be removed'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(mysql_user.absent(name), ret)
with patch.dict(mysql_user.__opts__, {'test': False}):
comt = 'User frank_exampledb@localhost has been removed'
ret.update({'comment': comt, 'result': True, 'changes': {'frank_exampledb': 'Absent'}})
self.assertDictEqual(mysql_user.absent(name), ret)
with patch.object(mysql_user, '_get_mysql_error', mock_str):
comt = 'User frank_exampledb@localhost has been removed'
ret.update({'comment': 'salt', 'result': False, 'changes': {}})
self.assertDictEqual(mysql_user.absent(name), ret)
comt = 'User frank_exampledb@localhost has been removed'
ret.update({'comment': 'salt'})
self.assertDictEqual(mysql_user.absent(name), ret)
with patch.object(mysql_user, '_get_mysql_error', mock_none):
comt = 'User frank_exampledb@localhost is not present, so it cannot be removed'
ret.update({'comment': comt, 'result': True, 'changes': {}})
self.assertDictEqual(mysql_user.absent(name), ret)
|
'Test to verify the named container if it exist.'
| def test_present(self):
| name = 'web01'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
mock = MagicMock(side_effect=[False, True, True, True, True, True, True])
mock_t = MagicMock(side_effect=[None, True, 'frozen', 'frozen', 'stopped', 'running', 'running'])
with patch.dict(lxc.__salt__, {'lxc.exists': mock, 'lxc.state': mock_t}):
comt = "Clone source 'True' does not exist"
ret.update({'comment': comt})
self.assertDictEqual(lxc.present(name, clone_from=True), ret)
with patch.dict(lxc.__opts__, {'test': True}):
comt = "Container 'web01' will be cloned from True"
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(lxc.present(name, clone_from=True), ret)
comt = "Container 'web01' already exists"
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(lxc.present(name, clone_from=True), ret)
comt = "Container 'web01' would be unfrozen"
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(lxc.present(name, running=True, clone_from=True), ret)
comt = "Container '{0}' would be stopped".format(name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(lxc.present(name, running=False, clone_from=True), ret)
comt = "Container 'web01' already exists and is stopped"
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(lxc.present(name, running=False, clone_from=True), ret)
with patch.dict(lxc.__opts__, {'test': False}):
comt = "Container 'web01' already exists"
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(lxc.present(name, clone_from=True), ret)
|
'Test to ensure a container is not present, destroying it if present.'
| def test_absent(self):
| name = 'web01'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock = MagicMock(side_effect=[False, True, True])
mock_des = MagicMock(return_value={'state': True})
with patch.dict(lxc.__salt__, {'lxc.exists': mock, 'lxc.destroy': mock_des}):
comt = "Container '{0}' does not exist".format(name)
ret.update({'comment': comt})
self.assertDictEqual(lxc.absent(name), ret)
with patch.dict(lxc.__opts__, {'test': True}):
comt = "Container '{0}' would be destroyed".format(name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(lxc.absent(name), ret)
with patch.dict(lxc.__opts__, {'test': False}):
comt = "Container '{0}' was destroyed".format(name)
ret.update({'comment': comt, 'result': True, 'changes': {'state': True}})
self.assertDictEqual(lxc.absent(name), ret)
|
'Test to ensure that a container is running.'
| def test_running(self):
| name = 'web01'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
mock = MagicMock(return_value={'state': {'new': 'stop'}})
mock_t = MagicMock(side_effect=[None, 'running', 'stopped', 'start'])
with patch.dict(lxc.__salt__, {'lxc.exists': mock, 'lxc.state': mock_t, 'lxc.start': mock}):
comt = "Container '{0}' does not exist".format(name)
ret.update({'comment': comt})
self.assertDictEqual(lxc.running(name), ret)
comt = "Container 'web01' is already running"
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(lxc.running(name), ret)
with patch.dict(lxc.__opts__, {'test': True}):
comt = "Container 'web01' would be started"
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(lxc.running(name), ret)
with patch.dict(lxc.__opts__, {'test': False}):
comt = "Unable to start container 'web01'"
ret.update({'comment': comt, 'result': False, 'changes': {'state': {'new': 'stop', 'old': 'start'}}})
self.assertDictEqual(lxc.running(name), ret)
|
'Test to ensure that a container is frozen.'
| def test_frozen(self):
| name = 'web01'
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
mock = MagicMock(return_value={'state': {'new': 'stop'}})
mock_t = MagicMock(side_effect=['frozen', 'stopped', 'stopped'])
with patch.dict(lxc.__salt__, {'lxc.freeze': mock, 'lxc.state': mock_t}):
comt = "Container '{0}' is already frozen".format(name)
ret.update({'comment': comt})
self.assertDictEqual(lxc.frozen(name), ret)
with patch.dict(lxc.__opts__, {'test': True}):
comt = "Container 'web01' would be started and frozen"
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(lxc.frozen(name), ret)
with patch.dict(lxc.__opts__, {'test': False}):
comt = "Unable to start and freeze container 'web01'"
ret.update({'comment': comt, 'result': False, 'changes': {'state': {'new': 'stop', 'old': 'stopped'}}})
self.assertDictEqual(lxc.frozen(name), ret)
|
'Test to ensure that a container is stopped.'
| def test_stopped(self):
| name = 'web01'
ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
mock = MagicMock(return_value={'state': {'new': 'stop'}})
mock_t = MagicMock(side_effect=[None, 'stopped', 'frozen', 'frozen'])
with patch.dict(lxc.__salt__, {'lxc.stop': mock, 'lxc.state': mock_t}):
comt = "Container '{0}' does not exist".format(name)
ret.update({'comment': comt})
self.assertDictEqual(lxc.stopped(name), ret)
comt = "Container '{0}' is already stopped".format(name)
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(lxc.stopped(name), ret)
with patch.dict(lxc.__opts__, {'test': True}):
comt = "Container 'web01' would be stopped"
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(lxc.stopped(name), ret)
with patch.dict(lxc.__opts__, {'test': False}):
comt = "Unable to stop container 'web01'"
ret.update({'comment': comt, 'result': False, 'changes': {'state': {'new': 'stop', 'old': 'frozen'}}})
self.assertDictEqual(lxc.stopped(name), ret)
|
'Test to execute set_pass func.'
| def test_set_pass(self):
| comment = 'The lxc.set_pass state is no longer supported. Please see the LXC states documentation for further information.'
ret = {'name': 'web01', 'comment': comment, 'result': False, 'changes': {}}
self.assertDictEqual(lxc.set_pass('web01'), ret)
|
'Test to edit LXC configuration options'
| def test_edited_conf(self):
| name = 'web01'
comment = '{0} lxc.conf will be edited'.format(name)
ret = {'name': name, 'result': True, 'comment': comment, 'changes': {}}
with patch.object(salt.utils, 'warn_until', MagicMock()):
with patch.dict(lxc.__opts__, {'test': True}):
self.assertDictEqual(lxc.edited_conf(name), ret)
with patch.dict(lxc.__opts__, {'test': False}):
mock = MagicMock(return_value={})
with patch.dict(lxc.__salt__, {'lxc.update_lxc_conf': mock}):
self.assertDictEqual(lxc.edited_conf(name), {'name': 'web01'})
|
'Test to block state execution until you are able to get the lock'
| def test_lock(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
with patch.dict(zk_concurrency.__opts__, {'test': True}):
ret.update({'comment': 'Attempt to acquire lock', 'result': None})
self.assertDictEqual(zk_concurrency.lock('salt', 'dude'), ret)
with patch.dict(zk_concurrency.__opts__, {'test': False}):
mock = MagicMock(return_value=True)
with patch.dict(zk_concurrency.__salt__, {'zk_concurrency.lock': mock}):
ret.update({'comment': 'lock acquired', 'result': True})
self.assertDictEqual(zk_concurrency.lock('salt', 'dude', 'stack'), ret)
|
'Test to remove lease from semaphore'
| def test_unlock(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
with patch.dict(zk_concurrency.__opts__, {'test': True}):
ret.update({'comment': 'Released lock if it is here', 'result': None})
self.assertDictEqual(zk_concurrency.unlock('salt'), ret)
with patch.dict(zk_concurrency.__opts__, {'test': False}):
mock = MagicMock(return_value=True)
with patch.dict(zk_concurrency.__salt__, {'zk_concurrency.unlock': mock}):
ret.update({'comment': '', 'result': True})
self.assertDictEqual(zk_concurrency.unlock('salt', identifier='stack'), ret)
|
'Test to ensure min party of nodes and the blocking behavior'
| def test_min_party(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
with patch.dict(zk_concurrency.__opts__, {'test': True}):
ret.update({'comment': 'Attempt to ensure min_party', 'result': None})
self.assertDictEqual(zk_concurrency.min_party('salt', 'dude', 1), ret)
with patch.dict(zk_concurrency.__opts__, {'test': False}):
mock = MagicMock(return_value=['1', '2', '3'])
with patch.dict(zk_concurrency.__salt__, {'zk_concurrency.party_members': mock}):
ret.update({'comment': 'Currently 3 nodes, which is >= 2', 'result': True})
self.assertDictEqual(zk_concurrency.min_party('salt', 'dude', 2), ret)
ret.update({'comment': ('Blocked until 2 nodes were available. ' + 'Unblocked after 3 nodes became available'), 'result': True})
self.assertDictEqual(zk_concurrency.min_party('salt', 'dude', 2, True), ret)
ret.update({'comment': 'Currently 3 nodes, which is < 4', 'result': False})
self.assertDictEqual(zk_concurrency.min_party('salt', 'dude', 4), ret)
|
'test that a subsequent calls of setenv changes nothing'
| def test_setenv(self):
| ret = envstate.setenv('test', 'value')
self.assertEqual(ret['changes'], {'test': 'value'})
ret = envstate.setenv('test', 'other')
self.assertEqual(ret['changes'], {'test': 'other'})
ret = envstate.setenv('test', 'other')
self.assertEqual(ret['changes'], {})
|
'test that setenv can be invoked with dict'
| def test_setenv_dict(self):
| ret = envstate.setenv('notimportant', {'test': 'value'})
self.assertEqual(ret['changes'], {'test': 'value'})
|
'test that setenv can not be invoked with int
(actually it\'s anything other than strings and dict)'
| def test_setenv_int(self):
| ret = envstate.setenv('test', 1)
self.assertEqual(ret['result'], False)
|
'test that ``false_unsets`` option removes variable from environment'
| def test_setenv_unset(self):
| ret = envstate.setenv('test', 'value')
self.assertEqual(ret['changes'], {'test': 'value'})
ret = envstate.setenv('notimportant', {'test': False}, false_unsets=True)
self.assertEqual(ret['changes'], {'test': None})
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial'})
|
'test that ``clear_all`` option sets other values to \'\''
| def test_setenv_clearall(self):
| ret = envstate.setenv('test', 'value', clear_all=True)
self.assertEqual(ret['changes'], {'test': 'value', 'INITIAL': ''})
self.assertEqual(envstate.os.environ, {'test': 'value', 'INITIAL': ''})
|
'test that ``clear_all`` option combined with ``false_unsets``
unsets other values from environment'
| def test_setenv_clearall_with_unset(self):
| ret = envstate.setenv('test', 'value', false_unsets=True, clear_all=True)
self.assertEqual(ret['changes'], {'test': 'value', 'INITIAL': None})
self.assertEqual(envstate.os.environ, {'test': 'value'})
|
'test basically same things that above tests but with multiple values passed'
| def test_setenv_unset_multi(self):
| ret = envstate.setenv('notimportant', {'foo': 'bar'})
self.assertEqual(ret['changes'], {'foo': 'bar'})
with patch.dict(envstate.__salt__, {'reg.read_value': MagicMock()}):
ret = envstate.setenv('notimportant', {'test': False, 'foo': 'baz'}, false_unsets=True)
self.assertEqual(ret['changes'], {'test': None, 'foo': 'baz'})
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'foo': 'baz'})
with patch.dict(envstate.__salt__, {'reg.read_value': MagicMock()}):
ret = envstate.setenv('notimportant', {'test': False, 'foo': 'bax'})
self.assertEqual(ret['changes'], {'test': '', 'foo': 'bax'})
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'foo': 'bax', 'test': ''})
|
'test that imitating action returns good values'
| def test_setenv_test_mode(self):
| with patch.dict(envstate.__opts__, {'test': True}):
ret = envstate.setenv('test', 'value')
self.assertEqual(ret['changes'], {'test': 'value'})
ret = envstate.setenv('INITIAL', 'initial')
self.assertEqual(ret['changes'], {})
|
'Test to ensure that a group is present'
| def test_present(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': {}}
ret.update({'comment': 'Error: Conflicting options "members" with "addusers" and/or "delusers" can not be used together. ', 'result': None})
self.assertDictEqual(group.present('salt', delusers=True, members=True), ret)
ret.update({'comment': 'Error. Same user(s) can not be added and deleted simultaneously'})
self.assertDictEqual(group.present('salt', addusers=['a'], delusers=['a']), ret)
ret.update({'comment': 'The following group attributes are set to be changed:\nkey0: value0\nkey1: value1\n'})
mock = MagicMock(side_effect=[OrderedDict((('key0', 'value0'), ('key1', 'value1'))), False, False, False])
with patch.object(group, '_changes', mock):
with patch.dict(group.__opts__, {'test': True}):
self.assertDictEqual(group.present('salt'), ret)
ret.update({'comment': 'Group salt set to be added'})
self.assertDictEqual(group.present('salt'), ret)
with patch.dict(group.__opts__, {'test': False}):
mock = MagicMock(return_value=[{'gid': 1, 'name': 'stack'}])
with patch.dict(group.__salt__, {'group.getent': mock}):
ret.update({'result': False, 'comment': 'Group salt is not present but gid 1 is already taken by group stack'})
self.assertDictEqual(group.present('salt', 1), ret)
mock = MagicMock(return_value=False)
with patch.dict(group.__salt__, {'group.add': mock}):
ret.update({'comment': 'Failed to create new group salt'})
self.assertDictEqual(group.present('salt'), ret)
|
'Test to ensure that the named group is absent'
| def test_absent(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': {}}
mock = MagicMock(side_effect=[True, True, True, False])
with patch.dict(group.__salt__, {'group.info': mock}):
with patch.dict(group.__opts__, {'test': True}):
ret.update({'result': None, 'comment': 'Group salt is set for removal'})
self.assertDictEqual(group.absent('salt'), ret)
with patch.dict(group.__opts__, {'test': False}):
mock = MagicMock(side_effect=[True, False])
with patch.dict(group.__salt__, {'group.delete': mock}):
ret.update({'result': True, 'changes': {'salt': ''}, 'comment': 'Removed group salt'})
self.assertDictEqual(group.absent('salt'), ret)
ret.update({'changes': {}, 'result': False, 'comment': 'Failed to remove group salt'})
self.assertDictEqual(group.absent('salt'), ret)
ret.update({'result': True, 'comment': 'Group not present'})
self.assertDictEqual(group.absent('salt'), ret)
|
'Test to ensure the grafana dashboard exists and is managed.'
| def test_dashboard_present(self):
| name = 'myservice'
rows = ['systemhealth', 'requests', 'title']
row = [{'panels': [{'id': 'a'}], 'title': 'systemhealth'}]
ret = {'name': name, 'result': None, 'changes': {}, 'comment': ''}
comt1 = "Dashboard myservice is set to be updated. The following rows set to be updated: ['systemhealth']"
self.assertRaises(SaltInvocationError, grafana.dashboard_present, name, profile=False)
self.assertRaises(SaltInvocationError, grafana.dashboard_present, name, True, True)
mock = MagicMock(side_effect=[{'hosts': True, 'index': False}, {'hosts': True, 'index': True}, {'hosts': True, 'index': True}, {'hosts': True, 'index': True}, {'hosts': True, 'index': True}, {'hosts': True, 'index': True}, {'hosts': True, 'index': True}])
mock_f = MagicMock(side_effect=[False, False, True, True, True, True])
mock_t = MagicMock(return_value='')
mock_i = MagicMock(return_value=False)
source = {'dashboard': '["rows", {"rows":["baz", null, 1.0, 2]}]'}
mock_dict = MagicMock(return_value={'_source': source})
with patch.dict(grafana.__salt__, {'config.option': mock, 'elasticsearch.exists': mock_f, 'pillar.get': mock_t, 'elasticsearch.get': mock_dict, 'elasticsearch.index': mock_i}):
self.assertRaises(SaltInvocationError, grafana.dashboard_present, name)
with patch.dict(grafana.__opts__, {'test': True}):
self.assertRaises(SaltInvocationError, grafana.dashboard_present, name)
comt = 'Dashboard {0} is set to be created.'.format(name)
ret.update({'comment': comt})
self.assertDictEqual(grafana.dashboard_present(name, True), ret)
mock = MagicMock(return_value={'rows': [{'panels': 'b', 'title': 'systemhealth'}]})
with patch.object(json, 'loads', mock):
ret.update({'comment': comt1, 'result': None})
self.assertDictEqual(grafana.dashboard_present(name, True, rows=row), ret)
with patch.object(json, 'loads', MagicMock(return_value={'rows': {}})):
self.assertRaises(SaltInvocationError, grafana.dashboard_present, name, rows_from_pillar=rows)
comt = 'Dashboard myservice is up to date'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(grafana.dashboard_present(name, True), ret)
mock = MagicMock(return_value={'rows': [{'panels': 'b', 'title': 'systemhealth'}]})
with patch.dict(grafana.__opts__, {'test': False}):
with patch.object(json, 'loads', mock):
comt = 'Failed to update dashboard myservice.'
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(grafana.dashboard_present(name, True, rows=row), ret)
|
'Test to ensure the named grafana dashboard is deleted.'
| def test_dashboard_absent(self):
| name = 'myservice'
ret = {'name': name, 'result': None, 'changes': {}, 'comment': ''}
mock = MagicMock(side_effect=[{'hosts': True, 'index': False}, {'hosts': True, 'index': True}, {'hosts': True, 'index': True}])
mock_f = MagicMock(side_effect=[True, False])
with patch.dict(grafana.__salt__, {'config.option': mock, 'elasticsearch.exists': mock_f}):
self.assertRaises(SaltInvocationError, grafana.dashboard_absent, name)
with patch.dict(grafana.__opts__, {'test': True}):
comt = 'Dashboard myservice is set to be removed.'
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(grafana.dashboard_absent(name), ret)
comt = 'Dashboard myservice does not exist.'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(grafana.dashboard_absent(name), ret)
|
'Test to make sure we can set the monitor timeout value'
| def test_set_monitor(self):
| ret = {'changes': {'monitor': {'ac': 0}}, 'comment': '', 'name': 'monitor', 'result': True}
monitor_val = {'ac': 45, 'dc': 22}
with patch.dict(powercfg.__salt__, {'powercfg.get_monitor_timeout': MagicMock(return_value=monitor_val), 'powercfg.set_monitor_timeout': MagicMock(return_value=True)}):
self.assertEqual(powercfg.set_timeout('monitor', 0), ret)
|
'Test to make sure we can set the monitor timeout value'
| def test_set_monitor_already_set(self):
| ret = {'changes': {}, 'comment': 'monitor ac is already set with the value 0.', 'name': 'monitor', 'result': True}
monitor_val = {'ac': 0, 'dc': 0}
with patch.dict(powercfg.__salt__, {'powercfg.get_monitor_timeout': MagicMock(return_value=monitor_val), 'powercfg.set_monitor_timeout': MagicMock(return_value=True)}):
self.assertEqual(powercfg.set_timeout('monitor', 0), ret)
|
'Test to make sure we can set the monitor timeout value'
| def test_fail_invalid_setting(self):
| ret = {'changes': {}, 'comment': 'fakesetting is not a valid setting', 'name': 'fakesetting', 'result': False}
self.assertEqual(powercfg.set_timeout('fakesetting', 0), ret)
|
'Test to make sure we can set the monitor timeout value'
| def test_fail_invalid_power(self):
| ret = {'changes': {}, 'comment': 'fakepower is not a power type', 'name': 'monitor', 'result': False}
self.assertEqual(powercfg.set_timeout('monitor', 0, power='fakepower'), ret)
|
'Tests present on a domain that does not exist.'
| def test_present_when_domain_does_not_exist(self):
| self.conn.describe_elasticsearch_domain.side_effect = not_found_error
self.conn.describe_elasticsearch_domain_config.return_value = {'DomainConfig': domain_ret}
self.conn.create_elasticsearch_domain.return_value = {'DomainStatus': domain_ret}
result = self.salt_states['boto_elasticsearch_domain.present']('domain present', **domain_ret)
self.assertTrue(result['result'])
self.assertEqual(result['changes']['new']['domain']['ElasticsearchClusterConfig'], None)
|
'Tests absent on a domain that does not exist.'
| def test_absent_when_domain_does_not_exist(self):
| self.conn.describe_elasticsearch_domain.side_effect = not_found_error
result = self.salt_states['boto_elasticsearch_domain.absent']('test', 'mydomain')
self.assertTrue(result['result'])
self.assertEqual(result['changes'], {})
|
'Test to manage the computer\'s description field'
| def test_computer_desc(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
mock = MagicMock(side_effect=['salt', 'stack', 'stack'])
with patch.dict(win_system.__salt__, {'system.get_computer_desc': mock}):
ret.update({'comment': "Computer description already set to 'salt'"})
self.assertDictEqual(win_system.computer_desc('salt'), ret)
with patch.dict(win_system.__opts__, {'test': True}):
ret.update({'result': None, 'comment': "Computer description will be changed to 'salt'"})
self.assertDictEqual(win_system.computer_desc('salt'), ret)
with patch.dict(win_system.__opts__, {'test': False}):
mock = MagicMock(return_value={'Computer Description': 'nfs'})
with patch.dict(win_system.__salt__, {'system.set_computer_desc': mock}):
ret.update({'result': False, 'comment': "Unable to set computer description to 'salt'"})
self.assertDictEqual(win_system.computer_desc('salt'), ret)
|
'Test to manage the computer\'s name'
| def test_computer_name(self):
| ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''}
mock = MagicMock(return_value='salt')
with patch.dict(win_system.__salt__, {'system.get_computer_name': mock}):
mock = MagicMock(side_effect=[None, 'SALT', 'Stack', 'stack'])
with patch.dict(win_system.__salt__, {'system.get_pending_computer_name': mock}):
ret.update({'comment': "Computer name already set to 'salt'"})
self.assertDictEqual(win_system.computer_name('salt'), ret)
ret.update({'comment': "The current computer name is 'salt', but will be changed to 'salt' on the next reboot"})
self.assertDictEqual(win_system.computer_name('salt'), ret)
with patch.dict(win_system.__opts__, {'test': True}):
ret.update({'result': None, 'comment': "Computer name will be changed to 'salt'"})
self.assertDictEqual(win_system.computer_name('salt'), ret)
with patch.dict(win_system.__opts__, {'test': False}):
mock = MagicMock(return_value=False)
with patch.dict(win_system.__salt__, {'system.set_computer_name': mock}):
ret.update({'comment': "Unable to set computer name to 'salt'", 'result': False})
self.assertDictEqual(win_system.computer_name('salt'), ret)
|
'Tests present when the swagger file is invalid.'
| def test_present_when_swagger_file_is_invalid(self):
| result = {}
with TempSwaggerFile(create_invalid_file=True) as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertFalse(result.get('result', True))
|
'Tests scenario where no action will be taken since we\'re already
at desired state'
| def test_present_when_stage_is_already_at_desired_deployment(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_deployment.return_value = deployment1_ret
self.conn.get_stage.return_value = stage1_deployment1_ret
self.conn.update_stage.side_effect = ClientError(error_content, 'update_stage should not be called')
result = {}
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertFalse(result.get('abort'))
self.assertTrue(result.get('current'))
self.assertIs(result.get('result'), True)
self.assertNotIn('update_stage should not be called', result.get('comment', ''))
|
'Tests scenario where the deployment is current except for the need to update stage variables
from {} to {\'var1\':\'val1\'}'
| def test_present_when_stage_is_already_at_desired_deployment_and_needs_stage_variables_update(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_deployment.return_value = deployment1_ret
self.conn.get_stage.return_value = stage1_deployment1_ret
self.conn.update_stage.return_value = stage1_deployment1_vars_ret
result = {}
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', stage_variables={'var1': 'val1'}, **conn_parameters)
self.assertFalse(result.get('abort'))
self.assertTrue(result.get('current'))
self.assertIs(result.get('result'), True)
|
'Tests scenario where we merely reassociate a stage to a pre-existing
deployments'
| def test_present_when_stage_exists_and_is_to_associate_to_existing_deployment(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_deployment.return_value = deployment2_ret
self.conn.get_deployments.return_value = deployments_ret
self.conn.get_stage.return_value = stage1_deployment2_ret
self.conn.update_stage.return_value = stage1_deployment1_ret
self.conn.create_stage.side_effect = ClientError(error_content, 'create_stage')
self.conn.create_deployment.side_effect = ClientError(error_content, 'create_deployment')
result = {}
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertTrue(result.get('publish'))
self.assertIs(result.get('result'), True)
self.assertFalse(result.get('abort'))
self.assertTrue(result.get('changes', {}).get('new', [{}])[0])
|
'Tests creation of a new api/model/resource given nothing has been created previously'
| def test_present_when_stage_is_to_associate_to_new_deployment(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.return_value = api_ret
self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
self.conn.create_model.return_value = mock_model_ret
self.conn.get_resources.return_value = resources_ret
self.conn.create_resource.side_effect = ClientError(error_content, 'create_resource')
self.conn.put_method.return_value = method_ret
self.conn.put_integration.return_value = method_integration_ret
self.conn.put_method_response.return_value = method_response_200_ret
self.conn.put_intgration_response.return_value = method_integration_response_200_ret
result = {}
with patch.dict(self.funcs, {'boto_lambda.describe_function': MagicMock(return_value={'function': function_ret})}):
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('result'), True)
self.assertIs(result.get('abort'), None)
|
'Tests creation of a new api/model/resource given nothing has been created previously,
and we failed on creating the top level api object.'
| def test_present_when_stage_associating_to_new_deployment_errored_on_api_creation(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.side_effect = ClientError(error_content, 'create_rest_api')
result = {}
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('abort'), True)
self.assertIs(result.get('result'), False)
self.assertIn('create_rest_api', result.get('comment', ''))
|
'Tests creation of a new api/model/resource given nothing has been created previously,
and we failed on creating the models after successful creation of top level api object.'
| def test_present_when_stage_associating_to_new_deployment_errored_on_model_creation(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.return_value = api_ret
self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
self.conn.create_model.side_effect = ClientError(error_content, 'create_model')
result = {}
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('abort'), True)
self.assertIs(result.get('result'), False)
self.assertIn('create_model', result.get('comment', ''))
|
'Tests creation of a new api/model/resource given nothing has been created previously,
and we failed on creating the resource (paths) after successful creation of top level api/model
objects.'
| def test_present_when_stage_associating_to_new_deployment_errored_on_resource_creation(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.return_value = api_ret
self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
self.conn.create_model.return_value = mock_model_ret
self.conn.get_resources.return_value = root_resources_ret
self.conn.create_resource.side_effect = ClientError(error_content, 'create_resource')
result = {}
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('abort'), True)
self.assertIs(result.get('result'), False)
self.assertIn('create_resource', result.get('comment', ''))
|
'Tests creation of a new api/model/resource given nothing has been created previously,
and we failed on adding a post method to the resource after successful creation of top level
api, model, resource objects.'
| def test_present_when_stage_associating_to_new_deployment_errored_on_put_method(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.return_value = api_ret
self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
self.conn.create_model.return_value = mock_model_ret
self.conn.get_resources.return_value = resources_ret
self.conn.create_resource.side_effect = ClientError(error_content, 'create_resource')
self.conn.put_method.side_effect = ClientError(error_content, 'put_method')
result = {}
with patch.dict(self.funcs, {'boto_lambda.describe_function': MagicMock(return_value={'function': function_ret})}):
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('abort'), True)
self.assertIs(result.get('result'), False)
self.assertIn('put_method', result.get('comment', ''))
|
'Tests creation of a new api/model/resource given nothing has been created previously,
and we failed on adding a post method due to a lamda look up failure after successful
creation of top level api, model, resource objects.'
| def test_present_when_stage_associating_to_new_deployment_errored_on_lambda_function_lookup(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.return_value = api_ret
self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
self.conn.create_model.return_value = mock_model_ret
self.conn.get_resources.return_value = resources_ret
self.conn.create_resource.side_effect = ClientError(error_content, 'create_resource')
self.conn.put_method.return_value = method_ret
self.conn.put_integration.side_effect = ClientError(error_content, 'put_integration should not be invoked')
result = {}
with patch.dict(self.funcs, {'boto_lambda.describe_function': MagicMock(return_value={'error': 'no such lambda'})}):
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('result'), False)
self.assertNotIn('put_integration should not be invoked', result.get('comment', ''))
self.assertIn('not find lambda function', result.get('comment', ''))
|
'Tests creation of a new api/model/resource given nothing has been created previously,
and we failed on adding an integration for the post method to the resource after
successful creation of top level api, model, resource objects.'
| def test_present_when_stage_associating_to_new_deployment_errored_on_put_integration(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.return_value = api_ret
self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
self.conn.create_model.return_value = mock_model_ret
self.conn.get_resources.return_value = resources_ret
self.conn.create_resource.side_effect = ClientError(error_content, 'create_resource')
self.conn.put_method.return_value = method_ret
self.conn.put_integration.side_effect = ClientError(error_content, 'put_integration')
result = {}
with patch.dict(self.funcs, {'boto_lambda.describe_function': MagicMock(return_value={'function': function_ret})}):
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('abort'), True)
self.assertIs(result.get('result'), False)
self.assertIn('put_integration', result.get('comment', ''))
|
'Tests creation of a new api/model/resource given nothing has been created previously,
and we failed on adding a method response for the post method to the resource after
successful creation of top level api, model, resource objects.'
| def test_present_when_stage_associating_to_new_deployment_errored_on_put_method_response(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.return_value = api_ret
self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
self.conn.create_model.return_value = mock_model_ret
self.conn.get_resources.return_value = resources_ret
self.conn.create_resource.side_effect = ClientError(error_content, 'create_resource')
self.conn.put_method.return_value = method_ret
self.conn.put_integration.return_value = method_integration_ret
self.conn.put_method_response.side_effect = ClientError(error_content, 'put_method_response')
result = {}
with patch.dict(self.funcs, {'boto_lambda.describe_function': MagicMock(return_value={'function': function_ret})}):
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('abort'), True)
self.assertIs(result.get('result'), False)
self.assertIn('put_method_response', result.get('comment', ''))
|
'Tests creation of a new api/model/resource given nothing has been created previously,
and we failed on adding an integration response for the post method to the resource after
successful creation of top level api, model, resource objects.'
| def test_present_when_stage_associating_to_new_deployment_errored_on_put_integration_response(self):
| self.conn.get_rest_apis.return_value = no_apis_ret
self.conn.create_rest_api.return_value = api_ret
self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
self.conn.create_model.return_value = mock_model_ret
self.conn.get_resources.return_value = resources_ret
self.conn.create_resource.side_effect = ClientError(error_content, 'create_resource')
self.conn.put_method.return_value = method_ret
self.conn.put_integration.return_value = method_integration_ret
self.conn.put_method_response.return_value = method_response_200_ret
self.conn.put_integration_response.side_effect = ClientError(error_content, 'put_integration_response')
result = {}
with patch.dict(self.funcs, {'boto_lambda.describe_function': MagicMock(return_value={'function': function_ret})}):
with TempSwaggerFile() as swagger_file:
result = self.salt_states['boto_apigateway.present']('api present', 'unit test api', swagger_file, 'test', False, 'arn:aws:iam::1234:role/apigatewayrole', **conn_parameters)
self.assertIs(result.get('abort'), True)
self.assertIs(result.get('result'), False)
self.assertIn('put_integration_response', result.get('comment', ''))
|
'Tests scenario where the given api_name does not exist, absent state should return True
with no changes.'
| def test_absent_when_rest_api_does_not_exist(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_stage.side_effect = ClientError(error_content, 'get_stage should not be called')
result = self.salt_states['boto_apigateway.absent']('api present', 'no_such_rest_api', 'no_such_stage', nuke_api=False, **conn_parameters)
self.assertIs(result.get('result'), True)
self.assertNotIn('get_stage should not be called', result.get('comment', ''))
self.assertEqual(result.get('changes'), {})
|
'Tests scenario where the stagename doesn\'t exist'
| def test_absent_when_stage_is_invalid(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_stage.return_value = stage1_deployment1_ret
self.conn.delete_stage.side_effect = ClientError(error_content, 'delete_stage')
result = self.salt_states['boto_apigateway.absent']('api present', 'unit test api', 'no_such_stage', nuke_api=False, **conn_parameters)
self.assertTrue(result.get('abort', False))
|
'Tests scenario where the stagename exists'
| def test_absent_when_stage_is_valid_and_only_one_stage_is_associated_to_deployment(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_stage.return_value = stage1_deployment1_ret
self.conn.delete_stage.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
self.conn.get_stages.return_value = no_stages_ret
self.conn.delete_deployment.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
result = self.salt_states['boto_apigateway.absent']('api present', 'unit test api', 'test', nuke_api=False, **conn_parameters)
self.assertTrue(result.get('result', False))
|
'Tests scenario where the stagename exists and there are two stages associated with same deployment'
| def test_absent_when_stage_is_valid_and_two_stages_are_associated_to_deployment(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_stage.return_value = stage1_deployment1_ret
self.conn.delete_stage.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
self.conn.get_stages.return_value = stages_stage2_ret
result = self.salt_states['boto_apigateway.absent']('api present', 'unit test api', 'test', nuke_api=False, **conn_parameters)
self.assertTrue(result.get('result', False))
|
'Tests scenario where stagename exists and is deleted, but a failure occurs when trying to delete
the deployment which is no longer associated to any other stages'
| def test_absent_when_failing_to_delete_a_deployment_no_longer_associated_with_any_stages(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_stage.return_value = stage1_deployment1_ret
self.conn.delete_stage.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
self.conn.get_stages.return_value = no_stages_ret
self.conn.delete_deployment.side_effect = ClientError(error_content, 'delete_deployment')
result = self.salt_states['boto_apigateway.absent']('api present', 'unit test api', 'test', nuke_api=False, **conn_parameters)
self.assertTrue(result.get('abort', False))
|
'Tests scenario where the stagename exists and there are no stages associated with same deployment,
the api would be deleted.'
| def test_absent_when_nuke_api_and_no_more_stages_deployments_remain(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_stage.return_value = stage1_deployment1_ret
self.conn.delete_stage.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
self.conn.get_stages.return_value = no_stages_ret
self.conn.get_deployments.return_value = deployments_ret
self.conn.delete_rest_api.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
result = self.salt_states['boto_apigateway.absent']('api present', 'unit test api', 'test', nuke_api=True, **conn_parameters)
self.assertIs(result.get('result'), True)
self.assertIsNot(result.get('abort'), True)
self.assertIs(result.get('changes', {}).get('new', [{}])[0].get('delete_api', {}).get('deleted'), True)
|
'Tests scenario where the stagename exists and there are two stages associated with same deployment,
though nuke_api is requested, due to remaining deployments, we will not call the delete_rest_api call.'
| def test_absent_when_nuke_api_and_other_stages_deployments_exist(self):
| self.conn.get_rest_apis.return_value = apis_ret
self.conn.get_stage.return_value = stage1_deployment1_ret
self.conn.delete_stage.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
self.conn.get_stages.return_value = stages_stage2_ret
self.conn.get_deployments.return_value = deployments_ret
self.conn.delete_rest_api.side_effect = ClientError(error_content, 'unexpected_api_delete')
result = self.salt_states['boto_apigateway.absent']('api present', 'unit test api', 'test', nuke_api=True, **conn_parameters)
self.assertIs(result.get('result'), True)
self.assertIsNot(result.get('abort'), True)
|
'Tests correct error processing for describe_usage_plan failure'
| def test_usage_plan_present_if_describe_fails(self, *args):
| with patch.dict(boto_apigateway.__salt__, {'boto_apigateway.describe_usage_plans': MagicMock(return_value={'error': 'error'})}):
result = boto_apigateway.usage_plan_present('name', 'plan_name', **conn_parameters)
self.assertIn('result', result)
self.assertEqual(result['result'], False)
self.assertIn('comment', result)
self.assertEqual(result['comment'], 'Failed to describe existing usage plans')
self.assertIn('changes', result)
self.assertEqual(result['changes'], {})
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.