1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
#
# Authors: Andrew Kutz <akutz@vmware.com>
#
# This file is part of cloud-init. See LICENSE file for license information.
import base64
import gzip
import os
import pytest
from cloudinit import dmi, helpers, safeyaml, settings
from cloudinit.sources import DataSourceVMware
from tests.unittests.helpers import (
CiTestCase,
FilesystemMockingTestCase,
mock,
populate_dir,
)
PRODUCT_NAME_FILE_PATH = "/sys/class/dmi/id/product_name"
PRODUCT_NAME = "VMware7,1"
PRODUCT_UUID = "82343CED-E4C7-423B-8F6B-0D34D19067AB"
REROOT_FILES = {
DataSourceVMware.PRODUCT_UUID_FILE_PATH: PRODUCT_UUID,
PRODUCT_NAME_FILE_PATH: PRODUCT_NAME,
}
VMW_MULTIPLE_KEYS = [
"ssh-rsa AAAAB3NzaC1yc2EAAAA... test1@vmw.com",
"ssh-rsa AAAAB3NzaC1yc2EAAAA... test2@vmw.com",
]
VMW_SINGLE_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAA... test@vmw.com"
VMW_METADATA_YAML = """instance-id: cloud-vm
local-hostname: cloud-vm
network:
version: 2
ethernets:
nics:
match:
name: ens*
dhcp4: yes
"""
VMW_USERDATA_YAML = """## template: jinja
#cloud-config
users:
- default
"""
VMW_VENDORDATA_YAML = """## template: jinja
#cloud-config
runcmd:
- echo "Hello, world."
"""
@pytest.fixture(autouse=True)
def common_patches():
with mock.patch("cloudinit.util.platform.platform", return_value="Linux"):
with mock.patch.multiple(
"cloudinit.dmi",
is_container=mock.Mock(return_value=False),
is_FreeBSD=mock.Mock(return_value=False),
):
yield
class TestDataSourceVMware(CiTestCase):
"""
Test common functionality that is not transport specific.
"""
def setUp(self):
super(TestDataSourceVMware, self).setUp()
self.tmp = self.tmp_dir()
def test_no_data_access_method(self):
ds = get_ds(self.tmp)
ds.vmware_rpctool = None
ret = ds.get_data()
self.assertFalse(ret)
def test_get_host_info(self):
host_info = DataSourceVMware.get_host_info()
self.assertTrue(host_info)
self.assertTrue(host_info["hostname"])
self.assertTrue(host_info["local-hostname"])
self.assertTrue(host_info["local_hostname"])
self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4])
class TestDataSourceVMwareEnvVars(FilesystemMockingTestCase):
"""
Test the envvar transport.
"""
def setUp(self):
super(TestDataSourceVMwareEnvVars, self).setUp()
self.tmp = self.tmp_dir()
os.environ[DataSourceVMware.VMX_GUESTINFO] = "1"
self.create_system_files()
def tearDown(self):
del os.environ[DataSourceVMware.VMX_GUESTINFO]
return super(TestDataSourceVMwareEnvVars, self).tearDown()
def create_system_files(self):
rootd = self.tmp_dir()
populate_dir(
rootd,
{
DataSourceVMware.PRODUCT_UUID_FILE_PATH: PRODUCT_UUID,
},
)
self.assertTrue(self.reRoot(rootd))
def assert_get_data_ok(self, m_fn, m_fn_call_count=6):
ds = get_ds(self.tmp)
ds.vmware_rpctool = None
ret = ds.get_data()
self.assertTrue(ret)
self.assertEqual(m_fn_call_count, m_fn.call_count)
self.assertEqual(
ds.data_access_method, DataSourceVMware.DATA_ACCESS_METHOD_ENVVAR
)
return ds
def assert_metadata(self, metadata, m_fn, m_fn_call_count=6):
ds = self.assert_get_data_ok(m_fn, m_fn_call_count)
assert_metadata(self, ds, metadata)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_get_subplatform(self, m_fn):
m_fn.side_effect = [VMW_METADATA_YAML, "", "", "", "", ""]
ds = self.assert_get_data_ok(m_fn, m_fn_call_count=4)
self.assertEqual(
ds.subplatform,
"%s (%s)"
% (
DataSourceVMware.DATA_ACCESS_METHOD_ENVVAR,
DataSourceVMware.get_guestinfo_envvar_key_name("metadata"),
),
)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_get_data_metadata_only(self, m_fn):
m_fn.side_effect = [VMW_METADATA_YAML, "", "", "", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_get_data_userdata_only(self, m_fn):
m_fn.side_effect = ["", VMW_USERDATA_YAML, "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_get_data_vendordata_only(self, m_fn):
m_fn.side_effect = ["", "", VMW_VENDORDATA_YAML, ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_get_data_metadata_base64(self, m_fn):
data = base64.b64encode(VMW_METADATA_YAML.encode("utf-8"))
m_fn.side_effect = [data, "base64", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_get_data_metadata_b64(self, m_fn):
data = base64.b64encode(VMW_METADATA_YAML.encode("utf-8"))
m_fn.side_effect = [data, "b64", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_get_data_metadata_gzip_base64(self, m_fn):
data = VMW_METADATA_YAML.encode("utf-8")
data = gzip.compress(data)
data = base64.b64encode(data)
m_fn.side_effect = [data, "gzip+base64", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_get_data_metadata_gz_b64(self, m_fn):
data = VMW_METADATA_YAML.encode("utf-8")
data = gzip.compress(data)
data = base64.b64encode(data)
m_fn.side_effect = [data, "gz+b64", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_metadata_single_ssh_key(self, m_fn):
metadata = DataSourceVMware.load_json_or_yaml(VMW_METADATA_YAML)
metadata["public_keys"] = VMW_SINGLE_KEY
metadata_yaml = safeyaml.dumps(metadata)
m_fn.side_effect = [metadata_yaml, "", "", ""]
self.assert_metadata(metadata, m_fn, m_fn_call_count=4)
@mock.patch(
"cloudinit.sources.DataSourceVMware.guestinfo_envvar_get_value"
)
def test_metadata_multiple_ssh_keys(self, m_fn):
metadata = DataSourceVMware.load_json_or_yaml(VMW_METADATA_YAML)
metadata["public_keys"] = VMW_MULTIPLE_KEYS
metadata_yaml = safeyaml.dumps(metadata)
m_fn.side_effect = [metadata_yaml, "", "", ""]
self.assert_metadata(metadata, m_fn, m_fn_call_count=4)
class TestDataSourceVMwareGuestInfo(FilesystemMockingTestCase):
"""
Test the guestinfo transport on a VMware platform.
"""
def setUp(self):
super(TestDataSourceVMwareGuestInfo, self).setUp()
self.tmp = self.tmp_dir()
self.create_system_files()
def create_system_files(self):
rootd = self.tmp_dir()
populate_dir(
rootd,
{
DataSourceVMware.PRODUCT_UUID_FILE_PATH: PRODUCT_UUID,
PRODUCT_NAME_FILE_PATH: PRODUCT_NAME,
},
)
self.assertTrue(self.reRoot(rootd))
def assert_get_data_ok(self, m_fn, m_fn_call_count=6):
ds = get_ds(self.tmp)
ds.vmware_rpctool = "vmware-rpctool"
ret = ds.get_data()
self.assertTrue(ret)
self.assertEqual(m_fn_call_count, m_fn.call_count)
self.assertEqual(
ds.data_access_method,
DataSourceVMware.DATA_ACCESS_METHOD_GUESTINFO,
)
return ds
def assert_metadata(self, metadata, m_fn, m_fn_call_count=6):
ds = self.assert_get_data_ok(m_fn, m_fn_call_count)
assert_metadata(self, ds, metadata)
def test_ds_valid_on_vmware_platform(self):
system_type = dmi.read_dmi_data("system-product-name")
self.assertEqual(system_type, PRODUCT_NAME)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_get_subplatform(self, m_fn):
m_fn.side_effect = [VMW_METADATA_YAML, "", "", "", "", ""]
ds = self.assert_get_data_ok(m_fn, m_fn_call_count=4)
self.assertEqual(
ds.subplatform,
"%s (%s)"
% (
DataSourceVMware.DATA_ACCESS_METHOD_GUESTINFO,
DataSourceVMware.get_guestinfo_key_name("metadata"),
),
)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_get_data_userdata_only(self, m_fn):
m_fn.side_effect = ["", VMW_USERDATA_YAML, "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_get_data_vendordata_only(self, m_fn):
m_fn.side_effect = ["", "", VMW_VENDORDATA_YAML, ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_metadata_single_ssh_key(self, m_fn):
metadata = DataSourceVMware.load_json_or_yaml(VMW_METADATA_YAML)
metadata["public_keys"] = VMW_SINGLE_KEY
metadata_yaml = safeyaml.dumps(metadata)
m_fn.side_effect = [metadata_yaml, "", "", ""]
self.assert_metadata(metadata, m_fn, m_fn_call_count=4)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_metadata_multiple_ssh_keys(self, m_fn):
metadata = DataSourceVMware.load_json_or_yaml(VMW_METADATA_YAML)
metadata["public_keys"] = VMW_MULTIPLE_KEYS
metadata_yaml = safeyaml.dumps(metadata)
m_fn.side_effect = [metadata_yaml, "", "", ""]
self.assert_metadata(metadata, m_fn, m_fn_call_count=4)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_get_data_metadata_base64(self, m_fn):
data = base64.b64encode(VMW_METADATA_YAML.encode("utf-8"))
m_fn.side_effect = [data, "base64", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_get_data_metadata_b64(self, m_fn):
data = base64.b64encode(VMW_METADATA_YAML.encode("utf-8"))
m_fn.side_effect = [data, "b64", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_get_data_metadata_gzip_base64(self, m_fn):
data = VMW_METADATA_YAML.encode("utf-8")
data = gzip.compress(data)
data = base64.b64encode(data)
m_fn.side_effect = [data, "gzip+base64", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_get_data_metadata_gz_b64(self, m_fn):
data = VMW_METADATA_YAML.encode("utf-8")
data = gzip.compress(data)
data = base64.b64encode(data)
m_fn.side_effect = [data, "gz+b64", "", ""]
self.assert_get_data_ok(m_fn, m_fn_call_count=4)
class TestDataSourceVMwareGuestInfo_InvalidPlatform(FilesystemMockingTestCase):
"""
Test the guestinfo transport on a non-VMware platform.
"""
def setUp(self):
super(TestDataSourceVMwareGuestInfo_InvalidPlatform, self).setUp()
self.tmp = self.tmp_dir()
self.create_system_files()
def create_system_files(self):
rootd = self.tmp_dir()
populate_dir(
rootd,
{
DataSourceVMware.PRODUCT_UUID_FILE_PATH: PRODUCT_UUID,
},
)
self.assertTrue(self.reRoot(rootd))
@mock.patch("cloudinit.sources.DataSourceVMware.guestinfo_get_value")
def test_ds_invalid_on_non_vmware_platform(self, m_fn):
system_type = dmi.read_dmi_data("system-product-name")
self.assertEqual(system_type, None)
m_fn.side_effect = [VMW_METADATA_YAML, "", "", "", "", ""]
ds = get_ds(self.tmp)
ds.vmware_rpctool = "vmware-rpctool"
ret = ds.get_data()
self.assertFalse(ret)
def assert_metadata(test_obj, ds, metadata):
test_obj.assertEqual(metadata.get("instance-id"), ds.get_instance_id())
test_obj.assertEqual(metadata.get("local-hostname"), ds.get_hostname())
expected_public_keys = metadata.get("public_keys")
if not isinstance(expected_public_keys, list):
expected_public_keys = [expected_public_keys]
test_obj.assertEqual(expected_public_keys, ds.get_public_ssh_keys())
test_obj.assertIsInstance(ds.get_public_ssh_keys(), list)
def get_ds(temp_dir):
ds = DataSourceVMware.DataSourceVMware(
settings.CFG_BUILTIN, None, helpers.Paths({"run_dir": temp_dir})
)
ds.vmware_rpctool = "vmware-rpctool"
return ds
# vi: ts=4 expandtab
|