summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/sources/DataSourceGCE.py15
-rw-r--r--tests/unittests/test_datasource/test_gce.py20
2 files changed, 26 insertions, 9 deletions
diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py
index 2da34a99..bebc9918 100644
--- a/cloudinit/sources/DataSourceGCE.py
+++ b/cloudinit/sources/DataSourceGCE.py
@@ -213,16 +213,15 @@ def read_md(address=None, platform_check=True):
if md['availability-zone']:
md['availability-zone'] = md['availability-zone'].split('/')[-1]
- encoding = instance_data.get('user-data-encoding')
- if encoding:
+ if 'user-data' in instance_data:
+ # instance_data was json, so values are all utf-8 strings.
+ ud = instance_data['user-data'].encode("utf-8")
+ encoding = instance_data.get('user-data-encoding')
if encoding == 'base64':
- md['user-data'] = b64decode(instance_data.get('user-data'))
- else:
+ ud = b64decode(ud)
+ elif encoding:
LOG.warning('unknown user-data-encoding: %s, ignoring', encoding)
-
- if 'user-data' in md:
- ret['user-data'] = md['user-data']
- del md['user-data']
+ ret['user-data'] = ud
ret['meta-data'] = md
ret['success'] = True
diff --git a/tests/unittests/test_datasource/test_gce.py b/tests/unittests/test_datasource/test_gce.py
index f77c2c40..eb3cec42 100644
--- a/tests/unittests/test_datasource/test_gce.py
+++ b/tests/unittests/test_datasource/test_gce.py
@@ -38,11 +38,20 @@ GCE_META_ENCODING = {
'instance/hostname': 'server.project-baz.local',
'instance/zone': 'baz/bang',
'instance/attributes': {
- 'user-data': b64encode(b'/bin/echo baz\n').decode('utf-8'),
+ 'user-data': b64encode(b'#!/bin/echo baz\n').decode('utf-8'),
'user-data-encoding': 'base64',
}
}
+GCE_USER_DATA_TEXT = {
+ 'instance/id': '12345',
+ 'instance/hostname': 'server.project-baz.local',
+ 'instance/zone': 'baz/bang',
+ 'instance/attributes': {
+ 'user-data': '#!/bin/sh\necho hi mom\ntouch /run/up-now\n',
+ }
+}
+
HEADERS = {'Metadata-Flavor': 'Google'}
MD_URL_RE = re.compile(
r'http://metadata.google.internal/computeMetadata/v1/.*')
@@ -135,7 +144,16 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase):
shostname = GCE_META_PARTIAL.get('instance/hostname').split('.')[0]
self.assertEqual(shostname, self.ds.get_hostname())
+ def test_userdata_no_encoding(self):
+ """check that user-data is read."""
+ _set_mock_metadata(GCE_USER_DATA_TEXT)
+ self.ds.get_data()
+ self.assertEqual(
+ GCE_USER_DATA_TEXT['instance/attributes']['user-data'].encode(),
+ self.ds.get_userdata_raw())
+
def test_metadata_encoding(self):
+ """user-data is base64 encoded if user-data-encoding is 'base64'."""
_set_mock_metadata(GCE_META_ENCODING)
self.ds.get_data()