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 |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 5 | import utils |
| 6 | |
| 7 | |
| 8 | def 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 | |
Hanna Arhipova | ae0e72a | 2019-02-12 13:57:26 +0200 | [diff] [blame] | 16 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame^] | 17 | @pytest.mark.smoke |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 18 | def test_duplicate_ips(local_salt_client): |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 19 | testname = os.path.basename(__file__).split('.')[0] |
| 20 | config = utils.get_configuration() |
| 21 | skipped_ifaces = config.get(testname)["skipped_ifaces"] |
| 22 | |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 23 | local_salt_client.cmd(tgt='*', |
| 24 | fun='saltutil.refresh_grains', |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 25 | expr_form='compound') |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 26 | nodes = local_salt_client.cmd(tgt='*', |
| 27 | fun='grains.item', |
| 28 | param='ip4_interfaces', |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 29 | expr_form='compound') |
| 30 | |
| 31 | ipv4_list = [] |
| 32 | for node in nodes: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 33 | if isinstance(nodes[node], bool): |
| 34 | # TODO: do not skip node |
| 35 | print ("{} node is skipped".format(node)) |
| 36 | continue |
Dmitriy Kruglov | ac4a14e | 2019-01-23 09:37:13 +0100 | [diff] [blame] | 37 | 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 |