Best Python code snippet using tempest_python
test_account_generator.py
Source:test_account_generator.py
...31 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()...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!