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