blob: dccee56d97cf5d98e98d256967289c4e38256e3c [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')
30def check_kibana(local_salt_client):
31 salt_output = local_salt_client.cmd(
32 'kibana:server',
33 'test.ping',
34 expr_form='pillar')
35 if not salt_output:
36 pytest.skip("Kibana service or kibana:server pillar \
37 are not found on this environment.")
38
39
40@pytest.fixture(scope='session')
41def check_grafana(local_salt_client):
42 salt_output = local_salt_client.cmd(
43 'grafana:client',
44 'test.ping',
45 expr_form='pillar')
46 if not salt_output:
47 pytest.skip("Grafana service or grafana:client pillar \
48 are not found on this environment.")
49
50
Mikhail Chernike6d470f2018-08-08 18:29:57 +020051def pytest_namespace():
52 return {'contrail': None}
53
54
55@pytest.fixture(scope='module')
56def contrail(local_salt_client):
57 probe = local_salt_client.cmd(
58 'opencontrail:control',
59 'pillar.get',
60 'opencontrail:control:version',
61 expr_form='pillar')
62 if not probe:
63 pytest.skip("Contrail is not found on this environment")
64 versions = set(probe.values())
65 if len(versions) != 1:
66 pytest.fail('Contrail versions are not the same: {}'.format(probe))
67 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +020068
69
70@pytest.fixture(autouse=True, scope='session')
71def print_node_version(local_salt_client):
72 """
73 Gets info about each node using salt command, info is represented as a dictionary with :
74 {node_name1: output1, node_name2: ...}
75
76 :print to output the table with results after completing all tests if nodes and salt output exist.
77 Prints nothing otherwise
78 :return None
79 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +020080 try:
81 filename_with_versions = "/etc/image_version"
82 cat_image_version_file = "if [ -f '{name}' ]; then \
83 cat {name}; \
84 else \
85 echo BUILD_TIMESTAMP='no {name}'; \
86 echo BUILD_TIMESTAMP_RFC='no {name}'; \
87 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +020088
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +020089 list_version = local_salt_client.cmd(
90 '*',
91 'cmd.run',
92 'echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
93 expr_form='compound')
94 if list_version.__len__() == 0:
95 yield
96 parsed = {k: v.split('\n') for k, v in list_version.items()}
97 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +020098
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +020099 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200100
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200101 report_text = template.format("NODE", *columns)
102 for node, data in sorted(parsed.items()):
103 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200104
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200105 def write_report():
106 print(report_text)
107 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200108 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200109 except Exception as e:
110 print("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200111 yield