diff options
Diffstat (limited to 'tests/unit/utils/test_utils.py')
| -rw-r--r-- | tests/unit/utils/test_utils.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 205d3d87..abb85353 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -23,8 +23,12 @@ import unittest import hashlib import hmac +import mock + from boto.utils import Password from boto.utils import pythonize_name +from boto.utils import _build_instance_metadata_url +from boto.utils import retry_url class TestPassword(unittest.TestCase): @@ -105,5 +109,82 @@ class TestPythonizeName(unittest.TestCase): self.assertEqual(pythonize_name('HTTPStatus200Ok'), 'http_status_200_ok') +class TestBuildInstanceMetadataURL(unittest.TestCase): + def test_normal(self): + # This is the all-defaults case. + self.assertEqual(_build_instance_metadata_url( + 'http://169.254.169.254', + 'latest', + 'meta-data' + ), + 'http://169.254.169.254/latest/meta-data/' + ) + + def test_custom_path(self): + self.assertEqual(_build_instance_metadata_url( + 'http://169.254.169.254', + 'latest', + 'dynamic' + ), + 'http://169.254.169.254/latest/dynamic/' + ) + + def test_custom_version(self): + self.assertEqual(_build_instance_metadata_url( + 'http://169.254.169.254', + '1.0', + 'meta-data' + ), + 'http://169.254.169.254/1.0/meta-data/' + ) + + def test_custom_url(self): + self.assertEqual(_build_instance_metadata_url( + 'http://10.0.1.5', + 'latest', + 'meta-data' + ), + 'http://10.0.1.5/latest/meta-data/' + ) + + def test_all_custom(self): + self.assertEqual(_build_instance_metadata_url( + 'http://10.0.1.5', + '2013-03-22', + 'user-data' + ), + 'http://10.0.1.5/2013-03-22/user-data/' + ) + + +class TestRetryURL(unittest.TestCase): + def setUp(self): + self.urlopen_patch = mock.patch('urllib2.urlopen') + self.opener_patch = mock.patch('urllib2.build_opener') + self.urlopen = self.urlopen_patch.start() + self.opener = self.opener_patch.start() + + def tearDown(self): + self.urlopen_patch.stop() + self.opener_patch.stop() + + def set_normal_response(self, response): + fake_response = mock.Mock() + fake_response.read.return_value = response + self.urlopen.return_value = fake_response + + def set_no_proxy_allowed_response(self, response): + fake_response = mock.Mock() + fake_response.read.return_value = response + self.opener.return_value.open.return_value = fake_response + + def test_retry_url_uses_proxy(self): + self.set_normal_response('normal response') + self.set_no_proxy_allowed_response('no proxy response') + + response = retry_url('http://10.10.10.10/foo', num_retries=1) + self.assertEqual(response, 'no proxy response') + + if __name__ == '__main__': unittest.main() |
