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 | """ |
| 13 | def normalize_value(value_in_string): |
| 14 | """ |
| 15 | Changes to INT if value_in_string is parcible to int |
| 16 | Replaces \t with spaces if value_in_string is a string |
| 17 | |
| 18 | :param value_in_string: |
| 19 | :return: |
| 20 | """ |
| 21 | if '\t' in value_in_string: |
| 22 | return value_in_string.replace('\t', ' ') |
| 23 | |
| 24 | try: |
| 25 | return int(value_in_string) |
| 26 | except ValueError: |
| 27 | pass |
| 28 | |
| 29 | return value_in_string |
| 30 | |
| 31 | issues = dict() |
| 32 | expected_kernel_params_by_nodes = local_salt_client.cmd( |
| 33 | tgt="L@"+','.join(nodes_in_group), |
| 34 | fun='pillar.get', |
| 35 | param="linux:system:kernel:sysctl", |
| 36 | expr_form='compound' |
| 37 | ) |
| 38 | |
| 39 | # Gather all params names from pillars and request their availability |
| 40 | # To get only specified values from system need to request them in the nex format |
| 41 | # 'sysctl param1 param2 param3 param4' |
| 42 | |
| 43 | for node in expected_kernel_params_by_nodes.keys(): |
| 44 | actual_kernel_params_for_node = local_salt_client.cmd( |
| 45 | tgt=node, |
| 46 | fun='cmd.run', |
| 47 | param="sysctl {}".format(" ".join(expected_kernel_params_by_nodes[node].keys())), |
| 48 | expr_form='compound' |
| 49 | ) |
| 50 | # make transfer string to dict format |
| 51 | # it does a magic from |
| 52 | # "vm.watermark_scale_factor = 10\nvm.zone_reclaim_mode = 0" |
| 53 | # to |
| 54 | # { |
| 55 | # "vm.zone_reclaim_mode": "0", |
| 56 | # "vm.watermark_scale_factor": "10" |
| 57 | # } |
| 58 | |
| 59 | values = {param.split(' = ')[0]: normalize_value(param.split(' = ')[-1]) |
| 60 | for param in actual_kernel_params_for_node[node].split('\n')} |
| 61 | |
| 62 | differences = [ "Parameter '{}' is not set === Expected '{}' === Got in sysctl '{}'".format(key, expected_kernel_params_by_nodes[node].get(key), actual) |
| 63 | for key, actual in values.items() |
| 64 | if expected_kernel_params_by_nodes[node].get(key) != actual ] |
| 65 | if differences.__len__() > 0: |
| 66 | issues[node] = differences |
| 67 | |
| 68 | assert issues.__len__() == 0, json.dumps(issues, indent=4) |
| 69 | |
| 70 | |