Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 1 | import pytest |
| 2 | import json |
Ievgeniia Zadorozhna | 6baf787 | 2019-01-25 19:09:30 +0300 | [diff] [blame] | 3 | import utils |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 4 | import logging |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 5 | import itertools |
| 6 | from functools import lru_cache |
Ievgeniia Zadorozhna | 6baf787 | 2019-01-25 19:09:30 +0300 | [diff] [blame] | 7 | |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 8 | |
| 9 | @lru_cache(maxsize=32) |
| 10 | def targeted_minions(target): |
| 11 | """ |
| 12 | Returns nodes associated with salt target |
| 13 | :param target: str, salt target in COMPOUND notation like I@nova:server |
| 14 | More here https://docs.saltproject.io/en/latest/topics/targeting/compound.html |
| 15 | :return: list of nodes or [] |
| 16 | """ |
| 17 | salt_client = pytest.local_salt_client |
| 18 | return list(salt_client.test_ping( |
| 19 | tgt=target, |
| 20 | expr_form='compound')) |
| 21 | |
| 22 | |
| 23 | def is_deb_in_exception(inconsistency_rule, package_name, node_hostname): |
| 24 | for salt_target, excluded_packages in inconsistency_rule.items(): |
| 25 | if package_name in excluded_packages \ |
| 26 | and node_hostname in targeted_minions(salt_target): |
Ekaterina Chernova | b480675 | 2019-07-26 16:02:18 +0300 | [diff] [blame] | 27 | return True |
| 28 | return False |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 29 | |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 30 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 31 | @pytest.mark.full |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 32 | def test_check_package_versions(local_salt_client, nodes_in_group): |
Ekaterina Chernova | b480675 | 2019-07-26 16:02:18 +0300 | [diff] [blame] | 33 | """Validate package has same versions on all the nodes. |
| 34 | Steps: |
| 35 | 1) Collect packages for nodes_in_group |
| 36 | "salt -C '<group_of_nodes>' cmd.run 'lowpkg.list_pkgs'" |
| 37 | 2) Exclude nodes without packages and exceptions |
Ekaterina Chernova | 7ea9715 | 2019-07-29 17:09:12 +0300 | [diff] [blame] | 38 | 3) Go through each package and save it with version from each node to the |
Ekaterina Chernova | b480675 | 2019-07-26 16:02:18 +0300 | [diff] [blame] | 39 | list. Mark 'No version' if package is not found. |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 40 | If package name in the exception list or in inconsistency_rule, |
| 41 | ignore it. |
Ekaterina Chernova | b480675 | 2019-07-26 16:02:18 +0300 | [diff] [blame] | 42 | 4) Compare items in that list - they should be equal and match the amout of nodes |
| 43 | |
| 44 | """ |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 45 | salt = local_salt_client |
| 46 | # defines packages specific to the nodes |
| 47 | inconsistency_rule = { |
| 48 | "I@backupninja:server": [ |
| 49 | "rsync", "sysstat", "xz-utils"], |
| 50 | "I@elasticsearch:server": [ |
| 51 | "python-elasticsearch"], |
| 52 | # PROD-30833 |
| 53 | "I@octavia:manager:controller_worker:loadbalancer_topology:SINGLE": [ |
| 54 | "netfilter-persistent", |
| 55 | "gunicorn", |
| 56 | "octavia-worker", |
| 57 | "octavia-health-manager", |
| 58 | "octavia-housekeeping"] |
| 59 | } |
Hanna Arhipova | 8fd295c | 2019-03-07 13:46:43 +0200 | [diff] [blame] | 60 | exclude_packages = utils.get_configuration().get("skipped_packages", []) |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 61 | |
| 62 | group_name, nodes = nodes_in_group |
| 63 | packages_versions_by_nodes = salt.cmd(tgt="L@"+','.join(nodes), |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 64 | fun='lowpkg.list_pkgs', |
| 65 | expr_form='compound') |
Oleksii Zhurba | dad1acc | 2018-03-26 14:09:38 -0500 | [diff] [blame] | 66 | # Let's exclude cid01 and dbs01 nodes from this check |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 67 | exclude_nodes = targeted_minions("I@galera:master or I@gerrit:client") |
Oleksii Zhurba | 599801a | 2019-06-04 17:26:51 -0500 | [diff] [blame] | 68 | |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 69 | total_nodes = [i |
| 70 | for i in nodes |
| 71 | if i not in exclude_nodes] |
Oleksii Zhurba | 5f768c5 | 2018-08-07 17:27:57 -0500 | [diff] [blame] | 72 | if len(total_nodes) < 2: |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 73 | pytest.skip("Nothing to compare - only 1 node") |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 74 | |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 75 | packages_with_different_versions = dict() |
| 76 | packages_names = set(itertools.chain.from_iterable( |
| 77 | [packages_versions_by_nodes[node].keys() |
| 78 | for node in total_nodes]) |
| 79 | ) |
| 80 | |
Hanna Arhipova | 8fd295c | 2019-03-07 13:46:43 +0200 | [diff] [blame] | 81 | for deb in packages_names: |
| 82 | if deb in exclude_packages: |
| 83 | continue |
Ekaterina Chernova | b480675 | 2019-07-26 16:02:18 +0300 | [diff] [blame] | 84 | |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 85 | node_and_version = [ |
| 86 | (node, packages_versions_by_nodes[node].get(deb, "No package")) |
| 87 | for node in total_nodes |
| 88 | if not is_deb_in_exception(inconsistency_rule, deb, node) |
| 89 | ] |
| 90 | |
| 91 | if set([version for node, version in node_and_version]).__len__() > 1: |
| 92 | packages_with_different_versions[deb] = [ |
| 93 | f"{node}: {version}" |
| 94 | for node, version in node_and_version] |
| 95 | |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 96 | assert len(packages_with_different_versions) == 0, ( |
| 97 | "Non-uniform package versions are installed on '{}' group of nodes:\n" |
| 98 | "{}".format( |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 99 | group_name, json.dumps(packages_with_different_versions, indent=4)) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 100 | ) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 101 | |
| 102 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 103 | @pytest.mark.full |
Ievgeniia Zadorozhna | 6baf787 | 2019-01-25 19:09:30 +0300 | [diff] [blame] | 104 | def test_packages_are_latest(local_salt_client, nodes_in_group): |
| 105 | config = utils.get_configuration() |
| 106 | skip = config.get("test_packages")["skip_test"] |
Ievgeniia Zadorozhna | 0c306fd | 2019-11-12 16:03:51 +0300 | [diff] [blame] | 107 | if skip.lower() == 'true': |
Ievgeniia Zadorozhna | 6baf787 | 2019-01-25 19:09:30 +0300 | [diff] [blame] | 108 | pytest.skip("Test for the latest packages is disabled") |
| 109 | skipped_pkg = config.get("test_packages")["skipped_packages"] |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 110 | group, nodes = nodes_in_group |
Ievgeniia Zadorozhna | 6baf787 | 2019-01-25 19:09:30 +0300 | [diff] [blame] | 111 | info_salt = local_salt_client.cmd( |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 112 | tgt='L@' + ','.join(nodes), |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 113 | param='apt list --upgradable 2>/dev/null | grep -v Listing', |
Ievgeniia Zadorozhna | 6baf787 | 2019-01-25 19:09:30 +0300 | [diff] [blame] | 114 | expr_form='compound') |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 115 | for node in nodes: |
Ievgeniia Zadorozhna | 6baf787 | 2019-01-25 19:09:30 +0300 | [diff] [blame] | 116 | result = [] |
| 117 | if info_salt[node]: |
| 118 | upg_list = info_salt[node].split('\n') |
| 119 | for i in upg_list: |
| 120 | if i.split('/')[0] not in skipped_pkg: |
| 121 | result.append(i) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 122 | assert not result, ( |
| 123 | "Packages are not of latest version on '{}' node:\n{}".format( |
| 124 | node, "\n".join(result)) |
| 125 | ) |
Ievgeniia Zadorozhna | 6baf787 | 2019-01-25 19:09:30 +0300 | [diff] [blame] | 126 | |
| 127 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 128 | @pytest.mark.full |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 129 | def test_check_module_versions(local_salt_client, nodes_in_group): |
Ekaterina Chernova | 7ea9715 | 2019-07-29 17:09:12 +0300 | [diff] [blame] | 130 | # defines modules specific to the concrete nodes |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 131 | inconsistency_rule = {"I@elasticsearch:server": ["elasticsearch"]} |
Hanna Arhipova | 8fd295c | 2019-03-07 13:46:43 +0200 | [diff] [blame] | 132 | exclude_modules = utils.get_configuration().get("skipped_modules", []) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 133 | group, nodes = nodes_in_group |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 134 | pre_check = local_salt_client.cmd( |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 135 | tgt="L@"+','.join(nodes), |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 136 | param='dpkg -l | grep "python-pip "', |
Oleksii Zhurba | 5f768c5 | 2018-08-07 17:27:57 -0500 | [diff] [blame] | 137 | expr_form='compound') |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 138 | if list(pre_check.values()).count('') > 0: |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 139 | pytest.skip("pip is not installed on one or more nodes") |
Oleksii Zhurba | 5f768c5 | 2018-08-07 17:27:57 -0500 | [diff] [blame] | 140 | |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 141 | exclude_nodes = targeted_minions("I@galera:master or I@gerrit:client") |
Oleksii Zhurba | 599801a | 2019-06-04 17:26:51 -0500 | [diff] [blame] | 142 | |
| 143 | # PROD-30833 |
| 144 | gtw01 = local_salt_client.pillar_get( |
| 145 | param='_param:openstack_gateway_node01_hostname') or 'gtw01' |
| 146 | cluster_domain = local_salt_client.pillar_get( |
| 147 | param='_param:cluster_domain') or '.local' |
| 148 | gtw01 += '.' + cluster_domain |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 149 | if gtw01 in nodes: |
| 150 | octavia = local_salt_client.cmd(tgt="L@" + ','.join(nodes), |
Oleksii Zhurba | 599801a | 2019-06-04 17:26:51 -0500 | [diff] [blame] | 151 | fun='pillar.get', |
| 152 | param='octavia:manager:enabled', |
| 153 | expr_form='compound') |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 154 | gtws = [gtw for gtw in list(octavia.values()) if gtw] |
Oleksii Zhurba | 599801a | 2019-06-04 17:26:51 -0500 | [diff] [blame] | 155 | if len(gtws) == 1: |
| 156 | exclude_nodes.append(gtw01) |
| 157 | logging.info("gtw01 node is skipped in test_check_module_versions") |
| 158 | |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 159 | total_nodes = [i for i in list(pre_check.keys()) if i not in exclude_nodes] |
Oleksii Zhurba | 5f768c5 | 2018-08-07 17:27:57 -0500 | [diff] [blame] | 160 | |
| 161 | if len(total_nodes) < 2: |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 162 | pytest.skip("Nothing to compare - only 1 node") |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 163 | list_of_pip_packages = local_salt_client.cmd( |
| 164 | tgt="L@"+','.join(nodes), |
| 165 | fun='pip.freeze', expr_form='compound') |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 166 | |
Ekaterina Chernova | 7ea9715 | 2019-07-29 17:09:12 +0300 | [diff] [blame] | 167 | nodes_with_packages = [] |
Oleksii Zhurba | a32d92f | 2018-03-29 16:22:35 -0500 | [diff] [blame] | 168 | |
Ekaterina Chernova | 7ea9715 | 2019-07-29 17:09:12 +0300 | [diff] [blame] | 169 | modules_with_different_versions = [] |
Hanna Arhipova | 8fd295c | 2019-03-07 13:46:43 +0200 | [diff] [blame] | 170 | packages_names = set() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 171 | |
Oleksii Zhurba | 5f768c5 | 2018-08-07 17:27:57 -0500 | [diff] [blame] | 172 | for node in total_nodes: |
Ekaterina Chernova | 7ea9715 | 2019-07-29 17:09:12 +0300 | [diff] [blame] | 173 | nodes_with_packages.append(node) |
Hanna Arhipova | 8fd295c | 2019-03-07 13:46:43 +0200 | [diff] [blame] | 174 | packages_names.update([x.split("=")[0] for x in list_of_pip_packages[node]]) |
| 175 | list_of_pip_packages[node] = dict([x.split("==") for x in list_of_pip_packages[node]]) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 176 | |
Hanna Arhipova | 8fd295c | 2019-03-07 13:46:43 +0200 | [diff] [blame] | 177 | for deb in packages_names: |
| 178 | if deb in exclude_modules: |
| 179 | continue |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 180 | diff = [] |
| 181 | row = [] |
Ekaterina Chernova | 7ea9715 | 2019-07-29 17:09:12 +0300 | [diff] [blame] | 182 | for node in nodes_with_packages: |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 183 | if deb in list(list_of_pip_packages[node].keys()): |
Hanna Arhipova | 8fd295c | 2019-03-07 13:46:43 +0200 | [diff] [blame] | 184 | diff.append(list_of_pip_packages[node][deb]) |
| 185 | row.append("{}: {}".format(node, list_of_pip_packages[node][deb])) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 186 | else: |
| 187 | row.append("{}: No module".format(node)) |
Ekaterina Chernova | 7ea9715 | 2019-07-29 17:09:12 +0300 | [diff] [blame] | 188 | if diff.count(diff[0]) < len(nodes_with_packages): |
| 189 | if not is_deb_in_exception(inconsistency_rule, deb, row): |
| 190 | row.sort() |
| 191 | row.insert(0, deb) |
| 192 | modules_with_different_versions.append(row) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 193 | assert len(modules_with_different_versions) == 0, ( |
| 194 | "Non-uniform pip modules are installed on '{}' group of nodes:\n" |
| 195 | "{}".format( |
| 196 | group, json.dumps(modules_with_different_versions, indent=4)) |
| 197 | ) |
Hanna Arhipova | 5a56ae6 | 2021-02-11 16:27:52 +0200 | [diff] [blame] | 198 | |
| 199 | |
| 200 | @pytest.mark.full |
| 201 | def test_restricted_updates_repo(local_salt_client): |
| 202 | restricted_repo_enabled = local_salt_client.pillar_get( |
| 203 | tgt="I@salt:master", |
Hanna Arhipova | 1d87dbe | 2021-02-16 19:26:27 +0200 | [diff] [blame] | 204 | param='_param:updates_mirantis_login', |
Hanna Arhipova | 5a56ae6 | 2021-02-11 16:27:52 +0200 | [diff] [blame] | 205 | expr_form='compound') |
| 206 | if not restricted_repo_enabled: |
Hanna Arhipova | 2d53841 | 2021-08-17 10:44:41 +0300 | [diff] [blame^] | 207 | pytest.skip("This env doesn't require the restricted ubuntu repo") |
Hanna Arhipova | 5a56ae6 | 2021-02-11 16:27:52 +0200 | [diff] [blame] | 208 | |
| 209 | repos_by_nodes=local_salt_client.cmd( |
| 210 | tgt="*", |
| 211 | param="apt-cache policy |grep updates.mirantis.com" |
| 212 | ) |
| 213 | |
Hanna Arhipova | 1d87dbe | 2021-02-16 19:26:27 +0200 | [diff] [blame] | 214 | assert all(list(repos_by_nodes.values())), \ |
Hanna Arhipova | 5a56ae6 | 2021-02-11 16:27:52 +0200 | [diff] [blame] | 215 | "Next nodes don't have updates.mirantis.com in sources.list: {}".\ |
| 216 | format({node for node, repo |
| 217 | in repos_by_nodes.items() |
| 218 | if not repo}) |