Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 1 | import pytest |
| 2 | import json |
Hanna Arhipova | 16e93fb | 2019-01-23 19:03:01 +0200 | [diff] [blame] | 3 | import utils |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 4 | import logging |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 5 | |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 6 | # Some nodes can have services that are not applicable for other nodes in similar group. |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 7 | # For example , there are 3 node in kvm group, but just kvm03 node has srv-volumes-backup.mount service |
| 8 | # in service.get_all |
| 9 | # NODE NAME SERVICE_NAME |
Ekaterina Chernova | b2804bc | 2019-07-25 16:13:29 +0300 | [diff] [blame] | 10 | inconsistency_rule = {"kvm03": ["srv-volumes-backup.mount", "rsync", "sysstat"]} |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 11 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 12 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 13 | @pytest.mark.full |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 14 | def test_check_services(local_salt_client, nodes_in_group): |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 15 | """ |
| 16 | Skips services if they are not consistent for all node. |
| 17 | Inconsistent services will be checked with another test case |
| 18 | """ |
Oleksii Zhurba | dd17609 | 2019-04-26 15:29:10 -0500 | [diff] [blame] | 19 | exclude_services = utils.get_configuration().get("skipped_services", []) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 20 | group, nodes = nodes_in_group |
| 21 | services_by_nodes = local_salt_client.cmd(tgt="L@"+','.join(nodes), |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 22 | fun='service.get_all', |
| 23 | expr_form='compound') |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 24 | |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame^] | 25 | if len(list(services_by_nodes.keys())) < 2: |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 26 | pytest.skip("Nothing to compare - only 1 node") |
| 27 | |
Oleksii Zhurba | 599801a | 2019-06-04 17:26:51 -0500 | [diff] [blame] | 28 | # PROD-30833 |
| 29 | gtw01 = local_salt_client.pillar_get( |
| 30 | param='_param:openstack_gateway_node01_hostname') or 'gtw01' |
| 31 | cluster_domain = local_salt_client.pillar_get( |
| 32 | param='_param:cluster_domain') or '.local' |
| 33 | gtw01 += '.' + cluster_domain |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 34 | if gtw01 in nodes: |
| 35 | octavia = local_salt_client.cmd(tgt="L@" + ','.join(nodes), |
Oleksii Zhurba | 599801a | 2019-06-04 17:26:51 -0500 | [diff] [blame] | 36 | fun='pillar.get', |
| 37 | param='octavia:manager:enabled', |
| 38 | expr_form='compound') |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame^] | 39 | gtws = [gtw for gtw in list(octavia.values()) if gtw] |
| 40 | if len(gtws) == 1 and gtw01 in list(services_by_nodes.keys()): |
Oleksii Zhurba | 599801a | 2019-06-04 17:26:51 -0500 | [diff] [blame] | 41 | services_by_nodes.pop(gtw01) |
| 42 | logging.info("gtw01 node is skipped in test_check_services") |
| 43 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 44 | nodes = [] |
| 45 | pkts_data = [] |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 46 | all_services = set() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 47 | |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 48 | for node in services_by_nodes: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 49 | if not services_by_nodes[node]: |
| 50 | # TODO: do not skip node |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 51 | logging.info("Node {} is skipped".format(node)) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 52 | continue |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 53 | nodes.append(node) |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 54 | all_services.update(services_by_nodes[node]) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 55 | |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 56 | for srv in all_services: |
Hanna Arhipova | 8fd295c | 2019-03-07 13:46:43 +0200 | [diff] [blame] | 57 | if srv in exclude_services: |
| 58 | continue |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 59 | service_existence = dict() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 60 | for node in nodes: |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 61 | short_name_of_node = node.split('.')[0] |
| 62 | if inconsistency_rule.get(short_name_of_node) is not None and srv in inconsistency_rule[short_name_of_node]: |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 63 | # Skip the checking of some service on the specific node |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 64 | break |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 65 | elif srv in services_by_nodes[node]: |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 66 | # Found service on node |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 67 | service_existence[node] = "+" |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 68 | else: |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 69 | # Not found expected service on node |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 70 | service_existence[node] = "No service" |
| 71 | if set(service_existence.values()).__len__() > 1: |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame^] | 72 | report = ["{node}: {status}".format(node=node, status=status) for node, status in list(service_existence.items())] |
Hanna Arhipova | ab91964 | 2019-03-27 20:05:38 +0200 | [diff] [blame] | 73 | report.sort() |
| 74 | report.insert(0, srv) |
| 75 | pkts_data.append(report) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 76 | assert len(pkts_data) == 0, ( |
| 77 | "Non-uniform services are running on '{}' group of nodes:\n{}".format( |
| 78 | group, json.dumps(pkts_data, indent=4)) |
| 79 | ) |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 80 | |
| 81 | |
| 82 | # TODO : remake this test to make workable https://mirantis.jira.com/browse/PROD-25958 |
| 83 | |
| 84 | # def _check_services_on_special_node(local_salt_client, nodes_in_group): |
| 85 | # """ |
| 86 | # Check that specific node has service. |
| 87 | # Nodes and proper services should be defined in inconsistency_rule dictionary |
| 88 | # |
| 89 | # :print: Table with nodes which don't have required services and not existed services |
| 90 | # """ |
| 91 | # |
| 92 | # output = local_salt_client.cmd("L@" + ','.join(nodes_in_group), 'service.get_all', expr_form='compound') |
| 93 | # if len(output.keys()) < 2: |
| 94 | # pytest.skip("Nothing to compare - just 1 node") |
| 95 | # |
| 96 | # def is_proper_service_for_node(_service, _node): |
| 97 | # """ |
| 98 | # Return True if service exists on node and exists in inconsistency_rule |
| 99 | # Return True if service doesn't exists on node and doesn't exists in inconsistency_rule |
| 100 | # Return False otherwise |
| 101 | # :param _service: string |
| 102 | # :param _node: string full name of node |
| 103 | # :return: bool, read description for further details |
| 104 | # """ |
| 105 | # short_name_of_node = _node.split('.')[0] |
| 106 | # if short_name_of_node not in inconsistency_rule.keys(): |
| 107 | # return False |
| 108 | # |
| 109 | # if _service in inconsistency_rule[short_name_of_node] and \ |
| 110 | # _service in output[_node]: |
| 111 | # # Return True if service exists on node and exists in inconsistency_rule |
| 112 | # return True |
| 113 | # |
| 114 | # if _service not in inconsistency_rule[short_name_of_node] and \ |
| 115 | # _service not in output[_node]: |
| 116 | # # Return True if service exists on node and exists in inconsistency_rule |
| 117 | # return True |
| 118 | # print("return False for {} in {}".format(_service, _node)) |
| 119 | # # error_text = "" |
| 120 | # return False |
| 121 | # |
| 122 | # errors = list() |
| 123 | # for node, expected_services in inconsistency_rule.items(): |
| 124 | # print("Check {} , {} ".format(node, expected_services)) |
| 125 | # # Skip if there is no proper node. Find nodes that contains node_title (like 'kvm03') in their titles |
| 126 | # if not any([node in node_name for node_name in output.keys()]): |
| 127 | # continue |
| 128 | # for expected_service in expected_services: |
| 129 | # service_on_nodes = {_node: expected_service if expected_service in _service else None |
| 130 | # for _node, _service |
| 131 | # in output.items()} |
| 132 | # print([is_proper_service_for_node(expected_service, _node) |
| 133 | # for _node |
| 134 | # in output.keys()]) |
| 135 | # if not all([is_proper_service_for_node(expected_service, _node) |
| 136 | # for _node |
| 137 | # in output.keys()]): |
| 138 | # errors.append(service_on_nodes) |
| 139 | # |
| 140 | # assert errors.__len__() == 0, json.dumps(errors, indent=4) |
| 141 | # assert False |