blob: bbd532e372adcf736f205d6ca081e3dd66c3c138 [file] [log] [blame]
Dmitriy Kruglove961f272019-01-23 09:37:13 +01001from collections import Counter
2from pprint import pformat
3import os
Oleksii Zhurba23c18332019-05-09 18:53:40 -05004import pytest
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03005import utils
Dmitriy Kruglove961f272019-01-23 09:37:13 +01006
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
Dmitriy Kruglove961f272019-01-23 09:37:13 +010016
Oleksii Zhurba23c18332019-05-09 18:53:40 -050017@pytest.mark.smoke
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030018def test_duplicate_ips(local_salt_client):
Dmitriy Kruglove961f272019-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
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030023 local_salt_client.cmd(tgt='*',
24 fun='saltutil.refresh_grains',
Dmitriy Kruglove961f272019-01-23 09:37:13 +010025 expr_form='compound')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030026 nodes = local_salt_client.cmd(tgt='*',
27 fun='grains.item',
28 param='ip4_interfaces',
Dmitriy Kruglove961f272019-01-23 09:37:13 +010029 expr_form='compound')
30
31 ipv4_list = []
32 for node in nodes:
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030033 if isinstance(nodes[node], bool):
34 # TODO: do not skip node
35 print ("{} node is skipped".format(node))
36 continue
Dmitriy Kruglove961f272019-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