-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathtest_cosalib_cmdlib.py
180 lines (151 loc) · 4.4 KB
/
test_cosalib_cmdlib.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
import datetime
import os
import platform
import pytest
import subprocess
import sys
import uuid
sys.path.insert(0, 'src')
from cosalib import cmdlib
PY_MAJOR, PY_MINOR, PY_PATCH = platform.python_version_tuple()
def test_runcmd():
"""
Verify runcmd returns expected information
"""
result = cmdlib.runcmd(['echo', 'hi'])
assert result.stdout is None
with pytest.raises(FileNotFoundError):
cmdlib.runcmd(['idonotexist'])
# If we are not at least on Python 3.7 we must skip the following test
if PY_MAJOR == 3 and PY_MINOR >= 7:
result = cmdlib.runcmd(['echo', 'hi'], capture_output=True)
assert result.stdout == b'hi\n'
def test_write_and_load_json(tmpdir):
"""
Ensure write_json writes loadable json
"""
data = {
'test': ['data'],
}
path = os.path.join(tmpdir, 'data.json')
cmdlib.write_json(path, data)
# Ensure the file exists
assert os.path.isfile(path)
# Ensure the data matches
assert cmdlib.load_json(path) == data
def test_sha256sum_file(tmpdir):
"""
Verify we get the proper sha256 sum
"""
test_file = os.path.join(tmpdir, 'testfile')
with open(test_file, 'w') as f:
f.write('test')
# $ sha256sum testfile
# 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
e = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
shasum = cmdlib.sha256sum_file(test_file)
assert shasum == e
def test_fatal(capsys):
"""
Ensure that fatal does indeed attempt to exit
"""
test_string = str(uuid.uuid4())
err = None
with pytest.raises(SystemExit) as err:
cmdlib.fatal(test_string)
# Check that our test string is in stderr
assert test_string in str(err)
def test_info(capsys):
"""
Verify test_info writes properly to stderr without exit
"""
test_string = str(uuid.uuid4())
cmdlib.info(test_string)
captured = capsys.readouterr()
assert test_string in captured.err
def test_rfc3339_time():
"""
Verify the format returned from rfc3339_time
"""
t = cmdlib.rfc3339_time()
assert datetime.datetime.strptime(t, '%Y-%m-%dT%H:%M:%SZ')
# now and utcnow don't set TZ's. We should get a raise
with pytest.raises(AssertionError):
cmdlib.rfc3339_time(datetime.datetime.now())
def test_rm_allow_noent(tmpdir):
"""
Ensure rm_allow_noent works both with existing and non existing files
"""
test_path = os.path.join(tmpdir, 'testfile')
with open(test_path, 'w') as f:
f.write('test')
# Exists
cmdlib.rm_allow_noent(test_path)
# Doesn't exist
cmdlib.rm_allow_noent(test_path)
def test_image_info(tmpdir):
cmdlib.runcmd([
"qemu-img", "create", "-f", "qcow2", f"{tmpdir}/test.qcow2", "10M"])
assert cmdlib.image_info(f"{tmpdir}/test.qcow2").get('format') == "qcow2"
cmdlib.runcmd([
"qemu-img", "create", "-f", "vpc",
'-o', 'force_size,subformat=fixed',
f"{tmpdir}/test.vpc", "10M"])
assert cmdlib.image_info(f"{tmpdir}/test.vpc").get('format') == "vpc"
def test_merge_dicts(tmpdir):
x = {
1: "Nope",
2: ["a", "b", "c"],
3: {"3a": True}
}
y = {4: True}
z = {1: "yup",
3: {
"3a": False,
"3b": "found"
}
}
# merge y into x
m = cmdlib.merge_dicts(x, y)
for i in range(1, 4):
assert i in m
assert y[4] is True
# merge z into x
m = cmdlib.merge_dicts(x, z)
assert m[1] == "Nope"
assert x[2] == m[2]
assert m[3]["3a"] is True
assert m[3]["3b"] == "found"
# merge x into z
m = cmdlib.merge_dicts(z, x)
assert m[1] == "yup"
assert x[2] == m[2]
assert m[3] == z[3]
def test_flatten_image_yaml(tmpdir):
fn = f"{tmpdir}/image.yaml"
with open(fn, 'w') as f:
f.write("""
size: 10
extra-kargs:
- foobar
unique-key-a: true
""")
o = cmdlib.flatten_image_yaml(fn)
assert o['size'] == 10
assert o['extra-kargs'] == ['foobar']
assert o['unique-key-a']
with open(fn, 'a') as f:
f.write("include: image-base.yaml")
base_fn = f"{tmpdir}/image-base.yaml"
with open(base_fn, 'w') as f:
f.write("""
size: 8
extra-kargs:
- bazboo
unique-key-b: true
""")
o = cmdlib.flatten_image_yaml(fn)
assert o['size'] == 10
assert o['extra-kargs'] == ['foobar', 'bazboo']
assert o['unique-key-a']
assert o['unique-key-b']