forked from SUSE/qe-sap-deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
537 lines (445 loc) · 15.4 KB
/
conftest.py
File metadata and controls
537 lines (445 loc) · 15.4 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import os
import yaml
from unittest import mock
import pytest
# pylint: disable=redefined-outer-name
@pytest.fixture()
def config_data_sample():
"""
Config data as if obtained from yaml file.
'variables' section must contains only one string, list and dict.
:return:
dict based data structure
"""
def _callback(
provider="pinocchio",
az_region="westeurope",
hana_ips=None,
hana_disk_configuration=None,
):
# Default values
hana_ips = hana_ips if hana_ips else ["10.0.0.2", "10.0.0.3"]
hana_disk_configuration = (
hana_disk_configuration
if hana_disk_configuration
else {"disk_type": "hdd,hdd,hdd", "disks_size": "64,64,64"}
)
# Config template
config = {
"name": "geppetto",
"terraform": {
"provider": provider,
"variables": {
"az_region": az_region,
"hana_ips": hana_ips,
"hana_data_disks_configuration": hana_disk_configuration,
},
},
"ansible": {
"hana_urls": ["SAPCAR_URL", "SAP_HANA_URL", "SAP_CLIENT_SAR_URL"]
},
}
return config
return _callback
@pytest.fixture
def config_yaml_sample():
"""
create yaml config data sample with one string, list and dict variable.
:return:
dict based data structure
"""
config = """---
apiver: {}
provider: {}
terraform:
{}
variables:
az_region: "westeurope"
hana_ips: ["10.0.0.2", "10.0.0.3"]
hana_data_disks_configuration:
disk_type: "hdd,hdd,hdd"
disks_size: "64,64,64"
ansible:
az_storage_account_name: SOMEONE
az_container_name: SOMETHING
az_sas_token: SECRET
hana_media:
- SAPCAR_EXE
- SAP_HANA_EXE
- SAP_CLIENT_SAR_EXE
hana_vars:
sap_hana_install_software_directory: /hana/shared/install
sap_hana_install_master_password: 'DoNotUseThisPassw0rd'
sap_hana_install_sid: 'UT0'
sap_hana_install_instance_number: '00'
sap_domain: "qe-test.example.com"
primary_site: 'goofy'
secondary_site: 'miky'
"""
def _callback(provider="pinocchio", apiver=3, template_file=None):
tfvar_template_setting = ""
if template_file is not None:
tfvar_template_setting = f"tfvar_template: {template_file}"
return config.format(apiver, provider, tfvar_template_setting)
return _callback
@pytest.fixture
def config_yaml_sample_for_terraform():
"""
create yaml config data sample with one string, list and dict variable.
:return:
dict based data structure
"""
config = """---
apiver: {}
provider: {}
{}
ansible:
az_storage_account_name: SOMEONE
az_container_name: SOMETHING
az_sas_token: SECRET
hana_media:
- SAPCAR_EXE
- SAP_HANA_EXE
- SAP_CLIENT_SAR_EXE
hana_vars:
sap_hana_install_software_directory: /hana/shared/install
sap_hana_install_master_password: 'DoNotUseThisPassw0rd'
sap_hana_install_sid: 'UT0'
sap_hana_install_instance_number: '00'
sap_domain: "qe-test.example.com"
primary_site: 'goofy'
secondary_site: 'miky'
"""
def _callback(terraform_section, provider="pinocchio", apiver=3):
return config.format(apiver, provider, terraform_section)
return _callback
@pytest.fixture
def provider_dir(tmpdir):
'''
It also implicitly create the terraform folder if missing
'''
def _callback(provider):
provider_path = os.path.join(tmpdir, "terraform", provider)
if not os.path.isdir(provider_path):
os.makedirs(provider_path)
return provider_path
return _callback
@pytest.fixture
def playbooks_dir(tmpdir):
def _callback():
playbooks_path = os.path.join(tmpdir, "ansible", "playbooks")
if not os.path.isdir(playbooks_path):
os.makedirs(playbooks_path)
return playbooks_path
return _callback
@pytest.fixture
def create_playbooks(playbooks_dir):
def _callback(playbook_list):
playbook_filename_list = []
for playbook in playbook_list:
ans_plybk_path = playbooks_dir()
playbook_filename = os.path.join(ans_plybk_path, playbook + ".yaml")
with open(playbook_filename, "w", encoding="utf-8") as file:
file.write("")
playbook_filename_list.append(playbook_filename)
return playbook_filename_list
return _callback
@pytest.fixture
def ansible_config():
def _callback(provider, playbooks, apiver=3):
config_content = f"""---
apiver: {apiver}
provider: {provider}
ansible:
az_container_name: pippo
az_storage_account_name: pippo
az_sas_token: SECRET
hana_media:
- pippo"""
if apiver < 4:
for seq in playbooks:
config_content += f"\n {seq}:"
for play in playbooks[seq]:
config_content += f"\n - {play}.yaml"
else:
config_content += "\n sequences:"
for seq in playbooks:
config_content += f"\n {seq}:"
for play in playbooks[seq]:
config_content += f"\n - {play}.yaml"
return config_content
return _callback
FAKE_BIN_PATH = "/paese/della/cuccagna/"
ANSIBLE_EXE = FAKE_BIN_PATH + "ansible"
ANSIBLEPB_EXE = FAKE_BIN_PATH + "ansible-playbook"
@pytest.fixture()
def ansible_exe_call():
def _callback(inventory):
return f"{ANSIBLE_EXE} -vv -i {inventory} all -a true --ssh-extra-args=\"-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new\""
return _callback
@pytest.fixture()
def ansible_sudo_wait_call():
def _callback(inventory):
return (
f"{ANSIBLE_EXE} -vv -i {inventory} all "
"-m shell "
"-a 'i=0; while [ $i -lt 35 ]; do sudo -n true && exit 0; sleep 5; i=$((i+1)); done; exit 1' "
'--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"'
)
return _callback
@pytest.fixture
def mock_call_ansibleplaybook():
"""
create a mock.call with some default elements
```
mock.call('ansible-playbook -i inventory, playbook', env={'ANSIBLE_PIPELINING', 'True'})
```
"""
def _callback(inventory, playbook, verbosity="-vv", arguments=None, env=None):
playbook_cmd = [ANSIBLEPB_EXE, verbosity, "-i", inventory, playbook]
if arguments is not None:
playbook_cmd += arguments
if env is None:
original_env = dict(os.environ)
original_env["ANSIBLE_PIPELINING"] = "True"
original_env["ANSIBLE_TIMEOUT"] = "20"
else:
original_env = env
return mock.call(cmd=' '.join(playbook_cmd), env=original_env)
return _callback
@pytest.fixture
def create_inventory(provider_dir):
"""
Create an empty inventory file
"""
def _callback(provider):
provider_path = provider_dir(provider)
inventory_filename = os.path.join(provider_path, "inventory.yaml")
with open(inventory_filename, "w", encoding="utf-8") as file:
file.write("")
return inventory_filename
return _callback
@pytest.fixture
def base_args(tmpdir):
"""
Return bare minimal list of arguments to run any sub-command
Args:
base_dir (str): used for -b
config_file (str): used for -c
"""
def _callback(base_dir=None, config_file=None, verbose=True):
args = []
if verbose:
args.append("--verbose")
args.append("--base-dir")
if base_dir is None:
args.append(str(tmpdir))
else:
args.append(str(base_dir))
args.append("--config-file")
if config_file is None:
# create an empty config.yaml
config_file_name = str(tmpdir / "config.yaml")
with open(config_file_name, "w", encoding="utf-8") as file:
file.write("")
args.append(config_file_name)
else:
args.append(config_file)
return args
return _callback
@pytest.fixture
def args_helper(tmpdir, base_args, provider_dir):
def _callback(provider, conf, tfvar_template=None):
provider_path = provider_dir(provider)
tfvar_path = os.path.join(provider_path, "terraform.tfvars")
ansiblevars_path = os.path.join(tmpdir, "ansible", "playbooks", "vars")
if not os.path.isdir(ansiblevars_path):
os.makedirs(ansiblevars_path)
hana_media = os.path.join(ansiblevars_path, "hana_media.yaml")
hana_vars = os.path.join(ansiblevars_path, "hana_vars.yaml")
config_file_name = str(tmpdir / "config.yaml")
with open(config_file_name, "w", encoding="utf-8") as file:
file.write(conf)
if tfvar_template is not None:
with open(tfvar_template["file"], "w", encoding="utf-8") as file:
for line in tfvar_template["data"]:
file.write(line)
args = base_args(base_dir=tmpdir, config_file=config_file_name)
return args, provider_path, tfvar_path, hana_media, hana_vars
return _callback
@pytest.fixture
def configure_helper(args_helper):
'''
Only suitable for tests about the configure sub-command
'''
def _callback(provider, conf, tfvar_template=None):
args, _, tfvar_path, hana_media, hana_vars = args_helper(
provider, conf, tfvar_template
)
args.append("configure")
return args, tfvar_path, hana_media, hana_vars
return _callback
@pytest.fixture
def create_config():
"""Create one element for the list in the configure.json related to trento_deploy.py -s"""
def _callback(typ, reg, ver):
config = {"type": typ, "registry": reg}
if ver:
config["version"] = ver
return config
return _callback
@pytest.fixture
def line_match():
"""
return True if matcher string is present at least one in the string_list
"""
def _callback(string_list, matcher):
return len([s for s in string_list if matcher in s]) > 0
return _callback
@pytest.fixture
def check_duplicate():
"""
Fixture to test trento_cluster_install.sh content
Check for duplicated lines
Args:
lines (list(str)): list of string, each string is a trento_cluster_install.sh line
Returns:
tuple: True/False result, if False str about the error message
"""
def _callback(lines):
for line in lines:
if len([s for s in lines if line.strip() in s.strip()]) != 1:
return (False, "Line '" + line + "' appear more than one time")
if "--set" in line:
setting = line.split(" ")[1]
setting_field = setting.split("=")[0]
if len([s for s in lines if setting_field in s]) != 1:
return (
False,
"Setting '" + setting_field + "' appear more than one time",
)
return (True, "")
return _callback
@pytest.fixture
def check_mandatory_args(capsys, tmpdir):
"""
Given a cli to test and a subcommand string,
check that both -c and -b are mandatory
"""
def _callback(cli_to_test, subcmd):
try:
cli_to_test([subcmd])
except SystemExit:
pass
captured = capsys.readouterr()
if "error:" not in captured.err:
return False
# Try with b but without c
try:
cli_to_test(["-b", str(tmpdir), subcmd])
except SystemExit:
pass
captured = capsys.readouterr()
if "error:" not in captured.err:
return False
# Try with c but without b
try:
cli_to_test(["-c", str(tmpdir), subcmd])
except SystemExit:
pass
captured = capsys.readouterr()
if "error:" not in captured.err:
return False
return True
return _callback
@pytest.fixture
def validate_hana_media():
"""
Validate hana_media.yaml file needed by Ansible
```
az_storage_account_name: <ACCOUNT>
az_container_name: <CONTAINER>
az_sas_token: <SAS_TOKEN>
az_key_name: <KEY>
az_blobs:
- <SAPCAR_EXE>
- <IMDB_SERVER_SAR>
- <IMDB_CLIENT_SAR>
```
"""
def _callback(
hana_media_file,
account="ACCOUNT",
container="CONTAINER",
token=None,
key=None,
sapcar="SAPCAR_EXE",
imdb_srv="IMDB_SERVER_SAR",
imdb_cln="IMDB_CLIENT_SAR",
):
with open(hana_media_file, "r", encoding="utf-8") as file:
data = yaml.load(file, Loader=yaml.FullLoader)
if "az_storage_account_name" not in data:
return (
False,
"az_storage_account_name missing in the generated hana_media.yaml",
)
if account != data["az_storage_account_name"]:
return (
False,
f"az_storage_account_name value is {data['az_storage_account_name']} and not expected {account}",
)
if "az_container_name" not in data:
return (
False,
"az_container_name missing in the generated hana_media.yaml",
)
if container != data["az_container_name"]:
return (
False,
f"az_container_name value is {data['az_container_name']} and not expected {container}",
)
# az_sas_token is optional, test it only if requested
if token:
if "az_sas_token" not in data:
return (
False,
"az_sas_token missing in the generated hana_media.yaml",
)
if token != data["az_sas_token"]:
return (
False,
f"az_sas_token value is {data['az_sas_token']} and not expected {token}",
)
# az_key_name is optional, test it only if requested
if key:
if "az_key_name" not in data:
return (
False,
"az_key_name missing in the generated hana_media.yaml",
)
if key != data["az_key_name"]:
return (
False,
f"az_key_name value is {data['az_key_name']} and not expected {key}",
)
blob_key = "az_blobs"
if blob_key not in data:
return (
False,
f"{blob_key} section missing in the generated hana_media.yaml",
)
urls_num = len(data[blob_key])
if urls_num != 3:
return (
False,
f"Number of elements in {blob_key} is {urls_num} and not 3",
)
if sapcar not in data[blob_key]:
return False, f"{sapcar} missing in {blob_key}: {data[blob_key]}"
if imdb_srv not in data[blob_key]:
return False, f"{imdb_srv} missing in {blob_key}: {data[blob_key]}"
if imdb_cln not in data[blob_key]:
return False, f"{imdb_cln} missing in {blob_key}: {data[blob_key]}"
return (True, "")
return _callback