| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved | 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 3 # | 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a | 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the | 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including | 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- | 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit | 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- | 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: | 10 # lowing conditions: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 import unittest | 23 import unittest |
| 24 import time | 24 import time |
| 25 | 25 |
| 26 import boto | 26 import boto |
| 27 from boto.ec2.networkinterface import NetworkInterfaceCollection | 27 from boto.ec2.networkinterface import NetworkInterfaceCollection |
| 28 from boto.ec2.networkinterface import NetworkInterfaceSpecification | 28 from boto.ec2.networkinterface import NetworkInterfaceSpecification |
| 29 from boto.ec2.networkinterface import PrivateIPAddress | 29 from boto.ec2.networkinterface import PrivateIPAddress |
| 30 | 30 |
| 31 | 31 |
| 32 class TestVPCConnection(unittest.TestCase): | 32 class TestVPCConnection(unittest.TestCase): |
| 33 |
| 33 def setUp(self): | 34 def setUp(self): |
| 35 # Registry of instances to be removed |
| 36 self.instances = [] |
| 37 # Registry for cleaning up the vpc after all instances are terminated |
| 38 # in the format [ ( func, (arg1, ... argn) ) ] |
| 39 self.post_terminate_cleanups = [] |
| 40 |
| 34 self.api = boto.connect_vpc() | 41 self.api = boto.connect_vpc() |
| 35 vpc = self.api.create_vpc('10.0.0.0/16') | 42 self.vpc = self.api.create_vpc('10.0.0.0/16') |
| 36 self.addCleanup(self.api.delete_vpc, vpc.id) | |
| 37 | 43 |
| 38 # Need time for the VPC to be in place. :/ | 44 # Need time for the VPC to be in place. :/ |
| 39 time.sleep(5) | 45 time.sleep(5) |
| 40 self.subnet = self.api.create_subnet(vpc.id, '10.0.0.0/24') | 46 self.subnet = self.api.create_subnet(self.vpc.id, '10.0.0.0/24') |
| 41 self.addCleanup(self.api.delete_subnet, self.subnet.id) | 47 # Register the subnet to be deleted after instance termination |
| 48 self.post_terminate_cleanups.append((self.api.delete_subnet, (self.subne
t.id,))) |
| 42 | 49 |
| 43 # Need time for the subnet to be in place. | 50 # Need time for the subnet to be in place. |
| 44 time.sleep(10) | 51 time.sleep(10) |
| 45 | 52 |
| 53 def post_terminate_cleanup(self): |
| 54 """Helper to run clean up tasks after instances are removed.""" |
| 55 for fn, args in self.post_terminate_cleanups: |
| 56 fn(*args) |
| 57 # Give things time to catch up each time |
| 58 time.sleep(10) |
| 59 |
| 60 # Now finally delete the vpc |
| 61 if self.vpc: |
| 62 self.api.delete_vpc(self.vpc.id) |
| 63 |
| 64 def terminate_instances(self): |
| 65 """Helper to remove all instances and kick off additional cleanup |
| 66 once they are terminated. |
| 67 """ |
| 68 for instance in self.instances: |
| 69 self.terminate_instance(instance) |
| 70 self.post_terminate_cleanup() |
| 71 |
| 46 def terminate_instance(self, instance): | 72 def terminate_instance(self, instance): |
| 47 instance.terminate() | 73 instance.terminate() |
| 48 for i in xrange(300): | 74 for i in xrange(300): |
| 49 instance.update() | 75 instance.update() |
| 50 if instance.state == 'terminated': | 76 if instance.state == 'terminated': |
| 51 # Give it a litle more time to settle. | 77 # Give it a litle more time to settle. |
| 52 time.sleep(10) | 78 time.sleep(30) |
| 53 return | 79 return |
| 54 else: | 80 else: |
| 55 time.sleep(10) | 81 time.sleep(10) |
| 56 | 82 |
| 83 def delete_elastic_ip(self, eip): |
| 84 # Fetch a new copy of the eip so we're up to date |
| 85 new_eip = self.api.get_all_addresses([eip.public_ip])[0] |
| 86 if new_eip.association_id: |
| 87 new_eip.disassociate() |
| 88 new_eip.release() |
| 89 time.sleep(10) |
| 90 |
| 57 def test_multi_ip_create(self): | 91 def test_multi_ip_create(self): |
| 58 interface = NetworkInterfaceSpecification( | 92 interface = NetworkInterfaceSpecification( |
| 59 device_index=0, subnet_id=self.subnet.id, | 93 device_index=0, subnet_id=self.subnet.id, |
| 60 private_ip_address='10.0.0.21', | 94 private_ip_address='10.0.0.21', |
| 61 description="This is a test interface using boto.", | 95 description="This is a test interface using boto.", |
| 62 delete_on_termination=True, private_ip_addresses=[ | 96 delete_on_termination=True, private_ip_addresses=[ |
| 63 PrivateIPAddress(private_ip_address='10.0.0.22', | 97 PrivateIPAddress(private_ip_address='10.0.0.22', |
| 64 primary=False), | 98 primary=False), |
| 65 PrivateIPAddress(private_ip_address='10.0.0.23', | 99 PrivateIPAddress(private_ip_address='10.0.0.23', |
| 66 primary=False), | 100 primary=False), |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 delete_on_termination=True | 138 delete_on_termination=True |
| 105 ) | 139 ) |
| 106 interfaces = NetworkInterfaceCollection(interface) | 140 interfaces = NetworkInterfaceCollection(interface) |
| 107 | 141 |
| 108 reservation = self.api.run_instances( | 142 reservation = self.api.run_instances( |
| 109 image_id='ami-a0cd60c9', | 143 image_id='ami-a0cd60c9', |
| 110 instance_type='m1.small', | 144 instance_type='m1.small', |
| 111 network_interfaces=interfaces | 145 network_interfaces=interfaces |
| 112 ) | 146 ) |
| 113 instance = reservation.instances[0] | 147 instance = reservation.instances[0] |
| 114 self.addCleanup(self.terminate_instance, instance) | 148 self.instances.append(instance) |
| 149 self.addCleanup(self.terminate_instances) |
| 115 | 150 |
| 116 # Give it a **LONG** time to start up. | 151 # Give it a **LONG** time to start up. |
| 117 # Because the public IP won't be there right away. | 152 # Because the public IP won't be there right away. |
| 118 time.sleep(60) | 153 time.sleep(60) |
| 119 | 154 |
| 120 retrieved = self.api.get_all_reservations( | 155 retrieved = self.api.get_all_reservations( |
| 121 instance_ids=[ | 156 instance_ids=[ |
| 122 instance.id | 157 instance.id |
| 123 ] | 158 ] |
| 124 ) | 159 ) |
| 125 self.assertEqual(len(retrieved), 1) | 160 self.assertEqual(len(retrieved), 1) |
| 126 retrieved_instances = retrieved[0].instances | 161 retrieved_instances = retrieved[0].instances |
| 127 self.assertEqual(len(retrieved_instances), 1) | 162 self.assertEqual(len(retrieved_instances), 1) |
| 128 retrieved_instance = retrieved_instances[0] | 163 retrieved_instance = retrieved_instances[0] |
| 129 | 164 |
| 130 self.assertEqual(len(retrieved_instance.interfaces), 1) | 165 self.assertEqual(len(retrieved_instance.interfaces), 1) |
| 131 interface = retrieved_instance.interfaces[0] | 166 interface = retrieved_instance.interfaces[0] |
| 132 | 167 |
| 133 # There ought to be a public IP there. | 168 # There ought to be a public IP there. |
| 134 # We can't reason about the IP itself, so just make sure it vaguely | 169 # We can't reason about the IP itself, so just make sure it vaguely |
| 135 # resembles an IP (& isn't empty/``None``)... | 170 # resembles an IP (& isn't empty/``None``)... |
| 136 self.assertTrue(interface.publicIp.count('.') >= 3) | 171 self.assertTrue(interface.publicIp.count('.') >= 3) |
| 137 | 172 |
| 173 def test_associate_elastic_ip(self): |
| 174 interface = NetworkInterfaceSpecification( |
| 175 associate_public_ip_address=False, |
| 176 subnet_id=self.subnet.id, |
| 177 # Just for testing. |
| 178 delete_on_termination=True |
| 179 ) |
| 180 interfaces = NetworkInterfaceCollection(interface) |
| 181 |
| 182 reservation = self.api.run_instances( |
| 183 image_id='ami-a0cd60c9', |
| 184 instance_type='m1.small', |
| 185 network_interfaces=interfaces |
| 186 ) |
| 187 instance = reservation.instances[0] |
| 188 # Register instance to be removed |
| 189 self.instances.append(instance) |
| 190 # Add terminate instances helper as cleanup command |
| 191 self.addCleanup(self.terminate_instances) |
| 192 |
| 193 # Create an internet gateway so we can attach an eip |
| 194 igw = self.api.create_internet_gateway() |
| 195 # Wait on gateway before attaching |
| 196 time.sleep(5) |
| 197 # Attach and register clean up tasks |
| 198 self.api.attach_internet_gateway(igw.id, self.vpc.id) |
| 199 self.post_terminate_cleanups.append((self.api.detach_internet_gateway, (
igw.id, self.vpc.id))) |
| 200 self.post_terminate_cleanups.append((self.api.delete_internet_gateway, (
igw.id,))) |
| 201 |
| 202 # Allocate an elastic ip to this vpc |
| 203 eip = self.api.allocate_address('vpc') |
| 204 self.post_terminate_cleanups.append((self.delete_elastic_ip, (eip,))) |
| 205 |
| 206 # Wait on instance and eip then try to associate directly to instance |
| 207 time.sleep(60) |
| 208 eip.associate(instance.id) |
| 209 |
| 138 | 210 |
| 139 if __name__ == '__main__': | 211 if __name__ == '__main__': |
| 140 unittest.main() | 212 unittest.main() |
| OLD | NEW |