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