1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#!/usr/bin/env python
# Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
from tests.compat import mock, unittest
from boto.exception import BotoClientError
from boto.ec2.networkinterface import NetworkInterfaceCollection
from boto.ec2.networkinterface import NetworkInterfaceSpecification
from boto.ec2.networkinterface import PrivateIPAddress
from boto.ec2.networkinterface import Attachment, NetworkInterface
class NetworkInterfaceTests(unittest.TestCase):
def setUp(self):
self.attachment = Attachment()
self.attachment.id = 'eni-attach-1'
self.attachment.instance_id = 10
self.attachment.status = "some status"
self.attachment.device_index = 100
self.eni_one = NetworkInterface()
self.eni_one.id = 'eni-1'
self.eni_one.status = "one_status"
self.eni_one.attachment = self.attachment
self.eni_two = NetworkInterface()
self.eni_two.connection = mock.Mock()
self.eni_two.id = 'eni-2'
self.eni_two.status = "two_status"
self.eni_two.attachment = None
def test_update_with_validate_true_raises_value_error(self):
self.eni_one.connection = mock.Mock()
self.eni_one.connection.get_all_network_interfaces.return_value = []
with self.assertRaisesRegexp(ValueError, "^eni-1 is not a valid ENI ID$"):
self.eni_one.update(True)
def test_update_with_result_set_greater_than_0_updates_dict(self):
self.eni_two.connection.get_all_network_interfaces.return_value = [self.eni_one]
self.eni_two.update()
assert all([self.eni_two.status == "one_status",
self.eni_two.id == 'eni-1',
self.eni_two.attachment == self.attachment])
def test_update_returns_status(self):
self.eni_one.connection = mock.Mock()
self.eni_one.connection.get_all_network_interfaces.return_value = [self.eni_two]
retval = self.eni_one.update()
self.assertEqual(retval, "two_status")
def test_attach_calls_attach_eni(self):
self.eni_one.connection = mock.Mock()
self.eni_one.attach("instance_id", 11)
self.eni_one.connection.attach_network_interface.assert_called_with(
'eni-1',
"instance_id",
11,
dry_run=False
)
def test_detach_calls_detach_network_interface(self):
self.eni_one.connection = mock.Mock()
self.eni_one.detach()
self.eni_one.connection.detach_network_interface.assert_called_with(
'eni-attach-1',
False,
dry_run=False
)
def test_detach_with_no_attach_data(self):
self.eni_two.connection = mock.Mock()
self.eni_two.detach()
self.eni_two.connection.detach_network_interface.assert_called_with(
None, False, dry_run=False)
def test_detach_with_force_calls_detach_network_interface_with_force(self):
self.eni_one.connection = mock.Mock()
self.eni_one.detach(True)
self.eni_one.connection.detach_network_interface.assert_called_with(
'eni-attach-1', True, dry_run=False)
class TestNetworkInterfaceCollection(unittest.TestCase):
maxDiff = None
def setUp(self):
self.private_ip_address1 = PrivateIPAddress(
private_ip_address='10.0.0.10', primary=False)
self.private_ip_address2 = PrivateIPAddress(
private_ip_address='10.0.0.11', primary=False)
self.network_interfaces_spec1 = NetworkInterfaceSpecification(
device_index=1, subnet_id='subnet_id',
description='description1',
private_ip_address='10.0.0.54', delete_on_termination=False,
private_ip_addresses=[self.private_ip_address1,
self.private_ip_address2]
)
self.private_ip_address3 = PrivateIPAddress(
private_ip_address='10.0.1.10', primary=False)
self.private_ip_address4 = PrivateIPAddress(
private_ip_address='10.0.1.11', primary=False)
self.network_interfaces_spec2 = NetworkInterfaceSpecification(
device_index=2, subnet_id='subnet_id2',
description='description2',
groups=['group_id1', 'group_id2'],
private_ip_address='10.0.1.54', delete_on_termination=False,
private_ip_addresses=[self.private_ip_address3,
self.private_ip_address4]
)
self.network_interfaces_spec3 = NetworkInterfaceSpecification(
device_index=0, subnet_id='subnet_id2',
description='description2',
groups=['group_id1', 'group_id2'],
private_ip_address='10.0.1.54', delete_on_termination=False,
private_ip_addresses=[self.private_ip_address3,
self.private_ip_address4],
associate_public_ip_address=True
)
def test_param_serialization(self):
collection = NetworkInterfaceCollection(self.network_interfaces_spec1,
self.network_interfaces_spec2)
params = {}
collection.build_list_params(params)
self.assertDictEqual(params, {
'NetworkInterface.0.DeviceIndex': '1',
'NetworkInterface.0.DeleteOnTermination': 'false',
'NetworkInterface.0.Description': 'description1',
'NetworkInterface.0.PrivateIpAddress': '10.0.0.54',
'NetworkInterface.0.SubnetId': 'subnet_id',
'NetworkInterface.0.PrivateIpAddresses.0.Primary': 'false',
'NetworkInterface.0.PrivateIpAddresses.0.PrivateIpAddress':
'10.0.0.10',
'NetworkInterface.0.PrivateIpAddresses.1.Primary': 'false',
'NetworkInterface.0.PrivateIpAddresses.1.PrivateIpAddress':
'10.0.0.11',
'NetworkInterface.1.DeviceIndex': '2',
'NetworkInterface.1.Description': 'description2',
'NetworkInterface.1.DeleteOnTermination': 'false',
'NetworkInterface.1.PrivateIpAddress': '10.0.1.54',
'NetworkInterface.1.SubnetId': 'subnet_id2',
'NetworkInterface.1.SecurityGroupId.0': 'group_id1',
'NetworkInterface.1.SecurityGroupId.1': 'group_id2',
'NetworkInterface.1.PrivateIpAddresses.0.Primary': 'false',
'NetworkInterface.1.PrivateIpAddresses.0.PrivateIpAddress':
'10.0.1.10',
'NetworkInterface.1.PrivateIpAddresses.1.Primary': 'false',
'NetworkInterface.1.PrivateIpAddresses.1.PrivateIpAddress':
'10.0.1.11',
})
def test_add_prefix_to_serialization(self):
collection = NetworkInterfaceCollection(self.network_interfaces_spec1,
self.network_interfaces_spec2)
params = {}
collection.build_list_params(params, prefix='LaunchSpecification.')
# We already tested the actual serialization previously, so
# we're just checking a few keys to make sure we get the proper
# prefix.
self.assertDictEqual(params, {
'LaunchSpecification.NetworkInterface.0.DeviceIndex': '1',
'LaunchSpecification.NetworkInterface.0.DeleteOnTermination':
'false',
'LaunchSpecification.NetworkInterface.0.Description':
'description1',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddress':
'10.0.0.54',
'LaunchSpecification.NetworkInterface.0.SubnetId': 'subnet_id',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddresses.0.Primary':
'false',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddresses.0.PrivateIpAddress':
'10.0.0.10',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddresses.1.Primary': 'false',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddresses.1.PrivateIpAddress':
'10.0.0.11',
'LaunchSpecification.NetworkInterface.1.DeviceIndex': '2',
'LaunchSpecification.NetworkInterface.1.Description':
'description2',
'LaunchSpecification.NetworkInterface.1.DeleteOnTermination':
'false',
'LaunchSpecification.NetworkInterface.1.PrivateIpAddress':
'10.0.1.54',
'LaunchSpecification.NetworkInterface.1.SubnetId': 'subnet_id2',
'LaunchSpecification.NetworkInterface.1.SecurityGroupId.0':
'group_id1',
'LaunchSpecification.NetworkInterface.1.SecurityGroupId.1':
'group_id2',
'LaunchSpecification.NetworkInterface.1.PrivateIpAddresses.0.Primary':
'false',
'LaunchSpecification.NetworkInterface.1.PrivateIpAddresses.0.PrivateIpAddress':
'10.0.1.10',
'LaunchSpecification.NetworkInterface.1.PrivateIpAddresses.1.Primary':
'false',
'LaunchSpecification.NetworkInterface.1.PrivateIpAddresses.1.PrivateIpAddress':
'10.0.1.11',
})
def test_cant_use_public_ip(self):
collection = NetworkInterfaceCollection(self.network_interfaces_spec3,
self.network_interfaces_spec1)
params = {}
# First, verify we can't incorrectly create multiple interfaces with
# on having a public IP.
with self.assertRaises(BotoClientError):
collection.build_list_params(params, prefix='LaunchSpecification.')
# Next, ensure it can't be on device index 1.
self.network_interfaces_spec3.device_index = 1
collection = NetworkInterfaceCollection(self.network_interfaces_spec3)
params = {}
with self.assertRaises(BotoClientError):
collection.build_list_params(params, prefix='LaunchSpecification.')
def test_public_ip(self):
# With public IP.
collection = NetworkInterfaceCollection(self.network_interfaces_spec3)
params = {}
collection.build_list_params(params, prefix='LaunchSpecification.')
self.assertDictEqual(params, {
'LaunchSpecification.NetworkInterface.0.AssociatePublicIpAddress':
'true',
'LaunchSpecification.NetworkInterface.0.DeviceIndex': '0',
'LaunchSpecification.NetworkInterface.0.DeleteOnTermination':
'false',
'LaunchSpecification.NetworkInterface.0.Description':
'description2',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddress':
'10.0.1.54',
'LaunchSpecification.NetworkInterface.0.SubnetId': 'subnet_id2',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddresses.0.Primary':
'false',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddresses.0.PrivateIpAddress':
'10.0.1.10',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddresses.1.Primary':
'false',
'LaunchSpecification.NetworkInterface.0.PrivateIpAddresses.1.PrivateIpAddress':
'10.0.1.11',
'LaunchSpecification.NetworkInterface.0.SecurityGroupId.0':
'group_id1',
'LaunchSpecification.NetworkInterface.0.SecurityGroupId.1':
'group_id2',
})
if __name__ == '__main__':
unittest.main()
|