-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_utils.py
More file actions
191 lines (141 loc) · 6.3 KB
/
test_utils.py
File metadata and controls
191 lines (141 loc) · 6.3 KB
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
import shutil
import pytest
from ch_cli_tools.utils import *
HERE = os.path.dirname(os.path.realpath(__file__)).replace(os.path.sep, '/')
def test_image_name_from_docker_path():
assert image_name_from_dockerfile_path("a") == 'a'
assert image_name_from_dockerfile_path("a/b") == 'a-b'
assert image_name_from_dockerfile_path("a/src/b") == 'a-b'
assert image_name_from_dockerfile_path("a/tasks/b") == 'a-b'
assert image_name_from_dockerfile_path("cloudharness/a/b") == 'cloudharness-a-b'
assert image_name_from_dockerfile_path("cloudharness/a/b", 'reg') == 'reg/cloudharness-a-b'
def test_merge_configuration_directories():
try:
basedir = os.path.join(HERE, "resources")
res_path = os.path.join(basedir, 'conf-res')
if os.path.exists(res_path):
shutil.rmtree(res_path)
merge_configuration_directories(os.path.join(basedir, 'conf-source1'), res_path)
merge_configuration_directories(os.path.join(basedir, 'conf-source2'), res_path)
assert os.path.exists(os.path.join(res_path, "a.yaml"))
assert os.path.exists(os.path.join(res_path, "b.yaml"))
assert os.path.exists(os.path.join(res_path, "c.yaml"))
assert os.path.exists(os.path.join(res_path, "sub", "a.yaml"))
assert os.path.exists(os.path.join(res_path, "sub", "b.yaml"))
assert os.path.exists(os.path.join(res_path, "sub", "c.yaml"))
with open(os.path.join(res_path, "a.yaml")) as f:
a = yaml.load(f)
assert a['a'] == 'a1'
assert a['b']['ba'] == 'ba1'
assert a['b']['bb'] == 'bb'
assert a['b']['bc'] == 'bc'
with open(os.path.join(res_path, "sub", "a.yaml")) as f:
a = yaml.load(f)
assert a['a'] == 'a1'
assert a['b']['ba'] == 'ba1'
assert a['b']['bb'] == 'bb'
assert a['b']['bc'] == 'bc'
assert os.path.exists(os.path.join(res_path, "a.json"))
assert os.path.exists(os.path.join(res_path, "b.json"))
assert os.path.exists(os.path.join(res_path, "c.json"))
with open(os.path.join(res_path, "a.json")) as f:
a = json.load(f)
assert a['a'] == 'a1'
assert a['b']['ba'] == 'ba1'
assert a['b']['bb'] == 'bb'
assert a['b']['bc'] == 'bc'
finally:
if os.path.exists(res_path):
shutil.rmtree(res_path)
def test_merge_configuration_directories_envs():
try:
basedir = os.path.join(HERE, "resources")
res_path = os.path.join(basedir, 'conf-res-envs')
if os.path.exists(res_path):
shutil.rmtree(res_path)
merge_configuration_directories(os.path.join(basedir, 'conf-source1'), res_path, ("dev",))
#
assert os.path.exists(os.path.join(res_path, "a.yaml"))
assert os.path.exists(os.path.join(res_path, "b.yaml"))
assert os.path.exists(os.path.join(res_path, "sub", "a.yaml"))
assert os.path.exists(os.path.join(res_path, "sub", "b.yaml"))
with open(os.path.join(res_path, "a.yaml")) as f:
a = yaml.load(f)
assert a['a'] == 'dev'
merge_configuration_directories(os.path.join(basedir, 'conf-source2'), res_path)
assert os.path.exists(os.path.join(res_path, "c.yaml"))
with open(os.path.join(res_path, "a.yaml")) as f:
a = yaml.load(f)
assert a['a'] == 'a1'
finally:
if os.path.exists(res_path):
shutil.rmtree(res_path)
def test_guess_build_dependencies_from_dockerfile():
deps = guess_build_dependencies_from_dockerfile(os.path.join(HERE, "resources/applications/myapp"))
assert len(deps) == 1
assert deps[0] == "cloudharness-flask"
deps = guess_build_dependencies_from_dockerfile(os.path.join(HERE, "resources/applications/myapp/tasks/mytask"))
assert len(deps) == 0
def test_check_docker_manifest_exists():
assert check_docker_manifest_exists("quay.io", "keycloak/keycloak", "latest")
assert not check_docker_manifest_exists("quay.io", "keycloak/keycloak", "RANDOM_TAG")
def test_search_word_in_file():
assert len(search_word_in_file(os.path.join(HERE, './resources/applications/migration_app/Dockerfile'), "CLOUDHARNESS_BASE_DEBIAN")) == 1
def test_search_word_in_folder():
assert len(search_word_in_folder(os.path.join(HERE, './resources/applications/migration_app/'), "CLOUDHARNESS_BASE_DEBIAN")) == 2
def test_find_dockerfile_paths():
myapp_path = os.path.join(HERE, "resources/applications/myapp")
if not os.path.exists(os.path.join(myapp_path, "dependencies/a/.git")):
os.makedirs(os.path.join(myapp_path, "dependencies/a/.git"))
dockerfiles = find_dockerfiles_paths(myapp_path)
assert len(dockerfiles) == 2
assert next(d for d in dockerfiles if d.endswith("myapp")), "Must find the Dockerfile in the root directory"
assert next(d for d in dockerfiles if d.endswith("myapp/tasks/mytask")), "Must find the Dockerfile in the tasks directory"
class TestReplaceInDict:
def test_does_not_replace_in_keys(_):
src_dict = {
'foo': 1,
'bar': 2,
'baz': 3,
'foobar': 4,
}
new_dict = replace_in_dict(src_dict, 'foo', 'xxx')
assert new_dict.keys() == src_dict.keys()
def test_replaces_in_values(_):
src_dict = {
'a': 'foo',
'b': 'bar',
'c': 'baz',
'd': 3,
'e': 'foobar',
}
new_dict = replace_in_dict(src_dict, 'foo', 'xxx')
assert new_dict == {
'a': 'xxx',
'b': 'bar',
'c': 'baz',
'd': 3,
'e': 'xxxbar',
}
def test_replaces_in_values_within_lists(_):
src_dict = {
'a': ['foo', 'bar', 'baz', 3, 'foobar'],
}
new_dict = replace_in_dict(src_dict, 'foo', 'xxx')
assert new_dict['a'] == ['xxx', 'bar', 'baz', 3, 'xxxbar']
def test_replaces_in_values_within_nested_dict(_):
src_dict = {
'a': {
'a': 'foo',
'b': 'bar',
'c': 'foobar',
'e': ['foo', 'bar', 'foobar'],
},
}
new_dict = replace_in_dict(src_dict, 'foo', 'xxx')
assert new_dict['a'] == {
'a': 'xxx',
'b': 'bar',
'c': 'xxxbar',
'e': ['xxx', 'bar', 'xxxbar']
}