blob: bbd532e372adcf736f205d6ca081e3dd66c3c138 [file] [log] [blame]
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +01001from collections import Counter
2from pprint import pformat
3import os
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -05004import pytest
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +01005import 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
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050017@pytest.mark.smoke
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010018def test_duplicate_ips(local_salt_client):
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010019 testname = os.path.basename(__file__).split('.')[0]
20 config = utils.get_configuration()
21 skipped_ifaces = config.get(testname)["skipped_ifaces"]
22
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050023 local_salt_client.cmd(tgt='*',
24 fun='saltutil.refresh_grains',
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010025 expr_form='compound')
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050026 nodes = local_salt_client.cmd(tgt='*',
27 fun='grains.item',
28 param='ip4_interfaces',
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010029 expr_form='compound')
30
31 ipv4_list = []
32 for node in nodes:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050033 if isinstance(nodes[node], bool):
34 # TODO: do not skip node
35 print ("{} node is skipped".format(node))
36 continue
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010037 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