Best Python code snippet using tempest_python
isolated_creds.py
Source:isolated_creds.py
...170 if self.tempest_client:171 return tenant.get('id')172 else:173 return tenant.id174 def _create_network_resources(self, tenant_id):175 network = None176 subnet = None177 router = None178 data_utils.rand_name_root = data_utils.rand_name(self.name)179 network_name = data_utils.rand_name_root + "-network"180 network = self._create_network(network_name, tenant_id)181 try:182 subnet_name = data_utils.rand_name_root + "-subnet"183 subnet = self._create_subnet(subnet_name, tenant_id, network['id'])184 router_name = data_utils.rand_name_root + "-router"185 router = self._create_router(router_name, tenant_id)186 self._add_router_interface(router['id'], subnet['id'])187 except Exception:188 if router:189 self._clear_isolated_router(router['id'], router['name'])190 if subnet:191 self._clear_isolated_subnet(subnet['id'], subnet['name'])192 if network:193 self._clear_isolated_network(network['id'], network['name'])194 raise195 return network, subnet, router196 def _create_network(self, name, tenant_id):197 if self.tempest_client:198 resp, resp_body = self.network_admin_client.create_network(199 name, tenant_id=tenant_id)200 else:201 body = {'network': {'tenant_id': tenant_id, 'name': name}}202 resp_body = self.network_admin_client.create_network(body)203 return resp_body['network']204 def _create_subnet(self, subnet_name, tenant_id, network_id):205 if not self.tempest_client:206 body = {'subnet': {'name': subnet_name, 'tenant_id': tenant_id,207 'network_id': network_id, 'ip_version': 4}}208 base_cidr = netaddr.IPNetwork(self.config.network.tenant_network_cidr)209 mask_bits = self.config.network.tenant_network_mask_bits210 for subnet_cidr in base_cidr.subnet(mask_bits):211 try:212 if self.tempest_client:213 resp, resp_body = self.network_admin_client.create_subnet(214 network_id, str(subnet_cidr), name=subnet_name,215 tenant_id=tenant_id)216 else:217 body['subnet']['cidr'] = str(subnet_cidr)218 resp_body = self.network_admin_client.create_subnet(body)219 break220 except exceptions.BadRequest as e:221 if 'overlaps with another subnet' not in str(e):222 raise223 else:224 e = exceptions.BuildErrorException()225 e.message = 'Available CIDR for subnet creation could not be found'226 raise e227 return resp_body['subnet']228 def _create_router(self, router_name, tenant_id):229 external_net_id = dict(230 network_id=self.config.network.public_network_id)231 if self.tempest_client:232 resp, resp_body = self.network_admin_client.create_router(233 router_name,234 external_gateway_info=external_net_id,235 tenant_id=tenant_id)236 else:237 body = {'router': {'name': router_name, 'tenant_id': tenant_id,238 'external_gateway_info': external_net_id,239 'admin_state_up': True}}240 resp_body = self.network_admin_client.create_router(body)241 return resp_body['router']242 def _add_router_interface(self, router_id, subnet_id):243 if self.tempest_client:244 self.network_admin_client.add_router_interface_with_subnet_id(245 router_id, subnet_id)246 else:247 body = {'subnet_id': subnet_id}248 self.network_admin_client.add_interface_router(router_id, body)249 def get_primary_tenant(self):250 return self.isolated_creds.get('primary')[1]251 def get_primary_user(self):252 return self.isolated_creds.get('primary')[0]253 def get_alt_tenant(self):254 return self.isolated_creds.get('alt')[1]255 def get_alt_user(self):256 return self.isolated_creds.get('alt')[0]257 def get_admin_tenant(self):258 return self.isolated_creds.get('admin')[1]259 def get_admin_user(self):260 return self.isolated_creds.get('admin')[0]261 def get_primary_network(self):262 return self.isolated_net_resources.get('primary')[0]263 def get_primary_subnet(self):264 return self.isolated_net_resources.get('primary')[1]265 def get_primary_router(self):266 return self.isolated_net_resources.get('primary')[2]267 def get_admin_network(self):268 return self.isolated_net_resources.get('admin')[0]269 def get_admin_subnet(self):270 return self.isolated_net_resources.get('admin')[1]271 def get_admin_router(self):272 return self.isolated_net_resources.get('admin')[2]273 def get_alt_network(self):274 return self.isolated_net_resources.get('alt')[0]275 def get_alt_subnet(self):276 return self.isolated_net_resources.get('alt')[1]277 def get_alt_router(self):278 return self.isolated_net_resources.get('alt')[2]279 def get_primary_creds(self):280 if self.isolated_creds.get('primary'):281 user, tenant = self.isolated_creds['primary']282 username, tenant_name = self._get_cred_names(user, tenant)283 else:284 user, tenant = self._create_creds()285 username, tenant_name = self._get_cred_names(user, tenant)286 self.isolated_creds['primary'] = (user, tenant)287 LOG.info("Acquired isolated creds:\n user: %s, tenant: %s"288 % (username, tenant_name))289 if self.config.service_available.neutron:290 network, subnet, router = self._create_network_resources(291 self._get_tenant_id(tenant))292 self.isolated_net_resources['primary'] = (293 network, subnet, router,)294 LOG.info("Created isolated network resources for : \n"295 + " user: %s, tenant: %s" % (username, tenant_name))296 return username, tenant_name, self.password297 def get_admin_creds(self):298 if self.isolated_creds.get('admin'):299 user, tenant = self.isolated_creds['admin']300 username, tenant_name = self._get_cred_names(user, tenant)301 else:302 user, tenant = self._create_creds(admin=True)303 username, tenant_name = self._get_cred_names(user, tenant)304 self.isolated_creds['admin'] = (user, tenant)305 LOG.info("Acquired admin isolated creds:\n user: %s, tenant: %s"306 % (username, tenant_name))307 if self.config.service_available.neutron:308 network, subnet, router = self._create_network_resources(309 self._get_tenant_id(tenant))310 self.isolated_net_resources['admin'] = (311 network, subnet, router,)312 LOG.info("Created isolated network resources for : \n"313 + " user: %s, tenant: %s" % (username, tenant_name))314 return username, tenant_name, self.password315 def get_alt_creds(self):316 if self.isolated_creds.get('alt'):317 user, tenant = self.isolated_creds['alt']318 username, tenant_name = self._get_cred_names(user, tenant)319 else:320 user, tenant = self._create_creds()321 username, tenant_name = self._get_cred_names(user, tenant)322 self.isolated_creds['alt'] = (user, tenant)323 LOG.info("Acquired alt isolated creds:\n user: %s, tenant: %s"324 % (username, tenant_name))325 if self.config.service_available.neutron:326 network, subnet, router = self._create_network_resources(327 self._get_tenant_id(tenant))328 self.isolated_net_resources['alt'] = (329 network, subnet, router,)330 LOG.info("Created isolated network resources for : \n"331 + " user: %s, tenant: %s" % (username, tenant_name))332 return username, tenant_name, self.password333 def _clear_isolated_router(self, router_id, router_name):334 net_client = self.network_admin_client335 try:336 net_client.delete_router(router_id)337 except exceptions.NotFound:338 LOG.warn('router with name: %s not found for delete' %339 router_name)340 pass...
test_project_cleanup.py
Source:test_project_cleanup.py
...21 def setUp(self):22 super(TestProjectCleanup, self).setUp()23 self.conn = self.user_cloud_alt24 self.network_name = self.getUniqueString('network')25 def _create_network_resources(self):26 conn = self.conn27 self.net = conn.network.create_network(28 name=self.network_name,29 )30 self.subnet = conn.network.create_subnet(31 name=self.getUniqueString('subnet'),32 network_id=self.net.id,33 cidr='192.169.1.0/24',34 ip_version=4,35 )36 self.router = conn.network.create_router(37 name=self.getUniqueString('router')38 )39 conn.network.add_interface_to_router(40 self.router.id,41 subnet_id=self.subnet.id)42 def test_cleanup(self):43 self._create_network_resources()44 status_queue = queue.Queue()45 # First round - check no resources are old enough46 self.conn.project_cleanup(47 dry_run=True,48 wait_timeout=120,49 status_queue=status_queue,50 filters={'created_at': '2000-01-01'})51 self.assertTrue(status_queue.empty())52 # Second round - resource evaluation function return false, ensure53 # nothing identified54 self.conn.project_cleanup(55 dry_run=True,56 wait_timeout=120,57 status_queue=status_queue,...
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!!