Best Python code snippet using tempest_python
test_attach_interfaces.py
Source:test_attach_interfaces.py
...36 def setup_clients(cls):37 super(AttachInterfacesTestJSON, cls).setup_clients()38 cls.client = cls.os.interfaces_client39 cls.ports_client = cls.os.ports_client40 def wait_for_interface_status(self, server, port_id, status):41 """Waits for an interface to reach a given status."""42 body = (self.client.show_interface(server, port_id)43 ['interfaceAttachment'])44 interface_status = body['port_state']45 start = int(time.time())46 while(interface_status != status):47 time.sleep(self.build_interval)48 body = (self.client.show_interface(server, port_id)49 ['interfaceAttachment'])50 interface_status = body['port_state']51 timed_out = int(time.time()) - start >= self.build_timeout52 if interface_status != status and timed_out:53 message = ('Interface %s failed to reach %s status '54 '(current %s) within the required time (%s s).' %55 (port_id, status, interface_status,56 self.build_timeout))57 raise exceptions.TimeoutException(message)58 return body59 def _check_interface(self, iface, port_id=None, network_id=None,60 fixed_ip=None, mac_addr=None):61 self.assertIn('port_state', iface)62 if port_id:63 self.assertEqual(iface['port_id'], port_id)64 if network_id:65 self.assertEqual(iface['net_id'], network_id)66 if fixed_ip:67 self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip)68 if mac_addr:69 self.assertEqual(iface['mac_addr'], mac_addr)70 def _create_server_get_interfaces(self):71 server = self.create_test_server(wait_until='ACTIVE')72 ifs = (self.client.list_interfaces(server['id'])73 ['interfaceAttachments'])74 body = self.wait_for_interface_status(75 server['id'], ifs[0]['port_id'], 'ACTIVE')76 ifs[0]['port_state'] = body['port_state']77 return server, ifs78 def _test_create_interface(self, server):79 iface = (self.client.create_interface(server['id'])80 ['interfaceAttachment'])81 iface = self.wait_for_interface_status(82 server['id'], iface['port_id'], 'ACTIVE')83 self._check_interface(iface)84 return iface85 def _test_create_interface_by_network_id(self, server, ifs):86 network_id = ifs[0]['net_id']87 iface = self.client.create_interface(88 server['id'], net_id=network_id)['interfaceAttachment']89 iface = self.wait_for_interface_status(90 server['id'], iface['port_id'], 'ACTIVE')91 self._check_interface(iface, network_id=network_id)92 return iface93 def _test_create_interface_by_port_id(self, server, ifs):94 network_id = ifs[0]['net_id']95 port = self.ports_client.create_port(network_id=network_id)96 port_id = port['port']['id']97 self.addCleanup(self.ports_client.delete_port, port_id)98 iface = self.client.create_interface(99 server['id'], port_id=port_id)['interfaceAttachment']100 iface = self.wait_for_interface_status(101 server['id'], iface['port_id'], 'ACTIVE')102 self._check_interface(iface, port_id=port_id)103 return iface104 def _test_show_interface(self, server, ifs):105 iface = ifs[0]106 _iface = self.client.show_interface(107 server['id'], iface['port_id'])['interfaceAttachment']108 self._check_interface(iface, port_id=_iface['port_id'],109 network_id=_iface['net_id'],110 fixed_ip=_iface['fixed_ips'][0]['ip_address'],111 mac_addr=_iface['mac_addr'])112 def _test_delete_interface(self, server, ifs):113 # NOTE(danms): delete not the first or last, but one in the middle114 iface = ifs[1]...
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!!