Hanna Arhipova | 131b58e | 2019-07-22 17:24:29 +0300 | [diff] [blame] | 1 | import pytest |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 2 | import json |
| 3 | |
| 4 | |
Hanna Arhipova | 131b58e | 2019-07-22 17:24:29 +0300 | [diff] [blame] | 5 | @pytest.mark.xfail(reason='PROD-31892') |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 6 | def 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 Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 13 | |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 14 | 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 Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 33 | group, nodes = nodes_in_group |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 34 | expected_kernel_params_by_nodes = local_salt_client.cmd( |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 35 | tgt="L@"+','.join(nodes), |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 36 | 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 Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 45 | for node in list(expected_kernel_params_by_nodes.keys()): |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 46 | actual_kernel_params_for_node = local_salt_client.cmd( |
| 47 | tgt=node, |
| 48 | fun='cmd.run', |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 49 | param="sysctl {}".format(" ".join(list(expected_kernel_params_by_nodes[node].keys()))), |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 50 | 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 Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 65 | for key, actual in list(values.items()) |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 66 | if expected_kernel_params_by_nodes[node].get(key) != actual ] |
| 67 | if differences.__len__() > 0: |
| 68 | issues[node] = differences |
| 69 | |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 70 | 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 | ) |