blob: 392838f0c9429389e0f746ec617be9bcbb465ed1 [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 Arhipova1eef8312019-05-06 20:14:18 +03005import logging
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03006import utils
Dmitriy Kruglove961f272019-01-23 09:37:13 +01007
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
Dmitriy Kruglove961f272019-01-23 09:37:13 +010017
Oleksii Zhurba23c18332019-05-09 18:53:40 -050018@pytest.mark.smoke
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030019def test_duplicate_ips(local_salt_client):
Dmitriy Kruglove961f272019-01-23 09:37:13 +010020 testname = os.path.basename(__file__).split('.')[0]
21 config = utils.get_configuration()
22 skipped_ifaces = config.get(testname)["skipped_ifaces"]
23
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030024 local_salt_client.cmd(tgt='*',
25 fun='saltutil.refresh_grains',
Dmitriy Kruglove961f272019-01-23 09:37:13 +010026 expr_form='compound')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030027 nodes = local_salt_client.cmd(tgt='*',
28 fun='grains.item',
29 param='ip4_interfaces',
Dmitriy Kruglove961f272019-01-23 09:37:13 +010030 expr_form='compound')
31
32 ipv4_list = []
33 for node in nodes:
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030034 if isinstance(nodes[node], bool):
35 # TODO: do not skip node
Hanna Arhipova1eef8312019-05-06 20:14:18 +030036 logging.info("{} node is skipped".format(node))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030037 continue
Dmitriy Kruglove961f272019-01-23 09:37:13 +010038 for iface in nodes[node]['ip4_interfaces']:
39 # Omit 'ip-less' ifaces
40 if not nodes[node]['ip4_interfaces'][iface]:
41 continue
42 if iface in skipped_ifaces:
43 continue
44 ipv4_list.extend(nodes[node]['ip4_interfaces'][iface])
45 no_dups = (len(ipv4_list) == len(set(ipv4_list)))
46 if not no_dups:
47 ips_count = Counter(ipv4_list).most_common()
48 dup_ips = filter(lambda x: x[1] > 1, ips_count)
49 dup_ifaces = get_duplicate_ifaces(nodes, [v[0] for v in dup_ips])
50
51 msg = ("\nDuplicate IP addresses found:\n{}"
52 "\n\nThe following interfaces are affected:\n{}"
53 "".format(pformat(dup_ips), pformat(dup_ifaces)))
54 assert no_dups, msg