blob: 302c8a32268547037d4c489be25199d45276853e [file] [log] [blame]
Hanna Arhipova131b58e2019-07-22 17:24:29 +03001import pytest
Hanna Arhipovab9635f92019-07-18 19:00:35 +03002import json
3
4
Hanna Arhipova131b58e2019-07-22 17:24:29 +03005@pytest.mark.xfail(reason='PROD-31892')
Hanna Arhipovab9635f92019-07-18 19:00:35 +03006def test_sysctl_variables(local_salt_client, nodes_in_group):
7 """
8 # Request kernel setting from linux:system:kernel:sysctl
9 # Request the same setting from sysctl utility on the node
10 # Compare that value in sysctl equals to the same value in pillars
11
12 """
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020013
Hanna Arhipovab9635f92019-07-18 19:00:35 +030014 def normalize_value(value_in_string):
15 """
16 Changes to INT if value_in_string is parcible to int
17 Replaces \t with spaces if value_in_string is a string
18
19 :param value_in_string:
20 :return:
21 """
22 if '\t' in value_in_string:
23 return value_in_string.replace('\t', ' ')
24
25 try:
26 return int(value_in_string)
27 except ValueError:
28 pass
29
30 return value_in_string
31
32 issues = dict()
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020033 group, nodes = nodes_in_group
Hanna Arhipovab9635f92019-07-18 19:00:35 +030034 expected_kernel_params_by_nodes = local_salt_client.cmd(
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020035 tgt="L@"+','.join(nodes),
Hanna Arhipovab9635f92019-07-18 19:00:35 +030036 fun='pillar.get',
37 param="linux:system:kernel:sysctl",
38 expr_form='compound'
39 )
40
41 # Gather all params names from pillars and request their availability
42 # To get only specified values from system need to request them in the nex format
43 # 'sysctl param1 param2 param3 param4'
44
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030045 for node in list(expected_kernel_params_by_nodes.keys()):
Hanna Arhipovab9635f92019-07-18 19:00:35 +030046 actual_kernel_params_for_node = local_salt_client.cmd(
47 tgt=node,
48 fun='cmd.run',
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030049 param="sysctl {}".format(" ".join(list(expected_kernel_params_by_nodes[node].keys()))),
Hanna Arhipovab9635f92019-07-18 19:00:35 +030050 expr_form='compound'
51 )
52 # make transfer string to dict format
53 # it does a magic from
54 # "vm.watermark_scale_factor = 10\nvm.zone_reclaim_mode = 0"
55 # to
56 # {
57 # "vm.zone_reclaim_mode": "0",
58 # "vm.watermark_scale_factor": "10"
59 # }
60
61 values = {param.split(' = ')[0]: normalize_value(param.split(' = ')[-1])
62 for param in actual_kernel_params_for_node[node].split('\n')}
63
64 differences = [ "Parameter '{}' is not set === Expected '{}' === Got in sysctl '{}'".format(key, expected_kernel_params_by_nodes[node].get(key), actual)
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030065 for key, actual in list(values.items())
Hanna Arhipovab9635f92019-07-18 19:00:35 +030066 if expected_kernel_params_by_nodes[node].get(key) != actual ]
67 if differences.__len__() > 0:
68 issues[node] = differences
69
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020070 assert issues.__len__() == 0, (
71 "There are inconsistencies between kernel settings defined in pillars "
72 "and actual settings on nodes of '{}' group: {}".format(
73 group, json.dumps(issues, indent=4))
74 )