blob: 754f872e94dae7bc4e6a2d17b9c63f6346432e94 [file] [log] [blame]
Hanna Arhipova56eab942019-05-06 20:14:18 +03001import os
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00002import pytest
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +02003import atexit
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02004import utils
Hanna Arhipova56eab942019-05-06 20:14:18 +03005import logging
6
7logging.basicConfig(
8 filename="{dir}/full.log".format(
9 dir=os.environ.get("PYTEST_REPORT_DIR") if os.environ.get("PYTEST_REPORT_DIR") else '.'
10 ),
11 level=logging.DEBUG,
12 format='[%(asctime)-15s] [%(funcName)s:%(lineno)s] %(message)s'
13)
14
15
16@pytest.fixture(autouse=True)
17def add_testname_to_saltapi_logs(request):
18 logging.info("\n{sep}\n {testname} \n{sep}\n".format(
19 sep="*"*100,
20 testname=request.node.name
21 ))
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000022
23
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050024@pytest.fixture(scope='session')
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000025def local_salt_client():
26 return utils.init_salt_client()
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050027
Hanna Arhipova56eab942019-05-06 20:14:18 +030028
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050029nodes = utils.calculate_groups()
30
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050031
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020032@pytest.fixture(scope='session', params=nodes.items(), ids=nodes.keys())
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050033def nodes_in_group(request):
34 return request.param
Mikhail Chernike6d470f2018-08-08 18:29:57 +020035
36
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030037@pytest.fixture(scope='session')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060038def ctl_nodes_pillar(local_salt_client):
39 '''Return controller node pillars (OS or k8s ctls).
40 This will help to identify nodes to use for UI curl tests.
41 If no platform is installed (no OS or k8s) we need to skip
42 the test (product team use case).
43 '''
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050044 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060045 if salt_output:
46 return "keystone:server"
47 else:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050048 salt_output = local_salt_client.test_ping(tgt='etcd:server')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060049 return "etcd:server" if salt_output else pytest.skip("Neither \
50 Openstack nor k8s is found. Skipping test")
51
52
53@pytest.fixture(scope='session')
54def check_openstack(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050055 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060056 if not salt_output:
57 pytest.skip("Openstack not found or keystone:server pillar \
58 are not found on this environment.")
59
60
61@pytest.fixture(scope='session')
Ievgeniia Zadorozhna2f7a2a12019-10-10 15:57:09 +030062def check_ironic(local_salt_client):
63 salt_output = local_salt_client.test_ping(tgt='ironic:client')
64 if not salt_output:
65 pytest.skip("Ironic service is not found or ironic:client pillar is "
66 "not found on this environment.")
67
68
69@pytest.fixture(scope='session')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060070def check_drivetrain(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050071 salt_output = local_salt_client.test_ping(tgt='I@jenkins:client and not I@salt:master',
72 expr_form='compound')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060073 if not salt_output:
74 pytest.skip("Drivetrain service or jenkins:client pillar \
75 are not found on this environment.")
76
77
78@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030079def check_prometheus(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050080 salt_output = local_salt_client.test_ping(tgt='prometheus:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030081 if not salt_output:
82 pytest.skip("Prometheus service or prometheus:server pillar \
83 are not found on this environment.")
84
85
86@pytest.fixture(scope='session')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030087def check_alerta(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050088 salt_output = local_salt_client.test_ping(tgt='prometheus:alerta')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030089 if not salt_output:
90 pytest.skip("Alerta service or prometheus:alerta pillar \
91 are not found on this environment.")
92
93
94@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030095def check_kibana(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050096 salt_output = local_salt_client.test_ping(tgt='kibana:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030097 if not salt_output:
98 pytest.skip("Kibana service or kibana:server pillar \
99 are not found on this environment.")
100
101
102@pytest.fixture(scope='session')
103def check_grafana(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500104 salt_output = local_salt_client.test_ping(tgt='grafana:client')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300105 if not salt_output:
106 pytest.skip("Grafana service or grafana:client pillar \
107 are not found on this environment.")
108
109
Hanna Arhipovaa14488d2019-04-30 15:08:33 +0300110@pytest.fixture(scope='session')
111def check_cinder_backends(local_salt_client):
112 backends_cinder_available = local_salt_client.test_ping(tgt='cinder:controller')
113 if not backends_cinder_available or not any(backends_cinder_available.values()):
114 pytest.skip("Cinder service or cinder:controller:backend pillar \
115 are not found on this environment.")
116
117
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200118def pytest_namespace():
119 return {'contrail': None}
120
121
122@pytest.fixture(scope='module')
123def contrail(local_salt_client):
124 probe = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500125 tgt='opencontrail:control',
126 fun='pillar.get',
127 param='opencontrail:control:version',
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200128 expr_form='pillar')
129 if not probe:
130 pytest.skip("Contrail is not found on this environment")
131 versions = set(probe.values())
132 if len(versions) != 1:
133 pytest.fail('Contrail versions are not the same: {}'.format(probe))
134 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200135
136
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300137@pytest.fixture(scope='session')
138def check_kdt(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500139 kdt_nodes_available = local_salt_client.test_ping(
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300140 tgt="I@gerrit:client and I@kubernetes:pool and not I@salt:master",
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300141 expr_form='compound'
142 )
143 if not kdt_nodes_available:
144 pytest.skip("No 'kdt' nodes found. Skipping this test...")
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300145 return kdt_nodes_available.keys()
146
147
148@pytest.fixture(scope='session')
149def check_kfg(local_salt_client):
150 kfg_nodes_available = local_salt_client.cmd(
151 tgt="I@kubernetes:pool and I@salt:master",
152 expr_form='compound'
153 )
154 if not kfg_nodes_available:
155 pytest.skip("No cfg-under-Kubernetes nodes found. Skipping this test...")
156 return kfg_nodes_available.keys()
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300157
158
159@pytest.fixture(scope='session')
160def check_cicd(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500161 cicd_nodes_available = local_salt_client.test_ping(
162 tgt="I@gerrit:client and I@docker:swarm",
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300163 expr_form='compound'
164 )
165 if not cicd_nodes_available:
166 pytest.skip("No 'cid' nodes found. Skipping this test...")
167
168
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200169@pytest.fixture(autouse=True, scope='session')
170def print_node_version(local_salt_client):
171 """
172 Gets info about each node using salt command, info is represented as a dictionary with :
173 {node_name1: output1, node_name2: ...}
174
175 :print to output the table with results after completing all tests if nodes and salt output exist.
176 Prints nothing otherwise
177 :return None
178 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200179 try:
180 filename_with_versions = "/etc/image_version"
181 cat_image_version_file = "if [ -f '{name}' ]; then \
182 cat {name}; \
183 else \
184 echo BUILD_TIMESTAMP='no {name}'; \
185 echo BUILD_TIMESTAMP_RFC='no {name}'; \
186 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200187
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200188 list_version = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500189 tgt='*',
190 param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200191 expr_form='compound')
192 if list_version.__len__() == 0:
193 yield
194 parsed = {k: v.split('\n') for k, v in list_version.items()}
195 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200196
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200197 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200198
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200199 report_text = template.format("NODE", *columns)
200 for node, data in sorted(parsed.items()):
201 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200202
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200203 def write_report():
Hanna Arhipovad1018022019-05-24 15:53:24 +0300204 # DO NOT change to logging
205 print(report_text)
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200206 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200207 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200208 except Exception as e:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300209 logging.info("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300210 yield