Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame^] | 1 | import pytest |
| 2 | from collections import Counter |
| 3 | from pprint import pformat |
| 4 | import os |
| 5 | |
| 6 | import utils |
| 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 | |
| 17 | def test_duplicate_ips(local_salt_client): |
| 18 | active_nodes = utils.get_active_nodes() |
| 19 | |
| 20 | testname = os.path.basename(__file__).split('.')[0] |
| 21 | config = utils.get_configuration() |
| 22 | skipped_ifaces = config.get(testname)["skipped_ifaces"] |
| 23 | |
| 24 | local_salt_client.cmd('L@'+','.join(active_nodes), |
| 25 | 'saltutil.refresh_grains', |
| 26 | expr_form='compound') |
| 27 | nodes = local_salt_client.cmd('L@'+','.join(active_nodes), |
| 28 | 'grains.item', |
| 29 | ['ip4_interfaces'], |
| 30 | expr_form='compound') |
| 31 | |
| 32 | ipv4_list = [] |
| 33 | for node in nodes: |
| 34 | for iface in nodes[node]['ip4_interfaces']: |
| 35 | # Omit 'ip-less' ifaces |
| 36 | if not nodes[node]['ip4_interfaces'][iface]: |
| 37 | continue |
| 38 | if iface in skipped_ifaces: |
| 39 | continue |
| 40 | ipv4_list.extend(nodes[node]['ip4_interfaces'][iface]) |
| 41 | no_dups = (len(ipv4_list) == len(set(ipv4_list))) |
| 42 | if not no_dups: |
| 43 | ips_count = Counter(ipv4_list).most_common() |
| 44 | dup_ips = filter(lambda x: x[1] > 1, ips_count) |
| 45 | dup_ifaces = get_duplicate_ifaces(nodes, [v[0] for v in dup_ips]) |
| 46 | |
| 47 | msg = ("\nDuplicate IP addresses found:\n{}" |
| 48 | "\n\nThe following interfaces are affected:\n{}" |
| 49 | "".format(pformat(dup_ips), pformat(dup_ifaces))) |
| 50 | assert no_dups, msg |