forked from NVIDIA-Merlin/Merlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_extractor.py
208 lines (164 loc) · 7.32 KB
/
test_extractor.py
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
#!/usr/bin/env python
import json
import os
import shutil
import tempfile
from pathlib import Path
from extractor import SupportMatrixExtractor
DATAJSON = Path(__file__).parent / "fixtures" / "data.json"
SAMPLEJSON = Path(__file__).parent / "fixtures" / "sample.json"
SAMPLEAFTERJSON = Path(__file__).parent / "fixtures" / "sample_after.json"
def test_get_from_envfile():
ETC_OS_RELEASE = "/etc/os-release"
with tempfile.TemporaryFile() as f:
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.get_from_envfile(ETC_OS_RELEASE, "PRETTY_NAME")
assert (
xtr.data["x"]["22.02"].get("PRETTY_NAME") == "Debian GNU/Linux 10 (buster)"
)
xtr.get_from_envfile(ETC_OS_RELEASE, "PRETTY_NAME", "os")
assert xtr.data["x"]["22.02"].get("os") == "Debian GNU/Linux 10 (buster)"
xtr.get_from_envfile(ETC_OS_RELEASE, "BAZ")
assert xtr.data["x"]["22.02"].get("BAZ") == SupportMatrixExtractor.ERROR
def test_get_from_pip():
with tempfile.TemporaryFile() as f:
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.get_from_pip("pip")
assert xtr.data["x"]["22.02"].get("pip") == "22.0.4"
xtr.get_from_pip("spam")
assert xtr.data["x"]["22.02"].get("spam") == SupportMatrixExtractor.ERROR
def test_get_from_python():
with tempfile.TemporaryFile() as f:
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.get_from_python("json")
assert xtr.data["x"]["22.02"].get("json") == "2.0.9"
xtr.get_from_python("spam")
assert xtr.data["x"]["22.02"].get("spam") == SupportMatrixExtractor.ERROR
def test_get_from_env():
with tempfile.TemporaryFile() as f:
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.get_from_env("SHELL")
assert xtr.data["x"]["22.02"].get("SHELL") != SupportMatrixExtractor.ERROR
xtr.get_from_env("SHELL", "shell")
assert "shell" in xtr.data["x"]["22.02"].keys()
assert xtr.data["x"]["22.02"].get("shell") != SupportMatrixExtractor.ERROR
xtr.get_from_env("BAZ")
assert "BAZ" in xtr.data["x"]["22.02"].keys()
assert xtr.data["x"]["22.02"].get("BAZ") == SupportMatrixExtractor.ERROR
xtr.get_from_env("BAZ", "bar")
assert "bar" in xtr.data["x"]["22.02"].keys()
assert xtr.data["x"]["22.02"].get("bar") == SupportMatrixExtractor.ERROR
os.environ["SMX_COMPRESSED_SIZE"] = "7169310137"
xtr.get_from_env("SMX_COMPRESSED_SIZE", "compressedSize")
assert "compressedSize" in xtr.data["x"]["22.02"].keys()
assert xtr.data["x"]["22.02"].get("compressedSize") == "6.68 GB"
del os.environ["SMX_COMPRESSED_SIZE"]
def test_get_from_cmd():
with tempfile.TemporaryFile() as f:
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.get_from_cmd("cat /proc/1/cmdline", "cmdline")
assert xtr.data["x"]["22.02"].get("cmdline") != SupportMatrixExtractor.ERROR
xtr.get_from_cmd("cat /proc/0/x", "cmderr")
assert xtr.data["x"]["22.02"].get("cmderr") == SupportMatrixExtractor.ERROR
def test_insert_snippet():
with tempfile.TemporaryFile() as f:
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.insert_snippet("release", "99.99")
assert "99.99" in xtr.data["x"]["22.02"].get("release")
def test_precise_override():
with tempfile.TemporaryFile() as f:
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.insert_snippet("release", "99.99")
xtr.precise_override("release", "99.99", "42")
assert "42" in xtr.data["x"]["22.02"].get("release")
xtr.insert_snippet("spam", "99.99")
xtr.precise_override("spam", "88.88", "42")
assert "42" not in xtr.data["x"]["22.02"].get("spam")
assert "99.99" in xtr.data["x"]["22.02"].get("spam")
xtr.precise_override("blah", "99.99", "42")
assert "blah" not in xtr.data["x"]["22.02"]
def test_to_json():
with tempfile.NamedTemporaryFile() as f:
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.get_from_pip("pip")
assert xtr.to_json() == r'{"x": {"22.02": {"pip": "22.0.4"}}}'
def test_from_json():
with tempfile.NamedTemporaryFile() as f:
shutil.copyfile(DATAJSON, f.name)
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.from_json()
print(f"{xtr.data}")
assert "first" in xtr.data.keys()
assert "one" in xtr.data["first"]
xtr.get_from_env("SHELL", "first")
assert "first" in xtr.data.keys()
assert "one" in xtr.data["first"].keys()
assert "x" in xtr.data.keys()
assert "22.02" in xtr.data["x"]
with tempfile.NamedTemporaryFile() as f:
shutil.copyfile(DATAJSON, f.name)
xtr = SupportMatrixExtractor("first", "one", f.name)
xtr.from_json()
print(f"{xtr.data}")
assert "first" in xtr.data.keys()
assert "one" in xtr.data["first"]
assert xtr.data["first"]["one"] == "1"
assert "two" in xtr.data["first"]
assert xtr.data["first"]["two"] == "2"
def test_to_json_file():
with tempfile.NamedTemporaryFile() as f:
shutil.copyfile(SAMPLEJSON, f.name)
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.from_json()
xtr.get_from_pip("pip")
xtr.to_json_file()
a, b = "", ""
with open(SAMPLEAFTERJSON, encoding="utf-8") as fa, open(
f.name, encoding="utf-8"
) as fb:
a = json.load(fa)
b = json.load(fb)
assert a == b
with tempfile.NamedTemporaryFile() as f:
shutil.copyfile(SAMPLEJSON, f.name)
xtr = SupportMatrixExtractor("x", "22.02", f.name, force=True)
xtr.from_json()
xtr.get_from_pip("pip")
xtr.to_json_file()
a, b = "", ""
with open(SAMPLEAFTERJSON, encoding="utf-8") as fa, open(
f.name, encoding="utf-8"
) as fb:
a = json.load(fa)
b = json.load(fb)
assert a == b
def test_already_present():
# First test intentionally does not copy a data.json file.
xtr = SupportMatrixExtractor("merlin-training", "22.02", "blah")
assert xtr.already_present() is False
with tempfile.NamedTemporaryFile() as f:
shutil.copyfile(SAMPLEJSON, f.name)
xtr = SupportMatrixExtractor("merlin-training", "22.02", f.name)
xtr.from_json()
assert xtr.already_present() is True
with tempfile.NamedTemporaryFile() as f:
shutil.copyfile(SAMPLEJSON, f.name)
xtr = SupportMatrixExtractor("merlin-training", "22.02", f.name, force=True)
xtr.from_json()
assert xtr.already_present() is False
with tempfile.NamedTemporaryFile() as f:
shutil.copyfile(SAMPLEJSON, f.name)
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.from_json()
assert xtr.already_present() is False
with tempfile.NamedTemporaryFile() as f:
shutil.copyfile(DATAJSON, f.name)
xtr = SupportMatrixExtractor("x", "22.02", f.name)
xtr.from_json()
assert "first" in xtr.data.keys()
assert "one" in xtr.data["first"]
xtr.get_from_env("SHELL", "first")
assert "first" in xtr.data.keys()
assert "one" in xtr.data["first"].keys()
assert "x" in xtr.data.keys()
assert "22.02" in xtr.data["x"]