blob: cb90bc5b16f82a39da285dd1501ef20ed7ce375e [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import pytest
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +02002import atexit
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02003import 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
17
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030018@pytest.fixture(scope='session')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060019def 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 '''
25 salt_output = local_salt_client.cmd(
26 'keystone:server',
27 'test.ping',
28 expr_form='pillar')
29 if salt_output:
30 return "keystone:server"
31 else:
32 salt_output = local_salt_client.cmd(
33 'etcd:server',
34 'test.ping',
35 expr_form='pillar')
36 return "etcd:server" if salt_output else pytest.skip("Neither \
37 Openstack nor k8s is found. Skipping test")
38
39
40@pytest.fixture(scope='session')
41def check_openstack(local_salt_client):
42 salt_output = local_salt_client.cmd(
43 'keystone:server',
44 'test.ping',
45 expr_form='pillar')
46 if not salt_output:
47 pytest.skip("Openstack not found or keystone:server pillar \
48 are not found on this environment.")
49
50
51@pytest.fixture(scope='session')
52def check_drivetrain(local_salt_client):
53 salt_output = local_salt_client.cmd(
54 'I@jenkins:client and not I@salt:master',
55 'test.ping',
56 expr_form='compound')
57 if not salt_output:
58 pytest.skip("Drivetrain service or jenkins:client pillar \
59 are not found on this environment.")
60
61
62@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030063def check_prometheus(local_salt_client):
64 salt_output = local_salt_client.cmd(
65 'prometheus:server',
66 'test.ping',
67 expr_form='pillar')
68 if not salt_output:
69 pytest.skip("Prometheus service or prometheus:server pillar \
70 are not found on this environment.")
71
72
73@pytest.fixture(scope='session')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030074def check_alerta(local_salt_client):
75 salt_output = local_salt_client.cmd(
76 'prometheus:alerta',
77 'test.ping',
78 expr_form='pillar')
79 if not salt_output:
80 pytest.skip("Alerta service or prometheus:alerta pillar \
81 are not found on this environment.")
82
83
84@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030085def check_kibana(local_salt_client):
86 salt_output = local_salt_client.cmd(
87 'kibana:server',
88 'test.ping',
89 expr_form='pillar')
90 if not salt_output:
91 pytest.skip("Kibana service or kibana:server pillar \
92 are not found on this environment.")
93
94
95@pytest.fixture(scope='session')
96def check_grafana(local_salt_client):
97 salt_output = local_salt_client.cmd(
98 'grafana:client',
99 'test.ping',
100 expr_form='pillar')
101 if not salt_output:
102 pytest.skip("Grafana service or grafana:client pillar \
103 are not found on this environment.")
104
105
Mikhail Chernike6d470f2018-08-08 18:29:57 +0200106def pytest_namespace():
107 return {'contrail': None}
108
109
110@pytest.fixture(scope='module')
111def contrail(local_salt_client):
112 probe = local_salt_client.cmd(
113 'opencontrail:control',
114 'pillar.get',
115 'opencontrail:control:version',
116 expr_form='pillar')
117 if not probe:
118 pytest.skip("Contrail is not found on this environment")
119 versions = set(probe.values())
120 if len(versions) != 1:
121 pytest.fail('Contrail versions are not the same: {}'.format(probe))
122 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200123
124
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300125@pytest.fixture(scope='session')
126def check_kdt(local_salt_client):
127 kdt_nodes_available = local_salt_client.cmd(
128 "I@gerrit:client and I@kubernetes:pool",
129 "test.ping",
130 expr_form='compound'
131 )
132 if not kdt_nodes_available:
133 pytest.skip("No 'kdt' nodes found. Skipping this test...")
134
135
136@pytest.fixture(scope='session')
137def check_cicd(local_salt_client):
138 cicd_nodes_available = local_salt_client.cmd(
139 "I@gerrit:client and I@docker:swarm",
140 "test.ping",
141 expr_form='compound'
142 )
143 if not cicd_nodes_available:
144 pytest.skip("No 'cid' nodes found. Skipping this test...")
145
146
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200147@pytest.fixture(autouse=True, scope='session')
148def print_node_version(local_salt_client):
149 """
150 Gets info about each node using salt command, info is represented as a dictionary with :
151 {node_name1: output1, node_name2: ...}
152
153 :print to output the table with results after completing all tests if nodes and salt output exist.
154 Prints nothing otherwise
155 :return None
156 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200157 try:
158 filename_with_versions = "/etc/image_version"
159 cat_image_version_file = "if [ -f '{name}' ]; then \
160 cat {name}; \
161 else \
162 echo BUILD_TIMESTAMP='no {name}'; \
163 echo BUILD_TIMESTAMP_RFC='no {name}'; \
164 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200165
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200166 list_version = local_salt_client.cmd(
167 '*',
168 'cmd.run',
169 'echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
170 expr_form='compound')
171 if list_version.__len__() == 0:
172 yield
173 parsed = {k: v.split('\n') for k, v in list_version.items()}
174 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200175
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200176 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200177
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200178 report_text = template.format("NODE", *columns)
179 for node, data in sorted(parsed.items()):
180 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200181
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200182 def write_report():
183 print(report_text)
184 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200185 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200186 except Exception as e:
187 print("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200188 yield