| OLD | NEW |
| (Empty) | |
| 1 # Copyright (c) 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 # |
| 22 import os |
| 23 from tests.unit import unittest |
| 24 |
| 25 import boto |
| 26 from boto.compat import json |
| 27 from boto.exception import BotoServerError |
| 28 from boto.regioninfo import RegionInfo, load_endpoint_json, merge_endpoints |
| 29 from boto.regioninfo import load_regions, get_regions |
| 30 |
| 31 |
| 32 class TestRegionInfo(object): |
| 33 def __init__(self, connection=None, name=None, endpoint=None, |
| 34 connection_cls=None): |
| 35 self.connection = connection |
| 36 self.name = name |
| 37 self.endpoint = endpoint |
| 38 self.connection_cls = connection_cls |
| 39 |
| 40 |
| 41 class FakeConn(object): |
| 42 pass |
| 43 |
| 44 |
| 45 class TestEndpointLoading(unittest.TestCase): |
| 46 def setUp(self): |
| 47 super(TestEndpointLoading, self).setUp() |
| 48 |
| 49 def test_load_endpoint_json(self): |
| 50 endpoints = load_endpoint_json(boto.ENDPOINTS_PATH) |
| 51 self.assertTrue('ec2' in endpoints) |
| 52 self.assertEqual( |
| 53 endpoints['ec2']['us-east-1'], |
| 54 'ec2.us-east-1.amazonaws.com' |
| 55 ) |
| 56 |
| 57 def test_merge_endpoints(self): |
| 58 defaults = { |
| 59 'ec2': { |
| 60 'us-east-1': 'ec2.us-east-1.amazonaws.com', |
| 61 'us-west-1': 'ec2.us-west-1.amazonaws.com', |
| 62 } |
| 63 } |
| 64 additions = { |
| 65 # Top-level addition. |
| 66 's3': { |
| 67 'us-east-1': 's3.amazonaws.com' |
| 68 }, |
| 69 'ec2': { |
| 70 # Overwrite. This doesn't exist, just test data. |
| 71 'us-east-1': 'ec2.auto-resolve.amazonaws.com', |
| 72 # Deep addition. |
| 73 'us-west-2': 'ec2.us-west-2.amazonaws.com', |
| 74 } |
| 75 } |
| 76 |
| 77 endpoints = merge_endpoints(defaults, additions) |
| 78 self.assertEqual(endpoints, { |
| 79 'ec2': { |
| 80 'us-east-1': 'ec2.auto-resolve.amazonaws.com', |
| 81 'us-west-1': 'ec2.us-west-1.amazonaws.com', |
| 82 'us-west-2': 'ec2.us-west-2.amazonaws.com', |
| 83 }, |
| 84 's3': { |
| 85 'us-east-1': 's3.amazonaws.com' |
| 86 } |
| 87 }) |
| 88 |
| 89 def test_load_regions(self): |
| 90 # Just the defaults. |
| 91 endpoints = load_regions() |
| 92 self.assertTrue('us-east-1' in endpoints['ec2']) |
| 93 self.assertFalse('test-1' in endpoints['ec2']) |
| 94 |
| 95 # With ENV overrides. |
| 96 os.environ['BOTO_ENDPOINTS'] = os.path.join( |
| 97 os.path.dirname(__file__), |
| 98 'test_endpoints.json' |
| 99 ) |
| 100 self.addCleanup(os.environ.pop, 'BOTO_ENDPOINTS') |
| 101 endpoints = load_regions() |
| 102 self.assertTrue('us-east-1' in endpoints['ec2']) |
| 103 self.assertTrue('test-1' in endpoints['ec2']) |
| 104 self.assertEqual(endpoints['ec2']['test-1'], 'ec2.test-1.amazonaws.com') |
| 105 |
| 106 def test_get_regions(self): |
| 107 # With defaults. |
| 108 ec2_regions = get_regions('ec2') |
| 109 self.assertEqual(len(ec2_regions), 10) |
| 110 west_2 = None |
| 111 |
| 112 for region_info in ec2_regions: |
| 113 if region_info.name == 'us-west-2': |
| 114 west_2 = region_info |
| 115 break |
| 116 |
| 117 self.assertNotEqual(west_2, None, "Couldn't find the us-west-2 region!") |
| 118 self.assertTrue(isinstance(west_2, RegionInfo)) |
| 119 self.assertEqual(west_2.name, 'us-west-2') |
| 120 self.assertEqual(west_2.endpoint, 'ec2.us-west-2.amazonaws.com') |
| 121 self.assertEqual(west_2.connection_cls, None) |
| 122 |
| 123 def test_get_regions_overrides(self): |
| 124 ec2_regions = get_regions( |
| 125 'ec2', |
| 126 region_cls=TestRegionInfo, |
| 127 connection_cls=FakeConn |
| 128 ) |
| 129 self.assertEqual(len(ec2_regions), 10) |
| 130 west_2 = None |
| 131 |
| 132 for region_info in ec2_regions: |
| 133 if region_info.name == 'us-west-2': |
| 134 west_2 = region_info |
| 135 break |
| 136 |
| 137 self.assertNotEqual(west_2, None, "Couldn't find the us-west-2 region!") |
| 138 self.assertFalse(isinstance(west_2, RegionInfo)) |
| 139 self.assertTrue(isinstance(west_2, TestRegionInfo)) |
| 140 self.assertEqual(west_2.name, 'us-west-2') |
| 141 self.assertEqual(west_2.endpoint, 'ec2.us-west-2.amazonaws.com') |
| 142 self.assertEqual(west_2.connection_cls, FakeConn) |
| 143 |
| 144 |
| 145 if __name__ == '__main__': |
| 146 unittest.main() |
| OLD | NEW |