Best Python code snippet using tempest_python
test_dhcp_ipv6.py
Source:test_dhcp_ipv6.py
...48 for index, i in enumerate(things_list):49 if i['id'] == elem['id']:50 break51 del things_list[index]52 def _clean_network(self):53 body = self.client.list_ports()54 ports = body['ports']55 for port in ports:56 if (port['device_owner'].startswith('network:router_interface')57 and port['device_id'] in [r['id'] for r in self.routers]):58 self.client.remove_router_interface_with_port_id(59 port['device_id'], port['id']60 )61 else:62 if port['id'] in [p['id'] for p in self.ports]:63 self.client.delete_port(port['id'])64 self._remove_from_list_by_index(self.ports, port)65 body = self.client.list_subnets()66 subnets = body['subnets']67 for subnet in subnets:68 if subnet['id'] in [s['id'] for s in self.subnets]:69 self.client.delete_subnet(subnet['id'])70 self._remove_from_list_by_index(self.subnets, subnet)71 body = self.client.list_routers()72 routers = body['routers']73 for router in routers:74 if router['id'] in [r['id'] for r in self.routers]:75 self.client.delete_router(router['id'])76 self._remove_from_list_by_index(self.routers, router)77 def _get_ips_from_subnet(self, **kwargs):78 subnet = self.create_subnet(self.network, **kwargs)79 port_mac = data_utils.rand_mac_address()80 port = self.create_port(self.network, mac_address=port_mac)81 real_ip = next(iter(port['fixed_ips']), None)['ip_address']82 eui_ip = data_utils.get_ipv6_addr_by_EUI64(subnet['cidr'],83 port_mac).format()84 return real_ip, eui_ip85 @test.idempotent_id('e5517e62-6f16-430d-a672-f80875493d4c')86 def test_dhcpv6_stateless_eui64(self):87 """When subnets configured with RAs SLAAC (AOM=100) and DHCP stateless88 (AOM=110) both for radvd and dnsmasq, port shall receive IP address89 calculated from its MAC.90 """91 for ra_mode, add_mode in (92 ('slaac', 'slaac'),93 ('dhcpv6-stateless', 'dhcpv6-stateless'),94 ):95 kwargs = {'ipv6_ra_mode': ra_mode,96 'ipv6_address_mode': add_mode}97 real_ip, eui_ip = self._get_ips_from_subnet(**kwargs)98 self._clean_network()99 self.assertEqual(eui_ip, real_ip,100 ('Real port IP is %s, but shall be %s when '101 'ipv6_ra_mode=%s and ipv6_address_mode=%s') % (102 real_ip, eui_ip, ra_mode, add_mode))103 @test.idempotent_id('ae2f4a5d-03ff-4c42-a3b0-ce2fcb7ea832')104 def test_dhcpv6_stateless_no_ra(self):105 """When subnets configured with dnsmasq SLAAC and DHCP stateless106 and there is no radvd, port shall receive IP address calculated107 from its MAC and mask of subnet.108 """109 for ra_mode, add_mode in (110 (None, 'slaac'),111 (None, 'dhcpv6-stateless'),112 ):113 kwargs = {'ipv6_ra_mode': ra_mode,114 'ipv6_address_mode': add_mode}115 kwargs = {k: v for k, v in kwargs.iteritems() if v}116 real_ip, eui_ip = self._get_ips_from_subnet(**kwargs)117 self._clean_network()118 self.assertEqual(eui_ip, real_ip,119 ('Real port IP %s shall be equal to EUI-64 %s'120 'when ipv6_ra_mode=%s,ipv6_address_mode=%s') % (121 real_ip, eui_ip,122 ra_mode if ra_mode else "Off",123 add_mode if add_mode else "Off"))124 @test.idempotent_id('81f18ef6-95b5-4584-9966-10d480b7496a')125 def test_dhcpv6_invalid_options(self):126 """Different configurations for radvd and dnsmasq are not allowed"""127 for ra_mode, add_mode in (128 ('dhcpv6-stateless', 'dhcpv6-stateful'),129 ('dhcpv6-stateless', 'slaac'),130 ('slaac', 'dhcpv6-stateful'),131 ('dhcpv6-stateful', 'dhcpv6-stateless'),132 ('dhcpv6-stateful', 'slaac'),133 ('slaac', 'dhcpv6-stateless'),134 ):135 kwargs = {'ipv6_ra_mode': ra_mode,136 'ipv6_address_mode': add_mode}137 self.assertRaises(lib_exc.BadRequest,138 self.create_subnet,139 self.network,140 **kwargs)141 @test.idempotent_id('21635b6f-165a-4d42-bf49-7d195e47342f')142 def test_dhcpv6_stateless_no_ra_no_dhcp(self):143 """If no radvd option and no dnsmasq option is configured144 port shall receive IP from fixed IPs list of subnet.145 """146 real_ip, eui_ip = self._get_ips_from_subnet()147 self._clean_network()148 self.assertNotEqual(eui_ip, real_ip,149 ('Real port IP %s equal to EUI-64 %s when '150 'ipv6_ra_mode=Off and ipv6_address_mode=Off,'151 'but shall be taken from fixed IPs') % (152 real_ip, eui_ip))153 @test.idempotent_id('4544adf7-bb5f-4bdc-b769-b3e77026cef2')154 def test_dhcpv6_two_subnets(self):155 """When one IPv6 subnet configured with dnsmasq SLAAC or DHCP stateless156 and other IPv6 is with DHCP stateful, port shall receive EUI-64 IP157 addresses from first subnet and DHCP address from second one.158 Order of subnet creating should be unimportant.159 """160 for order in ("slaac_first", "dhcp_first"):161 for ra_mode, add_mode in (162 ('slaac', 'slaac'),163 ('dhcpv6-stateless', 'dhcpv6-stateless'),164 ):165 kwargs = {'ipv6_ra_mode': ra_mode,166 'ipv6_address_mode': add_mode}167 kwargs_dhcp = {'ipv6_address_mode': 'dhcpv6-stateful'}168 if order == "slaac_first":169 subnet_slaac = self.create_subnet(self.network, **kwargs)170 subnet_dhcp = self.create_subnet(171 self.network, **kwargs_dhcp)172 else:173 subnet_dhcp = self.create_subnet(174 self.network, **kwargs_dhcp)175 subnet_slaac = self.create_subnet(self.network, **kwargs)176 port_mac = data_utils.rand_mac_address()177 dhcp_ip = subnet_dhcp["allocation_pools"][0]["start"]178 eui_ip = data_utils.get_ipv6_addr_by_EUI64(179 subnet_slaac['cidr'],180 port_mac181 ).format()182 # TODO(sergsh): remove this when 1219795 is fixed183 dhcp_ip = [dhcp_ip, (netaddr.IPAddress(dhcp_ip) + 1).format()]184 port = self.create_port(self.network, mac_address=port_mac)185 real_ips = dict([(k['subnet_id'], k['ip_address'])186 for k in port['fixed_ips']])187 real_dhcp_ip, real_eui_ip = [real_ips[sub['id']]188 for sub in subnet_dhcp,189 subnet_slaac]190 self.client.delete_port(port['id'])191 self.ports.pop()192 body = self.client.list_ports()193 ports_id_list = [i['id'] for i in body['ports']]194 self.assertNotIn(port['id'], ports_id_list)195 self._clean_network()196 self.assertEqual(real_eui_ip,197 eui_ip,198 'Real IP is {0}, but shall be {1}'.format(199 real_eui_ip,200 eui_ip))201 self.assertIn(202 real_dhcp_ip, dhcp_ip,203 'Real IP is {0}, but shall be one from {1}'.format(204 real_dhcp_ip,205 str(dhcp_ip)))206 @test.idempotent_id('4256c61d-c538-41ea-9147-3c450c36669e')207 def test_dhcpv6_64_subnets(self):208 """When one IPv6 subnet configured with dnsmasq SLAAC or DHCP stateless209 and other IPv4 is with DHCP of IPv4, port shall receive EUI-64 IP210 addresses from first subnet and IPv4 DHCP address from second one.211 Order of subnet creating should be unimportant.212 """213 for order in ("slaac_first", "dhcp_first"):214 for ra_mode, add_mode in (215 ('slaac', 'slaac'),216 ('dhcpv6-stateless', 'dhcpv6-stateless'),217 ):218 kwargs = {'ipv6_ra_mode': ra_mode,219 'ipv6_address_mode': add_mode}220 if order == "slaac_first":221 subnet_slaac = self.create_subnet(self.network, **kwargs)222 subnet_dhcp = self.create_subnet(223 self.network, ip_version=4)224 else:225 subnet_dhcp = self.create_subnet(226 self.network, ip_version=4)227 subnet_slaac = self.create_subnet(self.network, **kwargs)228 port_mac = data_utils.rand_mac_address()229 dhcp_ip = subnet_dhcp["allocation_pools"][0]["start"]230 eui_ip = data_utils.get_ipv6_addr_by_EUI64(231 subnet_slaac['cidr'],232 port_mac233 ).format()234 # TODO(sergsh): remove this when 1219795 is fixed235 dhcp_ip = [dhcp_ip, (netaddr.IPAddress(dhcp_ip) + 1).format()]236 port = self.create_port(self.network, mac_address=port_mac)237 real_ips = dict([(k['subnet_id'], k['ip_address'])238 for k in port['fixed_ips']])239 real_dhcp_ip, real_eui_ip = [real_ips[sub['id']]240 for sub in subnet_dhcp,241 subnet_slaac]242 self._clean_network()243 self.assertTrue({real_eui_ip,244 real_dhcp_ip}.issubset([eui_ip] + dhcp_ip))245 self.assertEqual(real_eui_ip,246 eui_ip,247 'Real IP is {0}, but shall be {1}'.format(248 real_eui_ip,249 eui_ip))250 self.assertIn(251 real_dhcp_ip, dhcp_ip,252 'Real IP is {0}, but shall be one from {1}'.format(253 real_dhcp_ip,254 str(dhcp_ip)))255 @test.idempotent_id('4ab211a0-276f-4552-9070-51e27f58fecf')256 def test_dhcp_stateful(self):257 """With all options below, DHCPv6 shall allocate first258 address from subnet pool to port.259 """260 for ra_mode, add_mode in (261 ('dhcpv6-stateful', 'dhcpv6-stateful'),262 ('dhcpv6-stateful', None),263 (None, 'dhcpv6-stateful'),264 ):265 kwargs = {'ipv6_ra_mode': ra_mode,266 'ipv6_address_mode': add_mode}267 kwargs = {k: v for k, v in kwargs.iteritems() if v}268 subnet = self.create_subnet(self.network, **kwargs)269 port = self.create_port(self.network)270 port_ip = next(iter(port['fixed_ips']), None)['ip_address']271 dhcp_ip = subnet["allocation_pools"][0]["start"]272 # TODO(sergsh): remove this when 1219795 is fixed273 dhcp_ip = [dhcp_ip, (netaddr.IPAddress(dhcp_ip) + 1).format()]274 self._clean_network()275 self.assertIn(276 port_ip, dhcp_ip,277 'Real IP is {0}, but shall be one from {1}'.format(278 port_ip,279 str(dhcp_ip)))280 @test.idempotent_id('51a5e97f-f02e-4e4e-9a17-a69811d300e3')281 def test_dhcp_stateful_fixedips(self):282 """With all options below, port shall be able to get283 requested IP from fixed IP range not depending on284 DHCP stateful (not SLAAC!) settings configured.285 """286 for ra_mode, add_mode in (287 ('dhcpv6-stateful', 'dhcpv6-stateful'),288 ('dhcpv6-stateful', None),289 (None, 'dhcpv6-stateful'),290 ):291 kwargs = {'ipv6_ra_mode': ra_mode,292 'ipv6_address_mode': add_mode}293 kwargs = {k: v for k, v in kwargs.iteritems() if v}294 subnet = self.create_subnet(self.network, **kwargs)295 ip_range = netaddr.IPRange(subnet["allocation_pools"][0]["start"],296 subnet["allocation_pools"][0]["end"])297 ip = netaddr.IPAddress(random.randrange(ip_range.first,298 ip_range.last)).format()299 port = self.create_port(self.network,300 fixed_ips=[{'subnet_id': subnet['id'],301 'ip_address': ip}])302 port_ip = next(iter(port['fixed_ips']), None)['ip_address']303 self._clean_network()304 self.assertEqual(port_ip, ip,305 ("Port IP %s is not as fixed IP from "306 "port create request: %s") % (307 port_ip, ip))308 @test.idempotent_id('98244d88-d990-4570-91d4-6b25d70d08af')309 def test_dhcp_stateful_fixedips_outrange(self):310 """When port gets IP address from fixed IP range it311 shall be checked if it's from subnets range.312 """313 kwargs = {'ipv6_ra_mode': 'dhcpv6-stateful',314 'ipv6_address_mode': 'dhcpv6-stateful'}315 subnet = self.create_subnet(self.network, **kwargs)316 ip_range = netaddr.IPRange(subnet["allocation_pools"][0]["start"],317 subnet["allocation_pools"][0]["end"])318 ip = netaddr.IPAddress(random.randrange(319 ip_range.last + 1, ip_range.last + 10)).format()320 self.assertRaises(lib_exc.BadRequest,321 self.create_port,322 self.network,323 fixed_ips=[{'subnet_id': subnet['id'],324 'ip_address': ip}])325 @test.idempotent_id('57b8302b-cba9-4fbb-8835-9168df029051')326 def test_dhcp_stateful_fixedips_duplicate(self):327 """When port gets IP address from fixed IP range it328 shall be checked if it's not duplicate.329 """330 kwargs = {'ipv6_ra_mode': 'dhcpv6-stateful',331 'ipv6_address_mode': 'dhcpv6-stateful'}332 subnet = self.create_subnet(self.network, **kwargs)333 ip_range = netaddr.IPRange(subnet["allocation_pools"][0]["start"],334 subnet["allocation_pools"][0]["end"])335 ip = netaddr.IPAddress(random.randrange(336 ip_range.first, ip_range.last)).format()337 self.create_port(self.network,338 fixed_ips=[339 {'subnet_id': subnet['id'],340 'ip_address': ip}])341 self.assertRaisesRegexp(lib_exc.Conflict,342 "object with that identifier already exists",343 self.create_port,344 self.network,345 fixed_ips=[{'subnet_id': subnet['id'],346 'ip_address': ip}])347 def _create_subnet_router(self, kwargs):348 subnet = self.create_subnet(self.network, **kwargs)349 router = self.create_router(350 router_name=data_utils.rand_name("routerv6-"),351 admin_state_up=True)352 port = self.create_router_interface(router['id'],353 subnet['id'])354 body = self.client.show_port(port['port_id'])355 return subnet, body['port']356 @test.idempotent_id('e98f65db-68f4-4330-9fea-abd8c5192d4d')357 def test_dhcp_stateful_router(self):358 """With all options below the router interface shall359 receive DHCPv6 IP address from allocation pool.360 """361 for ra_mode, add_mode in (362 ('dhcpv6-stateful', 'dhcpv6-stateful'),363 ('dhcpv6-stateful', None),364 ):365 kwargs = {'ipv6_ra_mode': ra_mode,366 'ipv6_address_mode': add_mode}367 kwargs = {k: v for k, v in kwargs.iteritems() if v}368 subnet, port = self._create_subnet_router(kwargs)369 port_ip = next(iter(port['fixed_ips']), None)['ip_address']370 self._clean_network()371 self.assertEqual(port_ip, subnet['gateway_ip'],372 ("Port IP %s is not as first IP from "373 "subnets allocation pool: %s") % (374 port_ip, subnet['gateway_ip']))375 def tearDown(self):376 self._clean_network()...
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!!