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