blob: afe1afefeb1ebba9fb7a64245b3eabe3e4d4091e [file] [log] [blame]
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +01001import pytest
2from collections import Counter
3from pprint import pformat
4import os
5
6import 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
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010018def test_duplicate_ips(local_salt_client):
Dmitriy Kruglovac4a14e2019-01-23 09:37:13 +010019 active_nodes = utils.get_active_nodes()
20
21 testname = os.path.basename(__file__).split('.')[0]
22 config = utils.get_configuration()
23 skipped_ifaces = config.get(testname)["skipped_ifaces"]
24
25 local_salt_client.cmd('L@'+','.join(active_nodes),
26 'saltutil.refresh_grains',
27 expr_form='compound')
28 nodes = local_salt_client.cmd('L@'+','.join(active_nodes),
29 'grains.item',
30 ['ip4_interfaces'],
31 expr_form='compound')
32
33 ipv4_list = []
34 for node in nodes:
35 for iface in nodes[node]['ip4_interfaces']:
36 # Omit 'ip-less' ifaces
37 if not nodes[node]['ip4_interfaces'][iface]:
38 continue
39 if iface in skipped_ifaces:
40 continue
41 ipv4_list.extend(nodes[node]['ip4_interfaces'][iface])
42 no_dups = (len(ipv4_list) == len(set(ipv4_list)))
43 if not no_dups:
44 ips_count = Counter(ipv4_list).most_common()
45 dup_ips = filter(lambda x: x[1] > 1, ips_count)
46 dup_ifaces = get_duplicate_ifaces(nodes, [v[0] for v in dup_ips])
47
48 msg = ("\nDuplicate IP addresses found:\n{}"
49 "\n\nThe following interfaces are affected:\n{}"
50 "".format(pformat(dup_ips), pformat(dup_ifaces)))
51 assert no_dups, msg