Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 1 | from __future__ import print_function |
| 2 | from builtins import str |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 3 | import os |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 4 | import pytest |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 5 | import atexit |
Hanna Arhipova | 16e93fb | 2019-01-23 19:03:01 +0200 | [diff] [blame] | 6 | import utils |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 7 | import logging |
| 8 | |
| 9 | logging.basicConfig( |
| 10 | filename="{dir}/full.log".format( |
| 11 | dir=os.environ.get("PYTEST_REPORT_DIR") if os.environ.get("PYTEST_REPORT_DIR") else '.' |
| 12 | ), |
| 13 | level=logging.DEBUG, |
| 14 | format='[%(asctime)-15s] [%(funcName)s:%(lineno)s] %(message)s' |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | @pytest.fixture(autouse=True) |
| 19 | def add_testname_to_saltapi_logs(request): |
| 20 | logging.info("\n{sep}\n {testname} \n{sep}\n".format( |
| 21 | sep="*"*100, |
| 22 | testname=request.node.name |
| 23 | )) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 24 | |
| 25 | |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 26 | @pytest.fixture(scope='session') |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 27 | def local_salt_client(): |
| 28 | return utils.init_salt_client() |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 29 | |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 30 | |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 31 | nodes = utils.calculate_groups() |
| 32 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 33 | |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 34 | @pytest.fixture(scope='session', params=list(nodes.items()), ids=list(nodes.keys())) |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 35 | def nodes_in_group(request): |
| 36 | return request.param |
Mikhail Chernik | e6d470f | 2018-08-08 18:29:57 +0200 | [diff] [blame] | 37 | |
| 38 | |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 39 | @pytest.fixture(scope='session') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 40 | def ctl_nodes_pillar(local_salt_client): |
| 41 | '''Return controller node pillars (OS or k8s ctls). |
| 42 | This will help to identify nodes to use for UI curl tests. |
| 43 | If no platform is installed (no OS or k8s) we need to skip |
| 44 | the test (product team use case). |
| 45 | ''' |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 46 | salt_output = local_salt_client.test_ping(tgt='keystone:server') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 47 | if salt_output: |
| 48 | return "keystone:server" |
| 49 | else: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 50 | salt_output = local_salt_client.test_ping(tgt='etcd:server') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 51 | return "etcd:server" if salt_output else pytest.skip("Neither \ |
| 52 | Openstack nor k8s is found. Skipping test") |
| 53 | |
| 54 | |
| 55 | @pytest.fixture(scope='session') |
| 56 | def check_openstack(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 57 | salt_output = local_salt_client.test_ping(tgt='keystone:server') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 58 | if not salt_output: |
| 59 | pytest.skip("Openstack not found or keystone:server pillar \ |
| 60 | are not found on this environment.") |
| 61 | |
| 62 | |
| 63 | @pytest.fixture(scope='session') |
Ievgeniia Zadorozhna | 2f7a2a1 | 2019-10-10 15:57:09 +0300 | [diff] [blame] | 64 | def check_ironic(local_salt_client): |
| 65 | salt_output = local_salt_client.test_ping(tgt='ironic:client') |
| 66 | if not salt_output: |
| 67 | pytest.skip("Ironic service is not found or ironic:client pillar is " |
| 68 | "not found on this environment.") |
| 69 | |
| 70 | |
| 71 | @pytest.fixture(scope='session') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 72 | def check_drivetrain(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 73 | salt_output = local_salt_client.test_ping(tgt='I@jenkins:client and not I@salt:master', |
| 74 | expr_form='compound') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 75 | if not salt_output: |
| 76 | pytest.skip("Drivetrain service or jenkins:client pillar \ |
| 77 | are not found on this environment.") |
| 78 | |
| 79 | |
| 80 | @pytest.fixture(scope='session') |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 81 | def check_prometheus(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 82 | salt_output = local_salt_client.test_ping(tgt='prometheus:server') |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 83 | if not salt_output: |
| 84 | pytest.skip("Prometheus service or prometheus:server pillar \ |
| 85 | are not found on this environment.") |
| 86 | |
| 87 | |
| 88 | @pytest.fixture(scope='session') |
Ievgeniia Zadorozhna | df243ef | 2018-11-08 18:17:17 +0300 | [diff] [blame] | 89 | def check_alerta(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 90 | salt_output = local_salt_client.test_ping(tgt='prometheus:alerta') |
Ievgeniia Zadorozhna | df243ef | 2018-11-08 18:17:17 +0300 | [diff] [blame] | 91 | if not salt_output: |
| 92 | pytest.skip("Alerta service or prometheus:alerta pillar \ |
| 93 | are not found on this environment.") |
| 94 | |
| 95 | |
| 96 | @pytest.fixture(scope='session') |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 97 | def check_kibana(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 98 | salt_output = local_salt_client.test_ping(tgt='kibana:server') |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 99 | if not salt_output: |
| 100 | pytest.skip("Kibana service or kibana:server pillar \ |
| 101 | are not found on this environment.") |
| 102 | |
| 103 | |
| 104 | @pytest.fixture(scope='session') |
| 105 | def check_grafana(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 106 | salt_output = local_salt_client.test_ping(tgt='grafana:client') |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 107 | if not salt_output: |
| 108 | pytest.skip("Grafana service or grafana:client pillar \ |
| 109 | are not found on this environment.") |
| 110 | |
| 111 | |
Hanna Arhipova | a14488d | 2019-04-30 15:08:33 +0300 | [diff] [blame] | 112 | @pytest.fixture(scope='session') |
| 113 | def check_cinder_backends(local_salt_client): |
| 114 | backends_cinder_available = local_salt_client.test_ping(tgt='cinder:controller') |
| 115 | if not backends_cinder_available or not any(backends_cinder_available.values()): |
| 116 | pytest.skip("Cinder service or cinder:controller:backend pillar \ |
| 117 | are not found on this environment.") |
| 118 | |
| 119 | |
Mikhail Chernik | e6d470f | 2018-08-08 18:29:57 +0200 | [diff] [blame] | 120 | def pytest_namespace(): |
| 121 | return {'contrail': None} |
| 122 | |
| 123 | |
| 124 | @pytest.fixture(scope='module') |
| 125 | def contrail(local_salt_client): |
| 126 | probe = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 127 | tgt='opencontrail:control', |
| 128 | fun='pillar.get', |
| 129 | param='opencontrail:control:version', |
Mikhail Chernik | e6d470f | 2018-08-08 18:29:57 +0200 | [diff] [blame] | 130 | expr_form='pillar') |
| 131 | if not probe: |
| 132 | pytest.skip("Contrail is not found on this environment") |
| 133 | versions = set(probe.values()) |
| 134 | if len(versions) != 1: |
| 135 | pytest.fail('Contrail versions are not the same: {}'.format(probe)) |
| 136 | pytest.contrail = str(versions.pop())[:1] |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 137 | |
| 138 | |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 139 | @pytest.fixture(scope='session') |
| 140 | def check_kdt(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 141 | kdt_nodes_available = local_salt_client.test_ping( |
Hanna Arhipova | 4a79efd | 2019-04-24 11:12:55 +0300 | [diff] [blame] | 142 | tgt="I@gerrit:client and I@kubernetes:pool and not I@salt:master", |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 143 | expr_form='compound' |
| 144 | ) |
| 145 | if not kdt_nodes_available: |
| 146 | pytest.skip("No 'kdt' nodes found. Skipping this test...") |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 147 | return list(kdt_nodes_available.keys()) |
Hanna Arhipova | 4a79efd | 2019-04-24 11:12:55 +0300 | [diff] [blame] | 148 | |
| 149 | |
| 150 | @pytest.fixture(scope='session') |
| 151 | def check_kfg(local_salt_client): |
| 152 | kfg_nodes_available = local_salt_client.cmd( |
| 153 | tgt="I@kubernetes:pool and I@salt:master", |
| 154 | expr_form='compound' |
| 155 | ) |
| 156 | if not kfg_nodes_available: |
| 157 | pytest.skip("No cfg-under-Kubernetes nodes found. Skipping this test...") |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 158 | return list(kfg_nodes_available.keys()) |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 159 | |
| 160 | |
| 161 | @pytest.fixture(scope='session') |
| 162 | def check_cicd(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 163 | cicd_nodes_available = local_salt_client.test_ping( |
| 164 | tgt="I@gerrit:client and I@docker:swarm", |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 165 | expr_form='compound' |
| 166 | ) |
| 167 | if not cicd_nodes_available: |
| 168 | pytest.skip("No 'cid' nodes found. Skipping this test...") |
| 169 | |
| 170 | |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 171 | @pytest.fixture(autouse=True, scope='session') |
| 172 | def print_node_version(local_salt_client): |
| 173 | """ |
| 174 | Gets info about each node using salt command, info is represented as a dictionary with : |
| 175 | {node_name1: output1, node_name2: ...} |
| 176 | |
| 177 | :print to output the table with results after completing all tests if nodes and salt output exist. |
| 178 | Prints nothing otherwise |
| 179 | :return None |
| 180 | """ |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 181 | try: |
| 182 | filename_with_versions = "/etc/image_version" |
| 183 | cat_image_version_file = "if [ -f '{name}' ]; then \ |
| 184 | cat {name}; \ |
| 185 | else \ |
| 186 | echo BUILD_TIMESTAMP='no {name}'; \ |
| 187 | echo BUILD_TIMESTAMP_RFC='no {name}'; \ |
| 188 | fi ".format(name=filename_with_versions) |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 189 | |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 190 | list_version = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 191 | tgt='*', |
| 192 | param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file, |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 193 | expr_form='compound') |
| 194 | if list_version.__len__() == 0: |
| 195 | yield |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 196 | parsed = {k: v.split('\n') for k, v in list(list_version.items())} |
| 197 | columns = [name.split('=')[0] for name in list(parsed.values())[0]] |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 198 | |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 199 | template = "{:<40} | {:<25} | {:<25} | {:<25}\n" |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 200 | |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 201 | report_text = template.format("NODE", *columns) |
| 202 | for node, data in sorted(parsed.items()): |
| 203 | report_text += template.format(node, *[item.split("=")[1] for item in data]) |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 204 | |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 205 | def write_report(): |
Hanna Arhipova | d101802 | 2019-05-24 15:53:24 +0300 | [diff] [blame] | 206 | # DO NOT change to logging |
| 207 | print(report_text) |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 208 | atexit.register(write_report) |
Hanna Arhipova | 5ac4087 | 2018-12-17 20:04:49 +0200 | [diff] [blame] | 209 | yield |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 210 | except Exception as e: |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 211 | logging.info("print_node_version:: some error occurred: {}".format(e)) |
Hanna Arhipova | 4a79efd | 2019-04-24 11:12:55 +0300 | [diff] [blame] | 212 | yield |