| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 import httplib | 2 import httplib |
| 3 | 3 |
| 4 from datetime import datetime, timedelta | 4 from datetime import datetime, timedelta |
| 5 from mock import MagicMock, Mock, patch | 5 from mock import MagicMock, Mock |
| 6 from tests.unit import unittest | 6 from tests.unit import unittest |
| 7 from tests.unit import AWSMockServiceTestCase | 7 from tests.unit import AWSMockServiceTestCase |
| 8 | 8 |
| 9 import boto.ec2 | 9 import boto.ec2 |
| 10 | 10 |
| 11 from boto.regioninfo import RegionInfo | 11 from boto.regioninfo import RegionInfo |
| 12 from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping | 12 from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping |
| 13 from boto.ec2.connection import EC2Connection | 13 from boto.ec2.connection import EC2Connection |
| 14 from boto.ec2.snapshot import Snapshot | 14 from boto.ec2.snapshot import Snapshot |
| 15 from boto.ec2.reservedinstance import ReservedInstancesConfiguration | 15 from boto.ec2.reservedinstance import ReservedInstancesConfiguration |
| (...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 </item> | 695 </item> |
| 696 <item> | 696 <item> |
| 697 <privateIpAddress>10.0.0.150</privateIpAddress> | 697 <privateIpAddress>10.0.0.150</privateIpAddress> |
| 698 <primary>false</primary> | 698 <primary>false</primary> |
| 699 </item> | 699 </item> |
| 700 </privateIpAddressesSet> | 700 </privateIpAddressesSet> |
| 701 </item> | 701 </item> |
| 702 </networkInterfaceSet> | 702 </networkInterfaceSet> |
| 703 </DescribeNetworkInterfacesResponse>""" | 703 </DescribeNetworkInterfacesResponse>""" |
| 704 | 704 |
| 705 def test_get_all_network_interfaces(self): |
| 706 self.set_http_response(status_code=200) |
| 707 result = self.ec2.get_all_network_interfaces(network_interface_ids=['eni
-0f62d866']) |
| 708 self.assert_request_parameters({ |
| 709 'Action': 'DescribeNetworkInterfaces', |
| 710 'NetworkInterfaceId.1': 'eni-0f62d866'}, |
| 711 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 712 'SignatureVersion', 'Timestamp', |
| 713 'Version']) |
| 714 self.assertEqual(len(result), 1) |
| 715 self.assertEqual(result[0].id, 'eni-0f62d866') |
| 716 |
| 705 def test_attachment_has_device_index(self): | 717 def test_attachment_has_device_index(self): |
| 706 self.set_http_response(status_code=200) | 718 self.set_http_response(status_code=200) |
| 707 parsed = self.ec2.get_all_network_interfaces() | 719 parsed = self.ec2.get_all_network_interfaces() |
| 708 | 720 |
| 709 self.assertEqual(5, parsed[0].attachment.device_index) | 721 self.assertEqual(5, parsed[0].attachment.device_index) |
| 710 | 722 |
| 711 class TestGetAllImages(TestEC2ConnectionBase): | 723 class TestGetAllImages(TestEC2ConnectionBase): |
| 712 def default_body(self): | 724 def default_body(self): |
| 713 return """ | 725 return """ |
| 714 <DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2013-02-01/"> | 726 <DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2013-02-01/"> |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 961 """ | 973 """ |
| 962 Test snapshot trimming functionality by ensuring that expected calls | 974 Test snapshot trimming functionality by ensuring that expected calls |
| 963 are made when given a known set of volume snapshots. | 975 are made when given a known set of volume snapshots. |
| 964 """ | 976 """ |
| 965 def _get_snapshots(self): | 977 def _get_snapshots(self): |
| 966 """ | 978 """ |
| 967 Generate a list of fake snapshots with names and dates. | 979 Generate a list of fake snapshots with names and dates. |
| 968 """ | 980 """ |
| 969 snaps = [] | 981 snaps = [] |
| 970 | 982 |
| 971 # Generate some dates offset by days, weeks, months | 983 # Generate some dates offset by days, weeks, months. |
| 984 # This is to validate the various types of snapshot logic handled by |
| 985 # ``trim_snapshots``. |
| 972 now = datetime.now() | 986 now = datetime.now() |
| 973 dates = [ | 987 dates = [ |
| 974 now, | 988 now, |
| 975 now - timedelta(days=1), | 989 now - timedelta(days=1), |
| 976 now - timedelta(days=2), | 990 now - timedelta(days=2), |
| 977 now - timedelta(days=7), | 991 now - timedelta(days=7), |
| 978 now - timedelta(days=14), | 992 now - timedelta(days=14), |
| 979 datetime(now.year, now.month, 1) - timedelta(days=30), | 993 # We want to simulate 30/60/90-day snapshots, but February is |
| 980 datetime(now.year, now.month, 1) - timedelta(days=60), | 994 # short (only 28 days), so we decrease the delta by 2 days apiece. |
| 981 datetime(now.year, now.month, 1) - timedelta(days=90) | 995 # This prevents the ``delete_snapshot`` code below from being |
| 996 # called, since they don't fall outside the allowed timeframes |
| 997 # for the snapshots. |
| 998 datetime(now.year, now.month, 1) - timedelta(days=28), |
| 999 datetime(now.year, now.month, 1) - timedelta(days=58), |
| 1000 datetime(now.year, now.month, 1) - timedelta(days=88) |
| 982 ] | 1001 ] |
| 983 | 1002 |
| 984 for date in dates: | 1003 for date in dates: |
| 985 # Create a fake snapshot for each date | 1004 # Create a fake snapshot for each date |
| 986 snap = Snapshot(self.ec2) | 1005 snap = Snapshot(self.ec2) |
| 987 snap.tags['Name'] = 'foo' | 1006 snap.tags['Name'] = 'foo' |
| 988 # Times are expected to be ISO8601 strings | 1007 # Times are expected to be ISO8601 strings |
| 989 snap.start_time = date.strftime('%Y-%m-%dT%H:%M:%S.000Z') | 1008 snap.start_time = date.strftime('%Y-%m-%dT%H:%M:%S.000Z') |
| 990 snaps.append(snap) | 1009 snaps.append(snap) |
| 991 | 1010 |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1239 'Action': 'RegisterImage', | 1258 'Action': 'RegisterImage', |
| 1240 'ImageLocation': 's3://foo', | 1259 'ImageLocation': 's3://foo', |
| 1241 'Name': 'name', | 1260 'Name': 'name', |
| 1242 'Description': 'description', | 1261 'Description': 'description', |
| 1243 'SriovNetSupport': 'simple' | 1262 'SriovNetSupport': 'simple' |
| 1244 }, ignore_params_values=[ | 1263 }, ignore_params_values=[ |
| 1245 'AWSAccessKeyId', 'SignatureMethod', | 1264 'AWSAccessKeyId', 'SignatureMethod', |
| 1246 'SignatureVersion', 'Timestamp', | 1265 'SignatureVersion', 'Timestamp', |
| 1247 'Version' | 1266 'Version' |
| 1248 ]) | 1267 ]) |
| 1268 |
| 1269 def test_volume_delete_on_termination_on(self): |
| 1270 self.set_http_response(status_code=200) |
| 1271 self.ec2.register_image('name', 'description', |
| 1272 snapshot_id='snap-12345678', |
| 1273 delete_root_volume_on_termination=True) |
| 1274 |
| 1275 self.assert_request_parameters({ |
| 1276 'Action': 'RegisterImage', |
| 1277 'Name': 'name', |
| 1278 'Description': 'description', |
| 1279 'BlockDeviceMapping.1.DeviceName': None, |
| 1280 'BlockDeviceMapping.1.Ebs.DeleteOnTermination' : 'true', |
| 1281 'BlockDeviceMapping.1.Ebs.SnapshotId': 'snap-12345678', |
| 1282 }, ignore_params_values=[ |
| 1283 'AWSAccessKeyId', 'SignatureMethod', |
| 1284 'SignatureVersion', 'Timestamp', |
| 1285 'Version' |
| 1286 ]) |
| 1287 |
| 1288 |
| 1289 def test_volume_delete_on_termination_default(self): |
| 1290 self.set_http_response(status_code=200) |
| 1291 self.ec2.register_image('name', 'description', |
| 1292 snapshot_id='snap-12345678') |
| 1293 |
| 1294 self.assert_request_parameters({ |
| 1295 'Action': 'RegisterImage', |
| 1296 'Name': 'name', |
| 1297 'Description': 'description', |
| 1298 'BlockDeviceMapping.1.DeviceName': None, |
| 1299 'BlockDeviceMapping.1.Ebs.DeleteOnTermination' : 'false', |
| 1300 'BlockDeviceMapping.1.Ebs.SnapshotId': 'snap-12345678', |
| 1301 }, ignore_params_values=[ |
| 1302 'AWSAccessKeyId', 'SignatureMethod', |
| 1303 'SignatureVersion', 'Timestamp', |
| 1304 'Version' |
| 1305 ]) |
| 1249 | 1306 |
| 1250 | 1307 |
| 1251 class TestTerminateInstances(TestEC2ConnectionBase): | 1308 class TestTerminateInstances(TestEC2ConnectionBase): |
| 1252 def default_body(self): | 1309 def default_body(self): |
| 1253 return """<?xml version="1.0" ?> | 1310 return """<?xml version="1.0" ?> |
| 1254 <TerminateInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2013
-07-15/"> | 1311 <TerminateInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2013
-07-15/"> |
| 1255 <requestId>req-59a9ad52-0434-470c-ad48-4f89ded3a03e</requestId> | 1312 <requestId>req-59a9ad52-0434-470c-ad48-4f89ded3a03e</requestId> |
| 1256 <instancesSet> | 1313 <instancesSet> |
| 1257 <item> | 1314 <item> |
| 1258 <instanceId>i-000043a2</instanceId> | 1315 <instanceId>i-000043a2</instanceId> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1283 """ | 1340 """ |
| 1284 | 1341 |
| 1285 def test_default_behavior(self): | 1342 def test_default_behavior(self): |
| 1286 self.set_http_response(status_code=200) | 1343 self.set_http_response(status_code=200) |
| 1287 self.ec2.get_all_instances() | 1344 self.ec2.get_all_instances() |
| 1288 self.assert_request_parameters({ | 1345 self.assert_request_parameters({ |
| 1289 'Action': 'DescribeInstances'}, | 1346 'Action': 'DescribeInstances'}, |
| 1290 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', | 1347 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 1291 'SignatureVersion', 'Timestamp', 'Version']) | 1348 'SignatureVersion', 'Timestamp', 'Version']) |
| 1292 | 1349 |
| 1350 self.ec2.get_all_reservations() |
| 1351 self.assert_request_parameters({ |
| 1352 'Action': 'DescribeInstances'}, |
| 1353 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 1354 'SignatureVersion', 'Timestamp', 'Version']) |
| 1355 |
| 1356 self.ec2.get_only_instances() |
| 1357 self.assert_request_parameters({ |
| 1358 'Action': 'DescribeInstances'}, |
| 1359 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 1360 'SignatureVersion', 'Timestamp', 'Version']) |
| 1361 |
| 1293 def test_max_results(self): | 1362 def test_max_results(self): |
| 1294 self.set_http_response(status_code=200) | 1363 self.set_http_response(status_code=200) |
| 1295 self.ec2.get_all_instances( | 1364 self.ec2.get_all_instances( |
| 1296 max_results=10 | 1365 max_results=10 |
| 1297 ) | 1366 ) |
| 1298 self.assert_request_parameters({ | 1367 self.assert_request_parameters({ |
| 1299 'Action': 'DescribeInstances', | 1368 'Action': 'DescribeInstances', |
| 1300 'MaxResults': 10}, | 1369 'MaxResults': 10}, |
| 1301 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', | 1370 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 1302 'SignatureVersion', 'Timestamp', 'Version']) | 1371 'SignatureVersion', 'Timestamp', 'Version']) |
| 1303 | 1372 |
| 1373 def test_next_token(self): |
| 1374 self.set_http_response(status_code=200) |
| 1375 self.ec2.get_all_reservations( |
| 1376 next_token='abcdefgh', |
| 1377 ) |
| 1378 self.assert_request_parameters({ |
| 1379 'Action': 'DescribeInstances', |
| 1380 'NextToken': 'abcdefgh'}, |
| 1381 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 1382 'SignatureVersion', 'Timestamp', 'Version']) |
| 1304 | 1383 |
| 1305 class TestDescribeTags(TestEC2ConnectionBase): | 1384 class TestDescribeTags(TestEC2ConnectionBase): |
| 1306 | 1385 |
| 1307 def default_body(self): | 1386 def default_body(self): |
| 1308 return """ | 1387 return """ |
| 1309 <DescribeTagsResponse> | 1388 <DescribeTagsResponse> |
| 1310 </DescribeTagsResponse> | 1389 </DescribeTagsResponse> |
| 1311 """ | 1390 """ |
| 1312 | 1391 |
| 1313 def test_default_behavior(self): | 1392 def test_default_behavior(self): |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1393 </Response> | 1472 </Response> |
| 1394 """ | 1473 """ |
| 1395 | 1474 |
| 1396 def test_associate_address(self): | 1475 def test_associate_address(self): |
| 1397 self.set_http_response(status_code=200) | 1476 self.set_http_response(status_code=200) |
| 1398 result = self.ec2.associate_address(instance_id='i-1234', | 1477 result = self.ec2.associate_address(instance_id='i-1234', |
| 1399 public_ip='192.0.2.1') | 1478 public_ip='192.0.2.1') |
| 1400 self.assertEqual(False, result) | 1479 self.assertEqual(False, result) |
| 1401 | 1480 |
| 1402 | 1481 |
| 1482 class TestDescribeVolumes(TestEC2ConnectionBase): |
| 1483 def default_body(self): |
| 1484 return """ |
| 1485 <DescribeVolumesResponse xmlns="http://ec2.amazonaws.com/doc/2014-02
-01/"> |
| 1486 <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> |
| 1487 <volumeSet> |
| 1488 <item> |
| 1489 <volumeId>vol-1a2b3c4d</volumeId> |
| 1490 <size>80</size> |
| 1491 <snapshotId/> |
| 1492 <availabilityZone>us-east-1a</availabilityZone> |
| 1493 <status>in-use</status> |
| 1494 <createTime>YYYY-MM-DDTHH:MM:SS.SSSZ</createTime> |
| 1495 <attachmentSet> |
| 1496 <item> |
| 1497 <volumeId>vol-1a2b3c4d</volumeId> |
| 1498 <instanceId>i-1a2b3c4d</instanceId> |
| 1499 <device>/dev/sdh</device> |
| 1500 <status>attached</status> |
| 1501 <attachTime>YYYY-MM-DDTHH:MM:SS.SSSZ</attachTime> |
| 1502 <deleteOnTermination>false</deleteOnTermination> |
| 1503 </item> |
| 1504 </attachmentSet> |
| 1505 <volumeType>standard</volumeType> |
| 1506 <encrypted>true</encrypted> |
| 1507 </item> |
| 1508 <item> |
| 1509 <volumeId>vol-5e6f7a8b</volumeId> |
| 1510 <size>80</size> |
| 1511 <snapshotId/> |
| 1512 <availabilityZone>us-east-1a</availabilityZone> |
| 1513 <status>in-use</status> |
| 1514 <createTime>YYYY-MM-DDTHH:MM:SS.SSSZ</createTime> |
| 1515 <attachmentSet> |
| 1516 <item> |
| 1517 <volumeId>vol-5e6f7a8b</volumeId> |
| 1518 <instanceId>i-5e6f7a8b</instanceId> |
| 1519 <device>/dev/sdz</device> |
| 1520 <status>attached</status> |
| 1521 <attachTime>YYYY-MM-DDTHH:MM:SS.SSSZ</attachTime> |
| 1522 <deleteOnTermination>false</deleteOnTermination> |
| 1523 </item> |
| 1524 </attachmentSet> |
| 1525 <volumeType>standard</volumeType> |
| 1526 <encrypted>false</encrypted> |
| 1527 </item> |
| 1528 </volumeSet> |
| 1529 </DescribeVolumesResponse> |
| 1530 """ |
| 1531 |
| 1532 def test_get_all_volumes(self): |
| 1533 self.set_http_response(status_code=200) |
| 1534 result = self.ec2.get_all_volumes(volume_ids=['vol-1a2b3c4d', 'vol-5e6f7
a8b']) |
| 1535 self.assert_request_parameters({ |
| 1536 'Action': 'DescribeVolumes', |
| 1537 'VolumeId.1': 'vol-1a2b3c4d', |
| 1538 'VolumeId.2': 'vol-5e6f7a8b'}, |
| 1539 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 1540 'SignatureVersion', 'Timestamp', |
| 1541 'Version']) |
| 1542 self.assertEqual(len(result), 2) |
| 1543 self.assertEqual(result[0].id, 'vol-1a2b3c4d') |
| 1544 self.assertTrue(result[0].encrypted) |
| 1545 self.assertEqual(result[1].id, 'vol-5e6f7a8b') |
| 1546 self.assertFalse(result[1].encrypted) |
| 1547 |
| 1548 |
| 1549 class TestDescribeSnapshots(TestEC2ConnectionBase): |
| 1550 def default_body(self): |
| 1551 return """ |
| 1552 <DescribeSnapshotsResponse xmlns="http://ec2.amazonaws.com/doc/2014-
02-01/"> |
| 1553 <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> |
| 1554 <snapshotSet> |
| 1555 <item> |
| 1556 <snapshotId>snap-1a2b3c4d</snapshotId> |
| 1557 <volumeId>vol-1a2b3c4d</volumeId> |
| 1558 <status>pending</status> |
| 1559 <startTime>YYYY-MM-DDTHH:MM:SS.SSSZ</startTime> |
| 1560 <progress>80%</progress> |
| 1561 <ownerId>111122223333</ownerId> |
| 1562 <volumeSize>15</volumeSize> |
| 1563 <description>Daily Backup</description> |
| 1564 <tagSet/> |
| 1565 <encrypted>true</encrypted> |
| 1566 </item> |
| 1567 </snapshotSet> |
| 1568 <snapshotSet> |
| 1569 <item> |
| 1570 <snapshotId>snap-5e6f7a8b</snapshotId> |
| 1571 <volumeId>vol-5e6f7a8b</volumeId> |
| 1572 <status>completed</status> |
| 1573 <startTime>YYYY-MM-DDTHH:MM:SS.SSSZ</startTime> |
| 1574 <progress>100%</progress> |
| 1575 <ownerId>111122223333</ownerId> |
| 1576 <volumeSize>15</volumeSize> |
| 1577 <description>Daily Backup</description> |
| 1578 <tagSet/> |
| 1579 <encrypted>false</encrypted> |
| 1580 </item> |
| 1581 </snapshotSet> |
| 1582 </DescribeSnapshotsResponse> |
| 1583 """ |
| 1584 |
| 1585 def test_get_all_snapshots(self): |
| 1586 self.set_http_response(status_code=200) |
| 1587 result = self.ec2.get_all_snapshots(snapshot_ids=['snap-1a2b3c4d', 'snap
-5e6f7a8b']) |
| 1588 self.assert_request_parameters({ |
| 1589 'Action': 'DescribeSnapshots', |
| 1590 'SnapshotId.1': 'snap-1a2b3c4d', |
| 1591 'SnapshotId.2': 'snap-5e6f7a8b'}, |
| 1592 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 1593 'SignatureVersion', 'Timestamp', |
| 1594 'Version']) |
| 1595 self.assertEqual(len(result), 2) |
| 1596 self.assertEqual(result[0].id, 'snap-1a2b3c4d') |
| 1597 self.assertTrue(result[0].encrypted) |
| 1598 self.assertEqual(result[1].id, 'snap-5e6f7a8b') |
| 1599 self.assertFalse(result[1].encrypted) |
| 1600 |
| 1601 |
| 1602 class TestCreateVolume(TestEC2ConnectionBase): |
| 1603 def default_body(self): |
| 1604 return """ |
| 1605 <CreateVolumeResponse xmlns="http://ec2.amazonaws.com/doc/2014-05-01
/"> |
| 1606 <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> |
| 1607 <volumeId>vol-1a2b3c4d</volumeId> |
| 1608 <size>80</size> |
| 1609 <snapshotId/> |
| 1610 <availabilityZone>us-east-1a</availabilityZone> |
| 1611 <status>creating</status> |
| 1612 <createTime>YYYY-MM-DDTHH:MM:SS.000Z</createTime> |
| 1613 <volumeType>standard</volumeType> |
| 1614 <encrypted>true</encrypted> |
| 1615 </CreateVolumeResponse> |
| 1616 """ |
| 1617 |
| 1618 def test_create_volume(self): |
| 1619 self.set_http_response(status_code=200) |
| 1620 result = self.ec2.create_volume(80, 'us-east-1e', snapshot='snap-1a2b3c4
d', |
| 1621 encrypted=True) |
| 1622 self.assert_request_parameters({ |
| 1623 'Action': 'CreateVolume', |
| 1624 'AvailabilityZone': 'us-east-1e', |
| 1625 'Size': 80, |
| 1626 'SnapshotId': 'snap-1a2b3c4d', |
| 1627 'Encrypted': 'true'}, |
| 1628 ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', |
| 1629 'SignatureVersion', 'Timestamp', |
| 1630 'Version']) |
| 1631 self.assertEqual(result.id, 'vol-1a2b3c4d') |
| 1632 self.assertTrue(result.encrypted) |
| 1633 |
| 1403 if __name__ == '__main__': | 1634 if __name__ == '__main__': |
| 1404 unittest.main() | 1635 unittest.main() |
| OLD | NEW |