Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 1 | import pytest |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 2 | import atexit |
Hanna Arhipova | 16e93fb | 2019-01-23 19:03:01 +0200 | [diff] [blame] | 3 | import utils |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 4 | |
| 5 | |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 6 | @pytest.fixture(scope='session') |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 7 | def local_salt_client(): |
| 8 | return utils.init_salt_client() |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 9 | |
| 10 | nodes = utils.calculate_groups() |
| 11 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 12 | |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 13 | @pytest.fixture(scope='session', params=nodes.values(), ids=nodes.keys()) |
| 14 | def nodes_in_group(request): |
| 15 | return request.param |
Mikhail Chernik | e6d470f | 2018-08-08 18:29:57 +0200 | [diff] [blame] | 16 | |
| 17 | |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 18 | @pytest.fixture(scope='session') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 19 | def 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 Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 25 | salt_output = local_salt_client.test_ping(tgt='keystone:server') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 26 | if salt_output: |
| 27 | return "keystone:server" |
| 28 | else: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 29 | salt_output = local_salt_client.test_ping(tgt='etcd:server') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 30 | 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') |
| 35 | def check_openstack(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 36 | salt_output = local_salt_client.test_ping(tgt='keystone:server') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 37 | 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') |
| 43 | def check_drivetrain(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 44 | salt_output = local_salt_client.test_ping(tgt='I@jenkins:client and not I@salt:master', |
| 45 | expr_form='compound') |
Oleksii Zhurba | 25215d9 | 2019-01-31 16:35:57 -0600 | [diff] [blame] | 46 | 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 Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 52 | def check_prometheus(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 53 | salt_output = local_salt_client.test_ping(tgt='prometheus:server') |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 54 | 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 Zadorozhna | df243ef | 2018-11-08 18:17:17 +0300 | [diff] [blame] | 60 | def check_alerta(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 61 | salt_output = local_salt_client.test_ping(tgt='prometheus:alerta') |
Ievgeniia Zadorozhna | df243ef | 2018-11-08 18:17:17 +0300 | [diff] [blame] | 62 | 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 Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 68 | def check_kibana(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 69 | salt_output = local_salt_client.test_ping(tgt='kibana:server') |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 70 | 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') |
| 76 | def check_grafana(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 77 | salt_output = local_salt_client.test_ping(tgt='grafana:client') |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 78 | if not salt_output: |
| 79 | pytest.skip("Grafana service or grafana:client pillar \ |
| 80 | are not found on this environment.") |
| 81 | |
| 82 | |
Mikhail Chernik | e6d470f | 2018-08-08 18:29:57 +0200 | [diff] [blame] | 83 | def pytest_namespace(): |
| 84 | return {'contrail': None} |
| 85 | |
| 86 | |
| 87 | @pytest.fixture(scope='module') |
| 88 | def contrail(local_salt_client): |
| 89 | probe = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 90 | tgt='opencontrail:control', |
| 91 | fun='pillar.get', |
| 92 | param='opencontrail:control:version', |
Mikhail Chernik | e6d470f | 2018-08-08 18:29:57 +0200 | [diff] [blame] | 93 | 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 Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 100 | |
| 101 | |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 102 | @pytest.fixture(scope='session') |
| 103 | def check_kdt(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 104 | kdt_nodes_available = local_salt_client.test_ping( |
Hanna Arhipova | 4a79efd | 2019-04-24 11:12:55 +0300 | [diff] [blame] | 105 | 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] | 106 | expr_form='compound' |
| 107 | ) |
| 108 | if not kdt_nodes_available: |
| 109 | pytest.skip("No 'kdt' nodes found. Skipping this test...") |
Hanna Arhipova | 4a79efd | 2019-04-24 11:12:55 +0300 | [diff] [blame] | 110 | return kdt_nodes_available.keys() |
| 111 | |
| 112 | |
| 113 | @pytest.fixture(scope='session') |
| 114 | def 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 Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 122 | |
| 123 | |
| 124 | @pytest.fixture(scope='session') |
| 125 | def check_cicd(local_salt_client): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 126 | cicd_nodes_available = local_salt_client.test_ping( |
| 127 | tgt="I@gerrit:client and I@docker:swarm", |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 128 | expr_form='compound' |
| 129 | ) |
| 130 | if not cicd_nodes_available: |
| 131 | pytest.skip("No 'cid' nodes found. Skipping this test...") |
| 132 | |
| 133 | |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 134 | @pytest.fixture(autouse=True, scope='session') |
| 135 | def 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 Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 144 | 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 Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 152 | |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 153 | list_version = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 154 | tgt='*', |
| 155 | param='echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file, |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 156 | 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 Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 161 | |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 162 | template = "{:<40} | {:<25} | {:<25} | {:<25}\n" |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 163 | |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 164 | 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 Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 167 | |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 168 | def write_report(): |
| 169 | print(report_text) |
| 170 | atexit.register(write_report) |
Hanna Arhipova | 5ac4087 | 2018-12-17 20:04:49 +0200 | [diff] [blame] | 171 | yield |
Hanna Arhipova | 68cc2fe | 2018-12-17 19:13:10 +0200 | [diff] [blame] | 172 | except Exception as e: |
| 173 | print("print_node_version:: some error occurred: {}".format(e)) |
Hanna Arhipova | 4a79efd | 2019-04-24 11:12:55 +0300 | [diff] [blame] | 174 | yield |