desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Test revoking privileges on table'
def test_privileges_revoke_table(self):
with patch('salt.modules.postgres._run_psql', Mock(return_value={'retcode': 0})): with patch('salt.modules.postgres.has_privileges', Mock(return_value=True)): ret = postgres.privileges_revoke('baruwa', 'awl', 'table', 'ALL', maintenance_db='db_name', runas='user', host='testhost', port='testport', user='testuser', password='testpassword') query = 'REVOKE ALL ON TABLE public.awl FROM baruwa' postgres._run_psql.assert_called_once_with(['/usr/bin/pgsql', '--no-align', '--no-readline', '--no-psqlrc', '--no-password', '--username', 'testuser', '--host', 'testhost', '--port', 'testport', '--dbname', 'db_name', '-c', query], host='testhost', port='testport', password='testpassword', user='testuser', runas='user')
'Test revoking privileges on group'
def test_privileges_revoke_group(self):
with patch('salt.modules.postgres._run_psql', Mock(return_value={'retcode': 0})): with patch('salt.modules.postgres.has_privileges', Mock(return_value=True)): ret = postgres.privileges_revoke('baruwa', 'admins', 'group', maintenance_db='db_name', runas='user', host='testhost', port='testport', user='testuser', password='testpassword') query = 'REVOKE admins FROM baruwa' postgres._run_psql.assert_called_once_with(['/usr/bin/pgsql', '--no-align', '--no-readline', '--no-psqlrc', '--no-password', '--username', 'testuser', '--host', 'testhost', '--port', 'testport', '--dbname', 'db_name', '-c', query], host='testhost', port='testport', password='testpassword', user='testuser', runas='user')
'Test Initializing a postgres data directory'
def test_datadir_init(self):
with patch('salt.modules.postgres._run_initdb', Mock(return_value={'retcode': 0})): with patch('salt.modules.postgres.datadir_exists', Mock(return_value=False)): name = '/var/lib/pgsql/data' ret = postgres.datadir_init(name, user='postgres', password='test', runas='postgres') postgres._run_initdb.assert_called_once_with(name, auth='password', encoding='UTF8', locale=None, password='test', runas='postgres', user='postgres') self.assertTrue(ret)
'Test Checks if postgres data directory has been initialized'
def test_datadir_exists(self):
with patch('os.path.isfile', Mock(return_value=True)): name = '/var/lib/pgsql/data' ret = postgres.datadir_exists(name) self.assertTrue(ret)
'Test cases for Add, replace, or update the A and PTR (reverse) records for a host.'
def test_add_host(self):
with patch('salt.modules.ddns.update') as ddns_update: ddns_update.return_value = False self.assertFalse(ddns.add_host(zone='A', name='B', ttl=1, ip='172.27.0.0')) ddns_update.return_value = True self.assertTrue(ddns.add_host(zone='A', name='B', ttl=1, ip='172.27.0.0'))
'Tests for delete the forward and reverse records for a host.'
def test_delete_host(self):
with patch('salt.modules.ddns.delete') as ddns_delete: ddns_delete.return_value = False with patch.object(dns.query, 'udp') as mock: mock.answer = [{'address': 'localhost'}] self.assertFalse(ddns.delete_host(zone='A', name='B'))
'Test to add, replace, or update a DNS record.'
def test_update(self):
mock_request = textwrap.dedent(' id 29380\n opcode QUERY\n rcode NOERROR\n flags RD\n ;QUESTION\n name.zone. IN AAAA\n ;ANSWER\n ;AUTHORITY\n ;ADDITIONAL') mock_rdtype = 28 class MockRrset(object, ): def __init__(self): self.items = [{'address': 'localhost'}] self.ttl = 2 class MockAnswer(object, ): def __init__(self, *args, **kwargs): self.answer = [MockRrset()] def rcode(self): return 0 def mock_udp_query(*args, **kwargs): return MockAnswer with patch.object(dns.message, 'make_query', MagicMock(return_value=mock_request)): with patch.object(dns.query, 'udp', mock_udp_query()): with patch.object(dns.rdatatype, 'from_text', MagicMock(return_value=mock_rdtype)): with patch.object(ddns, '_get_keyring', return_value=None): with patch.object(ddns, '_config', return_value=None): self.assertTrue(ddns.update('zone', 'name', 1, 'AAAA', '::1'))
'Test to delete a DNS record.'
def test_delete(self):
file_data = json.dumps({'A': 'B'}) class MockAnswer(object, ): def __init__(self, *args, **kwargs): self.answer = [{'address': 'localhost'}] def rcode(self): return 0 def mock_udp_query(*args, **kwargs): return MockAnswer with patch.object(dns.query, 'udp', mock_udp_query()): with patch('salt.utils.files.fopen', mock_open(read_data=file_data), create=True): with patch.object(dns.tsigkeyring, 'from_text', return_value=True): with patch.object(ddns, '_get_keyring', return_value=None): with patch.object(ddns, '_config', return_value=None): self.assertTrue(ddns.delete(zone='A', name='B'))
'Test if it activate nbd for an image file.'
def test_connect(self):
mock = MagicMock(return_value=True) with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock}): with patch.object(os.path, 'isfile', MagicMock(return_value=False)): self.assertEqual(qemu_nbd.connect('/tmp/image.raw'), '') self.assertEqual(qemu_nbd.connect('/tmp/image.raw'), '') with patch.object(os.path, 'isfile', mock): with patch.object(glob, 'glob', MagicMock(return_value=['/dev/nbd0'])): with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock, 'cmd.retcode': MagicMock(side_effect=[1, 0])}): self.assertEqual(qemu_nbd.connect('/tmp/image.raw'), '/dev/nbd0') with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock, 'cmd.retcode': MagicMock(return_value=False)}): self.assertEqual(qemu_nbd.connect('/tmp/image.raw'), '')
'Test if it pass in the nbd connection device location, mount all partitions and return a dict of mount points.'
def test_mount(self):
mock = MagicMock(return_value=True) with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock}): self.assertDictEqual(qemu_nbd.mount('/dev/nbd0'), {})
'Test if it mount the named image via qemu-nbd and return the mounted roots'
def test_init(self):
mock = MagicMock(return_value=True) with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock}): self.assertEqual(qemu_nbd.init('/srv/image.qcow2'), '') with patch.object(os.path, 'isfile', mock): with patch.object(glob, 'glob', MagicMock(return_value=['/dev/nbd0'])): with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock, 'mount.mount': mock, 'cmd.retcode': MagicMock(side_effect=[1, 0])}): self.assertDictEqual(qemu_nbd.init('/srv/image.qcow2'), {'{0}/nbd/nbd0/nbd0'.format(tempfile.gettempdir()): '/dev/nbd0'})
'Test if it pass in the mnt dict returned from nbd_mount to unmount and disconnect the image from nbd.'
def test_clear(self):
mock_run = MagicMock(return_value=True) with patch.dict(qemu_nbd.__salt__, {'cmd.run': mock_run, 'mount.umount': MagicMock(side_effect=[False, True])}): self.assertDictEqual(qemu_nbd.clear({'/mnt/foo': '/dev/nbd0p1'}), {'/mnt/foo': '/dev/nbd0p1'}) self.assertDictEqual(qemu_nbd.clear({'/mnt/foo': '/dev/nbd0p1'}), {})
'Test CentOS is recognized :return:'
@patch('salt.utils.path.which', MagicMock(return_value=False)) def test_get_zone_centos(self):
with patch.dict(timezone.__grains__, {'os': 'centos'}): with patch('salt.modules.timezone._get_zone_etc_localtime', MagicMock(return_value=self.TEST_TZ)): assert (timezone.get_zone() == self.TEST_TZ)
'Test RedHat and Suse are recognized :return:'
@patch('salt.utils.path.which', MagicMock(return_value=False)) def test_get_zone_os_family_rh_suse(self):
for osfamily in ['RedHat', 'Suse']: with patch.dict(timezone.__grains__, {'os_family': [osfamily]}): with patch('salt.modules.timezone._get_zone_sysconfig', MagicMock(return_value=self.TEST_TZ)): assert (timezone.get_zone() == self.TEST_TZ)
'Test Debian and Gentoo are recognized :return:'
@patch('salt.utils.path.which', MagicMock(return_value=False)) def test_get_zone_os_family_debian_gentoo(self):
for osfamily in ['Debian', 'Gentoo']: with patch.dict(timezone.__grains__, {'os_family': [osfamily]}): with patch('salt.modules.timezone._get_zone_etc_timezone', MagicMock(return_value=self.TEST_TZ)): assert (timezone.get_zone() == self.TEST_TZ)
'Test *BSD and NILinuxRT are recognized :return:'
@patch('salt.utils.path.which', MagicMock(return_value=False)) def test_get_zone_os_family_allbsd_nilinuxrt(self):
for osfamily in ['FreeBSD', 'OpenBSD', 'NetBSD', 'NILinuxRT']: with patch.dict(timezone.__grains__, {'os_family': osfamily}): with patch('salt.modules.timezone._get_zone_etc_localtime', MagicMock(return_value=self.TEST_TZ)): assert (timezone.get_zone() == self.TEST_TZ)
'Test Slowlaris is recognized :return:'
@patch('salt.utils.path.which', MagicMock(return_value=False)) def test_get_zone_os_family_slowlaris(self):
with patch.dict(timezone.__grains__, {'os_family': ['Solaris']}): with patch('salt.modules.timezone._get_zone_solaris', MagicMock(return_value=self.TEST_TZ)): assert (timezone.get_zone() == self.TEST_TZ)
'Test IBM AIX is recognized :return:'
@patch('salt.utils.path.which', MagicMock(return_value=False)) def test_get_zone_os_family_aix(self):
with patch.dict(timezone.__grains__, {'os_family': ['AIX']}): with patch('salt.modules.timezone._get_zone_aix', MagicMock(return_value=self.TEST_TZ)): assert (timezone.get_zone() == self.TEST_TZ)
'Test zone set on RH series :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_set_zone_redhat(self):
with patch.dict(timezone.__grains__, {'os_family': ['RedHat']}): assert timezone.set_zone(self.TEST_TZ) (name, args, kwargs) = timezone.__salt__['file.sed'].mock_calls[0] assert (args == ('/etc/sysconfig/clock', '^ZONE=.*', 'ZONE="UTC"'))
'Test zone set on SUSE series :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_set_zone_suse(self):
with patch.dict(timezone.__grains__, {'os_family': ['Suse']}): assert timezone.set_zone(self.TEST_TZ) (name, args, kwargs) = timezone.__salt__['file.sed'].mock_calls[0] assert (args == ('/etc/sysconfig/clock', '^TIMEZONE=.*', 'TIMEZONE="UTC"'))
'Test zone set on Gentoo series :return:'
@skipIf(salt.utils.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_set_zone_gentoo(self):
with patch.dict(timezone.__grains__, {'os_family': ['Gentoo']}): _fopen = mock_open() with patch('salt.utils.files.fopen', _fopen): assert timezone.set_zone(self.TEST_TZ) (name, args, kwargs) = _fopen.mock_calls[0] assert (args == ('/etc/timezone', 'w')) (name, args, kwargs) = _fopen.return_value.__enter__.return_value.write.mock_calls[0] assert (args == ('UTC',))
'Test zone set on Debian series :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_set_zone_debian(self):
with patch.dict(timezone.__grains__, {'os_family': ['Debian']}): _fopen = mock_open() with patch('salt.utils.files.fopen', _fopen): assert timezone.set_zone(self.TEST_TZ) (name, args, kwargs) = _fopen.mock_calls[0] assert (args == ('/etc/timezone', 'w')) (name, args, kwargs) = _fopen.return_value.__enter__.return_value.write.mock_calls[0] assert (args == ('UTC',))
'Test get hwclock UTC/localtime :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=True)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_get_hwclock_timedate_utc(self):
with patch('salt.modules.timezone._timedatectl', MagicMock(return_value={'stdout': 'rtc in local tz'})): assert (timezone.get_hwclock() == 'UTC') with patch('salt.modules.timezone._timedatectl', MagicMock(return_value={'stdout': 'rtc in local tz:yes'})): assert (timezone.get_hwclock() == 'localtime')
'Test get hwclock on SUSE :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_get_hwclock_suse(self):
with patch.dict(timezone.__grains__, {'os_family': ['Suse']}): timezone.get_hwclock() (name, args, kwarg) = timezone.__salt__['cmd.run'].mock_calls[0] assert (args == (['tail', '-n', '1', '/etc/adjtime'],)) assert (kwarg == {'python_shell': False})
'Test get hwclock on RedHat :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_get_hwclock_redhat(self):
with patch.dict(timezone.__grains__, {'os_family': ['RedHat']}): timezone.get_hwclock() (name, args, kwarg) = timezone.__salt__['cmd.run'].mock_calls[0] assert (args == (['tail', '-n', '1', '/etc/adjtime'],)) assert (kwarg == {'python_shell': False})
'Test get hwclock on Debian :return:'
def _test_get_hwclock_debian(self):
with patch('salt.utils.path.which', MagicMock(return_value=False)): with patch('os.path.exists', MagicMock(return_value=True)): with patch('os.unlink', MagicMock()): with patch('os.symlink', MagicMock()): with patch.dict(timezone.__grains__, {'os_family': ['Debian']}): timezone.get_hwclock() (name, args, kwarg) = timezone.__salt__['cmd.run'].mock_calls[0] assert (args == (['tail', '-n', '1', '/etc/adjtime'],)) assert (kwarg == {'python_shell': False})
'Test get hwclock on Solaris :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_get_hwclock_solaris(self):
with patch.dict(timezone.__grains__, {'os_family': ['Solaris']}): assert (timezone.get_hwclock() == 'UTC') with patch('salt.utils.files.fopen', mock_open()): assert (timezone.get_hwclock() == 'localtime')
'Test get hwclock on AIX :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_get_hwclock_aix(self):
hwclock = 'localtime' if (not os.path.isfile('/etc/environment')): hwclock = 'UTC' with patch.dict(timezone.__grains__, {'os_family': ['AIX']}): assert (timezone.get_hwclock() == hwclock)
'Test set hwclock on AIX and NILinuxRT :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) def test_set_hwclock_aix_nilinuxrt(self):
for osfamily in ['AIX', 'NILinuxRT']: with patch.dict(timezone.__grains__, {'os_family': osfamily}): with self.assertRaises(SaltInvocationError): assert timezone.set_hwclock('forty two') assert timezone.set_hwclock('UTC')
'Test set hwclock on Solaris :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) @patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE')) def test_set_hwclock_solaris(self):
with patch.dict(timezone.__grains__, {'os_family': ['Solaris'], 'cpuarch': 'x86'}): with self.assertRaises(SaltInvocationError): assert timezone.set_hwclock('forty two') assert timezone.set_hwclock('UTC') (name, args, kwargs) = timezone.__salt__['cmd.retcode'].mock_calls[0] assert (args == (['rtc', '-z', 'GMT'],)) assert (kwargs == {'python_shell': False})
'Test set hwclock on arch :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) @patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE')) def test_set_hwclock_arch(self):
with patch.dict(timezone.__grains__, {'os_family': ['Arch']}): assert timezone.set_hwclock('UTC') (name, args, kwargs) = timezone.__salt__['cmd.retcode'].mock_calls[0] assert (args == (['timezonectl', 'set-local-rtc', 'false'],)) assert (kwargs == {'python_shell': False})
'Test set hwclock on RedHat :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) @patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE')) def test_set_hwclock_redhat(self):
with patch.dict(timezone.__grains__, {'os_family': ['RedHat']}): assert timezone.set_hwclock('UTC') (name, args, kwargs) = timezone.__salt__['file.sed'].mock_calls[0] assert (args == ('/etc/sysconfig/clock', '^ZONE=.*', 'ZONE="TEST_TIMEZONE"'))
'Test set hwclock on SUSE :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) @patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE')) def test_set_hwclock_suse(self):
with patch.dict(timezone.__grains__, {'os_family': ['Suse']}): assert timezone.set_hwclock('UTC') (name, args, kwargs) = timezone.__salt__['file.sed'].mock_calls[0] assert (args == ('/etc/sysconfig/clock', '^TIMEZONE=.*', 'TIMEZONE="TEST_TIMEZONE"'))
'Test set hwclock on Debian :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) @patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE')) def test_set_hwclock_debian(self):
with patch.dict(timezone.__grains__, {'os_family': ['Debian']}): assert timezone.set_hwclock('UTC') (name, args, kwargs) = timezone.__salt__['file.sed'].mock_calls[0] assert (args == ('/etc/default/rcS', '^UTC=.*', 'UTC=yes')) assert timezone.set_hwclock('localtime') (name, args, kwargs) = timezone.__salt__['file.sed'].mock_calls[1] assert (args == ('/etc/default/rcS', '^UTC=.*', 'UTC=no'))
'Test set hwclock on Gentoo :return:'
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows') @patch('salt.utils.path.which', MagicMock(return_value=False)) @patch('os.path.exists', MagicMock(return_value=True)) @patch('os.unlink', MagicMock()) @patch('os.symlink', MagicMock()) @patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE')) def test_set_hwclock_gentoo(self):
with patch.dict(timezone.__grains__, {'os_family': ['Gentoo']}): with self.assertRaises(SaltInvocationError): timezone.set_hwclock('forty two') timezone.set_hwclock('UTC') (name, args, kwargs) = timezone.__salt__['file.sed'].mock_calls[0] assert (args == ('/etc/conf.d/hwclock', '^clock=.*', 'clock="UTC"')) timezone.set_hwclock('localtime') (name, args, kwargs) = timezone.__salt__['file.sed'].mock_calls[1] assert (args == ('/etc/conf.d/hwclock', '^clock=.*', 'clock="local"'))
'Mock of create method'
@staticmethod def create(userid, tenantid):
cr_ec2 = MockEC2() cr_ec2.tenant_id = tenantid cr_ec2.user_id = userid return cr_ec2
'Mock of delete method'
def delete(self, userid, accesskey):
self.access = accesskey self.user_id = userid return True
'Mock of get method'
@staticmethod def get(user_id, access, profile, **connection_args):
cr_ec2 = MockEC2() cr_ec2.profile = profile cr_ec2.access = access cr_ec2.user_id = user_id cr_ec2.connection_args = connection_args return cr_ec2
'Mock of list method'
@staticmethod def list(user_id):
cr_ec2 = MockEC2() cr_ec2.user_id = user_id return [cr_ec2]
'Mock of list method'
@staticmethod def list():
return [MockEndpoints()]
'Mock of create method'
@staticmethod def create(region, service_id, publicurl, adminurl, internalurl):
return (region, service_id, publicurl, adminurl, internalurl)
'Mock of delete method'
@staticmethod def delete(id):
return id
'Mock of create method'
@staticmethod def create(name, service_type, description):
service = MockServices() service.id = '005' service.name = name service.description = description service.type = service_type return service
'Mock of get method'
def get(self, service_id):
service = MockServices() if (self.flag == 1): service.id = 'asd' return [service] elif (self.flag == 2): service.id = service_id return service return [service]
'Mock of list method'
def list(self):
service = MockServices() if (self.flag == 1): service.id = 'asd' return [service] return [service]
'Mock of delete method'
@staticmethod def delete(service_id):
return service_id
'Mock of create method'
@staticmethod def create(name):
return name
'Mock of get method'
def get(self, role_id):
role = MockRoles() if (self.flag == 1): role.id = None return role role.id = role_id return role
'Mock of list method'
@staticmethod def list():
return [MockRoles()]
'Mock of delete method'
@staticmethod def delete(role):
return role
'Mock of add_user_role method'
@staticmethod def add_user_role(user_id, role_id, tenant_id):
return (user_id, role_id, tenant_id)
'Mock of remove_user_role method'
@staticmethod def remove_user_role(user_id, role_id, tenant_id):
return (user_id, role_id, tenant_id)
'Mock of roles_for_user method'
@staticmethod def roles_for_user(user, tenant):
role = MockRoles() role.user_id = user role.tenant_id = tenant return [role]
'Mock of create method'
@staticmethod def create(name, description, enabled):
tenant = MockTenants() tenant.name = name tenant.description = description tenant.enabled = enabled return tenant
'Mock of get method'
def get(self, tenant_id):
tenant = MockTenants() if (self.flag == 1): tenant.id = None return tenant tenant.id = tenant_id return tenant
'Mock of list method'
@staticmethod def list():
return [MockTenants()]
'Mock of delete method'
@staticmethod def delete(tenant_id):
return tenant_id
'Mock of get_token method'
def get_token(self):
return {'id': self.id, 'expires': self.expires, 'user_id': self.user_id, 'tenant_id': self.tenant_id}
'Mock of create method'
def create(self, name, password, email, tenant_id, enabled):
user = MockUsers() user.name = name user.password = password user.email = email user.enabled = enabled self.tenant_id = tenant_id return user
'Mock of get method'
def get(self, user_id):
user = MockUsers() if (self.flag == 1): user.id = None return user user.id = user_id return user
'Mock of list method'
@staticmethod def list():
return [MockUsers()]
'Mock of delete method'
@staticmethod def delete(user_id):
return user_id
'Mock of update method'
@staticmethod def update(user, name, email, enabled):
return (user, name, email, enabled)
'Mock of update_password method'
@staticmethod def update_password(user, password):
return (user, password)
'Mock of Client method'
def Client(self, **kwargs):
if (self.flag == 1): raise Unauthorized return True
'Test if it create EC2-compatible credentials for user per tenant'
def test_ec2_credentials_create(self):
self.assertDictEqual(keystone.ec2_credentials_create(), {'Error': 'Could not resolve User ID'}) self.assertDictEqual(keystone.ec2_credentials_create(user_id='salt'), {'Error': 'Could not resolve Tenant ID'}) self.assertDictEqual(keystone.ec2_credentials_create(user_id='salt', tenant_id='72278'), {'access': '', 'tenant_id': '72278', 'secret': '', 'user_id': 'salt'})
'Test if it delete EC2-compatible credentials'
def test_ec2_credentials_delete(self):
self.assertDictEqual(keystone.ec2_credentials_delete(), {'Error': 'Could not resolve User ID'}) self.assertEqual(keystone.ec2_credentials_delete(user_id='salt', access_key='72278'), 'ec2 key "72278" deleted under user id "salt"')
'Test if it return ec2_credentials for a user (keystone ec2-credentials-get)'
def test_ec2_credentials_get(self):
self.assertDictEqual(keystone.ec2_credentials_get(), {'Error': 'Unable to resolve user id'}) self.assertDictEqual(keystone.ec2_credentials_get(user_id='salt'), {'Error': 'Access key is required'}) self.assertDictEqual(keystone.ec2_credentials_get(user_id='salt', access='72278', profile='openstack1'), {'salt': {'access': '72278', 'secret': '', 'tenant': '', 'user_id': 'salt'}})
'Test if it return a list of ec2_credentials for a specific user (keystone ec2-credentials-list)'
def test_ec2_credentials_list(self):
self.assertDictEqual(keystone.ec2_credentials_list(), {'Error': 'Unable to resolve user id'}) self.assertDictEqual(keystone.ec2_credentials_list(user_id='salt', profile='openstack1'), {'salt': {'access': '', 'secret': '', 'tenant_id': '', 'user_id': 'salt'}})
'Test if it return a specific endpoint (keystone endpoint-get)'
def test_endpoint_get(self):
self.assertDictEqual(keystone.endpoint_get('nova', 'RegionOne', profile='openstack'), {'Error': 'Could not find the specified service'}) ret = {'Error': 'Could not find endpoint for the specified service'} MockServices.flag = 1 self.assertDictEqual(keystone.endpoint_get('iptables', 'RegionOne', profile='openstack'), ret) MockServices.flag = 0 self.assertDictEqual(keystone.endpoint_get('iptables', 'RegionOne', profile='openstack'), {'adminurl': 'adminurl', 'id': '007', 'internalurl': 'internalurl', 'publicurl': 'publicurl', 'region': 'RegionOne', 'service_id': '117'})
'Test if it return a list of available endpoints (keystone endpoints-list)'
def test_endpoint_list(self):
self.assertDictEqual(keystone.endpoint_list(profile='openstack1'), {'007': {'adminurl': 'adminurl', 'id': '007', 'internalurl': 'internalurl', 'publicurl': 'publicurl', 'region': 'RegionOne', 'service_id': '117'}})
'Test if it create an endpoint for an Openstack service'
def test_endpoint_create(self):
self.assertDictEqual(keystone.endpoint_create('nova'), {'Error': 'Could not find the specified service'}) MockServices.flag = 2 self.assertDictEqual(keystone.endpoint_create('iptables', 'http://public/url', 'http://internal/url', 'http://adminurl/url', 'RegionOne'), {'adminurl': 'adminurl', 'id': '007', 'internalurl': 'internalurl', 'publicurl': 'publicurl', 'region': 'RegionOne', 'service_id': '117'})
'Test if it delete an endpoint for an Openstack service'
def test_endpoint_delete(self):
ret = {'Error': 'Could not find any endpoints for the service'} self.assertDictEqual(keystone.endpoint_delete('nova', 'RegionOne'), ret) with patch.object(keystone, 'endpoint_get', MagicMock(side_effect=[{'id': '117'}, None])): self.assertTrue(keystone.endpoint_delete('iptables', 'RegionOne'))
'Test if it create named role'
def test_role_create(self):
self.assertDictEqual(keystone.role_create('nova'), {'Error': 'Role "nova" already exists'}) self.assertDictEqual(keystone.role_create('iptables'), {'Error': 'Unable to resolve role id'})
'Test if it delete a role (keystone role-delete)'
def test_role_delete(self):
self.assertDictEqual(keystone.role_delete(), {'Error': 'Unable to resolve role id'}) self.assertEqual(keystone.role_delete('iptables'), 'Role ID iptables deleted')
'Test if it return a specific roles (keystone role-get)'
def test_role_get(self):
self.assertDictEqual(keystone.role_get(), {'Error': 'Unable to resolve role id'}) self.assertDictEqual(keystone.role_get(name='nova'), {'nova': {'id': '113', 'name': 'nova'}})
'Test if it return a list of available roles (keystone role-list)'
def test_role_list(self):
self.assertDictEqual(keystone.role_list(), {'nova': {'id': '113', 'name': 'nova', 'tenant_id': 'a1a1', 'user_id': '446'}})
'Test if it add service to Keystone service catalog'
def test_service_create(self):
MockServices.flag = 2 self.assertDictEqual(keystone.service_create('nova', 'compute', 'OpenStack Service'), {'iptables': {'description': 'description', 'id': '005', 'name': 'iptables', 'type': 'type'}})
'Test if it delete a service from Keystone service catalog'
def test_service_delete(self):
self.assertEqual(keystone.service_delete('iptables'), 'Keystone service ID "iptables" deleted')
'Test if it return a list of available services (keystone services-list)'
def test_service_get(self):
MockServices.flag = 0 self.assertDictEqual(keystone.service_get(), {'Error': 'Unable to resolve service id'}) MockServices.flag = 2 self.assertDictEqual(keystone.service_get(service_id='c965'), {'iptables': {'description': 'description', 'id': 'c965', 'name': 'iptables', 'type': 'type'}})
'Test if it return a list of available services (keystone services-list)'
def test_service_list(self):
MockServices.flag = 0 self.assertDictEqual(keystone.service_list(profile='openstack1'), {'iptables': {'description': 'description', 'id': '117', 'name': 'iptables', 'type': 'type'}})
'Test if it create a keystone tenant'
def test_tenant_create(self):
self.assertDictEqual(keystone.tenant_create('nova'), {'nova': {'description': 'description', 'id': '446', 'name': 'nova', 'enabled': 'True'}})
'Test if it delete a tenant (keystone tenant-delete)'
def test_tenant_delete(self):
self.assertDictEqual(keystone.tenant_delete(), {'Error': 'Unable to resolve tenant id'}) self.assertEqual(keystone.tenant_delete('nova'), 'Tenant ID nova deleted')
'Test if it return a specific tenants (keystone tenant-get)'
def test_tenant_get(self):
self.assertDictEqual(keystone.tenant_get(), {'Error': 'Unable to resolve tenant id'}) self.assertDictEqual(keystone.tenant_get(tenant_id='446'), {'nova': {'description': 'description', 'id': '446', 'name': 'nova', 'enabled': 'True'}})
'Test if it return a list of available tenants (keystone tenants-list)'
def test_tenant_list(self):
self.assertDictEqual(keystone.tenant_list(), {'nova': {'description': 'description', 'id': '446', 'name': 'nova', 'enabled': 'True'}})
'Test if it update a tenant\'s information (keystone tenant-update)'
def test_tenant_update(self):
self.assertDictEqual(keystone.tenant_update(), {'Error': 'Unable to resolve tenant id'})
'Test if it return the configured tokens (keystone token-get)'
def test_token_get(self):
self.assertDictEqual(keystone.token_get(), {'expires': 'No', 'id': '446', 'tenant_id': 'ae04', 'user_id': 'admin'})
'Test if it return a list of available users (keystone user-list)'
def test_user_list(self):
self.assertDictEqual(keystone.user_list(), {'nova': {'name': 'nova', 'tenant_id': 'a1a1', 'enabled': 'True', 'id': '446', 'password': 'salt', 'email': '[email protected]'}})
'Test if it return a specific users (keystone user-get)'
def test_user_get(self):
self.assertDictEqual(keystone.user_get(), {'Error': 'Unable to resolve user id'}) self.assertDictEqual(keystone.user_get(user_id='446'), {'nova': {'name': 'nova', 'tenant_id': 'a1a1', 'enabled': 'True', 'id': '446', 'password': 'salt', 'email': '[email protected]'}})
'Test if it create a user (keystone user-create)'
def test_user_create(self):
self.assertDictEqual(keystone.user_create(name='nova', password='salt', email='[email protected]', tenant_id='a1a1'), {'nova': {'name': 'nova', 'tenant_id': 'a1a1', 'enabled': 'True', 'id': '446', 'password': 'salt', 'email': '[email protected]'}})
'Test if it delete a user (keystone user-delete)'
def test_user_delete(self):
self.assertDictEqual(keystone.user_delete(), {'Error': 'Unable to resolve user id'}) self.assertEqual(keystone.user_delete('nova'), 'User ID nova deleted')
'Test if it update a user\'s information (keystone user-update)'
def test_user_update(self):
self.assertDictEqual(keystone.user_update(), {'Error': 'Unable to resolve user id'}) self.assertEqual(keystone.user_update('nova'), 'Info updated for user ID nova')
'Test if it verify a user\'s password'
def test_user_verify_password(self):
mock = MagicMock(return_value='http://127.0.0.1:35357/v2.0') with patch.dict(keystone.__salt__, {'config.option': mock}): self.assertDictEqual(keystone.user_verify_password(), {'Error': 'Unable to resolve user name'}) self.assertTrue(keystone.user_verify_password(user_id='446', name='nova')) MockClient.flag = 1 self.assertFalse(keystone.user_verify_password(user_id='446', name='nova'))
'Test if it update a user\'s password (keystone user-password-update)'
def test_user_password_update(self):
self.assertDictEqual(keystone.user_password_update(), {'Error': 'Unable to resolve user id'}) self.assertEqual(keystone.user_password_update('nova'), 'Password updated for user ID nova')
'Test if it add role for user in tenant (keystone user-role-add)'
def test_user_role_add(self):
self.assertEqual(keystone.user_role_add(user='nova', tenant='nova', role='nova'), '"nova" role added for user "nova" for "nova" tenant/project') MockRoles.flag = 1 self.assertDictEqual(keystone.user_role_add(user='nova', tenant='nova', role='nova'), {'Error': 'Unable to resolve role id'}) MockTenants.flag = 1 self.assertDictEqual(keystone.user_role_add(user='nova', tenant='nova'), {'Error': 'Unable to resolve tenant/project id'}) MockUsers.flag = 1 self.assertDictEqual(keystone.user_role_add(user='nova'), {'Error': 'Unable to resolve user id'})
'Test if it add role for user in tenant (keystone user-role-add)'
def test_user_role_remove(self):
MockUsers.flag = 1 self.assertDictEqual(keystone.user_role_remove(user='nova'), {'Error': 'Unable to resolve user id'}) MockUsers.flag = 0 MockTenants.flag = 1 self.assertDictEqual(keystone.user_role_remove(user='nova', tenant='nova'), {'Error': 'Unable to resolve tenant/project id'}) MockTenants.flag = 0 MockRoles.flag = 1 self.assertDictEqual(keystone.user_role_remove(user='nova', tenant='nova', role='nova'), {'Error': 'Unable to resolve role id'}) ret = '"nova" role removed for user "nova" under "nova" tenant' MockRoles.flag = 0 self.assertEqual(keystone.user_role_remove(user='nova', tenant='nova', role='nova'), ret)
'Test if it return a list of available user_roles (keystone user-roles-list)'
def test_user_role_list(self):
self.assertDictEqual(keystone.user_role_list(user='nova'), {'Error': 'Unable to resolve user or tenant/project id'}) self.assertDictEqual(keystone.user_role_list(user_name='nova', tenant_name='nova'), {'nova': {'id': '113', 'name': 'nova', 'tenant_id': '446', 'user_id': '446'}})
'Mock of listServers method'
def listServers(self, backend):
self.backend = backend return 'Name: server01 Status: UP Weight: 1 bIn: 22 bOut: 12\nName: server02 Status: MAINT Weight: 2 bIn: 0 bOut: 0'
'Mock of enableServer method'
def enableServer(self, server, backend):
self.backend = backend self.server = server return 'server enabled'
'Mock of disableServer method'
def disableServer(self, server, backend):
self.backend = backend self.server = server return 'server disabled'