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