Best Python code snippet using localstack_python
test_instance_attributes.py
Source:test_instance_attributes.py
...76 InstanceId=instance_id, Attribute='disableApiTermination')77 self.assertIn('DisableApiTermination', data)78 self.assertIn('Value', data['DisableApiTermination'])79 self.assertTrue(data['DisableApiTermination']['Value'])80 data = self.client.modify_instance_attribute(InstanceId=instance_id,81 Attribute='disableApiTermination', Value='False')82 data = self.client.describe_instance_attribute(83 InstanceId=instance_id, Attribute='disableApiTermination')84 self.assertFalse(data['DisableApiTermination']['Value'])85 data = self.client.modify_instance_attribute(InstanceId=instance_id,86 Attribute='disableApiTermination', Value='True')87 data = self.client.describe_instance_attribute(88 InstanceId=instance_id, Attribute='disableApiTermination')89 self.assertTrue(data['DisableApiTermination']['Value'])90 self.assertRaises('OperationNotPermitted',91 self.client.terminate_instances,92 InstanceIds=[instance_id])93 data = self.client.modify_instance_attribute(InstanceId=instance_id,94 DisableApiTermination={'Value': False})95 data = self.client.describe_instance_attribute(96 InstanceId=instance_id, Attribute='disableApiTermination')97 self.assertFalse(data['DisableApiTermination']['Value'])98 self.client.terminate_instances(InstanceIds=[instance_id])99 self.cancelResourceCleanUp(res_clean)100 self.get_instance_waiter().wait_delete(instance_id)101 @decorators.idempotent_id('50671a21-99bf-4514-acb0-97617f92e868')102 @testtools.skipUnless(CONF.aws.image_id, "image id is not defined")103 def test_instance_attributes_negative(self):104 instance_id = self.run_instance()105 self.assertRaises('InvalidParameterValue',106 self.client.describe_instance_attribute,107 InstanceId=instance_id, Attribute='fake_attribute')108 self.assertRaises('InvalidInstanceID.NotFound',109 self.client.describe_instance_attribute,110 InstanceId='i-0', Attribute='disableApiTermination')111 if base.TesterStateHolder().get_ec2_enabled():112 self.assertRaises('InvalidParameterCombination',113 self.client.describe_instance_attribute,114 InstanceId=instance_id, Attribute='sourceDestCheck')115 self.assertRaises('InvalidParameterValue',116 self.client.modify_instance_attribute,117 InstanceId=instance_id, Attribute='fake_attribute')118 self.assertRaises('MissingParameter',119 self.client.modify_instance_attribute,120 InstanceId=instance_id, Attribute='disableApiTermination')121 self.assertRaises('InvalidParameterCombination',122 self.client.modify_instance_attribute,123 InstanceId=instance_id)124 self.assertRaises('InvalidParameterCombination',125 self.client.modify_instance_attribute,126 InstanceId=instance_id, Attribute='disableApiTermination',127 Value='True', DisableApiTermination={'Value': False})128 ex_str = ('InvalidParameterCombination'129 if base.TesterStateHolder().get_ec2_enabled() else130 'InvalidGroup.NotFound')131 self.assertRaises(ex_str,132 self.client.modify_instance_attribute,133 InstanceId=instance_id, Groups=['sg-0'])134 if base.TesterStateHolder().get_ec2_enabled():135 self.assertRaises('InvalidParameterCombination',136 self.client.modify_instance_attribute,137 InstanceId=instance_id, Attribute='sourceDestCheck',138 Value='False')139 self.assertRaises('InvalidParameterValue',140 self.client.reset_instance_attribute,141 InstanceId=instance_id, Attribute='fake_attribute')142 self.assertRaises('InvalidParameterValue',143 self.client.reset_instance_attribute,144 InstanceId=instance_id, Attribute='disableApiTermination')145 self.assertRaises('InvalidParameterValue',146 self.client.reset_instance_attribute,147 InstanceId='i-0', Attribute='disableApiTermination')148 self.assertRaises('InvalidParameterValue',149 self.client.reset_instance_attribute,150 InstanceId=instance_id, Attribute='groupSet')151 self.assertRaises('InvalidParameterValue',152 self.client.reset_instance_attribute,153 InstanceId=instance_id, Attribute='instanceType')154 if base.TesterStateHolder().get_ec2_enabled():155 self.assertRaises('InvalidParameterCombination',156 self.client.reset_instance_attribute,157 InstanceId=instance_id, Attribute='sourceDestCheck')158 self.assertRaises('IncorrectInstanceState',159 self.client.modify_instance_attribute,160 InstanceId=instance_id, Attribute='instanceType',161 Value=CONF.aws.instance_type)162 self.assertRaises('IncorrectInstanceState',163 self.client.modify_instance_attribute,164 InstanceId=instance_id,165 InstanceType={'Value': CONF.aws.instance_type})166 self.client.terminate_instances(InstanceIds=[instance_id])167 self.get_instance_waiter().wait_delete(instance_id)168 @base.skip_without_vpc()169 @decorators.idempotent_id('6fd2c8eb-f7f9-420d-a8ae-5d5af3a49a35')170 @testtools.skipUnless(CONF.aws.image_id, "image id is not defined")171 def test_attributes_for_multiple_interfaces_negative(self):172 vpc_id, subnet_id = self.create_vpc_and_subnet('10.30.0.0/24')173 name = data_utils.rand_name('sgName')174 desc = data_utils.rand_name('sgDesc')175 data = self.client.create_security_group(VpcId=vpc_id, GroupName=name,176 Description=desc)177 group_id = data['GroupId']178 self.addResourceCleanUp(self.client.delete_security_group,179 GroupId=group_id)180 time.sleep(2)181 data = self.client.create_network_interface(SubnetId=subnet_id,182 Groups=[group_id])183 ni_id2 = data['NetworkInterface']['NetworkInterfaceId']184 self.addResourceCleanUp(self.client.delete_network_interface,185 NetworkInterfaceId=ni_id2)186 self.get_network_interface_waiter().wait_available(ni_id2)187 instance_id = self.run_instance(SubnetId=subnet_id)188 kwargs = {189 'DeviceIndex': 2,190 'InstanceId': instance_id,191 'NetworkInterfaceId': ni_id2192 }193 data = self.client.attach_network_interface(*[], **kwargs)194 self.assertRaises('InvalidInstanceID',195 self.client.describe_instance_attribute,196 InstanceId=instance_id, Attribute='groupSet')197 self.assertRaises('InvalidInstanceID',198 self.client.modify_instance_attribute,199 InstanceId=instance_id, Groups=['sg-0'])200 self.assertRaises('InvalidInstanceID',201 self.client.describe_instance_attribute,202 InstanceId=instance_id, Attribute='sourceDestCheck')203 self.assertRaises('InvalidInstanceID',204 self.client.modify_instance_attribute,205 InstanceId=instance_id, SourceDestCheck={'Value': False})206 self.assertRaises('InvalidInstanceID',207 self.client.reset_instance_attribute,208 InstanceId=instance_id, Attribute='sourceDestCheck')209 self.client.terminate_instances(InstanceIds=[instance_id])210 self.get_instance_waiter().wait_delete(instance_id)211 @base.skip_without_vpc()212 @decorators.idempotent_id('da26cc0d-6c2d-4638-97f1-1abfae8f00b5')213 @testtools.skipUnless(CONF.aws.image_id, "image id is not defined")214 def test_group_set_attribute(self):215 vpc_id, subnet_id = self.create_vpc_and_subnet('10.30.0.0/24')216 instance_id = self.run_instance(SubnetId=subnet_id)217 data = self.client.describe_instance_attribute(218 InstanceId=instance_id, Attribute='groupSet')219 self.assertIn('Groups', data)220 self.assertEqual(1, len(data['Groups']))221 default_group_id = data['Groups'][0]['GroupId']222 name = data_utils.rand_name('sgName')223 desc = data_utils.rand_name('sgDesc')224 data = self.client.create_security_group(VpcId=vpc_id, GroupName=name,225 Description=desc)226 group_id = data['GroupId']227 self.addResourceCleanUp(self.client.delete_security_group,228 GroupId=group_id)229 time.sleep(2)230 try:231 data = self.client.modify_instance_attribute(232 InstanceId=instance_id, Groups=[group_id])233 data = self.client.describe_instance_attribute(234 InstanceId=instance_id, Attribute='groupSet')235 self.assertIn('Groups', data)236 self.assertEqual(1, len(data['Groups']))237 self.assertNotEqual(default_group_id, data['Groups'][0]['GroupId'])238 self.assertRaises('DependencyViolation',239 self.client.delete_security_group,240 GroupId=group_id)241 finally:242 self.client.modify_instance_attribute(InstanceId=instance_id,243 Groups=[default_group_id])244 data = self.client.describe_instance_attribute(245 InstanceId=instance_id, Attribute='groupSet')246 self.assertIn('Groups', data)247 self.assertEqual(1, len(data['Groups']))248 self.assertEqual(default_group_id, data['Groups'][0]['GroupId'])249 self.client.terminate_instances(InstanceIds=[instance_id])250 self.get_instance_waiter().wait_delete(instance_id)251 @base.skip_without_vpc()252 @decorators.idempotent_id('8e7b37b5-1f2d-4c38-b51e-dcd0e726edb3')253 @testtools.skipUnless(CONF.aws.image_id, "image id is not defined")254 def test_source_dest_check_attribute(self):255 vpc_id, subnet_id = self.create_vpc_and_subnet('10.30.0.0/24')256 instance_id = self.run_instance(SubnetId=subnet_id)257 def do_check(value):258 data = self.client.describe_instance_attribute(259 InstanceId=instance_id, Attribute='sourceDestCheck')260 self.assertIn('SourceDestCheck', data)261 self.assertEqual(value, data['SourceDestCheck'].get('Value'))262 do_check(True)263 self.client.modify_instance_attribute(264 InstanceId=instance_id, Attribute='sourceDestCheck',265 Value='False')266 do_check(False)267 self.client.reset_instance_attribute(268 InstanceId=instance_id, Attribute='sourceDestCheck')269 do_check(True)270 self.client.modify_instance_attribute(271 InstanceId=instance_id, Attribute='sourceDestCheck',272 Value='False')273 do_check(False)274 self.client.terminate_instances(InstanceIds=[instance_id])275 self.get_instance_waiter().wait_delete(instance_id)276 @decorators.idempotent_id('a2640ab1-6aaa-4626-9f23-4aba52e3b88a')277 @testtools.skipUnless(CONF.aws.ebs_image_id, "EBS image id is not defined")278 @testtools.skipUnless(CONF.aws.instance_type_alt,279 "Alternative instance type is not defined")280 @testtools.skipUnless(CONF.aws.instance_type_alt != CONF.aws.instance_type,281 "Alternative instance type is not defined")282 def test_instance_type_attribute(self):283 instance_id = self.run_instance(ImageId=CONF.aws.ebs_image_id)284 self.client.stop_instances(InstanceIds=[instance_id])285 self.get_instance_waiter().wait_available(instance_id,286 final_set=('stopped'))287 instance = self.get_instance(instance_id)288 self.assertEqual(CONF.aws.instance_type, instance['InstanceType'])289 self.client.modify_instance_attribute(290 InstanceId=instance_id, Attribute='instanceType',291 Value=CONF.aws.instance_type)292 instance = self.get_instance(instance_id)293 self.assertEqual(CONF.aws.instance_type, instance['InstanceType'])294 self.client.modify_instance_attribute(295 InstanceId=instance_id,296 InstanceType={'Value': CONF.aws.instance_type_alt})297 instance = self.get_instance(instance_id)298 self.assertEqual(CONF.aws.instance_type_alt, instance['InstanceType'])299 self.client.start_instances(InstanceIds=[instance_id])300 self.get_instance_waiter().wait_available(instance_id,301 final_set=('running'))302 instance = self.get_instance(instance_id)303 self.assertEqual(CONF.aws.instance_type_alt, instance['InstanceType'])304 self.client.terminate_instances(InstanceIds=[instance_id])...
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!!