blob: cfadcacd46df8c986fec6ffc34aef68d972c07f7 [file] [log] [blame]
Denis Meltsaykin5189d282021-09-14 15:28:42 +02001import json
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02002import utils
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -05003import pytest
Oleksii Zhurbae592ed12018-06-21 18:01:09 -05004
5
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -05006@pytest.mark.sl_dup
Denis V. Meltsaykin2d354f02020-04-29 12:23:21 +02007# RabbitmqServiceDown, RabbitmqErrorLogsTooHigh
8# TODO: a better test
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -05009@pytest.mark.full
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000010def test_checking_rabbitmq_cluster(local_salt_client):
11 # disable config for this test
12 # it may be reintroduced in future
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050013 config = utils.get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000014 # request pillar data from rmq nodes
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050015 # TODO: pillar.get
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000016 rabbitmq_pillar_data = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050017 tgt='rabbitmq:server',
18 fun='pillar.get',
19 param='rabbitmq:cluster',
20 expr_form='pillar')
Denis V. Meltsaykin1d68b2e2021-01-28 11:11:22 +010021 # 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 Zhurbaa10927b2017-09-27 22:09:23 +000024 # 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. Meltsaykin2d354f02020-04-29 12:23:21 +020028
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 Meltsaykin5189d282021-09-14 15:28:42 +020053 suffix = ' --formatter json' if newer_rabbit else ''
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000054 # request actual data from rmq nodes
55 rabbit_actual_data = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050056 tgt='rabbitmq:server',
Denis V. Meltsaykin2d354f02020-04-29 12:23:21 +020057 param=r'rabbitmqctl cluster_status{} '
58 r'| grep "nodes,\|running_nodes"'.format(suffix),
59 expr_form='pillar'
60 )
Denis Meltsaykin5189d282021-09-14 15:28:42 +020061 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 Zhurbae592ed12018-06-21 18:01:09 -050072 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 Zhurba4bfd2ee2019-04-10 21:56:58 -050077 rabbitmq_pillar_data[node]['members'])
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050078 required_cluster_size_dict.update({node: cluster_size_from_the_node})
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000079
80 # find actual cluster size for each node
81 for node in rabbit_actual_data:
82 running_nodes_count = 0
Oleksii Zhurba3e06b622018-01-16 18:43:16 +000083 # rabbitmqctl cluster_status output contains
Denis V. Meltsaykin2d354f02020-04-29 12:23:21 +020084 # 2 * # of nodes 'rabbit@' entries
85 running_nodes_count = rabbit_actual_data[node].count('rabbit@') // 2
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000086 # 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. Meltsaykin2d354f02020-04-29 12:23:21 +020091 assert len(control_dict) == 0, (
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020092 "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 Chernovae32e3f92019-11-12 14:56:03 +030095 len(list(required_cluster_size_dict.keys())), control_dict)
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020096 )