blob: d1dbfe5f10a54c876ad13ded93c1ad08078d520e [file] [log] [blame]
Hanna Arhipova1eef8312019-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 Arhipovae6ed8e42019-05-15 16:27:08 +03004import utils
Hanna Arhipova1eef8312019-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 Arhipova1eef8312019-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
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -050032@pytest.fixture(scope='session', params=nodes.values(), ids=nodes.keys())
33def nodes_in_group(request):
34 return request.param
Mikhail Chernike6d470f2018-08-08 18:29:57 +020035
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060036
37@pytest.fixture(scope='session')
38def 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 '''
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030044 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060045 if salt_output:
46 return "keystone:server"
47 else:
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030048 salt_output = local_salt_client.test_ping(tgt='etcd:server')
Oleksii Zhurba2c2dc942019-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):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030055 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba2c2dc942019-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')
62def check_drivetrain(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030063 salt_output = local_salt_client.test_ping(tgt='I@jenkins:client and not I@salt:master',
64 expr_form='compound')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060065 if not salt_output:
66 pytest.skip("Drivetrain service or jenkins:client pillar \
67 are not found on this environment.")
68
Mikhail Chernike6d470f2018-08-08 18:29:57 +020069
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030070@pytest.fixture(scope='session')
71def check_prometheus(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030072 salt_output = local_salt_client.test_ping(tgt='prometheus:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030073 if not salt_output:
74 pytest.skip("Prometheus service or prometheus:server pillar \
75 are not found on this environment.")
76
77
78@pytest.fixture(scope='session')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030079def check_alerta(local_salt_client):
80 salt_output = local_salt_client.test_ping(tgt='prometheus:alerta')
81 if not salt_output:
82 pytest.skip("Alerta service or prometheus:alerta pillar \
83 are not found on this environment.")
84
85
86@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030087def check_kibana(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030088 salt_output = local_salt_client.test_ping(tgt='kibana:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030089 if not salt_output:
90 pytest.skip("Kibana service or kibana:server pillar \
91 are not found on this environment.")
92
93
94@pytest.fixture(scope='session')
95def check_grafana(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030096 salt_output = local_salt_client.test_ping(tgt='grafana:client')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030097 if not salt_output:
98 pytest.skip("Grafana service or grafana:client pillar \
99 are not found on this environment.")
100
101
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300102@pytest.fixture(scope='session')
103def check_cinder_backends(local_salt_client):
104 backends_cinder_available = local_salt_client.test_ping(tgt='cinder:controller')
105 if not backends_cinder_available or not any(backends_cinder_available.values()):
106 pytest.skip("Cinder service or cinder:controller:backend pillar \
107 are not found on this environment.")
108
109
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200110def pytest_namespace():
111 return {'contrail': None}
112
113
114@pytest.fixture(scope='module')
115def contrail(local_salt_client):
116 probe = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300117 tgt='opencontrail:control',
118 fun='pillar.get',
119 param='opencontrail:control:version',
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200120 expr_form='pillar')
121 if not probe:
122 pytest.skip("Contrail is not found on this environment")
123 versions = set(probe.values())
124 if len(versions) != 1:
125 pytest.fail('Contrail versions are not the same: {}'.format(probe))
126 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200127
128
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300129@pytest.fixture(scope='session')
130def check_kdt(local_salt_client):
131 kdt_nodes_available = local_salt_client.test_ping(
132 tgt="I@gerrit:client and I@kubernetes:pool and not I@salt:master",
133 expr_form='compound'
134 )
135 if not kdt_nodes_available:
136 pytest.skip("No 'kdt' nodes found. Skipping this test...")
137 return kdt_nodes_available.keys()
138
139
140@pytest.fixture(scope='session')
141def check_kfg(local_salt_client):
142 kfg_nodes_available = local_salt_client.cmd(
143 tgt="I@kubernetes:pool and I@salt:master",
144 expr_form='compound'
145 )
146 if not kfg_nodes_available:
147 pytest.skip("No cfg-under-Kubernetes nodes found. Skipping this test...")
148 return kfg_nodes_available.keys()
149
150
151@pytest.fixture(scope='session')
152def check_cicd(local_salt_client):
153 cicd_nodes_available = local_salt_client.test_ping(
154 tgt="I@gerrit:client and I@docker:swarm",
155 expr_form='compound'
156 )
157 if not cicd_nodes_available:
158 pytest.skip("No 'cid' nodes found. Skipping this test...")
159
160
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200161@pytest.fixture(autouse=True, scope='session')
162def print_node_version(local_salt_client):
163 """
164 Gets info about each node using salt command, info is represented as a dictionary with :
165 {node_name1: output1, node_name2: ...}
166
167 :print to output the table with results after completing all tests if nodes and salt output exist.
168 Prints nothing otherwise
169 :return None
170 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200171 try:
172 filename_with_versions = "/etc/image_version"
173 cat_image_version_file = "if [ -f '{name}' ]; then \
174 cat {name}; \
175 else \
176 echo BUILD_TIMESTAMP='no {name}'; \
177 echo BUILD_TIMESTAMP_RFC='no {name}'; \
178 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200179
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200180 list_version = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300181 tgt='*',
182 param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200183 expr_form='compound')
184 if list_version.__len__() == 0:
185 yield
186 parsed = {k: v.split('\n') for k, v in list_version.items()}
187 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200188
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200189 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200190
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200191 report_text = template.format("NODE", *columns)
192 for node, data in sorted(parsed.items()):
193 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200194
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200195 def write_report():
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300196 logging.info(report_text)
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200197 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200198 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200199 except Exception as e:
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300200 logging.info("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200201 yield