Denis Meltsaykin | 5189d28 | 2021-09-14 15:28:42 +0200 | [diff] [blame] | 1 | import json |
Hanna Arhipova | 16e93fb | 2019-01-23 19:03:01 +0200 | [diff] [blame] | 2 | import utils |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 3 | import pytest |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 4 | |
| 5 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 6 | @pytest.mark.sl_dup |
Denis V. Meltsaykin | 2d354f0 | 2020-04-29 12:23:21 +0200 | [diff] [blame] | 7 | # RabbitmqServiceDown, RabbitmqErrorLogsTooHigh |
| 8 | # TODO: a better test |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 9 | @pytest.mark.full |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 10 | def test_checking_rabbitmq_cluster(local_salt_client): |
| 11 | # disable config for this test |
| 12 | # it may be reintroduced in future |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 13 | config = utils.get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 14 | # request pillar data from rmq nodes |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 15 | # TODO: pillar.get |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 16 | rabbitmq_pillar_data = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 17 | tgt='rabbitmq:server', |
| 18 | fun='pillar.get', |
| 19 | param='rabbitmq:cluster', |
| 20 | expr_form='pillar') |
Denis V. Meltsaykin | 1d68b2e | 2021-01-28 11:11:22 +0100 | [diff] [blame] | 21 | # skip the test if there is no cluster data in pillars |
| 22 | if sum([len(v) for v in rabbitmq_pillar_data.values()]) == 0: |
| 23 | pytest.skip("No RabbitMQ cluster pillar available") |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 24 | # creating dictionary {node:cluster_size_for_the_node} |
| 25 | # with required cluster size for each node |
| 26 | control_dict = {} |
| 27 | required_cluster_size_dict = {} |
Denis V. Meltsaykin | 2d354f0 | 2020-04-29 12:23:21 +0200 | [diff] [blame] | 28 | |
| 29 | # check rabbitmq version |
| 30 | version_data = local_salt_client.cmd( |
| 31 | tgt='I@rabbitmq:server', |
| 32 | fun='pkg.version', |
| 33 | param='rabbitmq-server', |
| 34 | expr_form='compound' |
| 35 | ) |
| 36 | |
| 37 | rabbitmq_versions = set(version_data.values()) |
| 38 | |
| 39 | assert len(rabbitmq_versions) == 1, ( |
| 40 | "Non-matching RabbitMQ versions installed:{}".format(version_data) |
| 41 | ) |
| 42 | |
| 43 | rabbitmq_version = rabbitmq_versions.pop() |
| 44 | |
| 45 | # check if the installed RabbitMQ is 3.8 |
| 46 | newer_rabbit = int(local_salt_client.cmd( |
| 47 | tgt='I@salt:master', |
| 48 | fun='pkg.version_cmp', |
| 49 | param=['{}'.format(rabbitmq_version), '3.8'], |
| 50 | expr_form='compound' |
| 51 | ).popitem()[1]) >= 0 |
| 52 | |
Denis Meltsaykin | 5189d28 | 2021-09-14 15:28:42 +0200 | [diff] [blame] | 53 | suffix = ' --formatter json' if newer_rabbit else '' |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 54 | # request actual data from rmq nodes |
| 55 | rabbit_actual_data = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 56 | tgt='rabbitmq:server', |
Denis V. Meltsaykin | 2d354f0 | 2020-04-29 12:23:21 +0200 | [diff] [blame] | 57 | param=r'rabbitmqctl cluster_status{} ' |
| 58 | r'| grep "nodes,\|running_nodes"'.format(suffix), |
| 59 | expr_form='pillar' |
| 60 | ) |
Denis Meltsaykin | 5189d28 | 2021-09-14 15:28:42 +0200 | [diff] [blame] | 61 | if newer_rabbit: |
| 62 | temp = {} |
| 63 | for node in rabbit_actual_data: |
| 64 | try: |
| 65 | node_data = json.loads(rabbit_actual_data[node]) |
| 66 | temp[node] = "{}, {}".format( |
| 67 | node_data["disk_nodes"], node_data["running_nodes"]) |
| 68 | except json.decoder.JSONDecodeError: |
| 69 | pass |
| 70 | rabbit_actual_data = temp |
| 71 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 72 | for node in rabbitmq_pillar_data: |
| 73 | if node in config.get('skipped_nodes'): |
| 74 | del rabbit_actual_data[node] |
| 75 | continue |
| 76 | cluster_size_from_the_node = len( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 77 | rabbitmq_pillar_data[node]['members']) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 78 | required_cluster_size_dict.update({node: cluster_size_from_the_node}) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 79 | |
| 80 | # find actual cluster size for each node |
| 81 | for node in rabbit_actual_data: |
| 82 | running_nodes_count = 0 |
Oleksii Zhurba | 3e06b62 | 2018-01-16 18:43:16 +0000 | [diff] [blame] | 83 | # rabbitmqctl cluster_status output contains |
Denis V. Meltsaykin | 2d354f0 | 2020-04-29 12:23:21 +0200 | [diff] [blame] | 84 | # 2 * # of nodes 'rabbit@' entries |
| 85 | running_nodes_count = rabbit_actual_data[node].count('rabbit@') // 2 |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 86 | # update control dictionary with values |
| 87 | # {node:actual_cluster_size_for_node} |
| 88 | if required_cluster_size_dict[node] != running_nodes_count: |
| 89 | control_dict.update({node: running_nodes_count}) |
| 90 | |
Denis V. Meltsaykin | 2d354f0 | 2020-04-29 12:23:21 +0200 | [diff] [blame] | 91 | assert len(control_dict) == 0, ( |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 92 | "RabbitMQ cluster is probably " |
| 93 | "broken - the cluster size for each node should be ({}),\nbut the " |
| 94 | "following nodes have other values:\n{}".format( |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 95 | len(list(required_cluster_size_dict.keys())), control_dict) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 96 | ) |