How to use delete_network_interface method in localstack

Best Python code snippet using localstack_python

test_network_interfaces.py

Source:test_network_interfaces.py Github

copy

Full Screen

...63 time.sleep(2)64 self.assertRaises('DependencyViolation',65 self.client.delete_subnet,66 SubnetId=subnet_id)67 self.client.delete_network_interface(NetworkInterfaceId=ni_id)68 self.cancelResourceCleanUp(res_clean_ni)69 self.get_network_interface_waiter().wait_delete(ni_id)70 self.client.delete_subnet(SubnetId=subnet_id)71 self.cancelResourceCleanUp(res_clean_subnet)72 self.get_subnet_waiter().wait_delete(subnet_id)73 def test_create_network_interface(self):74 desc = data_utils.rand_name('ni')75 data = self.client.create_network_interface(SubnetId=self.subnet_id,76 Description=desc)77 ni_id = data['NetworkInterface']['NetworkInterfaceId']78 res_clean = self.addResourceCleanUp(79 self.client.delete_network_interface, NetworkInterfaceId=ni_id)80 ni = data['NetworkInterface']81 self.assertEqual(self.vpc_id, ni['VpcId'])82 self.assertEqual(self.subnet_id, ni['SubnetId'])83 self.assertEqual(desc, ni['Description'])84 self.assertNotEmpty(ni.get('Groups'))85 self.assertEqual('default', ni['Groups'][0]['GroupName'])86 address = ni.get('PrivateIpAddress')87 self.assertIsNotNone(address)88 addresses = ni.get('PrivateIpAddresses')89 self.assertIsNotNone(addresses)90 self.assertEqual(1, len(addresses))91 self.assertTrue(addresses[0]['Primary'])92 self.assertEqual(address, addresses[0]['PrivateIpAddress'])93 self.assertIsNotNone(ni.get('MacAddress'))94 self.assertIsNotNone(ni.get('OwnerId'))95 self.assertIsNotNone(ni.get('RequesterManaged'))96 self.assertIsNotNone(ni.get('SourceDestCheck'))97 self.get_network_interface_waiter().wait_available(ni_id)98 self.client.delete_network_interface(NetworkInterfaceId=ni_id)99 self.cancelResourceCleanUp(res_clean)100 self.get_network_interface_waiter().wait_delete(ni_id)101 self.assertRaises('InvalidNetworkInterfaceID.NotFound',102 self.client.describe_network_interfaces,103 NetworkInterfaceIds=[ni_id])104 # TODO(andrey-mp): add creation with addresses105 def test_create_max_network_interface(self):106 # NOTE(andrey-mp): wait some time while all ports will be deleted107 # for this subnet(that are deleting after previous test)108 time.sleep(5)109 data = self.client.describe_subnets(SubnetIds=[self.subnet_id])110 count_before = data['Subnets'][0]['AvailableIpAddressCount']111 addresses = []112 while True:113 try:114 data = self.client.create_network_interface(115 SubnetId=self.subnet_id)116 except botocore.exceptions.ClientError as e:117 error_code = e.response['Error']['Code']118 self.assertEqual('InsufficientFreeAddressesInSubnet',119 error_code, e.message)120 break121 ni_id = data['NetworkInterface']['NetworkInterfaceId']122 res_clean = self.addResourceCleanUp(123 self.client.delete_network_interface,124 NetworkInterfaceId=ni_id)125 addresses.append((ni_id, res_clean))126 data = self.client.describe_subnets(SubnetIds=[self.subnet_id])127 count_after = data['Subnets'][0]['AvailableIpAddressCount']128 # NOTE(andrey-mp): This is strange but Amazon can't create last NI129 # and Openstack can130 self.assertIn(count_after, [0, 1])131 self.assertEqual(len(addresses), count_before - count_after)132 for addr in addresses:133 self.client.delete_network_interface(NetworkInterfaceId=addr[0])134 self.cancelResourceCleanUp(addr[1])135 self.get_network_interface_waiter().wait_delete(addr[0])136 def test_unassign_primary_addresses(self):137 data = self.client.create_network_interface(SubnetId=self.subnet_id)138 ni_id = data['NetworkInterface']['NetworkInterfaceId']139 res_clean = self.addResourceCleanUp(140 self.client.delete_network_interface, NetworkInterfaceId=ni_id)141 primary_address = data['NetworkInterface'].get('PrivateIpAddress')142 self.get_network_interface_waiter().wait_available(ni_id)143 self.assertRaises('InvalidParameterValue',144 self.client.unassign_private_ip_addresses,145 NetworkInterfaceId=ni_id,146 PrivateIpAddresses=[primary_address])147 self.client.delete_network_interface(NetworkInterfaceId=ni_id)148 self.cancelResourceCleanUp(res_clean)149 self.get_network_interface_waiter().wait_delete(ni_id)150 def test_assign_unassign_private_addresses_by_count(self):151 data = self.client.describe_subnets(SubnetIds=[self.subnet_id])152 count = data['Subnets'][0]['AvailableIpAddressCount']153 data = self.client.create_network_interface(SubnetId=self.subnet_id)154 ni_id = data['NetworkInterface']['NetworkInterfaceId']155 res_clean = self.addResourceCleanUp(156 self.client.delete_network_interface, NetworkInterfaceId=ni_id)157 self.get_network_interface_waiter().wait_available(ni_id)158 data = self.client.assign_private_ip_addresses(159 NetworkInterfaceId=ni_id,160 SecondaryPrivateIpAddressCount=2)161 self._wait_assignment(ni_id, data)162 data = self.client.describe_subnets(SubnetIds=[self.subnet_id])163 count_after = data['Subnets'][0]['AvailableIpAddressCount']164 self.assertEqual(count - 3, count_after)165 data = self.client.describe_network_interfaces(166 NetworkInterfaceIds=[ni_id])167 addresses = []168 for addr in data['NetworkInterfaces'][0]['PrivateIpAddresses']:169 if not addr['Primary']:170 addresses.append(addr['PrivateIpAddress'])171 self.assertEqual(2, len(addresses))172 data = self.client.unassign_private_ip_addresses(173 NetworkInterfaceId=ni_id,174 PrivateIpAddresses=addresses)175 self._wait_assignment(ni_id, data)176 data = self.client.describe_subnets(SubnetIds=[self.subnet_id])177 count_after = data['Subnets'][0]['AvailableIpAddressCount']178 self.assertEqual(count - 1, count_after)179 self.client.delete_network_interface(NetworkInterfaceId=ni_id)180 self.cancelResourceCleanUp(res_clean)181 self.get_network_interface_waiter().wait_delete(ni_id)182 def test_assign_unassign_private_addresses_by_addresses(self):183 data = self.client.describe_subnets(SubnetIds=[self.subnet_id])184 count = data['Subnets'][0]['AvailableIpAddressCount']185 data = self.client.create_network_interface(SubnetId=self.subnet_id)186 ni_id = data['NetworkInterface']['NetworkInterfaceId']187 res_clean = self.addResourceCleanUp(188 self.client.delete_network_interface, NetworkInterfaceId=ni_id)189 self.get_network_interface_waiter().wait_available(ni_id)190 addresses = ['10.7.0.10', '10.7.0.11']191 data = self.client.assign_private_ip_addresses(192 NetworkInterfaceId=ni_id,193 PrivateIpAddresses=addresses)194 self._wait_assignment(ni_id, data)195 data = self.client.describe_subnets(SubnetIds=[self.subnet_id])196 count_after = data['Subnets'][0]['AvailableIpAddressCount']197 # NOTE(Alex): Amazon misses 1 IP address by some reason.198 self.assertIn(count_after, [count - 3, count - 4])199 data = self.client.describe_network_interfaces(200 NetworkInterfaceIds=[ni_id])201 assigned_addresses = []202 for addr in data['NetworkInterfaces'][0]['PrivateIpAddresses']:203 if not addr['Primary']:204 self.assertIn(addr['PrivateIpAddress'], addresses)205 assigned_addresses.append(addr['PrivateIpAddress'])206 self.assertEqual(2, len(assigned_addresses))207 data = self.client.unassign_private_ip_addresses(208 NetworkInterfaceId=ni_id,209 PrivateIpAddresses=addresses)210 self._wait_assignment(ni_id, data)211 data = self.client.describe_subnets(SubnetIds=[self.subnet_id])212 count_after = data['Subnets'][0]['AvailableIpAddressCount']213 self.assertIn(count_after, [count - 1, count - 2])214 self.client.delete_network_interface(NetworkInterfaceId=ni_id)215 self.cancelResourceCleanUp(res_clean)216 self.get_network_interface_waiter().wait_delete(ni_id)217 @testtools.skipUnless(CONF.aws.image_id, "image id is not defined")218 def test_attach_network_interface(self):219 data = self.client.create_network_interface(SubnetId=self.subnet_id)220 ni_id = data['NetworkInterface']['NetworkInterfaceId']221 self.addResourceCleanUp(self.client.delete_network_interface,222 NetworkInterfaceId=ni_id)223 ni = data['NetworkInterface']224 address = ni.get('PrivateIpAddress')225 self.assertIsNotNone(address)226 self.get_network_interface_waiter().wait_available(ni_id)227 instance_id = self.run_instance(SubnetId=self.subnet_id)228 # NOTE(andrey-mp): Amazon can't attach to device index = 0229 kwargs = {230 'DeviceIndex': 0,231 'InstanceId': instance_id,232 'NetworkInterfaceId': ni_id233 }234 self.assertRaises('InvalidParameterValue',235 self.client.attach_network_interface,236 **kwargs)237 kwargs = {238 'DeviceIndex': 2,239 'InstanceId': instance_id,240 'NetworkInterfaceId': ni_id241 }242 data = self.client.attach_network_interface(*[], **kwargs)243 attachment_id = data['AttachmentId']244 instance = self.get_instance(instance_id)245 nis = instance.get('NetworkInterfaces', [])246 self.assertEqual(2, len(nis))247 ids = [nis[0]['Attachment']['AttachmentId'],248 nis[1]['Attachment']['AttachmentId']]249 self.assertIn(attachment_id, ids)250 self.assertRaises('InvalidParameterValue',251 self.client.delete_network_interface,252 NetworkInterfaceId=ni_id)253 self.client.detach_network_interface(AttachmentId=attachment_id)254 self.client.terminate_instances(InstanceIds=[instance_id])255 self.get_instance_waiter().wait_delete(instance_id)256 @testtools.skipUnless(CONF.aws.image_id, "image id is not defined")257 def test_network_interfaces_are_not_deleted_on_termination(self):258 instance_id = self.run_instance(SubnetId=self.subnet_id)259 instance = self.get_instance(instance_id)260 nis = instance.get('NetworkInterfaces', [])261 self.assertEqual(1, len(nis))262 self.assertTrue(nis[0]['Attachment']['DeleteOnTermination'])263 ni_id = nis[0]['NetworkInterfaceId']264 attachment_id = nis[0]['Attachment']['AttachmentId']265 kwargs = {266 'NetworkInterfaceId': ni_id,267 'Attachment': {268 'AttachmentId': attachment_id,269 'DeleteOnTermination': False,270 }271 }272 self.client.modify_network_interface_attribute(*[], **kwargs)273 clean_ni = self.addResourceCleanUp(274 self.client.delete_network_interface, NetworkInterfaceId=ni_id)275 data = self.client.create_network_interface(SubnetId=self.subnet_id)276 ni_id2 = data['NetworkInterface']['NetworkInterfaceId']277 clean_ni2 = self.addResourceCleanUp(278 self.client.delete_network_interface, NetworkInterfaceId=ni_id2)279 self.get_network_interface_waiter().wait_available(ni_id2)280 kwargs = {281 'DeviceIndex': 2,282 'InstanceId': instance_id,283 'NetworkInterfaceId': ni_id2284 }285 data = self.client.attach_network_interface(*[], **kwargs)286 attachment_id = data['AttachmentId']287 instance = self.get_instance(instance_id)288 nis = instance.get('NetworkInterfaces', [])289 self.assertEqual(2, len(nis))290 ni = nis[0]291 if ni['Attachment']['AttachmentId'] != attachment_id:292 ni = nis[1]293 self.assertEqual(attachment_id, ni['Attachment']['AttachmentId'])294 self.assertFalse(ni['Attachment']['DeleteOnTermination'])295 self.client.terminate_instances(InstanceIds=[instance_id])296 self.get_instance_waiter().wait_delete(instance_id)297 self.get_network_interface_waiter().wait_available(ni_id)298 self.get_network_interface_waiter().wait_available(ni_id2)299 self.client.delete_network_interface(NetworkInterfaceId=ni_id)300 self.cancelResourceCleanUp(clean_ni)301 self.get_network_interface_waiter().wait_delete(ni_id)302 self.client.delete_network_interface(NetworkInterfaceId=ni_id2)303 self.cancelResourceCleanUp(clean_ni2)304 self.get_network_interface_waiter().wait_delete(ni_id2)305 @testtools.skipUnless(CONF.aws.image_id, "image id is not defined")306 def test_network_interfaces_are_deleted_on_termination(self):307 instance_id = self.run_instance(SubnetId=self.subnet_id)308 instance = self.get_instance(instance_id)309 nis = instance.get('NetworkInterfaces', [])310 self.assertEqual(1, len(nis))311 self.assertTrue(nis[0]['Attachment']['DeleteOnTermination'])312 ni_id = nis[0]['NetworkInterfaceId']313 data = self.client.create_network_interface(SubnetId=self.subnet_id)314 ni_id2 = data['NetworkInterface']['NetworkInterfaceId']315 self.addResourceCleanUp(self.client.delete_network_interface,316 NetworkInterfaceId=ni_id2)317 self.get_network_interface_waiter().wait_available(ni_id2)318 kwargs = {319 'DeviceIndex': 2,320 'InstanceId': instance_id,321 'NetworkInterfaceId': ni_id2322 }323 data = self.client.attach_network_interface(*[], **kwargs)324 attachment_id = data['AttachmentId']325 kwargs = {326 'NetworkInterfaceId': ni_id2,327 'Attachment': {328 'AttachmentId': attachment_id,329 'DeleteOnTermination': True,330 }331 }332 self.client.modify_network_interface_attribute(*[], **kwargs)333 self.client.terminate_instances(InstanceIds=[instance_id])334 self.get_instance_waiter().wait_delete(instance_id)335 self.get_network_interface_waiter().wait_delete(ni_id)336 self.get_network_interface_waiter().wait_delete(ni_id2)337 def test_network_interface_attribute_description(self):338 desc = data_utils.rand_name('ni')339 data = self.client.create_network_interface(340 SubnetId=self.subnet_id, Description=desc)341 ni_id = data['NetworkInterface']['NetworkInterfaceId']342 res_clean = self.addResourceCleanUp(343 self.client.delete_network_interface, NetworkInterfaceId=ni_id)344 self.get_network_interface_waiter().wait_available(ni_id)345 data = self.client.describe_network_interface_attribute(346 NetworkInterfaceId=ni_id, Attribute='description')347 self.assertEqual(desc, data['Description']['Value'])348 new_desc = data_utils.rand_name('new-ni')349 self.client.modify_network_interface_attribute(350 NetworkInterfaceId=ni_id, Description={'Value': new_desc})351 data = self.client.describe_network_interface_attribute(352 NetworkInterfaceId=ni_id, Attribute='description')353 self.assertEqual(new_desc, data['Description']['Value'])354 self.client.delete_network_interface(NetworkInterfaceId=ni_id)355 self.cancelResourceCleanUp(res_clean)356 self.get_network_interface_waiter().wait_delete(ni_id)357 def test_network_interface_attribute_source_dest_check(self):358 data = self.client.create_network_interface(SubnetId=self.subnet_id)359 ni_id = data['NetworkInterface']['NetworkInterfaceId']360 res_clean = self.addResourceCleanUp(361 self.client.delete_network_interface, NetworkInterfaceId=ni_id)362 self.get_network_interface_waiter().wait_available(ni_id)363 self.client.modify_network_interface_attribute(364 NetworkInterfaceId=ni_id, SourceDestCheck={'Value': False})365 data = self.client.describe_network_interface_attribute(366 NetworkInterfaceId=ni_id, Attribute='sourceDestCheck')367 self.assertFalse(data['SourceDestCheck']['Value'])368 # NOTE(andrey-mp): ResetNetworkInterfaceAttribute had inadequate json369 # scheme in botocore and doesn't work against Amazon.370 self.client.modify_network_interface_attribute(371 NetworkInterfaceId=ni_id, SourceDestCheck={'Value': True})372 data = self.client.describe_network_interface_attribute(373 NetworkInterfaceId=ni_id, Attribute='sourceDestCheck')374 self.assertEqual(True, data['SourceDestCheck']['Value'])375 self.client.delete_network_interface(NetworkInterfaceId=ni_id)376 self.cancelResourceCleanUp(res_clean)377 self.get_network_interface_waiter().wait_delete(ni_id)378 @testtools.skipUnless(CONF.aws.image_id, "image id is not defined")379 def test_network_interface_attribute_attachment(self):380 instance_id = self.run_instance(SubnetId=self.subnet_id)381 instance = self.get_instance(instance_id)382 nis = instance.get('NetworkInterfaces', [])383 self.assertEqual(1, len(nis))384 self.assertTrue(nis[0]['Attachment']['DeleteOnTermination'])385 ni_id = nis[0]['NetworkInterfaceId']386 data = self.client.describe_network_interface_attribute(387 NetworkInterfaceId=ni_id, Attribute='attachment')388 self.assertIn('Attachment', data)389 self.assertTrue(data['Attachment'].get('AttachmentId'))390 self.assertTrue(data['Attachment'].get('DeleteOnTermination'))391 self.assertEqual(0, data['Attachment'].get('DeviceIndex'))392 self.assertEqual(instance_id, data['Attachment'].get('InstanceId'))393 self.assertEqual('attached', data['Attachment'].get('Status'))394 self.client.terminate_instances(InstanceIds=[instance_id])395 self.get_instance_waiter().wait_delete(instance_id)396 def test_network_interface_attribute_empty_attachment(self):397 data = self.client.create_network_interface(SubnetId=self.subnet_id)398 ni_id = data['NetworkInterface']['NetworkInterfaceId']399 res_clean = self.addResourceCleanUp(400 self.client.delete_network_interface, NetworkInterfaceId=ni_id)401 self.get_network_interface_waiter().wait_available(ni_id)402 data = self.client.describe_network_interface_attribute(403 NetworkInterfaceId=ni_id, Attribute='attachment')404 self.assertNotIn('Attachment', data)405 self.client.delete_network_interface(NetworkInterfaceId=ni_id)406 self.cancelResourceCleanUp(res_clean)407 self.get_network_interface_waiter().wait_delete(ni_id)408 def test_network_interface_attribute_group_set(self):409 data = self.client.create_network_interface(SubnetId=self.subnet_id)410 ni_id = data['NetworkInterface']['NetworkInterfaceId']411 res_clean = self.addResourceCleanUp(412 self.client.delete_network_interface, NetworkInterfaceId=ni_id)413 self.get_network_interface_waiter().wait_available(ni_id)414 data = self.client.describe_network_interface_attribute(415 NetworkInterfaceId=ni_id, Attribute='groupSet')416 self.assertIn('Groups', data)417 self.assertEqual(1, len(data['Groups']))418 self.assertEqual('default', data['Groups'][0]['GroupName'])419 self.client.delete_network_interface(NetworkInterfaceId=ni_id)420 self.cancelResourceCleanUp(res_clean)421 self.get_network_interface_waiter().wait_delete(ni_id)422 def test_instance_attributes_negative(self):423 data = self.client.create_network_interface(SubnetId=self.subnet_id)424 ni_id = data['NetworkInterface']['NetworkInterfaceId']425 res_clean = self.addResourceCleanUp(426 self.client.delete_network_interface, NetworkInterfaceId=ni_id)427 self.get_network_interface_waiter().wait_available(ni_id)428 self.assertRaises('InvalidParameterCombination',429 self.client.describe_network_interface_attribute,430 NetworkInterfaceId=ni_id)431 self.assertRaises('InvalidParameterValue',432 self.client.describe_network_interface_attribute,433 NetworkInterfaceId=ni_id, Attribute='fake')434 self.assertRaises('InvalidNetworkInterfaceID.NotFound',435 self.client.describe_network_interface_attribute,436 NetworkInterfaceId='eni-0', Attribute='description')437 self.assertRaises('InvalidParameterCombination',438 self.client.modify_network_interface_attribute,439 NetworkInterfaceId=ni_id)440 self.assertRaises('MissingParameter',441 self.client.modify_network_interface_attribute,442 NetworkInterfaceId=ni_id,443 Attachment={'AttachmentId': 'fake'})444 self.assertRaises('InvalidAttachmentID.NotFound',445 self.client.modify_network_interface_attribute,446 NetworkInterfaceId=ni_id,447 Attachment={'AttachmentId': 'eni-attach-0',448 'DeleteOnTermination': True})449 self.assertRaises('InvalidGroup.NotFound',450 self.client.modify_network_interface_attribute,451 NetworkInterfaceId=ni_id, Groups=['sg-0'])452 self.assertRaises('InvalidParameterCombination',453 self.client.modify_network_interface_attribute,454 NetworkInterfaceId=ni_id, Description={})455 self.client.delete_network_interface(NetworkInterfaceId=ni_id)456 self.cancelResourceCleanUp(res_clean)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful