Best Python code snippet using tempest_python
test_attach_interfaces.py
Source:test_attach_interfaces.py  
...39        if fixed_ip:40            self.assertEqual(iface['fixed_ips'][0]['ip_address'], fixed_ip)41        if mac_addr:42            self.assertEqual(iface['mac_addr'], mac_addr)43    def _create_server_get_interfaces(self):44        resp, server = self.create_test_server(wait_until='ACTIVE')45        resp, ifs = self.client.list_interfaces(server['id'])46        self.assertEqual(200, resp.status)47        resp, body = self.client.wait_for_interface_status(48            server['id'], ifs[0]['port_id'], 'ACTIVE')49        ifs[0]['port_state'] = body['port_state']50        return server, ifs51    def _test_create_interface(self, server):52        resp, iface = self.client.create_interface(server['id'])53        self.assertEqual(200, resp.status)54        resp, iface = self.client.wait_for_interface_status(55            server['id'], iface['port_id'], 'ACTIVE')56        self._check_interface(iface)57        return iface58    def _test_create_interface_by_network_id(self, server, ifs):59        network_id = ifs[0]['net_id']60        resp, iface = self.client.create_interface(server['id'],61                                                   network_id=network_id)62        self.assertEqual(200, resp.status)63        resp, iface = self.client.wait_for_interface_status(64            server['id'], iface['port_id'], 'ACTIVE')65        self._check_interface(iface, network_id=network_id)66        return iface67    def _test_show_interface(self, server, ifs):68        iface = ifs[0]69        resp, _iface = self.client.show_interface(server['id'],70                                                  iface['port_id'])71        self.assertEqual(200, resp.status)72        self._check_interface(iface, port_id=_iface['port_id'],73                              network_id=_iface['net_id'],74                              fixed_ip=_iface['fixed_ips'][0]['ip_address'],75                              mac_addr=_iface['mac_addr'])76    def _test_delete_interface(self, server, ifs):77        # NOTE(danms): delete not the first or last, but one in the middle78        iface = ifs[1]79        resp, _ = self.client.delete_interface(server['id'], iface['port_id'])80        self.assertEqual(202, resp.status)81        _ifs = self.client.list_interfaces(server['id'])[1]82        start = int(time.time())83        while len(ifs) == len(_ifs):84            time.sleep(self.build_interval)85            _ifs = self.client.list_interfaces(server['id'])[1]86            timed_out = int(time.time()) - start >= self.build_timeout87            if len(ifs) == len(_ifs) and timed_out:88                message = ('Failed to delete interface within '89                           'the required time: %s sec.' % self.build_timeout)90                raise exceptions.TimeoutException(message)91        self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs])92        return _ifs93    def _compare_iface_list(self, list1, list2):94        # NOTE(danms): port_state will likely have changed, so just95        # confirm the port_ids are the same at least96        list1 = [x['port_id'] for x in list1]97        list2 = [x['port_id'] for x in list2]98        self.assertEqual(sorted(list1), sorted(list2))99    @test.attr(type='smoke')100    @test.services('network')101    def test_create_list_show_delete_interfaces(self):102        server, ifs = self._create_server_get_interfaces()103        interface_count = len(ifs)104        self.assertTrue(interface_count > 0)105        self._check_interface(ifs[0])106        iface = self._test_create_interface(server)107        ifs.append(iface)108        iface = self._test_create_interface_by_network_id(server, ifs)109        ifs.append(iface)110        resp, _ifs = self.client.list_interfaces(server['id'])111        self._compare_iface_list(ifs, _ifs)112        self._test_show_interface(server, ifs)113        _ifs = self._test_delete_interface(server, ifs)114        self.assertEqual(len(ifs) - 1, len(_ifs))115    @test.attr(type='smoke')116    @test.services('network')117    def test_add_remove_fixed_ip(self):118        # Add and Remove the fixed IP to server.119        server, ifs = self._create_server_get_interfaces()120        interface_count = len(ifs)121        self.assertTrue(interface_count > 0)122        self._check_interface(ifs[0])123        network_id = ifs[0]['net_id']124        resp, body = self.client.add_fixed_ip(server['id'],125                                              network_id)126        self.assertEqual(202, resp.status)127        # Remove the fixed IP from server.128        server_resp, server_detail = self.os.servers_client.get_server(129            server['id'])130        # Get the Fixed IP from server.131        fixed_ip = None132        for ip_set in server_detail['addresses']:133            for ip in server_detail['addresses'][ip_set]:...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!!
