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

Proposed by Lars Kellogg-Stedman
Status: Superseded
Proposed branch: ~larsks/cloud-init:bug/ec2-tests
Merge into: cloud-init:master
Diff against target: 98 lines (+34/-3)
2 files modified
cloudinit/url_helper.py (+9/-1)
tests/unittests/test_datasource/test_ec2.py (+25/-2)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Needs Fixing
cloud-init Commiters Pending
Review via email: mp+329654@code.launchpad.net

Description of the change

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.

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:177c0edb53d53479839f5a723783a72a8ae17b5e
https://jenkins.ubuntu.com/server/job/cloud-init-ci/209/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/209/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:9e5a3601ffb3d191b53b520741b5dcebfa425452
https://jenkins.ubuntu.com/server/job/cloud-init-ci/213/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/213/rebuild

review: Needs Fixing (continuous-integration)

Unmerged commits

1884378... by Lars Kellogg-Stedman

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.

29025c3... by Lars Kellogg-Stedman

url_helper: fail gracefully if oauthlib is not available

We are unable to ship python-oauthlib in RHEL. This commit allows
imports of url_helper to succeed even when oauthlib is unavailable
and OauthUrlHelper.oauth_headers to raise a NotImplementedException
when called.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py
2index 7cf76aa..c83061a 100644
3--- a/cloudinit/url_helper.py
4+++ b/cloudinit/url_helper.py
5@@ -17,7 +17,11 @@ import time
6 from email.utils import parsedate
7 from functools import partial
8
9-import oauthlib.oauth1 as oauth1
10+try:
11+ import oauthlib.oauth1 as oauth1
12+except ImportError:
13+ oauth1 = None
14+
15 from requests import exceptions
16
17 from six.moves.urllib.parse import (
18@@ -488,6 +492,10 @@ class OauthUrlHelper(object):
19
20 def oauth_headers(url, consumer_key, token_key, token_secret, consumer_secret,
21 timestamp=None):
22+
23+ if oauth1 is None:
24+ raise NotImplementedError('oauth support is not available')
25+
26 if timestamp:
27 timestamp = str(timestamp)
28 else:
29diff --git a/tests/unittests/test_datasource/test_ec2.py b/tests/unittests/test_datasource/test_ec2.py
30index 33d0261..7c9ac70 100644
31--- a/tests/unittests/test_datasource/test_ec2.py
32+++ b/tests/unittests/test_datasource/test_ec2.py
33@@ -167,17 +167,36 @@ class TestEc2(test_helpers.HttprettyTestCase):
34 self.datasource.min_metadata_version, 'meta-data', ''])
35
36 @property
37+ def metadata_urls(self):
38+ return ['/'.join([self.metadata_addr, version, 'meta-data'])
39+ for version in
40+ [self.datasource.min_metadata_version] +
41+ self.datasource.extended_metadata_versions]
42+
43+ @property
44 def userdata_url(self):
45 return '/'.join([
46 self.metadata_addr,
47 self.datasource.min_metadata_version, 'user-data'])
48
49+ @property
50+ def userdata_urls(self):
51+ return ['/'.join([self.metadata_addr, version, 'user-data'])
52+ for version in
53+ [self.datasource.min_metadata_version] +
54+ self.datasource.extended_metadata_versions]
55+
56 def _patch_add_cleanup(self, mpath, *args, **kwargs):
57 p = mock.patch(mpath, *args, **kwargs)
58 p.start()
59 self.addCleanup(p.stop)
60
61+ def bad_uri_handler(self, req, uri, headers):
62+ print('Request for invalid URL:', uri)
63+ return (999, headers, 'Invalid request for %s' % uri)
64+
65 def _setup_ds(self, sys_cfg, platform_data, md, ud=None):
66+ self.uris = []
67 distro = {}
68 paths = helpers.Paths({})
69 if sys_cfg is None:
70@@ -189,8 +208,10 @@ class TestEc2(test_helpers.HttprettyTestCase):
71 return_value=platform_data)
72
73 if md:
74- register_mock_metaserver(self.metadata_url, md)
75- register_mock_metaserver(self.userdata_url, ud)
76+ for url in self.metadata_urls:
77+ register_mock_metaserver(url, md)
78+ for url in self.userdata_urls:
79+ register_mock_metaserver(url, ud)
80
81 return ds
82
83@@ -268,6 +289,7 @@ class TestEc2(test_helpers.HttprettyTestCase):
84 Then the metadata services is crawled for more network config info.
85 When the platform data is valid, return True.
86 """
87+
88 m_is_bsd.return_value = False
89 m_dhcp.return_value = [{
90 'interface': 'eth9', 'fixed-address': '192.168.2.9',
91@@ -278,6 +300,7 @@ class TestEc2(test_helpers.HttprettyTestCase):
92 platform_data=self.valid_platform_data,
93 sys_cfg={'datasource': {'Ec2': {'strict_id': False}}},
94 md=DEFAULT_METADATA)
95+
96 ret = ds.get_data()
97 self.assertTrue(ret)
98 m_dhcp.assert_called_once_with()

Subscribers

People subscribed via source and target branches