blob: 3b55a26721955d0ec106321e37b13959c93c8208 [file] [log] [blame]
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +01001from collections import Counter
2from pprint import pformat
3import os
4
5import utils
6
7
8def 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 Arhipovaae0e72a2019-02-12 13:57:26 +020016
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010017def test_duplicate_ips(local_salt_client):
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010018 testname = os.path.basename(__file__).split('.')[0]
19 config = utils.get_configuration()
20 skipped_ifaces = config.get(testname)["skipped_ifaces"]
21
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050022 local_salt_client.cmd(tgt='*',
23 fun='saltutil.refresh_grains',
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010024 expr_form='compound')
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050025 nodes = local_salt_client.cmd(tgt='*',
26 fun='grains.item',
27 param='ip4_interfaces',
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010028 expr_form='compound')
29
30 ipv4_list = []
31 for node in nodes:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050032 if isinstance(nodes[node], bool):
33 # TODO: do not skip node
34 print ("{} node is skipped".format(node))
35 continue
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010036 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