How to use _fake_v2_response method in tempest

Best Python code snippet using tempest_python

test_account_generator.py

Source:test_account_generator.py Github

copy

Full Screen

1# Copyright (c) 2016 Hewlett-Packard Enterprise Development Company, L.P.2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may4# not use this file except in compliance with the License. You may obtain5# a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14import fixtures15import mock16from oslo_config import cfg17from tempest.cmd import account_generator18from tempest import config19from tempest.tests import base20from tempest.tests import fake_config21from tempest.tests.lib import fake_identity22class FakeOpts(object):23 def __init__(self, version=3):24 self.os_username = 'fake_user'25 self.os_password = 'fake_password'26 self.os_project_name = 'fake_project_name'27 self.os_tenant_name = None28 self.os_domain_name = 'fake_domain'29 self.tag = 'fake'30 self.concurrency = 231 self.with_admin = True32 self.identity_version = version33 self.accounts = 'fake_accounts.yml'34class MockHelpersMixin(object):35 def mock_config_and_opts(self, identity_version):36 self.useFixture(fake_config.ConfigFixture())37 self.patchobject(config, 'TempestConfigPrivate',38 fake_config.FakePrivate)39 self.opts = FakeOpts(version=identity_version)40 def mock_resource_creation(self):41 fake_resource = dict(id='id', name='name')42 self.user_create_fixture = self.useFixture(fixtures.MockPatch(43 self.cred_client + '.create_user', return_value=fake_resource))44 self.useFixture(fixtures.MockPatch(45 self.cred_client + '.create_project',46 return_value=fake_resource))47 self.useFixture(fixtures.MockPatch(48 self.cred_client + '.assign_user_role'))49 self.useFixture(fixtures.MockPatch(50 self.cred_client + '._check_role_exists',51 return_value=fake_resource))52 self.useFixture(fixtures.MockPatch(53 self.dynamic_creds + '._create_network',54 return_value=fake_resource))55 self.useFixture(fixtures.MockPatch(56 self.dynamic_creds + '._create_subnet',57 return_value=fake_resource))58 self.useFixture(fixtures.MockPatch(59 self.dynamic_creds + '._create_router',60 return_value=fake_resource))61 self.useFixture(fixtures.MockPatch(62 self.dynamic_creds + '._add_router_interface',63 return_value=fake_resource))64 def mock_domains(self):65 fake_domain_list = {'domains': [{'id': 'fake_domain',66 'name': 'Fake_Domain'}]}67 self.useFixture(fixtures.MockPatch(''.join([68 'tempest.lib.services.identity.v3.domains_client.'69 'DomainsClient.list_domains']),70 return_value=fake_domain_list))71 self.useFixture(fixtures.MockPatch(72 self.cred_client + '.assign_user_role_on_domain'))73class TestAccountGeneratorV2(base.TestCase, MockHelpersMixin):74 identity_version = 275 identity_response = fake_identity._fake_v2_response76 def setUp(self):77 super(TestAccountGeneratorV2, self).setUp()78 self.mock_config_and_opts(self.identity_version)79 self.useFixture(fixtures.MockPatch(80 'tempest.lib.auth.AuthProvider.set_auth',81 return_value=self.identity_response))82 def test_get_credential_provider(self):83 cp = account_generator.get_credential_provider(self.opts)84 admin_creds = cp.default_admin_creds85 self.assertEqual(self.opts.tag, cp.name)86 self.assertIn(str(self.opts.identity_version), cp.identity_version)87 self.assertEqual(self.opts.os_username, admin_creds.username)88 self.assertEqual(self.opts.os_project_name, admin_creds.tenant_name)89 self.assertEqual(self.opts.os_password, admin_creds.password)90 self.assertFalse(hasattr(admin_creds, 'domain_name'))91 def test_get_credential_provider_with_tenant(self):92 self.opts.os_project_name = None93 self.opts.os_tenant_name = 'fake_tenant'94 cp = account_generator.get_credential_provider(self.opts)95 admin_creds = cp.default_admin_creds96 self.assertEqual(self.opts.os_tenant_name, admin_creds.tenant_name)97class TestAccountGeneratorV3(TestAccountGeneratorV2):98 identity_version = 399 identity_response = fake_identity._fake_v3_response100 def setUp(self):101 super(TestAccountGeneratorV3, self).setUp()102 fake_domain_list = {'domains': [{'id': 'fake_domain'}]}103 self.useFixture(fixtures.MockPatch(''.join([104 'tempest.lib.services.identity.v3.domains_client.'105 'DomainsClient.list_domains']),106 return_value=fake_domain_list))107 def test_get_credential_provider(self):108 cp = account_generator.get_credential_provider(self.opts)109 admin_creds = cp.default_admin_creds110 self.assertEqual(self.opts.tag, cp.name)111 self.assertIn(str(self.opts.identity_version), cp.identity_version)112 self.assertEqual(self.opts.os_username, admin_creds.username)113 self.assertEqual(self.opts.os_project_name, admin_creds.tenant_name)114 self.assertEqual(self.opts.os_password, admin_creds.password)115 self.assertEqual(self.opts.os_domain_name, admin_creds.domain_name)116 def test_get_credential_provider_without_domain(self):117 self.opts.os_domain_name = None118 cp = account_generator.get_credential_provider(self.opts)119 admin_creds = cp.default_admin_creds120 self.assertIsNotNone(admin_creds.domain_name)121class TestGenerateResourcesV2(base.TestCase, MockHelpersMixin):122 identity_version = 2123 identity_response = fake_identity._fake_v2_response124 cred_client = 'tempest.lib.common.cred_client.V2CredsClient'125 dynamic_creds = 'tempest.common.dynamic_creds.DynamicCredentialProvider'126 def setUp(self):127 super(TestGenerateResourcesV2, self).setUp()128 self.mock_config_and_opts(self.identity_version)129 self.useFixture(fixtures.MockPatch(130 'tempest.lib.auth.AuthProvider.set_auth',131 return_value=self.identity_response))132 self.cred_provider = account_generator.get_credential_provider(133 self.opts)134 self.mock_resource_creation()135 def test_generate_resources_no_admin(self):136 cfg.CONF.set_default('swift', False, group='service_available')137 cfg.CONF.set_default('heat', False, group='service_available')138 cfg.CONF.set_default('operator_role', 'fake_operator',139 group='object-storage')140 cfg.CONF.set_default('reseller_admin_role', 'fake_reseller',141 group='object-storage')142 cfg.CONF.set_default('stack_owner_role', 'fake_owner',143 group='orchestration')144 resources = account_generator.generate_resources(145 self.cred_provider, admin=False)146 resource_types = [k for k, _ in resources]147 # No admin, no heat, no swift, expect two credentials only148 self.assertEqual(2, len(resources))149 # Ensure create_user was invoked twice (two distinct users)150 self.assertEqual(2, self.user_create_fixture.mock.call_count)151 self.assertIn('primary', resource_types)152 self.assertIn('alt', resource_types)153 self.assertNotIn('admin', resource_types)154 self.assertNotIn(['fake_operator'], resource_types)155 self.assertNotIn(['fake_reseller'], resource_types)156 self.assertNotIn(['fake_owner'], resource_types)157 for resource in resources:158 self.assertIsNotNone(resource[1].network)159 self.assertIsNotNone(resource[1].router)160 self.assertIsNotNone(resource[1].subnet)161 def test_generate_resources_admin(self):162 cfg.CONF.set_default('swift', False, group='service_available')163 cfg.CONF.set_default('heat', False, group='service_available')164 cfg.CONF.set_default('operator_role', 'fake_operator',165 group='object-storage')166 cfg.CONF.set_default('reseller_admin_role', 'fake_reseller',167 group='object-storage')168 cfg.CONF.set_default('stack_owner_role', 'fake_owner',169 group='orchestration')170 resources = account_generator.generate_resources(171 self.cred_provider, admin=True)172 resource_types = [k for k, _ in resources]173 # Admin, no heat, no swift, expect three credentials only174 self.assertEqual(3, len(resources))175 # Ensure create_user was invoked 3 times (3 distinct users)176 self.assertEqual(3, self.user_create_fixture.mock.call_count)177 self.assertIn('primary', resource_types)178 self.assertIn('alt', resource_types)179 self.assertIn('admin', resource_types)180 self.assertNotIn(['fake_operator'], resource_types)181 self.assertNotIn(['fake_reseller'], resource_types)182 self.assertNotIn(['fake_owner'], resource_types)183 for resource in resources:184 self.assertIsNotNone(resource[1].network)185 self.assertIsNotNone(resource[1].router)186 self.assertIsNotNone(resource[1].subnet)187 def test_generate_resources_swift_heat_admin(self):188 cfg.CONF.set_default('swift', True, group='service_available')189 cfg.CONF.set_default('heat', True, group='service_available')190 cfg.CONF.set_default('operator_role', 'fake_operator',191 group='object-storage')192 cfg.CONF.set_default('reseller_admin_role', 'fake_reseller',193 group='object-storage')194 cfg.CONF.set_default('stack_owner_role', 'fake_owner',195 group='orchestration')196 resources = account_generator.generate_resources(197 self.cred_provider, admin=True)198 resource_types = [k for k, _ in resources]199 # all options on, expect six credentials200 self.assertEqual(6, len(resources))201 # Ensure create_user was invoked 6 times (6 distinct users)202 self.assertEqual(6, self.user_create_fixture.mock.call_count)203 self.assertIn('primary', resource_types)204 self.assertIn('alt', resource_types)205 self.assertIn('admin', resource_types)206 self.assertIn(['fake_operator'], resource_types)207 self.assertIn(['fake_reseller'], resource_types)208 self.assertIn(['fake_owner', 'fake_operator'], resource_types)209 for resource in resources:210 self.assertIsNotNone(resource[1].network)211 self.assertIsNotNone(resource[1].router)212 self.assertIsNotNone(resource[1].subnet)213class TestGenerateResourcesV3(TestGenerateResourcesV2):214 identity_version = 3215 identity_response = fake_identity._fake_v3_response216 cred_client = 'tempest.lib.common.cred_client.V3CredsClient'217 def setUp(self):218 self.mock_domains()219 super(TestGenerateResourcesV3, self).setUp()220class TestDumpAccountsV2(base.TestCase, MockHelpersMixin):221 identity_version = 2222 identity_response = fake_identity._fake_v2_response223 cred_client = 'tempest.lib.common.cred_client.V2CredsClient'224 dynamic_creds = 'tempest.common.dynamic_creds.DynamicCredentialProvider'225 domain_is_in = False226 def setUp(self):227 super(TestDumpAccountsV2, self).setUp()228 self.mock_config_and_opts(self.identity_version)229 self.useFixture(fixtures.MockPatch(230 'tempest.lib.auth.AuthProvider.set_auth',231 return_value=self.identity_response))232 self.cred_provider = account_generator.get_credential_provider(233 self.opts)234 self.mock_resource_creation()235 cfg.CONF.set_default('swift', True, group='service_available')236 cfg.CONF.set_default('heat', True, group='service_available')237 self.resources = account_generator.generate_resources(238 self.cred_provider, admin=True)239 def test_dump_accounts(self):240 self.useFixture(fixtures.MockPatch('os.path.exists',241 return_value=False))242 mocked_open = mock.mock_open()243 with mock.patch('{}.open'.format(account_generator.__name__),244 mocked_open, create=True):245 with mock.patch('yaml.safe_dump') as yaml_dump_mock:246 account_generator.setup_logging()247 account_generator.dump_accounts(self.resources,248 self.opts.identity_version,249 self.opts.accounts)250 mocked_open.assert_called_once_with(self.opts.accounts, 'w')251 handle = mocked_open()252 # Ordered args in [0], keyword args in [1]253 accounts, f = yaml_dump_mock.call_args[0]254 self.assertEqual(handle, f)255 self.assertEqual(6, len(accounts))256 if self.domain_is_in:257 self.assertIn('domain_name', accounts[0].keys())258 else:259 self.assertNotIn('domain_name', accounts[0].keys())260 self.assertEqual(1, len([x for x in accounts if261 x.get('types') == ['admin']]))262 self.assertEqual(3, len([x for x in accounts if 'roles' in x]))263 for account in accounts:264 self.assertIn('resources', account)265 self.assertIn('network', account.get('resources'))266 def test_dump_accounts_existing_file(self):267 self.useFixture(fixtures.MockPatch('os.path.exists',268 return_value=True))269 rename_mock = self.useFixture(fixtures.MockPatch('os.rename')).mock270 backup_file = '.'.join((self.opts.accounts, 'bak'))271 mocked_open = mock.mock_open()272 with mock.patch('{}.open'.format(account_generator.__name__),273 mocked_open, create=True):274 with mock.patch('yaml.safe_dump') as yaml_dump_mock:275 account_generator.setup_logging()276 account_generator.dump_accounts(self.resources,277 self.opts.identity_version,278 self.opts.accounts)279 rename_mock.assert_called_once_with(self.opts.accounts, backup_file)280 mocked_open.assert_called_once_with(self.opts.accounts, 'w')281 handle = mocked_open()282 # Ordered args in [0], keyword args in [1]283 accounts, f = yaml_dump_mock.call_args[0]284 self.assertEqual(handle, f)285 self.assertEqual(6, len(accounts))286 if self.domain_is_in:287 self.assertIn('domain_name', accounts[0].keys())288 else:289 self.assertNotIn('domain_name', accounts[0].keys())290 self.assertEqual(1, len([x for x in accounts if291 x.get('types') == ['admin']]))292 self.assertEqual(3, len([x for x in accounts if 'roles' in x]))293 for account in accounts:294 self.assertIn('resources', account)295 self.assertIn('network', account.get('resources'))296class TestDumpAccountsV3(TestDumpAccountsV2):297 identity_version = 3298 identity_response = fake_identity._fake_v3_response299 cred_client = 'tempest.lib.common.cred_client.V3CredsClient'300 domain_is_in = True301 def setUp(self):302 self.mock_domains()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful