blob: 90e6d60ff33c4a52f8fee264614b75bb5725ab03 [file] [log] [blame]
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +01001import pytest
2from collections import Counter
3from pprint import pformat
4import os
5
6import utils
7
8
9def 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 Arhipovaae0e72a2019-02-12 13:57:26 +020017
18@pytest.mark.xfail
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010019def test_duplicate_ips(local_salt_client):
Hanna Arhipovaae0e72a2019-02-12 13:57:26 +020020 # It doesn't work for test_drivetrain job
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010021 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