Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 1 | import pytest |
| 2 | import cvp_checks.utils as utils |
| 3 | |
| 4 | |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 5 | @pytest.fixture(scope='session') |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 6 | def local_salt_client(): |
| 7 | return utils.init_salt_client() |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 8 | |
| 9 | nodes = utils.calculate_groups() |
| 10 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 11 | |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 12 | @pytest.fixture(scope='session', params=nodes.values(), ids=nodes.keys()) |
| 13 | def nodes_in_group(request): |
| 14 | return request.param |
Mikhail Chernik | e6d470f | 2018-08-08 18:29:57 +0200 | [diff] [blame] | 15 | |
| 16 | |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 17 | @pytest.fixture(scope='session') |
| 18 | def check_prometheus(local_salt_client): |
| 19 | salt_output = local_salt_client.cmd( |
| 20 | 'prometheus:server', |
| 21 | 'test.ping', |
| 22 | expr_form='pillar') |
| 23 | if not salt_output: |
| 24 | pytest.skip("Prometheus service or prometheus:server pillar \ |
| 25 | are not found on this environment.") |
| 26 | |
| 27 | |
| 28 | @pytest.fixture(scope='session') |
| 29 | def check_kibana(local_salt_client): |
| 30 | salt_output = local_salt_client.cmd( |
| 31 | 'kibana:server', |
| 32 | 'test.ping', |
| 33 | expr_form='pillar') |
| 34 | if not salt_output: |
| 35 | pytest.skip("Kibana service or kibana:server pillar \ |
| 36 | are not found on this environment.") |
| 37 | |
| 38 | |
| 39 | @pytest.fixture(scope='session') |
| 40 | def check_grafana(local_salt_client): |
| 41 | salt_output = local_salt_client.cmd( |
| 42 | 'grafana:client', |
| 43 | 'test.ping', |
| 44 | expr_form='pillar') |
| 45 | if not salt_output: |
| 46 | pytest.skip("Grafana service or grafana:client pillar \ |
| 47 | are not found on this environment.") |
| 48 | |
| 49 | |
Mikhail Chernik | e6d470f | 2018-08-08 18:29:57 +0200 | [diff] [blame] | 50 | def pytest_namespace(): |
| 51 | return {'contrail': None} |
| 52 | |
| 53 | |
| 54 | @pytest.fixture(scope='module') |
| 55 | def contrail(local_salt_client): |
| 56 | probe = local_salt_client.cmd( |
| 57 | 'opencontrail:control', |
| 58 | 'pillar.get', |
| 59 | 'opencontrail:control:version', |
| 60 | expr_form='pillar') |
| 61 | if not probe: |
| 62 | pytest.skip("Contrail is not found on this environment") |
| 63 | versions = set(probe.values()) |
| 64 | if len(versions) != 1: |
| 65 | pytest.fail('Contrail versions are not the same: {}'.format(probe)) |
| 66 | pytest.contrail = str(versions.pop())[:1] |
Hanna Arhipova | c01c676 | 2018-12-14 17:22:35 +0200 | [diff] [blame] | 67 | |
| 68 | |
| 69 | @pytest.fixture(autouse=True, scope='session') |
| 70 | def print_node_version(local_salt_client): |
| 71 | """ |
| 72 | Gets info about each node using salt command, info is represented as a dictionary with : |
| 73 | {node_name1: output1, node_name2: ...} |
| 74 | |
| 75 | :print to output the table with results after completing all tests if nodes and salt output exist. |
| 76 | Prints nothing otherwise |
| 77 | :return None |
| 78 | """ |
| 79 | filename_with_versions = "/etc/image_version" |
| 80 | cat_image_version_file = "if [ -f '{name}' ]; then \ |
| 81 | cat {name}; \ |
| 82 | else \ |
| 83 | echo BUILD_TIMESTAMP='no {name}'; \ |
| 84 | echo BUILD_TIMESTAMP_RFC='no {name}'; \ |
| 85 | fi ".format(name=filename_with_versions) |
| 86 | |
| 87 | list_version = local_salt_client.cmd( |
| 88 | '*', |
| 89 | 'cmd.run', |
| 90 | 'echo "NODE_INFO=$(uname -sr)" && ' + cat_image_version_file, |
| 91 | expr_form='compound') |
| 92 | if list_version.__len__() == 0: |
| 93 | yield |
| 94 | parsed = {k: v.split('\n') for k, v in list_version.items()} |
| 95 | columns = [name.split('=')[0] for name in parsed.values()[0]] |
| 96 | |
| 97 | template = "{:<40} | {:<25} | {:<25} | {:<25}\n" |
| 98 | |
| 99 | report_text = template.format("NODE", *columns) |
| 100 | for node, data in sorted(parsed.items()): |
| 101 | report_text += template.format(node, *[item.split("=")[1] for item in data]) |
| 102 | |
| 103 | def write_report(): |
| 104 | print(report_text) |
| 105 | atexit.register(write_report) |
| 106 | yield |