Merge ~chad.smith/cloud-init:fix-instance-identity-upgrade-path into cloud-init:master

Proposed by Chad Smith
Status: Merged
Approved by: Scott Moser
Approved revision: e0c764b2e05d8e02c78a043c25e6fe9077049dcc
Merged at revision: e0c764b2e05d8e02c78a043c25e6fe9077049dcc
Proposed branch: ~chad.smith/cloud-init:fix-instance-identity-upgrade-path
Merge into: cloud-init:master
Diff against target: 108 lines (+64/-1)
2 files modified
cloudinit/sources/DataSourceEc2.py (+6/-0)
tests/unittests/test_datasource/test_ec2.py (+58/-1)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Needs Fixing
cloud-init Commiters Pending
Review via email: mp+337475@code.launchpad.net

Description of the change

ec2: fix upgrade path to initialize datasource.identity when no present in cache

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:eea2bd8b2e6273371151f1fe77096daaa294db80
https://jenkins.ubuntu.com/server/job/cloud-init-ci/764/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

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

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

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

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

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

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

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

review: Needs Fixing (continuous-integration)
Revision history for this message
Ryan Harper (raharper) wrote :

typo in comment

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py
index e14553b..21e9ef8 100644
--- a/cloudinit/sources/DataSourceEc2.py
+++ b/cloudinit/sources/DataSourceEc2.py
@@ -147,6 +147,12 @@ class DataSourceEc2(sources.DataSource):
147 def get_instance_id(self):147 def get_instance_id(self):
148 if self.cloud_platform == Platforms.AWS:148 if self.cloud_platform == Platforms.AWS:
149 # Prefer the ID from the instance identity document, but fall back149 # Prefer the ID from the instance identity document, but fall back
150 if not getattr(self, 'identity', None):
151 # If re-using cached datasource, it's get_data run didn't
152 # setup self.identity. So we need to do that now.
153 api_version = self.get_metadata_api_version()
154 self.identity = ec2.get_instance_identity(
155 api_version, self.metadata_address).get('document', {})
150 return self.identity.get(156 return self.identity.get(
151 'instanceId', self.metadata['instance-id'])157 'instanceId', self.metadata['instance-id'])
152 else:158 else:
diff --git a/tests/unittests/test_datasource/test_ec2.py b/tests/unittests/test_datasource/test_ec2.py
index 0f7267b..8fcf60b 100644
--- a/tests/unittests/test_datasource/test_ec2.py
+++ b/tests/unittests/test_datasource/test_ec2.py
@@ -2,6 +2,7 @@
22
3import copy3import copy
4import httpretty4import httpretty
5import json
5import mock6import mock
67
7from cloudinit import helpers8from cloudinit import helpers
@@ -9,6 +10,29 @@ from cloudinit.sources import DataSourceEc2 as ec2
9from cloudinit.tests import helpers as test_helpers10from cloudinit.tests import helpers as test_helpers
1011
1112
13DYNAMIC_METADATA = {
14 "instance-identity": {
15 "document": json.dumps({
16 "devpayProductCodes": None,
17 "marketplaceProductCodes": ["1abc2defghijklm3nopqrs4tu"],
18 "availabilityZone": "us-west-2b",
19 "privateIp": "10.158.112.84",
20 "version": "2017-09-30",
21 "instanceId": "my-identity-id",
22 "billingProducts": None,
23 "instanceType": "t2.micro",
24 "accountId": "123456789012",
25 "imageId": "ami-5fb8c835",
26 "pendingTime": "2016-11-19T16:32:11Z",
27 "architecture": "x86_64",
28 "kernelId": None,
29 "ramdiskId": None,
30 "region": "us-west-2"
31 })
32 }
33}
34
35
12# collected from api version 2016-09-02/ with36# collected from api version 2016-09-02/ with
13# python3 -c 'import json37# python3 -c 'import json
14# from cloudinit.ec2_utils import get_instance_metadata as gm38# from cloudinit.ec2_utils import get_instance_metadata as gm
@@ -85,7 +109,7 @@ DEFAULT_METADATA = {
85 "public-keys": {"brickies": ["ssh-rsa AAAAB3Nz....w== brickies"]},109 "public-keys": {"brickies": ["ssh-rsa AAAAB3Nz....w== brickies"]},
86 "reservation-id": "r-01efbc9996bac1bd6",110 "reservation-id": "r-01efbc9996bac1bd6",
87 "security-groups": "my-wide-open",111 "security-groups": "my-wide-open",
88 "services": {"domain": "amazonaws.com", "partition": "aws"}112 "services": {"domain": "amazonaws.com", "partition": "aws"},
89}113}
90114
91115
@@ -341,6 +365,39 @@ class TestEc2(test_helpers.HttprettyTestCase):
341 self.assertEqual(expected, ds.network_config)365 self.assertEqual(expected, ds.network_config)
342366
343 @httpretty.activate367 @httpretty.activate
368 def test_ec2_get_instance_id_refreshes_identity_on_upgrade(self):
369 """get_instance-id gets DataSourceEc2Local.identity if not present.
370
371 This handles an upgrade case where the old pickled datasource didn't
372 set up self.identity, but 'systemctl cloud-init init' runs
373 get_instance_id which traces on missing self.identity. lp:1748354.
374 """
375 self.datasource = ec2.DataSourceEc2Local
376 ds = self._setup_ds(
377 platform_data=self.valid_platform_data,
378 sys_cfg={'datasource': {'Ec2': {'strict_id': False}}},
379 md=DEFAULT_METADATA)
380 # Mock 404s on all versions except latest
381 all_versions = (
382 [ds.min_metadata_version] + ds.extended_metadata_versions)
383 for ver in all_versions[:-1]:
384 register_mock_metaserver(
385 'http://169.254.169.254/{0}/meta-data/instance-id'.format(ver),
386 None)
387 ds.metadata_address = 'http://169.254.169.254'
388 register_mock_metaserver(
389 '{0}/{1}/meta-data/'.format(ds.metadata_address, all_versions[-1]),
390 DEFAULT_METADATA)
391 # Register dynamic/instance-identity document which we now read.
392 register_mock_metaserver(
393 '{0}/{1}/dynamic/'.format(ds.metadata_address, all_versions[-1]),
394 DYNAMIC_METADATA)
395 ds._cloud_platform = ec2.Platforms.AWS
396 # Setup cached metadata on the Datasource
397 ds.metadata = DEFAULT_METADATA
398 self.assertEqual('my-identity-id', ds.get_instance_id())
399
400 @httpretty.activate
344 @mock.patch('cloudinit.net.dhcp.maybe_perform_dhcp_discovery')401 @mock.patch('cloudinit.net.dhcp.maybe_perform_dhcp_discovery')
345 def test_valid_platform_with_strict_true(self, m_dhcp):402 def test_valid_platform_with_strict_true(self, m_dhcp):
346 """Valid platform data should return true with strict_id true."""403 """Valid platform data should return true with strict_id true."""

Subscribers

People subscribed via source and target branches