blob: 60b8f28b29a79d9d8577b98639826a9a64cdd4dd [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import pytest
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +02002import atexit
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02003import utils
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00004
5
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -05006@pytest.fixture(scope='session')
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00007def local_salt_client():
8 return utils.init_salt_client()
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -05009
10nodes = utils.calculate_groups()
11
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050012
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050013@pytest.fixture(scope='session', params=nodes.values(), ids=nodes.keys())
14def nodes_in_group(request):
15 return request.param
Mikhail Chernike6d470f2018-08-08 18:29:57 +020016
17
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030018@pytest.fixture(scope='session')
19def check_prometheus(local_salt_client):
20 salt_output = local_salt_client.cmd(
21 'prometheus:server',
22 'test.ping',
23 expr_form='pillar')
24 if not salt_output:
25 pytest.skip("Prometheus service or prometheus:server pillar \
26 are not found on this environment.")
27
28
29@pytest.fixture(scope='session')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030030def check_alerta(local_salt_client):
31 salt_output = local_salt_client.cmd(
32 'prometheus:alerta',
33 'test.ping',
34 expr_form='pillar')
35 if not salt_output:
36 pytest.skip("Alerta service or prometheus:alerta pillar \
37 are not found on this environment.")
38
39
40@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030041def check_kibana(local_salt_client):
42 salt_output = local_salt_client.cmd(
43 'kibana:server',
44 'test.ping',
45 expr_form='pillar')
46 if not salt_output:
47 pytest.skip("Kibana service or kibana:server pillar \
48 are not found on this environment.")
49
50
51@pytest.fixture(scope='session')
52def check_grafana(local_salt_client):
53 salt_output = local_salt_client.cmd(
54 'grafana:client',
55 'test.ping',
56 expr_form='pillar')
57 if not salt_output:
58 pytest.skip("Grafana service or grafana:client pillar \
59 are not found on this environment.")
60
61
Mikhail Chernike6d470f2018-08-08 18:29:57 +020062def pytest_namespace():
63 return {'contrail': None}
64
65
66@pytest.fixture(scope='module')
67def contrail(local_salt_client):
68 probe = local_salt_client.cmd(
69 'opencontrail:control',
70 'pillar.get',
71 'opencontrail:control:version',
72 expr_form='pillar')
73 if not probe:
74 pytest.skip("Contrail is not found on this environment")
75 versions = set(probe.values())
76 if len(versions) != 1:
77 pytest.fail('Contrail versions are not the same: {}'.format(probe))
78 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +020079
80
81@pytest.fixture(autouse=True, scope='session')
82def print_node_version(local_salt_client):
83 """
84 Gets info about each node using salt command, info is represented as a dictionary with :
85 {node_name1: output1, node_name2: ...}
86
87 :print to output the table with results after completing all tests if nodes and salt output exist.
88 Prints nothing otherwise
89 :return None
90 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +020091 try:
92 filename_with_versions = "/etc/image_version"
93 cat_image_version_file = "if [ -f '{name}' ]; then \
94 cat {name}; \
95 else \
96 echo BUILD_TIMESTAMP='no {name}'; \
97 echo BUILD_TIMESTAMP_RFC='no {name}'; \
98 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +020099
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200100 list_version = local_salt_client.cmd(
101 '*',
102 'cmd.run',
103 'echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
104 expr_form='compound')
105 if list_version.__len__() == 0:
106 yield
107 parsed = {k: v.split('\n') for k, v in list_version.items()}
108 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200109
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200110 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200111
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200112 report_text = template.format("NODE", *columns)
113 for node, data in sorted(parsed.items()):
114 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200115
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200116 def write_report():
117 print(report_text)
118 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200119 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200120 except Exception as e:
121 print("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200122 yield