← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~larsks/cloud-init:bug/ec2-tests into cloud-init:master

 

Lars Kellogg-Stedman has proposed merging ~larsks/cloud-init:bug/ec2-tests into cloud-init:master.

Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~larsks/cloud-init/+git/cloud-init/+merge/329654

test_ec2: metadata tests were mocking wrong urls

The ec2 metadata tests were only mocking one version of the metadata
api, but requests were made against both.  This fixes _setup_ds to
register mock data at both versions of the API, and adds a fallback
handler to raise an http error on requests to un-mocked requests.

-- 
Your team cloud-init commiters is requested to review the proposed merge of ~larsks/cloud-init:bug/ec2-tests into cloud-init:master.
diff --git a/tests/unittests/test_datasource/test_ec2.py b/tests/unittests/test_datasource/test_ec2.py
index 33d0261..826389e 100644
--- a/tests/unittests/test_datasource/test_ec2.py
+++ b/tests/unittests/test_datasource/test_ec2.py
@@ -2,6 +2,8 @@
 
 import httpretty
 import mock
+import re
+import requests
 
 from .. import helpers as test_helpers
 from cloudinit import helpers
@@ -167,17 +169,36 @@ class TestEc2(test_helpers.HttprettyTestCase):
             self.datasource.min_metadata_version, 'meta-data', ''])
 
     @property
+    def metadata_urls(self):
+        return ['/'.join([self.metadata_addr, version, 'meta-data'])
+                for version in
+                [self.datasource.min_metadata_version] +
+                self.datasource.extended_metadata_versions]
+
+    @property
     def userdata_url(self):
         return '/'.join([
             self.metadata_addr,
             self.datasource.min_metadata_version, 'user-data'])
 
+    @property
+    def userdata_urls(self):
+        return ['/'.join([self.metadata_addr, version, 'user-data'])
+                for version in
+                [self.datasource.min_metadata_version] +
+                self.datasource.extended_metadata_versions]
+
     def _patch_add_cleanup(self, mpath, *args, **kwargs):
         p = mock.patch(mpath, *args, **kwargs)
         p.start()
         self.addCleanup(p.stop)
 
+    def bad_uri_handler(self, req, uri, headers):
+        print 'Request for invalid URL:', uri
+        return (999, headers, 'Invalid request for %s' % uri)
+
     def _setup_ds(self, sys_cfg, platform_data, md, ud=None):
+        self.uris = []
         distro = {}
         paths = helpers.Paths({})
         if sys_cfg is None:
@@ -188,9 +209,25 @@ class TestEc2(test_helpers.HttprettyTestCase):
                 "cloudinit.sources.DataSourceEc2._collect_platform_data",
                 return_value=platform_data)
 
+        # This is a catchall that will raise an exception if we attempt
+        # to request a URL we're not explicitly handling.
+
+        httpretty.http.STATUSES[999] = 'Attempt to request unhandled URL'
         if md:
-            register_mock_metaserver(self.metadata_url, md)
-            register_mock_metaserver(self.userdata_url, ud)
+            for url in self.metadata_urls:
+                register_mock_metaserver(url, md)
+            for url in self.userdata_urls:
+                register_mock_metaserver(url, ud)
+
+        # This is a fallback mechanism that will return http status
+        # code 999 in the event that something requests a
+        # url we're not explicitly catching.
+        httpretty.register_uri(
+            httpretty.GET,
+            re.compile('.*'),
+            body=self.bad_uri_handler,
+            priority=-1
+        )
 
         return ds
 
@@ -258,6 +295,15 @@ class TestEc2(test_helpers.HttprettyTestCase):
             self.logs.getvalue())
 
     @httpretty.activate
+    def test_fetch_invalid_url(self):
+        self._setup_ds(
+            platform_data=self.valid_platform_data,
+            sys_cfg={'datasource': {'Ec2': {'strict_id': False}}},
+            md=DEFAULT_METADATA)
+        res = requests.get('%s/does-not-exist' % self.metadata_url)
+        self.assertEqual(res.status_code, 999)
+
+    @httpretty.activate
     @mock.patch('cloudinit.net.EphemeralIPv4Network')
     @mock.patch('cloudinit.net.dhcp.maybe_perform_dhcp_discovery')
     @mock.patch('cloudinit.sources.DataSourceEc2.util.is_FreeBSD')
@@ -268,6 +314,7 @@ class TestEc2(test_helpers.HttprettyTestCase):
         Then the metadata services is crawled for more network config info.
         When the platform data is valid, return True.
         """
+
         m_is_bsd.return_value = False
         m_dhcp.return_value = [{
             'interface': 'eth9', 'fixed-address': '192.168.2.9',
@@ -278,6 +325,7 @@ class TestEc2(test_helpers.HttprettyTestCase):
             platform_data=self.valid_platform_data,
             sys_cfg={'datasource': {'Ec2': {'strict_id': False}}},
             md=DEFAULT_METADATA)
+
         ret = ds.get_data()
         self.assertTrue(ret)
         m_dhcp.assert_called_once_with()

Follow ups