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 | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 6 | # Some nodes can have services that are not applicable for other noder in similar group. |
| 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 | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 18 | output = local_salt_client.cmd("L@"+','.join(nodes_in_group), 'service.get_all', expr_form='compound') |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 19 | |
| 20 | if len(output.keys()) < 2: |
| 21 | pytest.skip("Nothing to compare - only 1 node") |
| 22 | |
| 23 | nodes = [] |
| 24 | pkts_data = [] |
| 25 | my_set = set() |
| 26 | |
| 27 | for node in output: |
| 28 | nodes.append(node) |
| 29 | my_set.update(output[node]) |
| 30 | |
| 31 | for srv in my_set: |
| 32 | diff = [] |
| 33 | row = [] |
| 34 | for node in nodes: |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 35 | short_name_of_node = node.split('.')[0] |
| 36 | if inconsistency_rule.get(short_name_of_node) is not None and srv in inconsistency_rule[short_name_of_node]: |
| 37 | # Found service on node and it SHOULD be there |
| 38 | break |
| 39 | elif srv in output[node]: |
| 40 | # Found service on node |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 41 | diff.append(srv) |
| 42 | row.append("{}: +".format(node)) |
| 43 | else: |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 44 | # Not found expected service on node |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 45 | row.append("{}: No service".format(node)) |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 46 | if diff.__len__() > 0 and diff.count(diff[0]) < len(nodes): |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 47 | row.sort() |
| 48 | row.insert(0, srv) |
| 49 | pkts_data.append(row) |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 50 | assert len(pkts_data) == 0, \ |
Oleksii Zhurba | a32d92f | 2018-03-29 16:22:35 -0500 | [diff] [blame] | 51 | "Several problems found: {0}".format( |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 52 | json.dumps(pkts_data, indent=4)) |
Hanna Arhipova | feebf9e | 2018-12-24 12:45:54 +0200 | [diff] [blame] | 53 | |
| 54 | |
| 55 | # TODO : remake this test to make workable https://mirantis.jira.com/browse/PROD-25958 |
| 56 | |
| 57 | # def _check_services_on_special_node(local_salt_client, nodes_in_group): |
| 58 | # """ |
| 59 | # Check that specific node has service. |
| 60 | # Nodes and proper services should be defined in inconsistency_rule dictionary |
| 61 | # |
| 62 | # :print: Table with nodes which don't have required services and not existed services |
| 63 | # """ |
| 64 | # |
| 65 | # output = local_salt_client.cmd("L@" + ','.join(nodes_in_group), 'service.get_all', expr_form='compound') |
| 66 | # if len(output.keys()) < 2: |
| 67 | # pytest.skip("Nothing to compare - just 1 node") |
| 68 | # |
| 69 | # def is_proper_service_for_node(_service, _node): |
| 70 | # """ |
| 71 | # Return True if service exists on node and exists in inconsistency_rule |
| 72 | # Return True if service doesn't exists on node and doesn't exists in inconsistency_rule |
| 73 | # Return False otherwise |
| 74 | # :param _service: string |
| 75 | # :param _node: string full name of node |
| 76 | # :return: bool, read description for further details |
| 77 | # """ |
| 78 | # short_name_of_node = _node.split('.')[0] |
| 79 | # if short_name_of_node not in inconsistency_rule.keys(): |
| 80 | # return False |
| 81 | # |
| 82 | # if _service in inconsistency_rule[short_name_of_node] and \ |
| 83 | # _service in output[_node]: |
| 84 | # # Return True if service exists on node and exists in inconsistency_rule |
| 85 | # return True |
| 86 | # |
| 87 | # if _service not in inconsistency_rule[short_name_of_node] and \ |
| 88 | # _service not in output[_node]: |
| 89 | # # Return True if service exists on node and exists in inconsistency_rule |
| 90 | # return True |
| 91 | # print("return False for {} in {}".format(_service, _node)) |
| 92 | # # error_text = "" |
| 93 | # return False |
| 94 | # |
| 95 | # errors = list() |
| 96 | # for node, expected_services in inconsistency_rule.items(): |
| 97 | # print("Check {} , {} ".format(node, expected_services)) |
| 98 | # # Skip if there is no proper node. Find nodes that contains node_title (like 'kvm03') in their titles |
| 99 | # if not any([node in node_name for node_name in output.keys()]): |
| 100 | # continue |
| 101 | # for expected_service in expected_services: |
| 102 | # service_on_nodes = {_node: expected_service if expected_service in _service else None |
| 103 | # for _node, _service |
| 104 | # in output.items()} |
| 105 | # print([is_proper_service_for_node(expected_service, _node) |
| 106 | # for _node |
| 107 | # in output.keys()]) |
| 108 | # if not all([is_proper_service_for_node(expected_service, _node) |
| 109 | # for _node |
| 110 | # in output.keys()]): |
| 111 | # errors.append(service_on_nodes) |
| 112 | # |
| 113 | # assert errors.__len__() == 0, json.dumps(errors, indent=4) |
| 114 | # assert False |