blob: ca814c40ecfbb2241a88bc898b4a50f9c601f89e [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
Hanna Arhipova56eab942019-05-06 20:14:18 +03005import logging
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +01006import 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
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050018@pytest.mark.smoke
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010019def test_duplicate_ips(local_salt_client):
Dmitriy Kruglovac4a14e2019-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
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050024 local_salt_client.cmd(tgt='*',
25 fun='saltutil.refresh_grains',
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010026 expr_form='compound')
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050027 nodes = local_salt_client.cmd(tgt='*',
28 fun='grains.item',
29 param='ip4_interfaces',
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010030 expr_form='compound')
31
32 ipv4_list = []
33 for node in nodes:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050034 if isinstance(nodes[node], bool):
35 # TODO: do not skip node
Hanna Arhipova56eab942019-05-06 20:14:18 +030036 logging.info("{} node is skipped".format(node))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050037 continue
Dmitriy Kruglovac4a14e2019-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()
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030048 dup_ips = [x for x in ips_count if x[1] > 1]
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010049 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{}"
Dmitriy Kruglova34a3042019-08-20 11:45:35 +020053 "".format(pformat(dup_ips), pformat(dup_ifaces)))
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010054 assert no_dups, msg