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