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
|
# This file is part of cloud-init. See LICENSE file for license information.
from unittest import mock
import pytest
from cloudinit.sources.azure import identity
@pytest.fixture(autouse=True)
def mock_read_dmi_data():
with mock.patch.object(
identity.dmi, "read_dmi_data", return_value=None
) as m:
yield m
@pytest.fixture(autouse=True)
def mock_os_path_exists():
with mock.patch.object(identity.os.path, "exists") as m:
yield m
class TestByteSwapSystemUuid:
@pytest.mark.parametrize(
"system_uuid,swapped_uuid",
[
(
"527c2691-029f-fe4c-b1f4-a4da7ebac2cf",
"91267c52-9f02-4cfe-b1f4-a4da7ebac2cf",
),
(
"527C2691-029F-FE4C-B1F4-A4DA7EBAC2CD",
"91267c52-9f02-4cfe-b1f4-a4da7ebac2cd",
),
],
)
def test_values(self, system_uuid, swapped_uuid):
assert identity.byte_swap_system_uuid(system_uuid) == swapped_uuid
@pytest.mark.parametrize(
"system_uuid",
[
"",
"g",
"91267c52-9f02-4cfe-b1f4-a4da7ebac2c",
"91267c52-9f02-4cfe-b1f4-a4da7ebac2ccc",
"-----",
],
)
def test_errors(self, system_uuid):
with pytest.raises(ValueError) as exc_info:
identity.byte_swap_system_uuid(system_uuid)
assert exc_info.value.args[0] == "badly formed hexadecimal UUID string"
class TestConvertSystemUuidToVmId:
def test_gen1(self, monkeypatch):
system_uuid = "527c2691-029f-fe4c-b1f4-a4da7ebac2cf"
monkeypatch.setattr(identity, "is_vm_gen1", lambda: True)
swapped_uuid = "91267c52-9f02-4cfe-b1f4-a4da7ebac2cf"
assert (
identity.convert_system_uuid_to_vm_id(system_uuid) == swapped_uuid
)
def test_gen2(self, monkeypatch):
system_uuid = "527c2691-029f-fe4c-b1f4-a4da7ebac2cf"
monkeypatch.setattr(identity, "is_vm_gen1", lambda: False)
assert (
identity.convert_system_uuid_to_vm_id(system_uuid) == system_uuid
)
class TestIsVmGen1:
def test_gen1(self, mock_os_path_exists) -> None:
mock_os_path_exists.side_effect = lambda _: False
assert identity.is_vm_gen1() is True
def test_gen2_freebsd(self, mock_os_path_exists) -> None:
mock_os_path_exists.side_effect = lambda x: x == "/dev/efi"
assert identity.is_vm_gen1() is False
def test_gen2_linux(self, mock_os_path_exists) -> None:
mock_os_path_exists.side_effect = lambda x: x == "/sys/firmware/efi"
assert identity.is_vm_gen1() is False
class TestQuerySystemUuid:
@pytest.mark.parametrize(
"system_uuid",
[
"527c2691-029f-fe4c-b1f4-a4da7ebac2cf",
"527C2691-029F-FE4C-B1F4-A4DA7EBAC2CF",
],
)
def test_values(self, mock_read_dmi_data, system_uuid):
mock_read_dmi_data.return_value = system_uuid
assert identity.query_system_uuid() == system_uuid.lower()
assert mock_read_dmi_data.mock_calls == [mock.call("system-uuid")]
def test_errors(self, mock_read_dmi_data):
mock_read_dmi_data.return_value = None
with pytest.raises(RuntimeError) as exc_info:
identity.query_system_uuid()
assert exc_info.value.args[0] == "failed to read system-uuid"
class TestQueryVmId:
def test_gen1(self, monkeypatch):
system_uuid = "527c2691-029f-fe4c-b1f4-a4da7ebac2cf"
swapped_uuid = "91267c52-9f02-4cfe-b1f4-a4da7ebac2cf"
monkeypatch.setattr(identity, "query_system_uuid", lambda: system_uuid)
monkeypatch.setattr(identity, "is_vm_gen1", lambda: True)
assert identity.query_vm_id() == swapped_uuid
def test_gen2(self, monkeypatch):
system_uuid = "527c2691-029f-fe4c-b1f4-a4da7ebac2cf"
monkeypatch.setattr(identity, "query_system_uuid", lambda: system_uuid)
monkeypatch.setattr(identity, "is_vm_gen1", lambda: False)
assert identity.query_vm_id() == system_uuid
class TestChassisAssetTag:
def test_true_azure_cloud(self, caplog, mock_read_dmi_data):
mock_read_dmi_data.return_value = (
identity.ChassisAssetTag.AZURE_CLOUD.value
)
asset_tag = identity.ChassisAssetTag.query_system()
assert asset_tag == identity.ChassisAssetTag.AZURE_CLOUD
assert caplog.record_tuples == [
(
"cloudinit.sources.azure.identity",
10,
"Azure chassis asset tag: "
"'7783-7084-3265-9085-8269-3286-77' (AZURE_CLOUD)",
)
]
@pytest.mark.parametrize("tag", [None, "", "notazure"])
def test_false_on_nonazure_chassis(self, caplog, mock_read_dmi_data, tag):
mock_read_dmi_data.return_value = tag
asset_tag = identity.ChassisAssetTag.query_system()
assert asset_tag is None
assert caplog.record_tuples == [
(
"cloudinit.sources.azure.identity",
10,
"Non-Azure chassis asset tag: %r" % tag,
)
]
|