def test_is_not_multi_user(self): self.test_accounts = [self.test_accounts[0]] self.useFixture( mockpatch.Patch('tempest.common.accounts.read_accounts_yaml', return_value=self.test_accounts)) test_accounts_class = accounts.Accounts('test_name') self.assertFalse(test_accounts_class.is_multi_user())
def test_get_hash_dict(self): test_account_class = accounts.Accounts('test_name') hash_dict = test_account_class.get_hash_dict(self.test_accounts) hash_list = self._get_hash_list(self.test_accounts) for hash in hash_list: self.assertIn(hash, hash_dict.keys()) self.assertIn(hash_dict[hash], self.test_accounts)
def test_create_hash_file_no_previous_file(self): # Emulate the lock not existing on the filesystem self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False)) with mock.patch('__builtin__.open', mock.mock_open(), create=True): test_account_class = accounts.Accounts('v2', 'test_name') res = test_account_class._create_hash_file('12345') self.assertTrue(res, "_create_hash_file should return True if the " "pseudo-lock doesn't already exist")
def test_get_free_hash_no_free_accounts(self, lock_mock): hash_list = self._get_hash_list(self.test_accounts) # Emulate pre-existing lock dir self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True)) # Emulate all lcoks in list are in use self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) test_account_class = accounts.Accounts('test_name') self.assertRaises(exceptions.InvalidConfiguration, test_account_class._get_free_hash, hash_list)
def test_get_hash(self): self.stubs.Set(token_client.TokenClientJSON, 'raw_request', fake_identity._fake_v2_response) test_account_class = accounts.Accounts('test_name') hash_list = self._get_hash_list(self.test_accounts) test_cred_dict = self.test_accounts[3] test_creds = auth.get_credentials(**test_cred_dict) results = test_account_class.get_hash(test_creds) self.assertEqual(hash_list[3], results)
def test_create_hash_file_previous_file(self): # Emulate the lock existing on the filesystem self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) with mock.patch('six.moves.builtins.open', mock.mock_open(), create=True): test_account_class = accounts.Accounts('v2', 'test_name') res = test_account_class._create_hash_file('12345') self.assertFalse(res, "_create_hash_file should return False if the " "pseudo-lock file already exists")
def test_remove_hash_last_account(self, lock_mock): hash_list = self._get_hash_list(self.test_accounts) # Pretend the pseudo-lock is there self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) # Pretend the lock dir is empty self.useFixture(mockpatch.Patch('os.listdir', return_value=[])) test_account_class = accounts.Accounts('test_name') remove_mock = self.useFixture(mockpatch.Patch('os.remove')) rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir')) test_account_class.remove_hash(hash_list[2]) hash_path = os.path.join(accounts.CONF.lock_path, 'test_accounts', hash_list[2]) lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts') remove_mock.mock.assert_called_once_with(hash_path) rmdir_mock.mock.assert_called_once_with(lock_path)
def test_get_free_hash_no_previous_accounts(self, lock_mock): # Emulate no pre-existing lock self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False)) hash_list = self._get_hash_list(self.test_accounts) mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir')) self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False)) test_account_class = accounts.Accounts('test_name') with mock.patch('__builtin__.open', mock.mock_open(), create=True) as open_mock: test_account_class._get_free_hash(hash_list) lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts', hash_list[0]) open_mock.assert_called_once_with(lock_path, 'w') mkdir_path = os.path.join(accounts.CONF.lock_path, 'test_accounts') mkdir_mock.mock.assert_called_once_with(mkdir_path)
def test_networks_returned_with_creds(self): self.useFixture(mockpatch.Patch( 'tempest.common.accounts.read_accounts_yaml', return_value=self.test_accounts)) test_accounts_class = accounts.Accounts('v2', 'test_name') with mock.patch('tempest.services.compute.json.networks_client.' 'NetworksClientJSON.list_networks', return_value=[{'name': 'network-2', 'id': 'fake-id'}]): creds = test_accounts_class.get_creds_by_roles(['role-7']) self.assertTrue(isinstance(creds, cred_provider.TestResources)) network = creds.network self.assertIsNotNone(network) self.assertIn('name', network) self.assertIn('id', network) self.assertEqual('fake-id', network['id']) self.assertEqual('network-2', network['name'])
def test__get_creds_by_roles_one_role(self): self.useFixture(mockpatch.Patch( 'tempest.common.accounts.read_accounts_yaml', return_value=self.test_accounts)) test_accounts_class = accounts.Accounts('v2', 'test_name') hashes = test_accounts_class.hash_dict['roles']['role4'] temp_hash = hashes[0] get_free_hash_mock = self.useFixture(mockpatch.PatchObject( test_accounts_class, '_get_free_hash', return_value=temp_hash)) # Test a single role returns all matching roles test_accounts_class._get_creds(roles=['role4']) calls = get_free_hash_mock.mock.mock_calls self.assertEqual(len(calls), 1) args = calls[0][1][0] for i in hashes: self.assertIn(i, args)
def get_isolated_credentials(name, network_resources=None, force_tenant_isolation=False): # If a test requires a new account to work, it can have it via forcing # tenant isolation. A new account will be produced only for that test. # In case admin credentials are not available for the account creation, # the test should be skipped else it would fail. if CONF.auth.allow_tenant_isolation or force_tenant_isolation: return isolated_creds.IsolatedCreds( name=name, network_resources=network_resources) else: if CONF.auth.locking_credentials_provider: # Most params are not relevant for pre-created accounts return accounts.Accounts(name=name) else: return accounts.NotLockingAccounts(name=name)
def is_alt_available(): # If dynamic credentials is enabled admin will be available if CONF.auth.use_dynamic_credentials: return True # Check whether test accounts file has the admin specified or not if (CONF.auth.test_accounts_file and os.path.isfile(CONF.auth.test_accounts_file)): check_accounts = accounts.Accounts(name='check_alt') else: check_accounts = accounts.NotLockingAccounts(name='check_alt') try: if not check_accounts.is_multi_user(): return False else: return True except exceptions.InvalidConfiguration: return False
def is_admin_available(): is_admin = True # If dynamic credentials is enabled admin will be available if CONF.auth.use_dynamic_credentials: return is_admin # Check whether test accounts file has the admin specified or not elif (CONF.auth.test_accounts_file and os.path.isfile(CONF.auth.test_accounts_file)): check_accounts = accounts.Accounts(name='check_admin') if not check_accounts.admin_available(): is_admin = False else: try: cred_provider.get_configured_credentials('identity_admin', fill_in=False) except exceptions.InvalidConfiguration: is_admin = False return is_admin
def test__get_creds_by_roles_list_role(self): self.useFixture(mockpatch.Patch( 'tempest.common.accounts.read_accounts_yaml', return_value=self.test_accounts)) test_accounts_class = accounts.Accounts('v2', 'test_name') hashes = test_accounts_class.hash_dict['roles']['role4'] hashes2 = test_accounts_class.hash_dict['roles']['role2'] hashes = list(set(hashes) & set(hashes2)) temp_hash = hashes[0] get_free_hash_mock = self.useFixture(mockpatch.PatchObject( test_accounts_class, '_get_free_hash', return_value=temp_hash)) # Test an intersection of multiple roles test_accounts_class._get_creds(roles=['role2', 'role4']) calls = get_free_hash_mock.mock.mock_calls self.assertEqual(len(calls), 1) args = calls[0][1][0] for i in hashes: self.assertIn(i, args)
def test__get_creds_by_roles_no_admin(self): self.useFixture(mockpatch.Patch( 'tempest.common.accounts.read_accounts_yaml', return_value=self.test_accounts)) test_accounts_class = accounts.Accounts('v2', 'test_name') hashes = list(test_accounts_class.hash_dict['creds'].keys()) admin_hashes = test_accounts_class.hash_dict['roles'][ cfg.CONF.identity.admin_role] temp_hash = hashes[0] get_free_hash_mock = self.useFixture(mockpatch.PatchObject( test_accounts_class, '_get_free_hash', return_value=temp_hash)) # Test an intersection of multiple roles test_accounts_class._get_creds() calls = get_free_hash_mock.mock.mock_calls self.assertEqual(len(calls), 1) args = calls[0][1][0] self.assertEqual(len(args), 10) for i in admin_hashes: self.assertNotIn(i, args)
def test_get_free_hash_some_in_use_accounts(self, lock_mock): # Emulate no pre-existing lock self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True)) hash_list = self._get_hash_list(self.test_accounts) test_account_class = accounts.Accounts('test_name') def _fake_is_file(path): # Fake isfile() to return that the path exists unless a specific # hash is in the path if hash_list[3] in path: return False return True self.stubs.Set(os.path, 'isfile', _fake_is_file) with mock.patch('__builtin__.open', mock.mock_open(), create=True) as open_mock: test_account_class._get_free_hash(hash_list) lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts', hash_list[3]) open_mock.assert_called_once_with(lock_path, 'w')
def get_isolated_credentials(name): # If a test requires a new account to work, it can have it via forcing # tenant isolation. A new account will be produced only for that test. # In case admin credentials are not available for the account creation, # the test should be skipped else it would fail. #if CONF.auth.allow_tenant_isolation or force_tenant_isolation: # return isolated_creds.IsolatedCreds( # name=name, # network_resources=network_resources, # identity_version=identity_version) #else: #CONF.auth.test_accounts_file means arrow.conf->[auth]->test_accounts_file #test_accounts_file = /usr/share/openstack-tempest-kilo/etc/accounts.yaml #We can use two kinds of configuration files : arrow.conf or account.yaml if (CONF.auth.test_accounts_file and os.path.isfile(CONF.auth.test_accounts_file)): # Most params are not relevant for pre-created accounts return accounts.Accounts(name=name, identity_version=identity_version) else: return accounts.NotLockingAccounts(name=name, identity_version=identity_version)
def get_credentials_provider(name, network_resources=None, force_tenant_isolation=False, identity_version=None): # If a test requires a new account to work, it can have it via forcing # dynamic credentials. A new account will be produced only for that test. # In case admin credentials are not available for the account creation, # the test should be skipped else it would fail. if CONF.auth.use_dynamic_credentials or force_tenant_isolation: return dynamic_creds.DynamicCredentialProvider( name=name, network_resources=network_resources, identity_version=identity_version) else: if (CONF.auth.test_accounts_file and os.path.isfile(CONF.auth.test_accounts_file)): # Most params are not relevant for pre-created accounts return accounts.Accounts(name=name, identity_version=identity_version) else: return accounts.NotLockingAccounts( name=name, identity_version=identity_version)
def test_networks_returned_with_creds(self): test_accounts = [{ 'username': '******', 'tenant_name': 'test_tenant13', 'password': '******', 'resources': { 'network': 'network-1' } }, { 'username': '******', 'tenant_name': 'test_tenant14', 'password': '******', 'roles': ['role-7', 'role-11'], 'resources': { 'network': 'network-2' } }] self.useFixture( mockpatch.Patch('tempest.common.accounts.read_accounts_yaml', return_value=test_accounts)) test_accounts_class = accounts.Accounts('v2', 'test_name') with mock.patch( 'tempest.services.compute.json.networks_client.' 'NetworksClient.list_networks', return_value={ 'networks': [{ 'name': 'network-2', 'id': 'fake-id', 'label': 'network-2' }] }): creds = test_accounts_class.get_creds_by_roles(['role-7']) self.assertTrue(isinstance(creds, cred_provider.TestResources)) network = creds.network self.assertIsNotNone(network) self.assertIn('name', network) self.assertIn('id', network) self.assertEqual('fake-id', network['id']) self.assertEqual('network-2', network['name'])
def test_is_multi_user(self): test_accounts_class = accounts.Accounts('test_name') self.assertTrue(test_accounts_class.is_multi_user())