blob: 4858c831efcd115db270bbbd462326c894c928b7 [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 '''
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050025 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba25215d92019-01-31 16:35:57 -060026 if salt_output:
27 return "keystone:server"
28 else:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050029 salt_output = local_salt_client.test_ping(tgt='etcd:server')
Oleksii Zhurba25215d92019-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):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050036 salt_output = local_salt_client.test_ping(tgt='keystone:server')
Oleksii Zhurba25215d92019-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):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050044 salt_output = local_salt_client.test_ping(tgt='I@jenkins:client and not I@salt:master',
45 expr_form='compound')
Oleksii Zhurba25215d92019-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
50
51@pytest.fixture(scope='session')
Oleksii Zhurba8ce9fcf2018-10-05 18:38:22 +030052def check_prometheus(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050053 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')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030060def check_alerta(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050061 salt_output = local_salt_client.test_ping(tgt='prometheus:alerta')
Ievgeniia Zadorozhnadf243ef2018-11-08 18:17:17 +030062 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):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050069 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):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050077 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
Mikhail Chernike6d470f2018-08-08 18:29:57 +020083def pytest_namespace():
84 return {'contrail': None}
85
86
87@pytest.fixture(scope='module')
88def contrail(local_salt_client):
89 probe = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050090 tgt='opencontrail:control',
91 fun='pillar.get',
92 param='opencontrail:control:version',
Mikhail Chernike6d470f2018-08-08 18:29:57 +020093 expr_form='pillar')
94 if not probe:
95 pytest.skip("Contrail is not found on this environment")
96 versions = set(probe.values())
97 if len(versions) != 1:
98 pytest.fail('Contrail versions are not the same: {}'.format(probe))
99 pytest.contrail = str(versions.pop())[:1]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200100
101
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300102@pytest.fixture(scope='session')
103def check_kdt(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500104 kdt_nodes_available = local_salt_client.test_ping(
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300105 tgt="I@gerrit:client and I@kubernetes:pool and not I@salt:master",
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300106 expr_form='compound'
107 )
108 if not kdt_nodes_available:
109 pytest.skip("No 'kdt' nodes found. Skipping this test...")
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300110 return kdt_nodes_available.keys()
111
112
113@pytest.fixture(scope='session')
114def check_kfg(local_salt_client):
115 kfg_nodes_available = local_salt_client.cmd(
116 tgt="I@kubernetes:pool and I@salt:master",
117 expr_form='compound'
118 )
119 if not kfg_nodes_available:
120 pytest.skip("No cfg-under-Kubernetes nodes found. Skipping this test...")
121 return kfg_nodes_available.keys()
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300122
123
124@pytest.fixture(scope='session')
125def check_cicd(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500126 cicd_nodes_available = local_salt_client.test_ping(
127 tgt="I@gerrit:client and I@docker:swarm",
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300128 expr_form='compound'
129 )
130 if not cicd_nodes_available:
131 pytest.skip("No 'cid' nodes found. Skipping this test...")
132
133
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200134@pytest.fixture(autouse=True, scope='session')
135def print_node_version(local_salt_client):
136 """
137 Gets info about each node using salt command, info is represented as a dictionary with :
138 {node_name1: output1, node_name2: ...}
139
140 :print to output the table with results after completing all tests if nodes and salt output exist.
141 Prints nothing otherwise
142 :return None
143 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200144 try:
145 filename_with_versions = "/etc/image_version"
146 cat_image_version_file = "if [ -f '{name}' ]; then \
147 cat {name}; \
148 else \
149 echo BUILD_TIMESTAMP='no {name}'; \
150 echo BUILD_TIMESTAMP_RFC='no {name}'; \
151 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200152
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200153 list_version = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500154 tgt='*',
155 param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200156 expr_form='compound')
157 if list_version.__len__() == 0:
158 yield
159 parsed = {k: v.split('\n') for k, v in list_version.items()}
160 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200161
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200162 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200163
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200164 report_text = template.format("NODE", *columns)
165 for node, data in sorted(parsed.items()):
166 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200167
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200168 def write_report():
169 print(report_text)
170 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200171 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200172 except Exception as e:
173 print("print_node_version:: some error occurred: {}".format(e))
Hanna Arhipova4a79efd2019-04-24 11:12:55 +0300174 yield