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