blob: 0b832602ec1c25020cddc98066915a874fc86910 [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')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060019def ctl_nodes_pillar(local_salt_client):
20 '''Return controller node pillars (OS or k8s ctls).
21 This will help to identify nodes to use for UI curl tests.
22 If no platform is installed (no OS or k8s) we need to skip
23 the test (product team use case).
24 '''
25 salt_output = local_salt_client.cmd(
26 'keystone:server',
27 'test.ping',
28 expr_form='pillar')
29 if salt_output:
30 return "keystone:server"
31 else:
32 salt_output = local_salt_client.cmd(
33 'etcd:server',
34 'test.ping',
35 expr_form='pillar')
36 return "etcd:server" if salt_output else pytest.skip("Neither \
37 Openstack nor k8s is found. Skipping test")
38
39
40@pytest.fixture(scope='session')
41def check_openstack(local_salt_client):
42 salt_output = local_salt_client.cmd(
43 'keystone:server',
44 'test.ping',
45 expr_form='pillar')
46 if not salt_output:
47 pytest.skip("Openstack not found or keystone:server pillar \
48 are not found on this environment.")
49
50
51@pytest.fixture(scope='session')
52def check_drivetrain(local_salt_client):
53 salt_output = local_salt_client.cmd(
54 'I@jenkins:client and not I@salt:master',
55 'test.ping',
56 expr_form='compound')
57 if not salt_output:
58 pytest.skip("Drivetrain service or jenkins:client pillar \
59 are not found on this environment.")
60
61
62@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030063def check_prometheus(local_salt_client):
64 salt_output = local_salt_client.cmd(
65 'prometheus:server',
66 'test.ping',
67 expr_form='pillar')
68 if not salt_output:
69 pytest.skip("Prometheus service or prometheus:server pillar \
70 are not found on this environment.")
71
72
73@pytest.fixture(scope='session')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030074def check_alerta(local_salt_client):
75 salt_output = local_salt_client.cmd(
76 'prometheus:alerta',
77 'test.ping',
78 expr_form='pillar')
79 if not salt_output:
80 pytest.skip("Alerta service or prometheus:alerta pillar \
81 are not found on this environment.")
82
83
84@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030085def check_kibana(local_salt_client):
86 salt_output = local_salt_client.cmd(
87 'kibana:server',
88 'test.ping',
89 expr_form='pillar')
90 if not salt_output:
91 pytest.skip("Kibana service or kibana:server pillar \
92 are not found on this environment.")
93
94
95@pytest.fixture(scope='session')
96def check_grafana(local_salt_client):
97 salt_output = local_salt_client.cmd(
98 'grafana:client',
99 'test.ping',
100 expr_form='pillar')
101 if not salt_output:
102 pytest.skip("Grafana service or grafana:client pillar \
103 are not found on this environment.")
104
105
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200106def pytest_namespace():
107 return {'contrail': None}
108
109
110@pytest.fixture(scope='module')
111def contrail(local_salt_client):
112 probe = local_salt_client.cmd(
113 'opencontrail:control',
114 'pillar.get',
115 'opencontrail:control:version',
116 expr_form='pillar')
117 if not probe:
118 pytest.skip("Contrail is not found on this environment")
119 versions = set(probe.values())
120 if len(versions) != 1:
121 pytest.fail('Contrail versions are not the same: {}'.format(probe))
122 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200123
124
125@pytest.fixture(autouse=True, scope='session')
126def print_node_version(local_salt_client):
127 """
128 Gets info about each node using salt command, info is represented as a dictionary with :
129 {node_name1: output1, node_name2: ...}
130
131 :print to output the table with results after completing all tests if nodes and salt output exist.
132 Prints nothing otherwise
133 :return None
134 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200135 try:
136 filename_with_versions = "/etc/image_version"
137 cat_image_version_file = "if [ -f '{name}' ]; then \
138 cat {name}; \
139 else \
140 echo BUILD_TIMESTAMP='no {name}'; \
141 echo BUILD_TIMESTAMP_RFC='no {name}'; \
142 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200143
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200144 list_version = local_salt_client.cmd(
145 '*',
146 'cmd.run',
147 'echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
148 expr_form='compound')
149 if list_version.__len__() == 0:
150 yield
151 parsed = {k: v.split('\n') for k, v in list_version.items()}
152 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200153
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200154 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200155
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200156 report_text = template.format("NODE", *columns)
157 for node, data in sorted(parsed.items()):
158 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200159
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200160 def write_report():
161 print(report_text)
162 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200163 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200164 except Exception as e:
165 print("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200166 yield