blob: 8c5be345e2a8449f6332fbe9f441664f676594ff [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(
105 tgt="I@gerrit:client and I@kubernetes:pool",
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...")
110
111
112@pytest.fixture(scope='session')
113def check_cicd(local_salt_client):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500114 cicd_nodes_available = local_salt_client.test_ping(
115 tgt="I@gerrit:client and I@docker:swarm",
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300116 expr_form='compound'
117 )
118 if not cicd_nodes_available:
119 pytest.skip("No 'cid' nodes found. Skipping this test...")
120
121
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200122@pytest.fixture(autouse=True, scope='session')
123def print_node_version(local_salt_client):
124 """
125 Gets info about each node using salt command, info is represented as a dictionary with :
126 {node_name1: output1, node_name2: ...}
127
128 :print to output the table with results after completing all tests if nodes and salt output exist.
129 Prints nothing otherwise
130 :return None
131 """
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200132 try:
133 filename_with_versions = "/etc/image_version"
134 cat_image_version_file = "if [ -f '{name}' ]; then \
135 cat {name}; \
136 else \
137 echo BUILD_TIMESTAMP='no {name}'; \
138 echo BUILD_TIMESTAMP_RFC='no {name}'; \
139 fi ".format(name=filename_with_versions)
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200140
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200141 list_version = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500142 tgt='*',
143 param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file,
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200144 expr_form='compound')
145 if list_version.__len__() == 0:
146 yield
147 parsed = {k: v.split('\n') for k, v in list_version.items()}
148 columns = [name.split('=')[0] for name in parsed.values()[0]]
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200149
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200150 template = "{:<40} | {:<25} | {:<25} | {:<25}\n"
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200151
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200152 report_text = template.format("NODE", *columns)
153 for node, data in sorted(parsed.items()):
154 report_text += template.format(node, *[item.split("=")[1] for item in data])
Hanna Arhipovac01c6762018-12-14 17:22:35 +0200155
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200156 def write_report():
157 print(report_text)
158 atexit.register(write_report)
Hanna Arhipova5ac40872018-12-17 20:04:49 +0200159 yield
Hanna Arhipova68cc2fe2018-12-17 19:13:10 +0200160 except Exception as e:
161 print("print_node_version:: some error occurred: {}".format(e))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500162 yield