desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Tests existence of route table failure'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_route_table_does_not_exist_the_route_table_exists_method_returns_false(self):
route_table_existence_result = boto_vpc.route_table_exists('fake', **conn_parameters) self.assertFalse(route_table_existence_result)
'Tests checking route table without filters'
@mock_ec2 @skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493') def test_that_when_checking_if_a_route_table_exists_but_providing_no_filters_the_route_table_exists_method_raises_a_salt_invocation_error(self):
with self.assertRaisesRegex(SaltInvocationError, 'At least one of the following must be provided: id, name, or tags.'): boto_vpc.dhcp_options_exists(**conn_parameters)
'Tests associating route table successfully'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_associating_a_route_table_succeeds_the_associate_route_table_method_should_return_the_association_id(self):
vpc = self._create_vpc() subnet = self._create_subnet(vpc.id) route_table = self._create_route_table(vpc.id) association_id = boto_vpc.associate_route_table(route_table.id, subnet.id, **conn_parameters) self.assertTrue(association_id)
'Tests associating of route table to non-existent route table'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_associating_a_route_table_with_a_non_existent_route_table_the_associate_route_table_method_should_return_false(self):
vpc = self._create_vpc() subnet = self._create_subnet(vpc.id) association_id = boto_vpc.associate_route_table('fake', subnet.id, **conn_parameters) self.assertFalse(association_id)
'Tests associating of route table with non-existent subnet'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_associating_a_route_table_with_a_non_existent_subnet_the_associate_route_table_method_should_return_false(self):
vpc = self._create_vpc() route_table = self._create_route_table(vpc.id) association_id = boto_vpc.associate_route_table(route_table.id, 'fake', **conn_parameters) self.assertFalse(association_id)
'Tests disassociation of a route'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_disassociating_a_route_table_succeeds_the_disassociate_route_table_method_should_return_true(self):
vpc = self._create_vpc() subnet = self._create_subnet(vpc.id) route_table = self._create_route_table(vpc.id) association_id = self._associate_route_table(route_table.id, subnet.id) dhcp_disassociate_result = boto_vpc.disassociate_route_table(association_id, **conn_parameters) self.assertTrue(dhcp_disassociate_result)
'Tests successful creation of a route'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_creating_a_route_succeeds_the_create_route_method_should_return_true(self):
vpc = self._create_vpc() route_table = self._create_route_table(vpc.id) route_creation_result = boto_vpc.create_route(route_table.id, cidr_block, **conn_parameters) self.assertTrue(route_creation_result)
'Tests creation of route on non-existent route table'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_creating_a_route_with_a_non_existent_route_table_the_create_route_method_should_return_false(self):
route_creation_result = boto_vpc.create_route('fake', cidr_block, **conn_parameters) self.assertFalse(route_creation_result)
'Tests deleting route from route table'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_deleting_a_route_succeeds_the_delete_route_method_should_return_true(self):
vpc = self._create_vpc() route_table = self._create_route_table(vpc.id) route_deletion_result = boto_vpc.delete_route(route_table.id, cidr_block, **conn_parameters) self.assertTrue(route_deletion_result)
'Tests deleting route from a non-existent route table'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_deleting_a_route_with_a_non_existent_route_table_the_delete_route_method_should_return_false(self):
route_deletion_result = boto_vpc.delete_route('fake', cidr_block, **conn_parameters) self.assertFalse(route_deletion_result)
'Tests replacing route successfully'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_replacing_a_route_succeeds_the_replace_route_method_should_return_true(self):
vpc = self._create_vpc() route_table = self._create_route_table(vpc.id) route_replacing_result = boto_vpc.replace_route(route_table.id, cidr_block, **conn_parameters) self.assertTrue(route_replacing_result)
'Tests replacing a route when the route table doesn\'t exist'
@mock_ec2 @skipIf(True, 'Moto has not implemented this feature. Skipping for now.') def test_that_when_replacing_a_route_with_a_non_existent_route_table_the_replace_route_method_should_return_false(self):
route_replacing_result = boto_vpc.replace_route('fake', cidr_block, **conn_parameters) self.assertFalse(route_replacing_result)
'Run with 2 vpc ids and returns a message'
@mock_ec2 def test_request_vpc_peering_connection(self):
my_vpc = self._create_vpc() other_vpc = self._create_vpc() self.assertTrue(('msg' in boto_vpc.request_vpc_peering_connection(name='my_peering', requester_vpc_id=my_vpc.id, peer_vpc_id=other_vpc.id, **conn_parameters)))
'Must specify only one'
@mock_ec2 def test_raises_error_if_both_vpc_name_and_vpc_id_are_specified(self):
my_vpc = self._create_vpc() other_vpc = self._create_vpc() with self.assertRaises(SaltInvocationError): boto_vpc.request_vpc_peering_connection(name='my_peering', requester_vpc_id=my_vpc.id, requester_vpc_name='foobar', peer_vpc_id=other_vpc.id, **conn_parameters) boto_vpc.request_vpc_peering_connection(name='my_peering', requester_vpc_name='my_peering', peer_vpc_id=other_vpc.id, **conn_parameters)
'test _validate_sleep function with valid number'
def test_validate_sleep_valid_number(self):
self.assertEqual(mac_power._validate_sleep(179), 179)
'test _validate_sleep function with invalid number'
def test_validate_sleep_invalid_number(self):
self.assertRaises(SaltInvocationError, mac_power._validate_sleep, 181)
'test _validate_sleep function with valid string'
def test_validate_sleep_valid_string(self):
self.assertEqual(mac_power._validate_sleep('never'), 'Never') self.assertEqual(mac_power._validate_sleep('off'), 'Never')
'test _validate_sleep function with invalid string'
def test_validate_sleep_invalid_string(self):
self.assertRaises(SaltInvocationError, mac_power._validate_sleep, 'bob')
'test _validate_sleep function with True'
def test_validate_sleep_bool_true(self):
self.assertRaises(SaltInvocationError, mac_power._validate_sleep, True)
'test _validate_sleep function with False'
def test_validate_sleep_bool_false(self):
self.assertEqual(mac_power._validate_sleep(False), 'Never')
'test _validate_sleep function with True'
def test_validate_sleep_unexpected(self):
self.assertRaises(SaltInvocationError, mac_power._validate_sleep, 172.7)
'Test to update and get the random script to a random place'
def test_prep_bootstrap(self):
with patch.dict(seed.__salt__, {'config.gather_bootstrap_script': MagicMock(return_value='BS_PATH/BS')}): with patch.object(uuid, 'uuid4', return_value='UUID'): with patch.object(os.path, 'exists', return_value=True): with patch.object(os, 'chmod', return_value=None): with patch.object(shutil, 'copy', return_value=None): self.assertEqual(seed.prep_bootstrap('MPT'), ('MPT/tmp/UUID/BS', '/tmp/UUID')) self.assertEqual(seed.prep_bootstrap('/MPT'), ('/MPT/tmp/UUID/BS', '/tmp/UUID'))
'Test to seed a location (disk image, directory, or block device) with the minion config, approve the minion\'s key, and/or install salt-minion.'
def test_apply_(self):
mock = MagicMock(side_effect=[False, {'type': 'type', 'target': 'target'}, {'type': 'type', 'target': 'target'}, {'type': 'type', 'target': 'target'}]) with patch.dict(seed.__salt__, {'file.stats': mock}): self.assertEqual(seed.apply_('path'), 'path does not exist') with patch.object(seed, '_mount', return_value=False): self.assertEqual(seed.apply_('path'), 'target could not be mounted') with patch.object(seed, '_mount', return_value=True): with patch.object(os.path, 'join', return_value='A'): with patch.object(os, 'makedirs', MagicMock(side_effect=OSError('f'))): with patch.object(os.path, 'isdir', return_value=False): self.assertRaises(OSError, seed.apply_, 'p') with patch.object(os, 'makedirs', MagicMock()): with patch.object(seed, 'mkconfig', return_value='A'): with patch.object(seed, '_check_install', return_value=False): with patch.object(seed, '_umount', return_value=None): self.assertFalse(seed.apply_('path', install=False))
'Test for adding a user'
def test_add(self):
with patch.dict(pw_user.__grains__, {'os_family': 'RedHat'}): mock = MagicMock(return_value=0) with patch.dict(pw_user.__salt__, {'cmd.retcode': mock}): self.assertTrue(pw_user.add('a'))
'Test for deleting a user'
def test_delete(self):
mock = MagicMock(return_value=0) with patch.dict(pw_user.__salt__, {'cmd.retcode': mock}): self.assertTrue(pw_user.delete('A'))
'Test if user.getent already have a value'
def test_getent(self):
mock_user = 'saltdude' class MockData(object, ): pw_name = mock_user with patch('pwd.getpwall', MagicMock(return_value=[MockData()])): with patch.dict(pw_user.__context__, {'user.getent': mock_user}): self.assertEqual(pw_user.getent(), mock_user) with patch.object(pw_user, 'info', MagicMock(return_value=mock_user)): self.assertEqual(pw_user.getent(True)[0], mock_user)
'Test if user id given is same as previous id'
def test_chuid(self):
mock = MagicMock(return_value={'uid': 'A'}) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.chuid('name', 'A')) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'uid': 'A'}, {'uid': 'A'}]) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chuid('name', 'B')) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'uid': 'A'}, {'uid': 'B'}]) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.chuid('name', 'A'))
'Test if group id given is same as previous id'
def test_chgid(self):
mock = MagicMock(return_value={'gid': 1}) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.chgid('name', 1)) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'gid': 2}, {'gid': 2}]) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chgid('name', 1)) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'gid': 1}, {'gid': 2}]) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.chgid('name', 1))
'Test if shell given is same as previous shell'
def test_chshell(self):
mock = MagicMock(return_value={'shell': 'A'}) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.chshell('name', 'A')) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'shell': 'B'}, {'shell': 'B'}]) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chshell('name', 'A')) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'shell': 'A'}, {'shell': 'B'}]) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.chshell('name', 'A'))
'Test if home directory given is same as previous home directory'
def test_chhome(self):
mock = MagicMock(return_value={'home': 'A'}) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.chhome('name', 'A')) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'home': 'B'}, {'home': 'B'}]) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chhome('name', 'A')) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'home': 'A'}, {'home': 'B'}]) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.chhome('name', 'A'))
'Test if no group needs to be added'
def test_chgroups(self):
mock = MagicMock(return_value=False) with patch.dict(pw_user.__salt__, {'cmd.retcode': mock}): mock = MagicMock(return_value=['a', 'b', 'c', 'd']) with patch.object(pw_user, 'list_groups', mock): self.assertTrue(pw_user.chgroups('name', 'a, b, c, d')) mock = MagicMock(return_value=False) with patch.dict(pw_user.__salt__, {'cmd.retcode': mock}): mock = MagicMock(return_value=['a', 'b']) with patch.object(pw_user, 'list_groups', mock): self.assertTrue(pw_user.chgroups('name', 'a, b, c'))
'Change the user\'s Full Name'
def test_chfullname(self):
mock = MagicMock(return_value=False) with patch.object(pw_user, '_get_gecos', mock): self.assertFalse(pw_user.chfullname('name', 'fullname')) mock = MagicMock(return_value={'fullname': 'fullname'}) with patch.object(pw_user, '_get_gecos', mock): self.assertTrue(pw_user.chfullname('name', 'fullname')) mock = MagicMock(return_value={'fullname': u'Unic\xf8de name \u2460\u2462\u2461'}) with patch.object(pw_user, '_get_gecos', mock): self.assertTrue(pw_user.chfullname('name', u'Unic\xf8de name \u2460\u2462\u2461')) mock = MagicMock(return_value={'fullname': 'fullname'}) with patch.object(pw_user, '_get_gecos', mock): mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'fullname': 'fullname2'}) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chfullname('name', 'fullname1')) mock = MagicMock(return_value={'fullname': 'fullname2'}) with patch.object(pw_user, '_get_gecos', mock): mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'fullname': 'fullname2'}) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chfullname('name', 'fullname1'))
'Change the user\'s Room Number'
def test_chroomnumber(self):
mock = MagicMock(return_value=False) with patch.object(pw_user, '_get_gecos', mock): self.assertFalse(pw_user.chroomnumber('name', 1)) mock = MagicMock(return_value={'roomnumber': u'Unic\xf8de room \u2460\u2462\u2461'}) with patch.object(pw_user, '_get_gecos', mock): self.assertTrue(pw_user.chroomnumber('name', u'Unic\xf8de room \u2460\u2462\u2461')) mock = MagicMock(return_value={'roomnumber': '1'}) with patch.object(pw_user, '_get_gecos', mock): self.assertTrue(pw_user.chroomnumber('name', 1)) mock = MagicMock(return_value={'roomnumber': '2'}) with patch.object(pw_user, '_get_gecos', mock): mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'roomnumber': '3'}) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chroomnumber('name', 1)) mock = MagicMock(return_value={'roomnumber': '3'}) with patch.object(pw_user, '_get_gecos', mock): mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'roomnumber': '3'}) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chroomnumber('name', 1))
'Change the user\'s Work Phone'
def test_chworkphone(self):
mock = MagicMock(return_value=False) with patch.object(pw_user, '_get_gecos', mock): self.assertFalse(pw_user.chworkphone('name', 1)) mock = MagicMock(return_value={'workphone': '1'}) with patch.object(pw_user, '_get_gecos', mock): self.assertTrue(pw_user.chworkphone('name', 1)) mock = MagicMock(return_value={'workphone': u'Unic\xf8de phone number \u2460\u2462\u2461'}) with patch.object(pw_user, '_get_gecos', mock): self.assertTrue(pw_user.chworkphone('name', u'Unic\xf8de phone number \u2460\u2462\u2461')) mock = MagicMock(return_value={'workphone': '2'}) with patch.object(pw_user, '_get_gecos', mock): mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'workphone': '3'}) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chworkphone('name', 1)) mock = MagicMock(return_value={'workphone': '3'}) with patch.object(pw_user, '_get_gecos', mock): mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'workphone': '3'}) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chworkphone('name', 1))
'Change the user\'s Home Phone'
def test_chhomephone(self):
mock = MagicMock(return_value=False) with patch.object(pw_user, '_get_gecos', mock): self.assertFalse(pw_user.chhomephone('name', 1)) mock = MagicMock(return_value={'homephone': '1'}) with patch.object(pw_user, '_get_gecos', mock): self.assertTrue(pw_user.chhomephone('name', 1)) mock = MagicMock(return_value={'homephone': u'Unic\xf8de phone number \u2460\u2462\u2461'}) with patch.object(pw_user, '_get_gecos', mock): self.assertTrue(pw_user.chhomephone('name', u'Unic\xf8de phone number \u2460\u2462\u2461')) mock = MagicMock(return_value={'homephone': '2'}) with patch.object(pw_user, '_get_gecos', mock): mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'homephone': '3'}) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chhomephone('name', 1)) mock = MagicMock(return_value={'homephone': '3'}) with patch.object(pw_user, '_get_gecos', mock): mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'homephone': '3'}) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.chhomephone('name', 1))
'Return user information'
def test_info(self):
self.assertEqual(pw_user.info('name'), {}) mock = MagicMock(return_value=pwd.struct_passwd(('_TEST_GROUP', '*', 83, 83, 'AMaViS Daemon', '/var/virusmails', '/usr/bin/false'))) with patch.object(pwd, 'getpwnam', mock): mock = MagicMock(return_value='Group Name') with patch.object(pw_user, 'list_groups', mock): self.assertEqual(pw_user.info('name')['name'], '_TEST_GROUP')
'Return a list of groups the named user belongs to'
def test_list_groups(self):
mock_group = 'saltgroup' with patch('salt.utils.get_group_list', MagicMock(return_value=[mock_group])): self.assertEqual(pw_user.list_groups('name'), [mock_group])
'Return a list of all users'
def test_list_users(self):
mock_user = 'saltdude' class MockData(object, ): pw_name = mock_user with patch('pwd.getpwall', MagicMock(return_value=[MockData()])): self.assertEqual(pw_user.list_users(), [mock_user])
'Change the username for a named user'
def test_rename(self):
mock = MagicMock(return_value=False) with patch.object(pw_user, 'info', mock): self.assertRaises(CommandExecutionError, pw_user.rename, 'name', 1) mock = MagicMock(return_value=True) with patch.object(pw_user, 'info', mock): self.assertRaises(CommandExecutionError, pw_user.rename, 'name', 1) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'name': ''}, False, {'name': 'name'}]) with patch.object(pw_user, 'info', mock): self.assertTrue(pw_user.rename('name', 'name')) mock = MagicMock(return_value=None) with patch.dict(pw_user.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'name': ''}, False, {'name': ''}]) with patch.object(pw_user, 'info', mock): self.assertFalse(pw_user.rename('name', 'name'))
'Test for delete a bucket, or delete an object from a bucket.'
def test_delete(self):
with patch.object(s3, '_get_key', return_value=('key', 'keyid', 'service_url', 'verify_ssl', 'kms_keyid', 'location', 'role_arn', 'path_style', 'https_enable')): self.assertEqual(s3.delete('bucket'), 'A')
'Test for list the contents of a bucket, or return an object from a bucket.'
def test_get(self):
with patch.object(s3, '_get_key', return_value=('key', 'keyid', 'service_url', 'verify_ssl', 'kms_keyid', 'location', 'role_arn', 'path_style', 'https_enable')): self.assertEqual(s3.get(), 'A')
'Test for return the metadata for a bucket, or an object in a bucket.'
def test_head(self):
with patch.object(s3, '_get_key', return_value=('key', 'keyid', 'service_url', 'verify_ssl', 'kms_keyid', 'location', 'role_arn', 'path_style', 'https_enable')): self.assertEqual(s3.head('bucket'), 'A')
'Test for create a new bucket, or upload an object to a bucket.'
def test_put(self):
with patch.object(s3, '_get_key', return_value=('key', 'keyid', 'service_url', 'verify_ssl', 'kms_keyid', 'location', 'role_arn', 'path_style', 'https_enable')): self.assertEqual(s3.put('bucket'), 'A')
'Test for Create an image for a specific platform.'
def test_bootstrap(self):
mock = MagicMock(return_value=False) with patch.dict(genesis.__salt__, {'file.directory_exists': mock}): mock = MagicMock(side_effect=Exception('foo')) with patch.dict(genesis.__salt__, {'file.mkdir': mock}): self.assertEqual(genesis.bootstrap('platform', 'root'), {'Error': "Exception('foo',)"}) with patch.object(genesis, '_bootstrap_yum', return_value='A'): with patch.dict(genesis.__salt__, {'mount.umount': MagicMock(), 'file.rmdir': MagicMock(), 'file.directory_exists': MagicMock()}): with patch.dict(genesis.__salt__, {'disk.blkid': MagicMock(return_value={})}): self.assertEqual(genesis.bootstrap('rpm', 'root', 'dir'), None) with patch.object(genesis, '_bootstrap_deb', return_value='A'): with patch.dict(genesis.__salt__, {'mount.umount': MagicMock(), 'file.rmdir': MagicMock(), 'file.directory_exists': MagicMock()}): with patch.dict(genesis.__salt__, {'disk.blkid': MagicMock(return_value={})}): self.assertEqual(genesis.bootstrap('deb', 'root', 'dir'), None) with patch.object(genesis, '_bootstrap_pacman', return_value='A') as pacman_patch: with patch.dict(genesis.__salt__, {'mount.umount': MagicMock(), 'file.rmdir': MagicMock(), 'file.directory_exists': MagicMock(), 'disk.blkid': MagicMock(return_value={})}): genesis.bootstrap('pacman', 'root', 'dir') pacman_patch.assert_called_with('root', img_format='dir', exclude_pkgs=[], pkgs=[])
'Test for Return which platforms are available'
def test_avail_platforms(self):
with patch('salt.utils.path.which', MagicMock(return_value=False)): self.assertFalse(genesis.avail_platforms()['deb'])
'Test for Pack up a directory structure, into a specific format'
def test_pack(self):
with patch.object(genesis, '_tar', return_value='tar'): self.assertEqual(genesis.pack('name', 'root'), None)
'Test for Unpack an image into a directory structure'
def test_unpack(self):
with patch.object(genesis, '_untar', return_value='untar'): self.assertEqual(genesis.unpack('name', 'root'), None)
'Test for publish a command from the minion out to other minions.'
def test_publish(self):
with patch.object(raet_publish, '_publish', return_value='A'): self.assertEqual(raet_publish.publish('tgt', 'fun'), 'A')
'Test for return the full data about the publication, this is invoked in the same way as the publish function'
def test_full_data(self):
with patch.object(raet_publish, '_publish', return_value='A'): self.assertEqual(raet_publish.full_data('tgt', 'fun'), 'A')
'Test for execute a runner on the master and return the data from the runner function'
def test_runner(self):
with patch.dict(raet_publish.__opts__, {'id': 'id'}): with patch.object(salt.transport.Channel, 'factory', MagicMock()): self.assertTrue(raet_publish.runner('fun')) class MockFactory(object, ): '\n Mock factory class\n ' load = '' def send(self, load): '\n mock send method\n ' self.load = load raise SaltReqTimeoutError(load) with patch.dict(raet_publish.__opts__, {'id': 'id'}): with patch.object(salt.transport.Channel, 'factory', MagicMock(return_value=MockFactory())): self.assertEqual(raet_publish.runner(1), "'1' runner publish timed out")
'Test for Lists available (compiled) locales'
def test_list_avail(self):
with patch.dict(localemod.__salt__, {'cmd.run': MagicMock(return_value='A\nB')}): self.assertEqual(localemod.list_avail(), ['A', 'B'])
'Test for Get the current system locale'
def test_get_locale(self):
with patch.dict(localemod.__context__, {'salt.utils.systemd.booted': True}): with patch.dict(localemod.__grains__, {'os_family': ['Unknown']}): with patch.multiple(localemod, _parse_dbus_locale=MagicMock(return_value={'LANG': 'A'}), HAS_DBUS=True): self.assertEqual('A', localemod.get_locale()) localemod._parse_dbus_locale.assert_called_once_with() with patch.multiple(localemod, _parse_localectl=MagicMock(return_value={'LANG': 'A'}), HAS_DBUS=False): self.assertEqual('A', localemod.get_locale()) localemod._parse_localectl.assert_called_once_with() with patch.dict(localemod.__context__, {'salt.utils.systemd.booted': False}): with patch.dict(localemod.__grains__, {'os_family': ['Gentoo']}): with patch.dict(localemod.__salt__, {'cmd.run': MagicMock(return_value='A')}): with patch.object(localemod, '_parse_localectl', return_value={'LANG': 'A'}): self.assertEqual(localemod.get_locale(), 'A') with patch.dict(localemod.__grains__, {'os_family': ['RedHat']}): with patch.dict(localemod.__salt__, {'cmd.run': MagicMock(return_value='A=B')}): with patch.object(localemod, '_parse_localectl', return_value={'LANG': 'B'}): self.assertEqual(localemod.get_locale(), 'B') with patch.dict(localemod.__grains__, {'os_family': ['Unknown']}): with patch.dict(localemod.__salt__, {'cmd.run': MagicMock(return_value='A=B')}): self.assertRaises(CommandExecutionError, localemod.get_locale)
'Test for Sets the current system locale'
def test_set_locale(self):
with patch.dict(localemod.__context__, {'salt.utils.systemd.booted': True}): with patch.dict(localemod.__grains__, {'os_family': ['Unknown']}): with patch.object(localemod, '_localectl_set', return_value=True): self.assertTrue(localemod.set_locale('l')) with patch.dict(localemod.__context__, {'salt.utils.systemd.booted': False}): with patch.dict(localemod.__grains__, {'os_family': ['Gentoo']}): with patch.dict(localemod.__salt__, {'cmd.retcode': MagicMock(return_value='A')}): with patch.object(localemod, '_parse_localectl', return_value={'LANG': 'B'}): self.assertFalse(localemod.set_locale('l')) with patch.dict(localemod.__grains__, {'os_family': ['A']}): with patch.dict(localemod.__salt__, {'cmd.retcode': MagicMock(return_value=0)}): with patch('salt.utils.systemd.booted', return_value=False): self.assertRaises(CommandExecutionError, localemod.set_locale, 'A')
'Test for Check if a locale is available'
def test_avail(self):
with patch('salt.utils.locales.normalize_locale', MagicMock(return_value='en_US.UTF-8 UTF-8')): with patch.dict(localemod.__salt__, {'locale.list_avail': MagicMock(return_value=['A', 'B'])}): self.assertTrue(localemod.avail('locale'))
'Tests the return of gen_locale when the provided locale is not found'
def test_gen_locale_not_valid(self):
with patch.dict(localemod.__grains__, {'os': 'Debian'}): with patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')): with patch.dict(localemod.__salt__, {'file.search': MagicMock(return_value=False)}): self.assertFalse(localemod.gen_locale('foo'))
'Tests the return of successful gen_locale on Debian system'
def test_gen_locale_debian(self):
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337} with patch.dict(localemod.__grains__, {'os': 'Debian'}): with patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')): with patch.dict(localemod.__salt__, {'file.search': MagicMock(return_value=True), 'file.replace': MagicMock(return_value=True), 'cmd.run_all': MagicMock(return_value=ret)}): self.assertTrue(localemod.gen_locale('en_US.UTF-8 UTF-8'))
'Tests the return of successful gen_locale on Debian system without a charmap'
def test_gen_locale_debian_no_charmap(self):
def file_search(search, pattern, flags): '\n mock file.search\n ' if (len(pattern.split()) == 1): return False else: return True ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337} with patch.dict(localemod.__grains__, {'os': 'Debian'}): with patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')): with patch.dict(localemod.__salt__, {'file.search': file_search, 'file.replace': MagicMock(return_value=True), 'cmd.run_all': MagicMock(return_value=ret)}): self.assertTrue(localemod.gen_locale('en_US.UTF-8'))
'Test the return of successful gen_locale on Ubuntu system'
def test_gen_locale_ubuntu(self):
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337} with patch.dict(localemod.__salt__, {'file.replace': MagicMock(return_value=True), 'file.touch': MagicMock(return_value=None), 'file.append': MagicMock(return_value=None), 'cmd.run_all': MagicMock(return_value=ret)}): with patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')): with patch('os.listdir', MagicMock(return_value=['en_US'])): with patch.dict(localemod.__grains__, {'os': 'Ubuntu'}): self.assertTrue(localemod.gen_locale('en_US.UTF-8'))
'Tests the return of successful gen_locale on Gentoo system'
def test_gen_locale_gentoo(self):
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337} with patch.dict(localemod.__grains__, {'os_family': 'Gentoo'}): with patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')): with patch('os.listdir', MagicMock(return_value=['en_US.UTF-8'])): with patch.dict(localemod.__salt__, {'file.search': MagicMock(return_value=True), 'file.replace': MagicMock(return_value=True), 'cmd.run_all': MagicMock(return_value=ret)}): self.assertTrue(localemod.gen_locale('en_US.UTF-8 UTF-8'))
'Tests the return of successful gen_locale on Gentoo system without a charmap'
def test_gen_locale_gentoo_no_charmap(self):
def file_search(search, pattern, flags): '\n mock file.search\n ' if (len(pattern.split()) == 1): return False else: return True ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337} with patch.dict(localemod.__grains__, {'os_family': 'Gentoo'}): with patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')): with patch('os.listdir', MagicMock(return_value=['en_US.UTF-8'])): with patch.dict(localemod.__salt__, {'file.search': file_search, 'file.replace': MagicMock(return_value=True), 'cmd.run_all': MagicMock(return_value=ret)}): self.assertTrue(localemod.gen_locale('en_US.UTF-8'))
'Tests the return of successful gen_locale'
def test_gen_locale(self):
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337} with patch.dict(localemod.__salt__, {'cmd.run_all': MagicMock(return_value=ret), 'file.replace': MagicMock()}): with patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')): with patch('os.listdir', MagicMock(return_value=['en_US'])): self.assertTrue(localemod.gen_locale('en_US.UTF-8'))
'Tests the return of successful gen_locale'
def test_gen_locale_verbose(self):
ret = {'stdout': 'saltines', 'stderr': 'biscuits', 'retcode': 0, 'pid': 1337} with patch.dict(localemod.__salt__, {'cmd.run_all': MagicMock(return_value=ret), 'file.replace': MagicMock()}): with patch('salt.utils.path.which', MagicMock(return_value='/some/dir/path')): with patch('os.listdir', MagicMock(return_value=['en_US'])): self.assertEqual(localemod.gen_locale('en_US.UTF-8', verbose=True), ret)
'Test if it install a module from cpan'
def test_install(self):
mock = MagicMock(return_value='') with patch.dict(cpan.__salt__, {'cmd.run': mock}): mock = MagicMock(side_effect=[{'installed version': None}, {'installed version': '3.1'}]) with patch.object(cpan, 'show', mock): self.assertDictEqual(cpan.install('Alloy'), {'new': '3.1', 'old': None})
'Test if it install a module from cpan'
def test_install_error(self):
mock = MagicMock(return_value="don't know what it is") with patch.dict(cpan.__salt__, {'cmd.run': mock}): self.assertDictEqual(cpan.install('Alloy'), {'error': 'CPAN cannot identify this package', 'new': None, 'old': None})
'Test if it remove a module using cpan'
def test_remove(self):
with patch('os.listdir', MagicMock(return_value=[''])): mock = MagicMock(return_value='') with patch.dict(cpan.__salt__, {'cmd.run': mock}): mock = MagicMock(return_value={'installed version': '2.1', 'cpan build dirs': [''], 'installed file': '/root'}) with patch.object(cpan, 'show', mock): self.assertDictEqual(cpan.remove('Alloy'), {'new': None, 'old': '2.1'})
'Test if it try to remove an unexist module using cpan'
def test_remove_unexist_error(self):
mock = MagicMock(return_value="don't know what it is") with patch.dict(cpan.__salt__, {'cmd.run': mock}): self.assertDictEqual(cpan.remove('Alloy'), {'error': 'This package does not seem to exist'})
'Test if it remove non installed module using cpan'
def test_remove_noninstalled_error(self):
mock = MagicMock(return_value={'installed version': None}) with patch.object(cpan, 'show', mock): self.assertDictEqual(cpan.remove('Alloy'), {'new': None, 'old': None})
'Test if it gives no cpan error while removing'
def test_remove_nopan_error(self):
ret = {'error': 'No CPAN data available to use for uninstalling'} mock = MagicMock(return_value={'installed version': '2.1'}) with patch.object(cpan, 'show', mock): self.assertDictEqual(cpan.remove('Alloy'), ret)
'Test if it list installed Perl module'
def test_list(self):
mock = MagicMock(return_value='') with patch.dict(cpan.__salt__, {'cmd.run': mock}): self.assertDictEqual(cpan.list_(), {})
'Test if it show information about a specific Perl module'
def test_show(self):
mock = MagicMock(return_value='') with patch.dict(cpan.__salt__, {'cmd.run': mock}): self.assertDictEqual(cpan.show('Alloy'), {'error': 'This package does not seem to exist', 'name': 'Alloy'})
'Test if it show information about a specific Perl module'
def test_show_mock(self):
with patch('salt.modules.cpan.show', MagicMock(return_value={'Salt': 'salt'})): mock = MagicMock(return_value='Salt module installed') with patch.dict(cpan.__salt__, {'cmd.run': mock}): self.assertDictEqual(cpan.show('Alloy'), {'Salt': 'salt'})
'Test if it return a dict of CPAN configuration values'
def test_show_config(self):
mock = MagicMock(return_value='') with patch.dict(cpan.__salt__, {'cmd.run': mock}): self.assertDictEqual(cpan.show_config(), {})
'Test if it run an arbitrary LDAP query and return the results.'
def test_search(self):
class MockConnect(object, ): '\n Mocking _connect method\n ' def __init__(self): self.bdn = None self.scope = None self._filter = None self.attrs = None def search_s(self, bdn, scope, _filter, attrs): '\n Mock function for search_s\n ' self.bdn = bdn self.scope = scope self._filter = _filter self.attrs = attrs return 'SALT' mock = MagicMock(return_value=True) with patch.dict(ldapmod.__salt__, {'config.option': mock}): with patch.object(ldapmod, '_connect', MagicMock(return_value=MockConnect())): with patch.object(time, 'time', MagicMock(return_value=0.0008)): self.assertDictEqual(ldapmod.search(filter='myhost'), {'count': 4, 'results': 'SALT', 'time': {'raw': '0.0', 'human': '0.0ms'}})
'Test if it list the packages currently installed in a dict'
def test_list_pkgs(self):
mock = MagicMock(return_value='') with patch.dict(rpm.__salt__, {'cmd.run': mock}): self.assertDictEqual(rpm.list_pkgs(), {})
'Test if it runs an rpm -Va on a system, and returns the results in a dict'
def test_verify(self):
mock = MagicMock(return_value={'stdout': '', 'stderr': '', 'retcode': 0, 'pid': 12345}) with patch.dict(rpm.__salt__, {'cmd.run_all': mock}): self.assertDictEqual(rpm.verify('httpd'), {})
'Test if it list the files that belong to a package.'
def test_file_list(self):
mock = MagicMock(return_value='') with patch.dict(rpm.__salt__, {'cmd.run': mock}): self.assertDictEqual(rpm.file_list('httpd'), {'errors': [], 'files': []})
'Test if it list the files that belong to a package'
def test_file_dict(self):
mock = MagicMock(return_value='') with patch.dict(rpm.__salt__, {'cmd.run': mock}): self.assertDictEqual(rpm.file_dict('httpd'), {'errors': [], 'packages': {}})
'Test if it return the name of the package that owns the file.'
def test_owner(self):
self.assertEqual(rpm.owner(), '') ret = 'file /usr/bin/salt-jenkins-build is not owned by any package' mock = MagicMock(return_value=ret) with patch.dict(rpm.__salt__, {'cmd.run_stdout': mock}): self.assertEqual(rpm.owner('/usr/bin/salt-jenkins-build'), '') ret = {'/usr/bin/vim': 'vim-enhanced-7.4.160-1.e17.x86_64', '/usr/bin/python': 'python-2.7.5-16.e17.x86_64'} mock = MagicMock(side_effect=['python-2.7.5-16.e17.x86_64', 'vim-enhanced-7.4.160-1.e17.x86_64']) with patch.dict(rpm.__salt__, {'cmd.run_stdout': mock}): self.assertDictEqual(rpm.owner('/usr/bin/python', '/usr/bin/vim'), ret)
'Test if checksum validate as expected'
def test_checksum(self):
ret = {'file1.rpm': True, 'file2.rpm': False, 'file3.rpm': False} mock = MagicMock(side_effect=[True, 0, True, 1, False, 0]) with patch.dict(rpm.__salt__, {'file.file_exists': mock, 'cmd.retcode': mock}): self.assertDictEqual(rpm.checksum('file1.rpm', 'file2.rpm', 'file3.rpm'), ret)
'Test package version is called RPM version if RPM-Python is installed :return:'
def test_version_cmp_rpm(self):
with patch('salt.modules.rpm.rpm.labelCompare', MagicMock(return_value=0)): with patch('salt.modules.rpm.HAS_RPM', True): self.assertEqual(0, rpm.version_cmp('1', '2'))
'Test package version is called RPM version if RPM-Python is installed :return:'
def test_version_cmp_fallback(self):
with patch('salt.modules.rpm.rpm.labelCompare', MagicMock(return_value=0)): with patch('salt.modules.rpm.HAS_RPM', False): self.assertEqual((-1), rpm.version_cmp('1', '2'))
'Test - Returns a string representing the package version or an empty string if not installed.'
def test_version(self):
version = LOWPKG_INFO['wget']['version'] mock = MagicMock(return_value=version) with patch.dict(aptpkg.__salt__, {'pkg_resource.version': mock}): self.assertEqual(aptpkg.version(*['wget']), version)
'Test - Check whether or not an upgrade is available for a given package.'
def test_upgrade_available(self):
with patch('salt.modules.aptpkg.latest_version', MagicMock(return_value='')): self.assertFalse(aptpkg.upgrade_available('wget'))
'Test - Add a repo key.'
def test_add_repo_key(self):
with patch('salt.modules.aptpkg.get_repo_keys', MagicMock(return_value=REPO_KEYS)): mock = MagicMock(return_value={'retcode': 0, 'stdout': 'OK'}) with patch.dict(aptpkg.__salt__, {'cmd.run_all': mock}): self.assertTrue(aptpkg.add_repo_key(keyserver='keyserver.ubuntu.com', keyid='FBB75451'))
'Test - Add a repo key using incomplete input data.'
def test_add_repo_key_failed(self):
with patch('salt.modules.aptpkg.get_repo_keys', MagicMock(return_value=REPO_KEYS)): kwargs = {'keyserver': 'keyserver.ubuntu.com'} mock = MagicMock(return_value={'retcode': 0, 'stdout': 'OK'}) with patch.dict(aptpkg.__salt__, {'cmd.run_all': mock}): self.assertRaises(SaltInvocationError, aptpkg.add_repo_key, **kwargs)
'Test - List known repo key details.'
def test_get_repo_keys(self):
mock = MagicMock(return_value={'retcode': 0, 'stdout': APT_KEY_LIST}) with patch.dict(aptpkg.__salt__, {'cmd.run_all': mock}): self.assertEqual(aptpkg.get_repo_keys(), REPO_KEYS)
'Test - List the files that belong to a package, grouped by package.'
def test_file_dict(self):
mock = MagicMock(return_value=LOWPKG_FILES) with patch.dict(aptpkg.__salt__, {'lowpkg.file_dict': mock}): self.assertEqual(aptpkg.file_dict('wget'), LOWPKG_FILES)
'Test - List the files that belong to a package.'
def test_file_list(self):
files = {'errors': LOWPKG_FILES['errors'], 'files': LOWPKG_FILES['packages']['wget']} mock = MagicMock(return_value=files) with patch.dict(aptpkg.__salt__, {'lowpkg.file_list': mock}): self.assertEqual(aptpkg.file_list('wget'), files)
'Test - View package state from the dpkg database.'
def test_get_selections(self):
selections = {'install': ['wget']} mock = MagicMock(return_value='wget DCTB DCTB DCTB DCTB DCTB DCTB install') with patch.dict(aptpkg.__salt__, {'cmd.run_stdout': mock}): self.assertEqual(aptpkg.get_selections('wget'), selections)
'Test - Return the information of the named package(s) installed on the system.'
def test_info_installed(self):
names = {'group': 'section', 'packager': 'maintainer', 'url': 'homepage'} installed = copy.deepcopy(LOWPKG_INFO) for name in names: if installed['wget'].get(names[name], False): installed['wget'][name] = installed['wget'].pop(names[name]) mock = MagicMock(return_value=LOWPKG_INFO) with patch.dict(aptpkg.__salt__, {'lowpkg.info': mock}): self.assertEqual(aptpkg.info_installed('wget'), installed)
'Test - Return the name of the package that owns the file.'
def test_owner(self):
paths = ['/usr/bin/wget'] mock = MagicMock(return_value='wget: /usr/bin/wget') with patch.dict(aptpkg.__salt__, {'cmd.run_stdout': mock}): self.assertEqual(aptpkg.owner(*paths), 'wget')
'Test - Updates the APT database to latest packages based upon repositories.'
def test_refresh_db(self):
refresh_db = {'http://security.ubuntu.com trusty-security InRelease': True, 'http://security.ubuntu.com trusty-security/main Sources': True, 'http://security.ubuntu.com trusty-security/main Translation-en': None, 'http://security.ubuntu.com trusty-security/main amd64 Packages': True, 'http://security.ubuntu.com trusty-security/main i386 Packages': True} mock = MagicMock(return_value={'retcode': 0, 'stdout': APT_Q_UPDATE}) with patch('salt.utils.pkg.clear_rtag', MagicMock()): with patch.dict(aptpkg.__salt__, {'cmd.run_all': mock}): self.assertEqual(aptpkg.refresh_db(), refresh_db)
'Test - Update the APT database using unreachable repositories.'
def test_refresh_db_failed(self):
kwargs = {'failhard': True} mock = MagicMock(return_value={'retcode': 0, 'stdout': APT_Q_UPDATE_ERROR}) with patch('salt.utils.pkg.clear_rtag', MagicMock()): with patch.dict(aptpkg.__salt__, {'cmd.run_all': mock}): self.assertRaises(CommandExecutionError, aptpkg.refresh_db, **kwargs)
'Test - Remove packages not required by another package.'
def test_autoremove(self):
with patch('salt.modules.aptpkg.list_pkgs', MagicMock(return_value=PACKAGES)): patch_kwargs = {'__salt__': {'config.get': MagicMock(return_value=True), 'cmd.run': MagicMock(return_value=AUTOREMOVE)}} with patch.multiple(aptpkg, **patch_kwargs): self.assertEqual(aptpkg.autoremove(), dict()) self.assertEqual(aptpkg.autoremove(purge=True), dict()) self.assertEqual(aptpkg.autoremove(list_only=True), list()) self.assertEqual(aptpkg.autoremove(list_only=True, purge=True), list())
'Test - Remove packages.'
def test_remove(self):
with patch('salt.modules.aptpkg._uninstall', MagicMock(return_value=UNINSTALL)): self.assertEqual(aptpkg.remove(name='tmux'), UNINSTALL)
'Test - Remove packages along with all configuration files.'
def test_purge(self):
with patch('salt.modules.aptpkg._uninstall', MagicMock(return_value=UNINSTALL)): self.assertEqual(aptpkg.purge(name='tmux'), UNINSTALL)
'Test - Upgrades all packages.'
def test_upgrade(self):
with patch('salt.utils.pkg.clear_rtag', MagicMock()): with patch('salt.modules.aptpkg.list_pkgs', MagicMock(return_value=UNINSTALL)): mock_cmd = MagicMock(return_value={'retcode': 0, 'stdout': UPGRADE}) patch_kwargs = {'__salt__': {'config.get': MagicMock(return_value=True), 'cmd.run_all': mock_cmd}} with patch.multiple(aptpkg, **patch_kwargs): self.assertEqual(aptpkg.upgrade(), dict())
'Test to start the specified service'
def test_start(self):
with patch.object(os.path, 'join', return_value='A'): with patch.dict(service.__salt__, {'service.run': MagicMock(return_value=True)}): self.assertTrue(service.start('name'))
'Test to stop the specified service'
def test_stop(self):
with patch.object(os.path, 'join', return_value='A'): with patch.dict(service.__salt__, {'service.run': MagicMock(return_value=True)}): self.assertTrue(service.stop('name'))
'Test to restart the specified service'
def test_restart(self):
with patch.object(os.path, 'join', return_value='A'): with patch.dict(service.__salt__, {'service.run': MagicMock(return_value=True)}): self.assertTrue(service.restart('name'))