<s> import time <EOL> from oslo_log import log <EOL> from tempest . lib . common . utils import data_utils <EOL> import testtools <EOL> from ec2api . tests . functional import base <EOL> from ec2api . tests . functional import config <EOL> CONF = config . CONF <EOL> LOG = log . getLogger ( __name__ ) <EOL> class InstanceAttributeTest ( base . EC2TestCase ) : <EOL> @ testtools . skipUnless ( CONF . aws . ebs_image_id , "<STR_LIT>" ) <EOL> def test_describe_instance_attributes ( self ) : <EOL> instance_id = self . run_instance ( ImageId = CONF . aws . ebs_image_id ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> bdms = data . get ( '<STR_LIT>' , [ ] ) <EOL> self . assertNotEmpty ( bdms ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( bdms ) ) <EOL> self . assertIn ( '<STR_LIT>' , bdms [ <NUM_LIT:0> ] ) <EOL> self . assertIn ( '<STR_LIT>' , bdms [ <NUM_LIT:0> ] ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertIn ( '<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> self . assertFalse ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertNotEmpty ( data [ '<STR_LIT>' ] , data ) <EOL> self . assertTrue ( '<STR_LIT>' in data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> or '<STR_LIT>' in data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) <EOL> or data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertIn ( '<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( CONF . aws . instance_type , data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertIn ( '<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . client . terminate_instances ( InstanceIds = [ instance_id ] ) <EOL> self . get_instance_waiter ( ) . wait_delete ( instance_id ) <EOL> @ testtools . skipUnless ( CONF . aws . image_id , "<STR_LIT>" ) <EOL> def test_disable_api_termination_attribute ( self ) : <EOL> instance_id = self . run_instance ( DisableApiTermination = True ) <EOL> res_clean = self . addResourceCleanUp ( <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , <EOL> DisableApiTermination = { '<STR_LIT>' : False } ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertIn ( '<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> data = self . client . modify_instance_attribute ( InstanceId = instance_id , <EOL> Attribute = '<STR_LIT>' , Value = '<STR_LIT:False>' ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertFalse ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> data = self . client . modify_instance_attribute ( InstanceId = instance_id , <EOL> Attribute = '<STR_LIT>' , Value = '<STR_LIT:True>' ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertTrue ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . terminate_instances , <EOL> InstanceIds = [ instance_id ] ) <EOL> data = self . client . modify_instance_attribute ( InstanceId = instance_id , <EOL> DisableApiTermination = { '<STR_LIT>' : False } ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertFalse ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . client . terminate_instances ( InstanceIds = [ instance_id ] ) <EOL> self . cancelResourceCleanUp ( res_clean ) <EOL> self . get_instance_waiter ( ) . wait_delete ( instance_id ) <EOL> @ testtools . skipUnless ( CONF . aws . image_id , "<STR_LIT>" ) <EOL> def test_instance_attributes_negative ( self ) : <EOL> instance_id = self . run_instance ( ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . describe_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . describe_instance_attribute , <EOL> InstanceId = '<STR_LIT>' , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . describe_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' , <EOL> Value = '<STR_LIT:True>' , DisableApiTermination = { '<STR_LIT>' : False } ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , Groups = [ '<STR_LIT>' ] ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' , Value = '<STR_LIT:False>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . reset_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . reset_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . reset_instance_attribute , <EOL> InstanceId = '<STR_LIT>' , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . reset_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . reset_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . reset_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' , <EOL> Value = CONF . aws . instance_type ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , <EOL> InstanceType = { '<STR_LIT>' : CONF . aws . instance_type } ) <EOL> self . client . terminate_instances ( InstanceIds = [ instance_id ] ) <EOL> self . get_instance_waiter ( ) . wait_delete ( instance_id ) <EOL> @ base . skip_without_vpc ( ) <EOL> @ testtools . skipUnless ( CONF . aws . image_id , "<STR_LIT>" ) <EOL> def test_attributes_for_multiple_interfaces_negative ( self ) : <EOL> vpc_id , subnet_id = self . create_vpc_and_subnet ( '<STR_LIT>' ) <EOL> name = data_utils . rand_name ( '<STR_LIT>' ) <EOL> desc = data_utils . rand_name ( '<STR_LIT>' ) <EOL> data = self . client . create_security_group ( VpcId = vpc_id , GroupName = name , <EOL> Description = desc ) <EOL> group_id = data [ '<STR_LIT>' ] <EOL> self . addResourceCleanUp ( self . client . delete_security_group , <EOL> GroupId = group_id ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> data = self . client . create_network_interface ( SubnetId = subnet_id , <EOL> Groups = [ group_id ] ) <EOL> ni_id2 = data [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . addResourceCleanUp ( self . client . delete_network_interface , <EOL> NetworkInterfaceId = ni_id2 ) <EOL> self . get_network_interface_waiter ( ) . wait_available ( ni_id2 ) <EOL> instance_id = self . run_instance ( SubnetId = subnet_id ) <EOL> kwargs = { <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : instance_id , <EOL> '<STR_LIT>' : ni_id2 <EOL> } <EOL> data = self . client . attach_network_interface ( * [ ] , ** kwargs ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . describe_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , Groups = [ '<STR_LIT>' ] ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . describe_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . modify_instance_attribute , <EOL> InstanceId = instance_id , SourceDestCheck = { '<STR_LIT>' : False } ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . reset_instance_attribute , <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . client . terminate_instances ( InstanceIds = [ instance_id ] ) <EOL> self . get_instance_waiter ( ) . wait_delete ( instance_id ) <EOL> @ base . skip_without_vpc ( ) <EOL> @ testtools . skipUnless ( CONF . aws . image_id , "<STR_LIT>" ) <EOL> def test_group_set_attribute ( self ) : <EOL> vpc_id , subnet_id = self . create_vpc_and_subnet ( '<STR_LIT>' ) <EOL> instance_id = self . run_instance ( SubnetId = subnet_id ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( data [ '<STR_LIT>' ] ) ) <EOL> default_group_id = data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> name = data_utils . rand_name ( '<STR_LIT>' ) <EOL> desc = data_utils . rand_name ( '<STR_LIT>' ) <EOL> data = self . client . create_security_group ( VpcId = vpc_id , GroupName = name , <EOL> Description = desc ) <EOL> group_id = data [ '<STR_LIT>' ] <EOL> self . addResourceCleanUp ( self . client . delete_security_group , <EOL> GroupId = group_id ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> try : <EOL> data = self . client . modify_instance_attribute ( <EOL> InstanceId = instance_id , Groups = [ group_id ] ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( data [ '<STR_LIT>' ] ) ) <EOL> self . assertNotEqual ( default_group_id , data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . assertRaises ( '<STR_LIT>' , <EOL> self . client . delete_security_group , <EOL> GroupId = group_id ) <EOL> finally : <EOL> self . client . modify_instance_attribute ( InstanceId = instance_id , <EOL> Groups = [ default_group_id ] ) <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( data [ '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( default_group_id , data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . client . terminate_instances ( InstanceIds = [ instance_id ] ) <EOL> self . get_instance_waiter ( ) . wait_delete ( instance_id ) <EOL> @ base . skip_without_vpc ( ) <EOL> @ testtools . skipUnless ( CONF . aws . image_id , "<STR_LIT>" ) <EOL> def test_source_dest_check_attribute ( self ) : <EOL> vpc_id , subnet_id = self . create_vpc_and_subnet ( '<STR_LIT>' ) <EOL> instance_id = self . run_instance ( SubnetId = subnet_id ) <EOL> def do_check ( value ) : <EOL> data = self . client . describe_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , data ) <EOL> self . assertEqual ( value , data [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) ) <EOL> do_check ( True ) <EOL> self . client . modify_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' , <EOL> Value = '<STR_LIT:False>' ) <EOL> do_check ( False ) <EOL> self . client . reset_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' ) <EOL> do_check ( True ) <EOL> self . client . modify_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' , <EOL> Value = '<STR_LIT:False>' ) <EOL> do_check ( False ) <EOL> self . client . terminate_instances ( InstanceIds = [ instance_id ] ) <EOL> self . get_instance_waiter ( ) . wait_delete ( instance_id ) <EOL> @ testtools . skipUnless ( CONF . aws . ebs_image_id , "<STR_LIT>" ) <EOL> @ testtools . skipUnless ( CONF . aws . instance_type_alt , <EOL> "<STR_LIT>" ) <EOL> @ testtools . skipUnless ( CONF . aws . instance_type_alt != CONF . aws . instance_type , <EOL> "<STR_LIT>" ) <EOL> def test_instance_type_attribute ( self ) : <EOL> instance_id = self . run_instance ( ImageId = CONF . aws . ebs_image_id ) <EOL> self . client . stop_instances ( InstanceIds = [ instance_id ] ) <EOL> self . get_instance_waiter ( ) . wait_available ( instance_id , <EOL> final_set = ( '<STR_LIT>' ) ) <EOL> instance = self . get_instance ( instance_id ) <EOL> self . assertEqual ( CONF . aws . instance_type , instance [ '<STR_LIT>' ] ) <EOL> self . client . modify_instance_attribute ( <EOL> InstanceId = instance_id , Attribute = '<STR_LIT>' , <EOL> Value = CONF . aws . instance_type ) <EOL> instance = self . get_instance ( instance_id ) <EOL> self . assertEqual ( CONF . aws . instance_type , instance [ '<STR_LIT>' ] ) <EOL> self . client . modify_instance_attribute ( <EOL> InstanceId = instance_id , <EOL> InstanceType = { '<STR_LIT>' : CONF . aws . instance_type_alt } ) <EOL> instance = self . get_instance ( instance_id ) <EOL> self . assertEqual ( CONF . aws . instance_type_alt , instance [ '<STR_LIT>' ] ) <EOL> self . client . start_instances ( InstanceIds = [ instance_id ] ) <EOL> self . get_instance_waiter ( ) . wait_available ( instance_id , <EOL> final_set = ( '<STR_LIT>' ) ) <EOL> instance = self . get_instance ( instance_id ) <EOL> self . assertEqual ( CONF . aws . instance_type_alt , instance [ '<STR_LIT>' ] ) <EOL> self . client . terminate_instances ( InstanceIds = [ instance_id ] ) <EOL> self . get_instance_waiter ( ) . wait_delete ( instance_id ) </s>
