Best Python code snippet using tempest_python
test_attach_interfaces.py
Source:test_attach_interfaces.py  
...106            server['id'], port_id=port_id)['interfaceAttachment']107        self._check_interface(iface, server_id=server['id'], port_id=port_id,108                              network_id=network_id)109        return iface110    def _test_create_interface_by_fixed_ips(self, server, ifs):111        network_id = ifs[0]['net_id']112        subnet_id = ifs[0]['fixed_ips'][0]['subnet_id']113        ip_list = net_utils.get_unused_ip_addresses(self.ports_client,114                                                    self.subnets_client,115                                                    network_id,116                                                    subnet_id,117                                                    1)118        fixed_ips = [{'ip_address': ip_list[0]}]119        iface = self.interfaces_client.create_interface(120            server['id'], net_id=network_id,121            fixed_ips=fixed_ips)['interfaceAttachment']122        self.addCleanup(self.ports_client.delete_port, iface['port_id'])123        self._check_interface(iface, server_id=server['id'],124                              fixed_ip=ip_list[0])125        return iface126    def _test_show_interface(self, server, ifs):127        iface = ifs[0]128        _iface = self.interfaces_client.show_interface(129            server['id'], iface['port_id'])['interfaceAttachment']130        self._check_interface(iface, port_id=_iface['port_id'],131                              network_id=_iface['net_id'],132                              fixed_ip=_iface['fixed_ips'][0]['ip_address'],133                              mac_addr=_iface['mac_addr'])134    def _test_delete_interface(self, server, ifs):135        # NOTE(danms): delete not the first or last, but one in the middle136        iface = ifs[1]137        self.interfaces_client.delete_interface(server['id'], iface['port_id'])138        _ifs = (self.interfaces_client.list_interfaces(server['id'])139                ['interfaceAttachments'])140        start = int(time.time())141        while len(ifs) == len(_ifs):142            time.sleep(self.build_interval)143            _ifs = (self.interfaces_client.list_interfaces(server['id'])144                    ['interfaceAttachments'])145            timed_out = int(time.time()) - start >= self.build_timeout146            if len(ifs) == len(_ifs) and timed_out:147                message = ('Failed to delete interface within '148                           'the required time: %s sec.' % self.build_timeout)149                raise lib_exc.TimeoutException(message)150        self.assertNotIn(iface['port_id'], [i['port_id'] for i in _ifs])151        return _ifs152    def _compare_iface_list(self, list1, list2):153        # NOTE(danms): port_state will likely have changed, so just154        # confirm the port_ids are the same at least155        list1 = [x['port_id'] for x in list1]156        list2 = [x['port_id'] for x in list2]157        self.assertEqual(sorted(list1), sorted(list2))158    @decorators.idempotent_id('73fe8f02-590d-4bf1-b184-e9ca81065051')159    @utils.services('network')160    def test_create_list_show_delete_interfaces_by_network_port(self):161        server, ifs = self._create_server_get_interfaces()162        interface_count = len(ifs)163        self.assertGreater(interface_count, 0)164        try:165            iface = self._test_create_interface(server)166        except lib_exc.BadRequest as e:167            msg = ('Multiple possible networks found, use a Network ID to be '168                   'more specific.')169            if not CONF.compute.fixed_network_name and six.text_type(e) == msg:170                raise171        else:172            ifs.append(iface)173        iface = self._test_create_interface_by_network_id(server, ifs)174        ifs.append(iface)175        iface = self._test_create_interface_by_port_id(server, ifs)176        ifs.append(iface)177        _ifs = (self.interfaces_client.list_interfaces(server['id'])178                ['interfaceAttachments'])179        self._compare_iface_list(ifs, _ifs)180        self._test_show_interface(server, ifs)181        _ifs = self._test_delete_interface(server, ifs)182        self.assertEqual(len(ifs) - 1, len(_ifs))183    @decorators.idempotent_id('d290c06c-f5b3-11e7-8ec8-002293781009')184    @utils.services('network')185    def test_create_list_show_delete_interfaces_by_fixed_ip(self):186        # NOTE(zhufl) By default only project that is admin or network owner187        # or project with role advsvc is authorised to create interfaces with188        # fixed-ip, so if we don't create network for each project, do not189        # test _test_create_interface_by_fixed_ips.190        if not (CONF.auth.use_dynamic_credentials and191                CONF.auth.create_isolated_networks and192                not CONF.network.shared_physical_network):193                raise self.skipException("Only owner network supports "194                                         "creating interface by fixed ip.")195        server, ifs = self._create_server_get_interfaces()196        interface_count = len(ifs)197        self.assertGreater(interface_count, 0)198        try:199            iface = self._test_create_interface(server)200        except lib_exc.BadRequest as e:201            msg = ('Multiple possible networks found, use a Network ID to be '202                   'more specific.')203            if not CONF.compute.fixed_network_name and six.text_type(e) == msg:204                raise205        else:206            ifs.append(iface)207        iface = self._test_create_interface_by_fixed_ips(server, ifs)208        ifs.append(iface)209        _ifs = (self.interfaces_client.list_interfaces(server['id'])210                ['interfaceAttachments'])211        self._compare_iface_list(ifs, _ifs)212        self._test_show_interface(server, ifs)213        _ifs = self._test_delete_interface(server, ifs)214        self.assertEqual(len(ifs) - 1, len(_ifs))215    @decorators.idempotent_id('2f3a0127-95c7-4977-92d2-bc5aec602fb4')216    def test_reassign_port_between_servers(self):217        """Tests the following:218        1. Create a port in Neutron.219        2. Create two servers in Nova.220        3. Attach the port to the first server.221        4. Detach the port from the first server....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!!
