Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 1 | from collections import Counter |
| 2 | from pprint import pformat |
| 3 | import os |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 4 | import pytest |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 5 | import logging |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 6 | import utils |
| 7 | |
| 8 | |
| 9 | def 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 Arhipova | ae0e72a | 2019-02-12 13:57:26 +0200 | [diff] [blame] | 17 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 18 | @pytest.mark.smoke |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 19 | def test_duplicate_ips(local_salt_client): |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 20 | testname = os.path.basename(__file__).split('.')[0] |
| 21 | config = utils.get_configuration() |
| 22 | skipped_ifaces = config.get(testname)["skipped_ifaces"] |
| 23 | |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 24 | local_salt_client.cmd(tgt='*', |
| 25 | fun='saltutil.refresh_grains', |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 26 | expr_form='compound') |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 27 | nodes = local_salt_client.cmd(tgt='*', |
| 28 | fun='grains.item', |
| 29 | param='ip4_interfaces', |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 30 | expr_form='compound') |
| 31 | |
| 32 | ipv4_list = [] |
| 33 | for node in nodes: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 34 | if isinstance(nodes[node], bool): |
| 35 | # TODO: do not skip node |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 36 | logging.info("{} node is skipped".format(node)) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 37 | continue |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 38 | 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 Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 48 | dup_ips = [x for x in ips_count if x[1] > 1] |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 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{}" |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 53 | "".format(pformat(dup_ips), pformat(dup_ifaces))) |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 54 | assert no_dups, msg |