forked from red-hat-storage/cephci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_suite.py
318 lines (246 loc) · 9.26 KB
/
init_suite.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
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
"""Retrieve and process CephCI suites."""
import os
from copy import deepcopy
from glob import glob
from typing import List
import yaml
from utility.log import Log
log = Log(__name__)
def merge_dicts(dict1, dict2):
"""
Returns dict1 by recursively merging dict2 into dict1
Args:
dict1 (dict): The dictionary corressponding to test in <test_suite>.
dict2 (dict): The dictionary corressponding to test in <overrides>.
Returns:
Dict -> dictionary after merging overrides dict into test_suite dict
"""
if isinstance(dict1, list) and isinstance(dict2, list):
dict1.extend(dict2)
return dict1
if not isinstance(dict1, dict) or not isinstance(dict2, dict):
return dict2
for k in dict2:
if k in dict1:
dict1[k] = merge_dicts(dict1[k], dict2[k])
else:
dict1[k] = dict2[k]
return dict1
def read_yaml(file_name):
"""read the given yaml
Args:
file_name: yaml file to read
Returns:
dict: data from yaml
"""
file_path = os.path.abspath(file_name)
with open(file_path) as fp:
data = yaml.safe_load(fp)
return data
def process_override(dir_name: str) -> List:
"""
Returns a readable dictionary based on the files found in dir_name.
The directory must contain two files if you want to override.
- <test_suite>.yaml
- overrides.yaml
overrides.yaml could have
tests:
- test:
index : <index of the test we want to update>
# index starts from 1 refering to the first test in test suite file.
# It is optional. default is 1
<key>: <override_value>
# give override_value as null if you want to ignore the existing key in test
- test:
<key>: <override_value>
clusters:
- <cluster_name>
- <cluster_name>:
<config>:
<key>: <override_value>
Args:
dir_name (str): The directory to be processed.
Returns:
Dict -> Test case after processing the override section.
"""
# At this point, the entry/item is a directory. Inside this folder, we
# support only three files
override_data = dict()
test_data = dict()
log.debug(f"Processing overrides of {dir_name}")
files = glob(os.path.join(dir_name, "*"))
for file in files:
if file.endswith("overrides.yaml"):
override_data = read_yaml(file)
continue
test_data = read_yaml(file)
if not override_data:
return test_data["tests"]
for test in override_data.get("tests", list()):
index = test["test"].pop("index", 1) - 1
merge_dicts(test_data["tests"][index]["test"], test["test"])
if not override_data.get("clusters"):
return test_data["tests"]
clusters = override_data["clusters"]
tests = test_data.get("tests")
for test in tests:
config = test["test"].pop("config", {})
for cluster in clusters:
if isinstance(cluster, str):
cluster_name = cluster
else:
cluster_name = [key for key in cluster.keys()][0]
override_config = cluster[cluster_name].get("config", {})
merge_dicts(config, override_config)
if "clusters" not in test["test"].keys():
test["test"]["clusters"] = dict()
test["test"]["clusters"][cluster_name] = dict(
{"config": deepcopy(config) if config else None}
)
return test_data["tests"]
class Directory:
"""process given suite directory and return fragments in the suite directory"""
def __init__(self, test_suite):
self.suite_dir = test_suite
@property
def fragments(self):
"""fragments of the suite directory
Returns:
list: sorted fragments in the suite directory
"""
log.info("Retrieving the fragments from the top level.")
path_ = os.path.join(self.suite_dir, "*")
log.debug(f"got path: {path_}")
files = glob(path_)
log.debug(f"Found the following: {files}")
sorted_ = sorted(files)
log.debug(f"sorted fragments: {sorted_}")
return sorted_
class Suite:
"""process suite data"""
def __init__(self, test_suites):
self._test_suites = test_suites
self.supported_patterns = (".yaml", ".yml")
@property
def suites(self):
return self.__collate()
def __collate(self):
"""
Collate the data from the given test suites into a dictionary.
A supported files contents are appended to the list of test information being
gathered. The recommended file extension of is yaml though we do support yml.
There is support for overrides with the help of python dict merge. Hence, there
would be cases that we don't support. At the time of implementation, we agreed
not to complicate it by performing deep merge of dictionaries.
How to override
Create a directory with link to the test suite (and or) overrides.yaml
(and or) clusters.yaml
- Direct
# cat suite.yaml
tests:
- test:
abort-on-fail: true
desc: Install software pre-requisites
module: install_prereq.py
name: setup pre-requisites
# cat overrides.yaml
tests:
- test:
abort-on-fail: false
# Final output should be
# cat suite.yaml
tests:
- test:
abort-on-fail: false
desc: Install software pre-requisites
module: install_prereq.py
name: setup pre-requisites
- Adding new keys
tests:
- test:
abort-on-fail: true
desc: Install software pre-requisites
module: install_prereq.py
name: setup pre-requisites
# cat overrides.yaml
tests:
- test:
config:
is_production: true
# final output should be
# cat suite.yaml
tests:
- test:
abort-on-fail: true
config:
is_production: true
desc: Install software pre-requisites
module: install_prereq.py
name: setup pre-requisites
Adding cluster overrides
# cat suite.yaml
tests:
- test:
name: Buckets and Objects test
desc: test_Mbuckets_with_Nobjects_aws4 on secondary
polarion-id: CEPH-9637
module: sanity_rgw_multisite.py
config:
script-name: test_Mbuckets_with_Nobjects.py
config-file-name: test_Mbuckets_with_Nobjects_aws4.yaml
timeout: 300
# cat overrides.yaml
clusters:
- site1
# final output should be
- test:
name: Buckets and Objects test
desc: test_Mbuckets_with_Nobjects_aws4 on secondary
polarion-id: CEPH-9637
module: sanity_rgw_multisite.py
clusters:
site1:
config:
script-name: test_Mbuckets_with_Nobjects.py
config-file-name: test_Mbuckets_with_Nobjects_aws4.yaml
timeout: 300
Returns:
dict:
suite = {
"tests": <list of tests from the yamls>,
"nan": <list of unsupported and not found file(s) or directory(s)>
}
"""
suites = dict({"tests": list(), "nan": list()})
for suite in self._test_suites:
if os.path.isfile(suite) and suite.endswith(self.supported_patterns):
tests = read_yaml(suite)
suites["tests"].extend(tests.get("tests"))
continue
if not os.path.isdir(suite):
log.debug(f"Not a supported file: {suite}")
suites["nan"].append(suite)
continue
suites["tests"].extend(process_override(suite))
return suites
def load_suites(test_suites):
"""high level wrapper to process list of test_suites
Args:
test_suites [list]: test_suites list may contain elements of
a. list of directory(s) AKA suites
b. list of suite file(yaml)
Returns:
dict: suite of tests and nan
"""
test_suite_catalogue = []
log.info(f"List of test suites provided: \n{test_suites}")
for test_suite in test_suites:
if os.path.isdir(test_suite):
log.info("Provided suite is a directory")
fragments = Directory(test_suite).fragments
log.debug(f"got fragments: \n{fragments}")
test_suite_catalogue.extend(fragments)
else:
log.info("Provided suite is a file")
test_suite_catalogue.append(test_suite)
return Suite(test_suite_catalogue).suites