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