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