blob: bd62ed4b6052f66d3faea5767c09a34e568c75a4 [file] [log] [blame]
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +03001from __future__ import print_function
2from builtins import str
Hanna Arhipova56eab942019-05-06 20:14:18 +03003import os
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00004import pytest
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +02005import atexit
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02006import utils
Hanna Arhipova56eab942019-05-06 20:14:18 +03007import logging
8
9logging.basicConfig(
10 filename="{dir}/full.log".format(
11 dir=os.environ.get("PYTEST_REPORT_DIR") if os.environ.get("PYTEST_REPORT_DIR") else '.'
12 ),
13 level=logging.DEBUG,
14 format='[%(asctime)-15s] [%(funcName)s:%(lineno)s] %(message)s'
15)
16
17
18@pytest.fixture(autouse=True)
19def add_testname_to_saltapi_logs(request):
20 logging.info("\n{sep}\n {testname} \n{sep}\n".format(
21 sep="*"*100,
22 testname=request.node.name
23 ))
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000024
25
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050026@pytest.fixture(scope='session')
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000027def local_salt_client():
28 return utils.init_salt_client()
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050029
Hanna Arhipova56eab942019-05-06 20:14:18 +030030
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050031nodes = utils.calculate_groups()
32
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050033
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030034@pytest.fixture(scope='session', params=list(nodes.items()), ids=list(nodes.keys()))
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050035def nodes_in_group(request):
36 return request.param
Mikhail Chernike6d470f2018-08-08 18:29:57 +020037
38
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030039@pytest.fixture(scope='session')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060040def ctl_nodes_pillar(local_salt_client):
41 '''Return controller node pillars (OS or k8s ctls).
42 This will help to identify nodes to use for UI curl tests.
43 If no platform is installed (no OS or k8s) we need to skip
44 the test (product team use case).
45 '''
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050046 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060047 if salt_output:
48 return "keystone:server"
49 else:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050050 salt_output = local_salt_client.test_ping(tgt='etcd:server')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060051 return "etcd:server" if salt_output else pytest.skip("Neither \
52 Openstack nor k8s is found. Skipping test")
53
54
55@pytest.fixture(scope='session')
56def check_openstack(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050057 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060058 if not salt_output:
59 pytest.skip("Openstack not found or keystone:server pillar \
60 are not found on this environment.")
61
62
63@pytest.fixture(scope='session')
Ievgeniia Zadorozhna2f7a2a12019-10-10 15:57:09 +030064def check_ironic(local_salt_client):
65 salt_output = local_salt_client.test_ping(tgt='ironic:client')
66 if not salt_output:
67 pytest.skip("Ironic service is not found or ironic:client pillar is "
68 "not found on this environment.")
69
70
71@pytest.fixture(scope='session')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060072def check_drivetrain(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050073 salt_output = local_salt_client.test_ping(tgt='I@jenkins:client and not I@salt:master',
74 expr_form='compound')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060075 if not salt_output:
76 pytest.skip("Drivetrain service or jenkins:client pillar \
77 are not found on this environment.")
78
79
80@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030081def check_prometheus(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050082 salt_output = local_salt_client.test_ping(tgt='prometheus:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030083 if not salt_output:
84 pytest.skip("Prometheus service or prometheus:server pillar \
85 are not found on this environment.")
86
87
88@pytest.fixture(scope='session')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030089def check_alerta(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050090 salt_output = local_salt_client.test_ping(tgt='prometheus:alerta')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030091 if not salt_output:
92 pytest.skip("Alerta service or prometheus:alerta pillar \
93 are not found on this environment.")
94
95
96@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030097def check_kibana(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050098 salt_output = local_salt_client.test_ping(tgt='kibana:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030099 if not salt_output:
100 pytest.skip("Kibana service or kibana:server pillar \
101 are not found on this environment.")
102
103
104@pytest.fixture(scope='session')
105def check_grafana(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500106 salt_output = local_salt_client.test_ping(tgt='grafana:client')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300107 if not salt_output:
108 pytest.skip("Grafana service or grafana:client pillar \
109 are not found on this environment.")
110
111
Hanna Arhipovaa14488d2019-04-30 15:08:33 +0300112@pytest.fixture(scope='session')
113def check_cinder_backends(local_salt_client):
114 backends_cinder_available = local_salt_client.test_ping(tgt='cinder:controller')
115 if not backends_cinder_available or not any(backends_cinder_available.values()):
116 pytest.skip("Cinder service or cinder:controller:backend pillar \
117 are not found on this environment.")
118
119
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200120def pytest_namespace():
121 return {'contrail': None}
122
123
124@pytest.fixture(scope='module')
125def contrail(local_salt_client):
126 probe = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500127 tgt='opencontrail:control',
128 fun='pillar.get',
129 param='opencontrail:control:version',
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200130 expr_form='pillar')
131 if not probe:
132 pytest.skip("Contrail is not found on this environment")
133 versions = set(probe.values())
134 if len(versions) != 1:
135 pytest.fail('Contrail versions are not the same: {}'.format(probe))
136 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200137
138
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300139@pytest.fixture(scope='session')
140def check_kdt(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500141 kdt_nodes_available = local_salt_client.test_ping(
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300142 tgt="I@gerrit:client and I@kubernetes:pool and not I@salt:master",
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300143 expr_form='compound'
144 )
145 if not kdt_nodes_available:
146 pytest.skip("No 'kdt' nodes found. Skipping this test...")
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300147 return list(kdt_nodes_available.keys())
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300148
149
150@pytest.fixture(scope='session')
151def check_kfg(local_salt_client):
152 kfg_nodes_available = local_salt_client.cmd(
153 tgt="I@kubernetes:pool and I@salt:master",
154 expr_form='compound'
155 )
156 if not kfg_nodes_available:
157 pytest.skip("No cfg-under-Kubernetes nodes found. Skipping this test...")
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300158 return list(kfg_nodes_available.keys())
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300159
160
161@pytest.fixture(scope='session')
162def check_cicd(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500163 cicd_nodes_available = local_salt_client.test_ping(
164 tgt="I@gerrit:client and I@docker:swarm",
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300165 expr_form='compound'
166 )
167 if not cicd_nodes_available:
168 pytest.skip("No 'cid' nodes found. Skipping this test...")
169
170
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200171@pytest.fixture(autouse=True, scope='session')
172def print_node_version(local_salt_client):
173 """
174 Gets info about each node using salt command, info is represented as a dictionary with :
175 {node_name1: output1, node_name2: ...}
176
177 :print to output the table with results after completing all tests if nodes and salt output exist.
178 Prints nothing otherwise
179 :return None
180 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200181 try:
182 filename_with_versions = "/etc/image_version"
183 cat_image_version_file = "if [ -f '{name}' ]; then \
184 cat {name}; \
185 else \
186 echo BUILD_TIMESTAMP='no {name}'; \
187 echo BUILD_TIMESTAMP_RFC='no {name}'; \
188 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200189
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200190 list_version = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500191 tgt='*',
192 param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200193 expr_form='compound')
194 if list_version.__len__() == 0:
195 yield
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300196 parsed = {k: v.split('\n') for k, v in list(list_version.items())}
197 columns = [name.split('=')[0] for name in list(parsed.values())[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200198
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200199 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200200
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200201 report_text = template.format("NODE", *columns)
202 for node, data in sorted(parsed.items()):
203 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200204
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200205 def write_report():
Hanna Arhipovad1018022019-05-24 15:53:24 +0300206 # DO NOT change to logging
207 print(report_text)
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200208 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200209 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200210 except Exception as e:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300211 logging.info("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300212 yield