blob: aabf8d149c8bb9b724192ef4b588779e4b5ec420 [file] [log] [blame]
Ekaterina Chernovac73bc4e2019-11-12 14:56:03 +03001from __future__ import print_function
2from builtins import str
Hanna Arhipova1eef8312019-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 Arhipovae6ed8e42019-05-15 16:27:08 +03006import utils
Hanna Arhipova1eef8312019-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():
Hanna Arhipova71bd38e2021-08-17 10:44:41 +030028 pytest.local_salt_client = utils.init_salt_client()
29 return pytest.local_salt_client
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050030
Hanna Arhipova1eef8312019-05-06 20:14:18 +030031
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050032nodes = utils.calculate_groups()
33
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050034
Ekaterina Chernovac73bc4e2019-11-12 14:56:03 +030035@pytest.fixture(scope='session', params=list(nodes.items()), ids=list(nodes.keys()))
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050036def nodes_in_group(request):
37 return request.param
Mikhail Chernike6d470f2018-08-08 18:29:57 +020038
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060039
40@pytest.fixture(scope='session')
41def ctl_nodes_pillar(local_salt_client):
42 '''Return controller node pillars (OS or k8s ctls).
43 This will help to identify nodes to use for UI curl tests.
44 If no platform is installed (no OS or k8s) we need to skip
45 the test (product team use case).
46 '''
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030047 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060048 if salt_output:
49 return "keystone:server"
50 else:
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030051 salt_output = local_salt_client.test_ping(tgt='etcd:server')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060052 return "etcd:server" if salt_output else pytest.skip("Neither \
53 Openstack nor k8s is found. Skipping test")
54
55
56@pytest.fixture(scope='session')
57def check_openstack(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030058 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060059 if not salt_output:
60 pytest.skip("Openstack not found or keystone:server pillar \
61 are not found on this environment.")
62
63
64@pytest.fixture(scope='session')
Ievgeniia Zadorozhna1475b6e2019-10-10 15:57:09 +030065def check_ironic(local_salt_client):
66 salt_output = local_salt_client.test_ping(tgt='ironic:client')
67 if not salt_output:
68 pytest.skip("Ironic service is not found or ironic:client pillar is "
69 "not found on this environment.")
70
71
72@pytest.fixture(scope='session')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060073def check_drivetrain(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030074 salt_output = local_salt_client.test_ping(tgt='I@jenkins:client and not I@salt:master',
75 expr_form='compound')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060076 if not salt_output:
77 pytest.skip("Drivetrain service or jenkins:client pillar \
78 are not found on this environment.")
79
Mikhail Chernike6d470f2018-08-08 18:29:57 +020080
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030081@pytest.fixture(scope='session')
82def check_prometheus(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030083 salt_output = local_salt_client.test_ping(tgt='prometheus:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030084 if not salt_output:
85 pytest.skip("Prometheus service or prometheus:server pillar \
86 are not found on this environment.")
87
88
89@pytest.fixture(scope='session')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030090def check_alerta(local_salt_client):
91 salt_output = local_salt_client.test_ping(tgt='prometheus:alerta')
92 if not salt_output:
93 pytest.skip("Alerta service or prometheus:alerta pillar \
94 are not found on this environment.")
95
96
97@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030098def check_kibana(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030099 salt_output = local_salt_client.test_ping(tgt='kibana:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300100 if not salt_output:
101 pytest.skip("Kibana service or kibana:server pillar \
102 are not found on this environment.")
103
104
105@pytest.fixture(scope='session')
106def check_grafana(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300107 salt_output = local_salt_client.test_ping(tgt='grafana:client')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +0300108 if not salt_output:
109 pytest.skip("Grafana service or grafana:client pillar \
110 are not found on this environment.")
111
112
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300113@pytest.fixture(scope='session')
114def check_cinder_backends(local_salt_client):
115 backends_cinder_available = local_salt_client.test_ping(tgt='cinder:controller')
116 if not backends_cinder_available or not any(backends_cinder_available.values()):
117 pytest.skip("Cinder service or cinder:controller:backend pillar \
118 are not found on this environment.")
119
120
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200121def pytest_namespace():
122 return {'contrail': None}
123
124
125@pytest.fixture(scope='module')
126def contrail(local_salt_client):
127 probe = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300128 tgt='opencontrail:control',
129 fun='pillar.get',
130 param='opencontrail:control:version',
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200131 expr_form='pillar')
132 if not probe:
133 pytest.skip("Contrail is not found on this environment")
134 versions = set(probe.values())
135 if len(versions) != 1:
136 pytest.fail('Contrail versions are not the same: {}'.format(probe))
137 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200138
139
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300140@pytest.fixture(scope='session')
141def check_kdt(local_salt_client):
142 kdt_nodes_available = local_salt_client.test_ping(
143 tgt="I@gerrit:client and I@kubernetes:pool and not I@salt:master",
144 expr_form='compound'
145 )
146 if not kdt_nodes_available:
147 pytest.skip("No 'kdt' nodes found. Skipping this test...")
Ekaterina Chernovac73bc4e2019-11-12 14:56:03 +0300148 return list(kdt_nodes_available.keys())
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300149
150
151@pytest.fixture(scope='session')
152def check_kfg(local_salt_client):
153 kfg_nodes_available = local_salt_client.cmd(
154 tgt="I@kubernetes:pool and I@salt:master",
155 expr_form='compound'
156 )
157 if not kfg_nodes_available:
158 pytest.skip("No cfg-under-Kubernetes nodes found. Skipping this test...")
Ekaterina Chernovac73bc4e2019-11-12 14:56:03 +0300159 return list(kfg_nodes_available.keys())
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300160
161
162@pytest.fixture(scope='session')
163def check_cicd(local_salt_client):
164 cicd_nodes_available = local_salt_client.test_ping(
165 tgt="I@gerrit:client and I@docker:swarm",
166 expr_form='compound'
167 )
168 if not cicd_nodes_available:
169 pytest.skip("No 'cid' nodes found. Skipping this test...")
170
171
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200172@pytest.fixture(autouse=True, scope='session')
173def print_node_version(local_salt_client):
174 """
175 Gets info about each node using salt command, info is represented as a dictionary with :
176 {node_name1: output1, node_name2: ...}
177
178 :print to output the table with results after completing all tests if nodes and salt output exist.
179 Prints nothing otherwise
180 :return None
181 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200182 try:
183 filename_with_versions = "/etc/image_version"
184 cat_image_version_file = "if [ -f '{name}' ]; then \
185 cat {name}; \
186 else \
187 echo BUILD_TIMESTAMP='no {name}'; \
188 echo BUILD_TIMESTAMP_RFC='no {name}'; \
189 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200190
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200191 list_version = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300192 tgt='*',
193 param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200194 expr_form='compound')
195 if list_version.__len__() == 0:
196 yield
Ekaterina Chernovac73bc4e2019-11-12 14:56:03 +0300197 parsed = {k: v.split('\n') for k, v in list(list_version.items())}
198 columns = [name.split('=')[0] for name in list(parsed.values())[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200199
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200200 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200201
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200202 report_text = template.format("NODE", *columns)
203 for node, data in sorted(parsed.items()):
204 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200205
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200206 def write_report():
Hanna Arhipova2c5611c2019-05-24 15:53:24 +0300207 # DO NOT change to logging
208 print(report_text)
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200209 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200210 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200211 except Exception as e:
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300212 logging.info("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200213 yield