blob: 8e3b130e0ae42c1d8ed7228769d69c4e17214370 [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import pytest
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +02002import atexit
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03003import 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
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060017
18@pytest.fixture(scope='session')
19def 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 '''
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030025 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060026 if salt_output:
27 return "keystone:server"
28 else:
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030029 salt_output = local_salt_client.test_ping(tgt='etcd:server')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060030 return "etcd:server" if salt_output else pytest.skip("Neither \
31 Openstack nor k8s is found. Skipping test")
32
33
34@pytest.fixture(scope='session')
35def check_openstack(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030036 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060037 if not salt_output:
38 pytest.skip("Openstack not found or keystone:server pillar \
39 are not found on this environment.")
40
41
42@pytest.fixture(scope='session')
43def check_drivetrain(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030044 salt_output = local_salt_client.test_ping(tgt='I@jenkins:client and not I@salt:master',
45 expr_form='compound')
Oleksii Zhurba2c2dc942019-01-31 16:35:57 -060046 if not salt_output:
47 pytest.skip("Drivetrain service or jenkins:client pillar \
48 are not found on this environment.")
49
Mikhail Chernike6d470f2018-08-08 18:29:57 +020050
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030051@pytest.fixture(scope='session')
52def check_prometheus(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030053 salt_output = local_salt_client.test_ping(tgt='prometheus:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030054 if not salt_output:
55 pytest.skip("Prometheus service or prometheus:server pillar \
56 are not found on this environment.")
57
58
59@pytest.fixture(scope='session')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030060def check_alerta(local_salt_client):
61 salt_output = local_salt_client.test_ping(tgt='prometheus:alerta')
62 if not salt_output:
63 pytest.skip("Alerta service or prometheus:alerta pillar \
64 are not found on this environment.")
65
66
67@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030068def check_kibana(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030069 salt_output = local_salt_client.test_ping(tgt='kibana:server')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030070 if not salt_output:
71 pytest.skip("Kibana service or kibana:server pillar \
72 are not found on this environment.")
73
74
75@pytest.fixture(scope='session')
76def check_grafana(local_salt_client):
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030077 salt_output = local_salt_client.test_ping(tgt='grafana:client')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030078 if not salt_output:
79 pytest.skip("Grafana service or grafana:client pillar \
80 are not found on this environment.")
81
82
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030083@pytest.fixture(scope='session')
84def check_cinder_backends(local_salt_client):
85 backends_cinder_available = local_salt_client.test_ping(tgt='cinder:controller')
86 if not backends_cinder_available or not any(backends_cinder_available.values()):
87 pytest.skip("Cinder service or cinder:controller:backend pillar \
88 are not found on this environment.")
89
90
Mikhail Chernike6d470f2018-08-08 18:29:57 +020091def pytest_namespace():
92 return {'contrail': None}
93
94
95@pytest.fixture(scope='module')
96def contrail(local_salt_client):
97 probe = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030098 tgt='opencontrail:control',
99 fun='pillar.get',
100 param='opencontrail:control:version',
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200101 expr_form='pillar')
102 if not probe:
103 pytest.skip("Contrail is not found on this environment")
104 versions = set(probe.values())
105 if len(versions) != 1:
106 pytest.fail('Contrail versions are not the same: {}'.format(probe))
107 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200108
109
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300110@pytest.fixture(scope='session')
111def check_kdt(local_salt_client):
112 kdt_nodes_available = local_salt_client.test_ping(
113 tgt="I@gerrit:client and I@kubernetes:pool and not I@salt:master",
114 expr_form='compound'
115 )
116 if not kdt_nodes_available:
117 pytest.skip("No 'kdt' nodes found. Skipping this test...")
118 return kdt_nodes_available.keys()
119
120
121@pytest.fixture(scope='session')
122def check_kfg(local_salt_client):
123 kfg_nodes_available = local_salt_client.cmd(
124 tgt="I@kubernetes:pool and I@salt:master",
125 expr_form='compound'
126 )
127 if not kfg_nodes_available:
128 pytest.skip("No cfg-under-Kubernetes nodes found. Skipping this test...")
129 return kfg_nodes_available.keys()
130
131
132@pytest.fixture(scope='session')
133def check_cicd(local_salt_client):
134 cicd_nodes_available = local_salt_client.test_ping(
135 tgt="I@gerrit:client and I@docker:swarm",
136 expr_form='compound'
137 )
138 if not cicd_nodes_available:
139 pytest.skip("No 'cid' nodes found. Skipping this test...")
140
141
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200142@pytest.fixture(autouse=True, scope='session')
143def print_node_version(local_salt_client):
144 """
145 Gets info about each node using salt command, info is represented as a dictionary with :
146 {node_name1: output1, node_name2: ...}
147
148 :print to output the table with results after completing all tests if nodes and salt output exist.
149 Prints nothing otherwise
150 :return None
151 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200152 try:
153 filename_with_versions = "/etc/image_version"
154 cat_image_version_file = "if [ -f '{name}' ]; then \
155 cat {name}; \
156 else \
157 echo BUILD_TIMESTAMP='no {name}'; \
158 echo BUILD_TIMESTAMP_RFC='no {name}'; \
159 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200160
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200161 list_version = local_salt_client.cmd(
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300162 tgt='*',
163 param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200164 expr_form='compound')
165 if list_version.__len__() == 0:
166 yield
167 parsed = {k: v.split('\n') for k, v in list_version.items()}
168 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200169
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200170 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200171
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200172 report_text = template.format("NODE", *columns)
173 for node, data in sorted(parsed.items()):
174 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200175
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200176 def write_report():
177 print(report_text)
178 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200179 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200180 except Exception as e:
181 print("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200182 yield