21
from urlparse import urlparse
21
from base64 import b64encode, b64decode
22
from six.moves.urllib_parse import urlparse
23
24
from cloudinit import settings
24
25
from cloudinit import helpers
30
31
'instance/id': '123',
31
32
'instance/zone': 'foo/bar',
32
33
'project/attributes/sshKeys': 'user:ssh-rsa AA2..+aRD0fyVw== root@server',
33
'instance/hostname': 'server.project-name.local',
34
'instance/attributes/user-data': '/bin/echo foo\n',
34
'instance/hostname': 'server.project-foo.local',
35
'instance/attributes/user-data': b'/bin/echo foo\n',
37
38
GCE_META_PARTIAL = {
39
'instance/hostname': 'server.project-name.local',
39
'instance/id': '1234',
40
'instance/hostname': 'server.project-bar.local',
41
'instance/zone': 'bar/baz',
45
'instance/id': '12345',
46
'instance/hostname': 'server.project-baz.local',
47
'instance/zone': 'baz/bang',
48
'instance/attributes/user-data': b64encode(b'/bin/echo baz\n'),
49
'instance/attributes/user-data-encoding': 'base64',
42
52
HEADERS = {'X-Google-Metadata-Request': 'True'}
43
MD_URL_RE = re.compile(r'http://metadata.google.internal./computeMetadata/v1/.*')
46
def _request_callback(method, uri, headers):
47
url_path = urlparse(uri).path
48
if url_path.startswith('/computeMetadata/v1/'):
49
path = url_path.split('/computeMetadata/v1/')[1:][0]
53
return (200, headers, GCE_META.get(path))
55
return (404, headers, '')
53
MD_URL_RE = re.compile(
54
r'http://metadata.google.internal./computeMetadata/v1/.*')
57
def _new_request_callback(gce_meta=None):
61
def _request_callback(method, uri, headers):
62
url_path = urlparse(uri).path
63
if url_path.startswith('/computeMetadata/v1/'):
64
path = url_path.split('/computeMetadata/v1/')[1:][0]
68
return (200, headers, gce_meta.get(path))
70
return (404, headers, '')
72
return _request_callback
58
75
class TestDataSourceGCE(test_helpers.HttprettyTestCase):
79
96
def test_metadata(self):
80
97
httpretty.register_uri(
81
98
httpretty.GET, MD_URL_RE,
82
body=_request_callback)
99
body=_new_request_callback())
83
100
self.ds.get_data()
85
self.assertEqual(GCE_META.get('instance/hostname'),
102
shostname = GCE_META.get('instance/hostname').split('.')[0]
103
self.assertEqual(shostname,
86
104
self.ds.get_hostname())
88
106
self.assertEqual(GCE_META.get('instance/id'),
103
121
def test_metadata_partial(self):
104
122
httpretty.register_uri(
105
123
httpretty.GET, MD_URL_RE,
106
body=_request_callback)
124
body=_new_request_callback(GCE_META_PARTIAL))
107
125
self.ds.get_data()
109
127
self.assertEqual(GCE_META_PARTIAL.get('instance/id'),
110
128
self.ds.get_instance_id())
112
self.assertEqual(GCE_META_PARTIAL.get('instance/hostname'),
113
self.ds.get_hostname())
130
shostname = GCE_META_PARTIAL.get('instance/hostname').split('.')[0]
131
self.assertEqual(shostname, self.ds.get_hostname())
134
def test_metadata_encoding(self):
135
httpretty.register_uri(
136
httpretty.GET, MD_URL_RE,
137
body=_new_request_callback(GCE_META_ENCODING))
141
GCE_META_ENCODING.get('instance/attributes/user-data'))
142
self.assertEqual(decoded, self.ds.get_userdata_raw())