<s> import time <EOL> import boto <EOL> from boto . compat import six <EOL> from tests . compat import unittest <EOL> from boto . ec2 . networkinterface import NetworkInterfaceCollection <EOL> from boto . ec2 . networkinterface import NetworkInterfaceSpecification <EOL> from boto . ec2 . networkinterface import PrivateIPAddress <EOL> class TestVPCConnection ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . instances = [ ] <EOL> self . post_terminate_cleanups = [ ] <EOL> self . api = boto . connect_vpc ( ) <EOL> self . vpc = self . api . create_vpc ( '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> self . subnet = self . api . create_subnet ( self . vpc . id , '<STR_LIT>' ) <EOL> self . post_terminate_cleanups . append ( ( self . api . delete_subnet , ( self . subnet . id , ) ) ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> def post_terminate_cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> for fn , args in self . post_terminate_cleanups : <EOL> fn ( * args ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> if self . vpc : <EOL> self . api . delete_vpc ( self . vpc . id ) <EOL> def terminate_instances ( self ) : <EOL> """<STR_LIT>""" <EOL> for instance in self . instances : <EOL> self . terminate_instance ( instance ) <EOL> self . post_terminate_cleanup ( ) <EOL> def terminate_instance ( self , instance ) : <EOL> instance . terminate ( ) <EOL> for i in six . moves . range ( <NUM_LIT> ) : <EOL> instance . update ( ) <EOL> if instance . state == '<STR_LIT>' : <EOL> time . sleep ( <NUM_LIT:30> ) <EOL> return <EOL> else : <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> def delete_elastic_ip ( self , eip ) : <EOL> new_eip = self . api . get_all_addresses ( [ eip . public_ip ] ) [ <NUM_LIT:0> ] <EOL> if new_eip . association_id : <EOL> new_eip . disassociate ( ) <EOL> new_eip . release ( ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> def test_multi_ip_create ( self ) : <EOL> interface = NetworkInterfaceSpecification ( <EOL> device_index = <NUM_LIT:0> , subnet_id = self . subnet . id , <EOL> private_ip_address = '<STR_LIT>' , <EOL> description = "<STR_LIT>" , <EOL> delete_on_termination = True , private_ip_addresses = [ <EOL> PrivateIPAddress ( private_ip_address = '<STR_LIT>' , <EOL> primary = False ) , <EOL> PrivateIPAddress ( private_ip_address = '<STR_LIT>' , <EOL> primary = False ) , <EOL> PrivateIPAddress ( private_ip_address = '<STR_LIT>' , <EOL> primary = False ) ] ) <EOL> interfaces = NetworkInterfaceCollection ( interface ) <EOL> reservation = self . api . run_instances ( image_id = '<STR_LIT>' , instance_type = '<STR_LIT>' , <EOL> network_interfaces = interfaces ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> instance = reservation . instances [ <NUM_LIT:0> ] <EOL> self . addCleanup ( self . terminate_instance , instance ) <EOL> retrieved = self . api . get_all_reservations ( instance_ids = [ instance . id ] ) <EOL> self . assertEqual ( len ( retrieved ) , <NUM_LIT:1> ) <EOL> retrieved_instances = retrieved [ <NUM_LIT:0> ] . instances <EOL> self . assertEqual ( len ( retrieved_instances ) , <NUM_LIT:1> ) <EOL> retrieved_instance = retrieved_instances [ <NUM_LIT:0> ] <EOL> self . assertEqual ( len ( retrieved_instance . interfaces ) , <NUM_LIT:1> ) <EOL> interface = retrieved_instance . interfaces [ <NUM_LIT:0> ] <EOL> private_ip_addresses = interface . private_ip_addresses <EOL> self . assertEqual ( len ( private_ip_addresses ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( private_ip_addresses [ <NUM_LIT:0> ] . private_ip_address , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( private_ip_addresses [ <NUM_LIT:0> ] . primary , True ) <EOL> self . assertEqual ( private_ip_addresses [ <NUM_LIT:1> ] . private_ip_address , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( private_ip_addresses [ <NUM_LIT:2> ] . private_ip_address , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( private_ip_addresses [ <NUM_LIT:3> ] . private_ip_address , <EOL> '<STR_LIT>' ) <EOL> def test_associate_public_ip ( self ) : <EOL> interface = NetworkInterfaceSpecification ( <EOL> associate_public_ip_address = True , <EOL> subnet_id = self . subnet . id , <EOL> delete_on_termination = True <EOL> ) <EOL> interfaces = NetworkInterfaceCollection ( interface ) <EOL> reservation = self . api . run_instances ( <EOL> image_id = '<STR_LIT>' , <EOL> instance_type = '<STR_LIT>' , <EOL> network_interfaces = interfaces <EOL> ) <EOL> instance = reservation . instances [ <NUM_LIT:0> ] <EOL> self . instances . append ( instance ) <EOL> self . addCleanup ( self . terminate_instances ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> retrieved = self . api . get_all_reservations ( <EOL> instance_ids = [ <EOL> instance . id <EOL> ] <EOL> ) <EOL> self . assertEqual ( len ( retrieved ) , <NUM_LIT:1> ) <EOL> retrieved_instances = retrieved [ <NUM_LIT:0> ] . instances <EOL> self . assertEqual ( len ( retrieved_instances ) , <NUM_LIT:1> ) <EOL> retrieved_instance = retrieved_instances [ <NUM_LIT:0> ] <EOL> self . assertEqual ( len ( retrieved_instance . interfaces ) , <NUM_LIT:1> ) <EOL> interface = retrieved_instance . interfaces [ <NUM_LIT:0> ] <EOL> self . assertTrue ( interface . publicIp . count ( '<STR_LIT:.>' ) >= <NUM_LIT:3> ) <EOL> def test_associate_elastic_ip ( self ) : <EOL> interface = NetworkInterfaceSpecification ( <EOL> associate_public_ip_address = False , <EOL> subnet_id = self . subnet . id , <EOL> delete_on_termination = True <EOL> ) <EOL> interfaces = NetworkInterfaceCollection ( interface ) <EOL> reservation = self . api . run_instances ( <EOL> image_id = '<STR_LIT>' , <EOL> instance_type = '<STR_LIT>' , <EOL> network_interfaces = interfaces <EOL> ) <EOL> instance = reservation . instances [ <NUM_LIT:0> ] <EOL> self . instances . append ( instance ) <EOL> self . addCleanup ( self . terminate_instances ) <EOL> igw = self . api . create_internet_gateway ( ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> self . api . attach_internet_gateway ( igw . id , self . vpc . id ) <EOL> self . post_terminate_cleanups . append ( ( self . api . detach_internet_gateway , ( igw . id , self . vpc . id ) ) ) <EOL> self . post_terminate_cleanups . append ( ( self . api . delete_internet_gateway , ( igw . id , ) ) ) <EOL> eip = self . api . allocate_address ( '<STR_LIT>' ) <EOL> self . post_terminate_cleanups . append ( ( self . delete_elastic_ip , ( eip , ) ) ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> eip . associate ( instance . id ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
