Dmitriy Kruglov | e961f27 | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 1 | from collections import Counter |
| 2 | from pprint import pformat |
| 3 | import os |
Oleksii Zhurba | 23c1833 | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 4 | import pytest |
Hanna Arhipova | 1eef831 | 2019-05-06 20:14:18 +0300 | [diff] [blame^] | 5 | import logging |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 6 | import utils |
Dmitriy Kruglov | e961f27 | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 7 | |
| 8 | |
| 9 | def get_duplicate_ifaces(nodes, ips): |
| 10 | dup_ifaces = {} |
| 11 | for node in nodes: |
| 12 | for iface in nodes[node]['ip4_interfaces']: |
| 13 | if set(nodes[node]['ip4_interfaces'][iface]) & set(ips): |
| 14 | dup_ifaces[node] = {iface: nodes[node]['ip4_interfaces'][iface]} |
| 15 | return dup_ifaces |
| 16 | |
Dmitriy Kruglov | e961f27 | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 17 | |
Oleksii Zhurba | 23c1833 | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 18 | @pytest.mark.smoke |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 19 | def test_duplicate_ips(local_salt_client): |
Dmitriy Kruglov | e961f27 | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 20 | testname = os.path.basename(__file__).split('.')[0] |
| 21 | config = utils.get_configuration() |
| 22 | skipped_ifaces = config.get(testname)["skipped_ifaces"] |
| 23 | |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 24 | local_salt_client.cmd(tgt='*', |
| 25 | fun='saltutil.refresh_grains', |
Dmitriy Kruglov | e961f27 | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 26 | expr_form='compound') |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 27 | nodes = local_salt_client.cmd(tgt='*', |
| 28 | fun='grains.item', |
| 29 | param='ip4_interfaces', |
Dmitriy Kruglov | e961f27 | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 30 | expr_form='compound') |
| 31 | |
| 32 | ipv4_list = [] |
| 33 | for node in nodes: |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 34 | if isinstance(nodes[node], bool): |
| 35 | # TODO: do not skip node |
Hanna Arhipova | 1eef831 | 2019-05-06 20:14:18 +0300 | [diff] [blame^] | 36 | logging.info("{} node is skipped".format(node)) |
Hanna Arhipova | e6ed8e4 | 2019-05-15 16:27:08 +0300 | [diff] [blame] | 37 | continue |
Dmitriy Kruglov | e961f27 | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 38 | for iface in nodes[node]['ip4_interfaces']: |
| 39 | # Omit 'ip-less' ifaces |
| 40 | if not nodes[node]['ip4_interfaces'][iface]: |
| 41 | continue |
| 42 | if iface in skipped_ifaces: |
| 43 | continue |
| 44 | ipv4_list.extend(nodes[node]['ip4_interfaces'][iface]) |
| 45 | no_dups = (len(ipv4_list) == len(set(ipv4_list))) |
| 46 | if not no_dups: |
| 47 | ips_count = Counter(ipv4_list).most_common() |
| 48 | dup_ips = filter(lambda x: x[1] > 1, ips_count) |
| 49 | dup_ifaces = get_duplicate_ifaces(nodes, [v[0] for v in dup_ips]) |
| 50 | |
| 51 | msg = ("\nDuplicate IP addresses found:\n{}" |
| 52 | "\n\nThe following interfaces are affected:\n{}" |
| 53 | "".format(pformat(dup_ips), pformat(dup_ifaces))) |
| 54 | assert no_dups, msg |