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

Proposed by Lars Kellogg-Stedman on 2017-08-26
Status: Rejected
Rejected by: Chad Smith on 2017-08-31
Proposed branch: ~larsks/cloud-init:bug/ec2-tests
Merge into: cloud-init:master
Prerequisite: ~larsks/cloud-init:feature/hide-oauthlib-import-failure
Diff against target: 70 lines (+25/-2)
1 file modified
tests/unittests/test_datasource/test_ec2.py (+25/-2)
Reviewer Review Type Date Requested Status
Lars Kellogg-Stedman (community) Approve on 2017-09-01
Scott Moser 2017-08-26 Needs Fixing on 2017-08-29
Server Team CI bot continuous-integration 2017-08-26 Approve on 2017-08-26
Review via email: mp+329660@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.

PASSED: Continuous integration, rev:1884378859ccd5638dc2e52d1082b89e892c7ad6
https://jenkins.ubuntu.com/server/job/cloud-init-ci/214/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

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

review: Approve (continuous-integration)
Scott Moser (smoser) wrote :

Lars,
As this is right now, it is definitely 'needs_fixing'.
 - you are not registering / using bad_uri_handler at all.
 - as a result your commit message doesn't match the code changes.

I recall from talking to you that this is because 'tox -e xenial' (xenial/ubuntu 16.04 having an older version of httppretty). The memory is fuzzy in my head though.

I do think we want to register a 'not found' response / exception, and make sure nothing ever gets out, as our unit tests shouldn't hit external resources.

If we simply can't achieve that with 16.04 versions of packages, then i'd be ok for these tests to be skipIf in that scenario.

review: Needs Fixing
Chad Smith (chad.smith) wrote :

Lars, thank you for this. I replaced this content with a slightly different approach which only mocks the instance-id of the extended_metadata_versions as that's the only url that is checked for 404s when determining which metadata service version to use.

I'm marking as Rejected only because a slightly different take for bad_url_handling and unit test setup to make a minimally mocked set of metadata endpoints.

For your and my reference in future, it looks like the following allows httpretty to raise MockErrors for unmocked url attempts from unit tests:

httpretty.HTTPretty.allow_net_connect=False.

Lars Kellogg-Stedman (larsks) wrote :

I do appreciate the chance to fix my own merge requests rather than having them replaced, but it's too late for that. In any case I'm glad it's working. You'll note that the final version of my code simply wasn't using the bad_uri_handler anymore, because there was no way to support that under Xenial.

review: Disapprove
Lars Kellogg-Stedman (larsks) wrote :

Actually, I didn't mean to reject this merge request. I'm not familiar enough with launchpad.

review: Approve

Unmerged commits

1884378... by Lars Kellogg-Stedman on 2017-08-25

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 on 2016-12-08

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/tests/unittests/test_datasource/test_ec2.py b/tests/unittests/test_datasource/test_ec2.py
2index 33d0261..7c9ac70 100644
3--- a/tests/unittests/test_datasource/test_ec2.py
4+++ b/tests/unittests/test_datasource/test_ec2.py
5@@ -167,17 +167,36 @@ class TestEc2(test_helpers.HttprettyTestCase):
6 self.datasource.min_metadata_version, 'meta-data', ''])
7
8 @property
9+ def metadata_urls(self):
10+ return ['/'.join([self.metadata_addr, version, 'meta-data'])
11+ for version in
12+ [self.datasource.min_metadata_version] +
13+ self.datasource.extended_metadata_versions]
14+
15+ @property
16 def userdata_url(self):
17 return '/'.join([
18 self.metadata_addr,
19 self.datasource.min_metadata_version, 'user-data'])
20
21+ @property
22+ def userdata_urls(self):
23+ return ['/'.join([self.metadata_addr, version, 'user-data'])
24+ for version in
25+ [self.datasource.min_metadata_version] +
26+ self.datasource.extended_metadata_versions]
27+
28 def _patch_add_cleanup(self, mpath, *args, **kwargs):
29 p = mock.patch(mpath, *args, **kwargs)
30 p.start()
31 self.addCleanup(p.stop)
32
33+ def bad_uri_handler(self, req, uri, headers):
34+ print('Request for invalid URL:', uri)
35+ return (999, headers, 'Invalid request for %s' % uri)
36+
37 def _setup_ds(self, sys_cfg, platform_data, md, ud=None):
38+ self.uris = []
39 distro = {}
40 paths = helpers.Paths({})
41 if sys_cfg is None:
42@@ -189,8 +208,10 @@ class TestEc2(test_helpers.HttprettyTestCase):
43 return_value=platform_data)
44
45 if md:
46- register_mock_metaserver(self.metadata_url, md)
47- register_mock_metaserver(self.userdata_url, ud)
48+ for url in self.metadata_urls:
49+ register_mock_metaserver(url, md)
50+ for url in self.userdata_urls:
51+ register_mock_metaserver(url, ud)
52
53 return ds
54
55@@ -268,6 +289,7 @@ class TestEc2(test_helpers.HttprettyTestCase):
56 Then the metadata services is crawled for more network config info.
57 When the platform data is valid, return True.
58 """
59+
60 m_is_bsd.return_value = False
61 m_dhcp.return_value = [{
62 'interface': 'eth9', 'fixed-address': '192.168.2.9',
63@@ -278,6 +300,7 @@ class TestEc2(test_helpers.HttprettyTestCase):
64 platform_data=self.valid_platform_data,
65 sys_cfg={'datasource': {'Ec2': {'strict_id': False}}},
66 md=DEFAULT_METADATA)
67+
68 ret = ds.get_data()
69 self.assertTrue(ret)
70 m_dhcp.assert_called_once_with()

Subscribers

People subscribed via source and target branches