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