Best Python code snippet using tempest_python
test_ports_rbac.py
Source:test_ports_rbac.py  
...71                                 rule="create_port:fixed_ips")72    @decorators.idempotent_id('2551e10d-006a-413c-925a-8c6f834c09ac')73    def test_create_port_fixed_ips(self):74        # Pick an unused ip address.75        ip_list = net_utils.get_unused_ip_addresses(self.ports_client,76                                                    self.subnets_client,77                                                    self.network['id'],78                                                    self.subnet['id'],79                                                    1)80        fixed_ips = [{'ip_address': ip_list[0]},81                     {'subnet_id': self.subnet['id']}]82        post_body = {'network_id': self.network['id'],83                     'fixed_ips': fixed_ips}84        self.rbac_utils.switch_role(self, toggle_rbac_role=True)85        self._create_port(**post_body)86    @rbac_rule_validation.action(service="neutron",87                                 rule="create_port:mac_address")88    @decorators.idempotent_id('aee6d0be-a7f3-452f-aefc-796b4eb9c9a8')89    def test_create_port_mac_address(self):90        post_body = {'network_id': self.network['id'],91                     'mac_address': data_utils.rand_mac_address()}92        self.rbac_utils.switch_role(self, toggle_rbac_role=True)93        self._create_port(**post_body)94    @rbac_rule_validation.action(service="neutron",95                                 rule="create_port:binding:profile")96    @decorators.idempotent_id('98fa38ab-c2ed-46a0-99f0-59f18cbd257a')97    def test_create_port_binding_profile(self):98        binding_profile = {"foo": "1"}99        post_body = {'network_id': self.network['id'],100                     'binding:profile': binding_profile}101        self.rbac_utils.switch_role(self, toggle_rbac_role=True)102        self._create_port(**post_body)103    @rbac_rule_validation.action(service="neutron",104                                 rule="create_port:allowed_address_pairs")105    @decorators.idempotent_id('b638d1f4-d903-4ca8-aa2a-6fd603c5ec3a')106    def test_create_port_allowed_address_pairs(self):107        # Create port with allowed address pair attribute108        allowed_address_pairs = [{'ip_address': self.port_ip_address,109                                  'mac_address': self.port_mac_address}]110        post_body = {'network_id': self.network['id'],111                     'allowed_address_pairs': allowed_address_pairs}112        self.rbac_utils.switch_role(self, toggle_rbac_role=True)113        self._create_port(**post_body)114    @rbac_rule_validation.action(service="neutron",115                                 rule="get_port",116                                 expected_error_code=404)117    @decorators.idempotent_id('a9d41cb8-78a2-4b97-985c-44e4064416f4')118    def test_show_port(self):119        self.rbac_utils.switch_role(self, toggle_rbac_role=True)120        self.ports_client.show_port(self.port['id'])121    @rbac_rule_validation.action(service="neutron",122                                 rule="get_port:binding:vif_type")123    @decorators.idempotent_id('125aff0b-8fed-4f8e-8410-338616594b06')124    def test_show_port_binding_vif_type(self):125        # Verify specific fields of a port126        fields = ['binding:vif_type']127        self.rbac_utils.switch_role(self, toggle_rbac_role=True)128        retrieved_port = self.ports_client.show_port(129            self.port['id'], fields=fields)['port']130        # Rather than throwing a 403, the field is not present, so raise exc.131        if fields[0] not in retrieved_port:132            raise rbac_exceptions.RbacActionFailed133    @rbac_rule_validation.action(service="neutron",134                                 rule="get_port:binding:vif_details")135    @decorators.idempotent_id('e42bfd77-fcce-45ee-9728-3424300f0d6f')136    def test_show_port_binding_vif_details(self):137        # Verify specific fields of a port138        fields = ['binding:vif_details']139        self.rbac_utils.switch_role(self, toggle_rbac_role=True)140        retrieved_port = self.ports_client.show_port(141            self.port['id'], fields=fields)['port']142        # Rather than throwing a 403, the field is not present, so raise exc.143        if fields[0] not in retrieved_port:144            raise rbac_exceptions.RbacActionFailed145    @rbac_rule_validation.action(service="neutron",146                                 rule="get_port:binding:host_id")147    @decorators.idempotent_id('8e61bcdc-6f81-443c-833e-44410266551e')148    def test_show_port_binding_host_id(self):149        # Verify specific fields of a port150        fields = ['binding:host_id']151        post_body = {'network_id': self.network['id'],152                     'binding:host_id': data_utils.rand_name('host-id')}153        port = self._create_port(**post_body)154        self.rbac_utils.switch_role(self, toggle_rbac_role=True)155        retrieved_port = self.ports_client.show_port(156            port['id'], fields=fields)['port']157        # Rather than throwing a 403, the field is not present, so raise exc.158        if fields[0] not in retrieved_port:159            raise rbac_exceptions.RbacActionFailed160    @rbac_rule_validation.action(service="neutron",161                                 rule="get_port:binding:profile")162    @decorators.idempotent_id('d497cea9-c4ad-42e0-acc9-8d257d6b01fc')163    def test_show_port_binding_profile(self):164        # Verify specific fields of a port165        fields = ['binding:profile']166        binding_profile = {"foo": "1"}167        post_body = {'network_id': self.network['id'],168                     'binding:profile': binding_profile}169        port = self._create_port(**post_body)170        self.rbac_utils.switch_role(self, toggle_rbac_role=True)171        retrieved_port = self.ports_client.show_port(172            port['id'], fields=fields)['port']173        # Rather than throwing a 403, the field is not present, so raise exc.174        if fields[0] not in retrieved_port:175            raise rbac_exceptions.RbacActionFailed176    @rbac_rule_validation.action(service="neutron",177                                 rule="update_port")178    @decorators.idempotent_id('afa80981-3c59-42fd-9531-3bcb2cd03711')179    def test_update_port(self):180        port = self.create_port(self.network)181        self.rbac_utils.switch_role(self, toggle_rbac_role=True)182        self.ports_client.update_port(port['id'], admin_state_up=False)183    @rbac_rule_validation.action(service="neutron",184                                 rule="update_port:mac_address")185    @decorators.idempotent_id('507140c8-7b14-4d63-b627-2103691d887e')186    def test_update_port_mac_address(self):187        port = self.create_port(self.network)188        self.rbac_utils.switch_role(self, toggle_rbac_role=True)189        self.ports_client.update_port(190            port['id'],191            mac_address=data_utils.rand_mac_address())192    @rbac_rule_validation.action(service="neutron",193                                 rule="update_port:fixed_ips")194    @decorators.idempotent_id('c091c825-532b-4c6f-a14f-affd3259c1c3')195    def test_update_port_fixed_ips(self):196        # Pick an ip address within the allocation_pools range.197        post_body = {'network_id': self.network['id']}198        port = self._create_port(**post_body)199        # Pick an unused ip address.200        ip_list = net_utils.get_unused_ip_addresses(self.ports_client,201                                                    self.subnets_client,202                                                    self.network['id'],203                                                    self.subnet['id'],204                                                    1)205        fixed_ips = [{'ip_address': ip_list[0]}]206        self.rbac_utils.switch_role(self, toggle_rbac_role=True)207        self.ports_client.update_port(port['id'],208                                      fixed_ips=fixed_ips)209    @rbac_rule_validation.action(service="neutron",210                                 rule="update_port:port_security_enabled")211    @decorators.idempotent_id('795541af-6652-4e35-9581-fd58224f7545')212    def test_update_port_security_enabled(self):213        port = self.create_port(self.network)214        self.rbac_utils.switch_role(self, toggle_rbac_role=True)215        self.ports_client.update_port(port['id'],216                                      security_groups=[])217    @rbac_rule_validation.action(service="neutron",218                                 rule="update_port:binding:host_id")219    @decorators.idempotent_id('24206a72-0d90-4712-918c-5c9a1ebef64d')220    def test_update_port_binding_host_id(self):221        post_body = {'network_id': self.network['id'],222                     'binding:host_id': 'rbac_test_host'}223        port = self._create_port(**post_body)224        updated_body = {'port_id': port['id'],225                        'binding:host_id': 'rbac_test_host_updated'}226        self.rbac_utils.switch_role(self, toggle_rbac_role=True)227        self.ports_client.update_port(**updated_body)228    @rbac_rule_validation.action(service="neutron",229                                 rule="update_port:binding:profile")230    @decorators.idempotent_id('990ea8d1-9257-4f71-a3bf-d6d0914625c5')231    def test_update_port_binding_profile(self):232        binding_profile = {"foo": "1"}233        post_body = {'network_id': self.network['id'],234                     'binding:profile': binding_profile}235        port = self._create_port(**post_body)236        new_binding_profile = {"foo": "2"}237        updated_body = {'port_id': port['id'],238                        'binding:profile': new_binding_profile}239        self.rbac_utils.switch_role(self, toggle_rbac_role=True)240        self.ports_client.update_port(**updated_body)241    @rbac_rule_validation.action(service="neutron",242                                 rule="update_port:allowed_address_pairs")243    @decorators.idempotent_id('729c2151-bb49-4f4f-9d58-3ed8819b7582')244    def test_update_port_allowed_address_pairs(self):245        # Pick an unused ip address.246        ip_list = net_utils.get_unused_ip_addresses(self.ports_client,247                                                    self.subnets_client,248                                                    self.network['id'],249                                                    self.subnet['id'],250                                                    1)251        # Update allowed address pair attribute of port252        address_pairs = [{'ip_address': ip_list[0],253                          'mac_address': data_utils.rand_mac_address()}]254        post_body = {'network_id': self.network['id']}255        port = self._create_port(**post_body)256        self.rbac_utils.switch_role(self, toggle_rbac_role=True)257        self.ports_client.update_port(port['id'],258                                      allowed_address_pairs=address_pairs)259    @rbac_rule_validation.action(service="neutron",260                                 rule="delete_port",...net_utils.py
Source:net_utils.py  
...13# under the License.14import itertools15import netaddr16from tempest.lib import exceptions as lib_exc17def get_unused_ip_addresses(ports_client, subnets_client,18                            network_id, subnet_id, count):19    """Return a list with the specified number of unused IP addresses20    This method uses the given ports_client to find the specified number of21    unused IP addresses on the given subnet using the supplied subnets_client22    """23    ports = ports_client.list_ports(network_id=network_id)['ports']24    subnet = subnets_client.show_subnet(subnet_id)25    ip_net = netaddr.IPNetwork(subnet['subnet']['cidr'])26    subnet_set = netaddr.IPSet(ip_net.iter_hosts())27    alloc_set = netaddr.IPSet()28    # prune out any addresses already allocated to existing ports29    for port in ports:30        for fixed_ip in port.get('fixed_ips'):31            alloc_set.add(fixed_ip['ip_address'])...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!!
