blob: 3a440a8495847e8c4e315096b351eb7d200abbf6 [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import pytest
2import cvp_checks.utils as utils
3
4
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -05005@pytest.fixture(scope='session')
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00006def local_salt_client():
7 return utils.init_salt_client()
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -05008
9nodes = utils.calculate_groups()
10
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050011
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050012@pytest.fixture(scope='session', params=nodes.values(), ids=nodes.keys())
13def nodes_in_group(request):
14 return request.param
Mikhail Chernike6d470f2018-08-08 18:29:57 +020015
16
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030017@pytest.fixture(scope='session')
18def check_prometheus(local_salt_client):
19 salt_output = local_salt_client.cmd(
20 'prometheus:server',
21 'test.ping',
22 expr_form='pillar')
23 if not salt_output:
24 pytest.skip("Prometheus service or prometheus:server pillar \
25 are not found on this environment.")
26
27
28@pytest.fixture(scope='session')
29def check_kibana(local_salt_client):
30 salt_output = local_salt_client.cmd(
31 'kibana:server',
32 'test.ping',
33 expr_form='pillar')
34 if not salt_output:
35 pytest.skip("Kibana service or kibana:server pillar \
36 are not found on this environment.")
37
38
39@pytest.fixture(scope='session')
40def check_grafana(local_salt_client):
41 salt_output = local_salt_client.cmd(
42 'grafana:client',
43 'test.ping',
44 expr_form='pillar')
45 if not salt_output:
46 pytest.skip("Grafana service or grafana:client pillar \
47 are not found on this environment.")
48
49
Mikhail Chernike6d470f2018-08-08 18:29:57 +020050def pytest_namespace():
51 return {'contrail': None}
52
53
54@pytest.fixture(scope='module')
55def contrail(local_salt_client):
56 probe = local_salt_client.cmd(
57 'opencontrail:control',
58 'pillar.get',
59 'opencontrail:control:version',
60 expr_form='pillar')
61 if not probe:
62 pytest.skip("Contrail is not found on this environment")
63 versions = set(probe.values())
64 if len(versions) != 1:
65 pytest.fail('Contrail versions are not the same: {}'.format(probe))
66 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +020067
68
69@pytest.fixture(autouse=True, scope='session')
70def print_node_version(local_salt_client):
71 """
72 Gets info about each node using salt command, info is represented as a dictionary with :
73 {node_name1: output1, node_name2: ...}
74
75 :print to output the table with results after completing all tests if nodes and salt output exist.
76 Prints nothing otherwise
77 :return None
78 """
79 filename_with_versions = "/etc/image_version"
80 cat_image_version_file = "if [ -f '{name}' ]; then \
81 cat {name}; \
82 else \
83 echo BUILD_TIMESTAMP='no {name}'; \
84 echo BUILD_TIMESTAMP_RFC='no {name}'; \
85 fi ".format(name=filename_with_versions)
86
87 list_version = local_salt_client.cmd(
88 '*',
89 'cmd.run',
90 'echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
91 expr_form='compound')
92 if list_version.__len__() == 0:
93 yield
94 parsed = {k: v.split('\n') for k, v in list_version.items()}
95 columns = [name.split('=')[0] for name in parsed.values()[0]]
96
97 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
98
99 report_text = template.format("NODE", *columns)
100 for node, data in sorted(parsed.items()):
101 report_text += template.format(node, *[item.split("=")[1] for item in data])
102
103 def write_report():
104 print(report_text)
105 atexit.register(write_report)
106 yield