|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +sys.path.insert(0,'plugins/action') |
| 5 | + |
| 6 | +import unittest |
| 7 | +from unittest.mock import patch, Mock, MagicMock, call |
| 8 | + |
| 9 | +import requests |
| 10 | +from requests.exceptions import SSLError, RequestException |
| 11 | +from ansible.parsing.yaml.objects import AnsibleSequence, AnsibleMapping |
| 12 | + |
| 13 | + |
| 14 | +#from icinga2_zones_conf import ActionModule |
| 15 | +from icinga2_zones_conf import get_sub_hierarchy |
| 16 | +from icinga2_zones_conf import get_best_endpoint_attrs |
| 17 | + |
| 18 | + |
| 19 | +class TestActionPlugin(unittest.TestCase): |
| 20 | + def test_get_sub_hierarchy(self): |
| 21 | + test_hierarchy = { |
| 22 | + "master": { |
| 23 | + "master-child": None, |
| 24 | + "eu": { |
| 25 | + "germany": { |
| 26 | + "berlin": None, |
| 27 | + "nuremberg": None |
| 28 | + }, |
| 29 | + "france": { |
| 30 | + "paris": None, |
| 31 | + } |
| 32 | + }, |
| 33 | + "us": { |
| 34 | + "washington": None, |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + test_cases = [ |
| 39 | + # name, groups, expected |
| 40 | + ( "master", [], {"master": {"master-child": None, "eu": {"germany": {"berlin": None, "nuremberg": None}, "france": {"paris": None}}, "us": {"washington": None}}} ), |
| 41 | + ( "master-child", [], {"master": {"master-child": None}} ), |
| 42 | + ( "eu", [], {"master": {"eu": {"germany": {"berlin": None, "nuremberg": None}, "france": {"paris": None}}}} ), |
| 43 | + ( "us", [], {"master": {"us": {"washington": None}}} ), |
| 44 | + ( "germany", [], {"eu": {"germany": {"berlin": None, "nuremberg": None}}} ), |
| 45 | + ( "berlin", [], {"germany": {"berlin": None}} ), |
| 46 | + ( "nuremberg", [], {"germany": {"nuremberg": None}} ), |
| 47 | + ( "washington", [], {"us": {"washington": None}} ), |
| 48 | + |
| 49 | + ( "master1", ["some-ansible-group", "master"], {"master": {"master-child": None, "eu": {"germany": {"berlin": None, "nuremberg": None}, "france": {"paris": None}}, "us": {"washington": None}}} ), |
| 50 | + ( "master2", ["master"], {"master": {"master-child": None, "eu": {"germany": {"berlin": None, "nuremberg": None}, "france": {"paris": None}}, "us": {"washington": None}}} ), |
| 51 | + ( "sat-ger-1", ["germany"], {"eu": {"germany": {"berlin": None, "nuremberg": None}}} ), |
| 52 | + ( "sat-ger-2", ["germany"], {"eu": {"germany": {"berlin": None, "nuremberg": None}}} ), |
| 53 | + ( "sat-ber-1", ["berlin"], {"germany": {"berlin": None}} ), |
| 54 | + |
| 55 | + ( "not-found", [], {} ), |
| 56 | + |
| 57 | + ] |
| 58 | + |
| 59 | + for name, groups, expected in test_cases: |
| 60 | + self.assertEqual(get_sub_hierarchy(test_hierarchy, name, groups), expected) |
| 61 | + |
| 62 | + |
| 63 | + def test_get_best_endpoint_attrs(self): |
| 64 | + test_inventory = { |
| 65 | + "hostvars": { |
| 66 | + "host1": { |
| 67 | + "inventory_hostname": "host1", |
| 68 | + "ansible_host": "192.168.122.10", |
| 69 | + "custom_host_var1": "10.0.1.10", |
| 70 | + "custom_host_var2": "10.0.2.10", |
| 71 | + }, |
| 72 | + "host2": { |
| 73 | + "inventory_hostname": "host2", |
| 74 | + "ansible_host": "192.168.122.20", |
| 75 | + "custom_host_var1": "10.0.1.20", |
| 76 | + "custom_port_var": "6556", |
| 77 | + }, |
| 78 | + } |
| 79 | + } |
| 80 | + test_cases = [ |
| 81 | + # name, host_options, port_variable, expected |
| 82 | + ( "host1", [], None, {"host": "192.168.122.10", "port": "5665"} ), |
| 83 | + ( "host1", ["custom_host_var1", "custom_host_var2"], None, {"host": "10.0.1.10", "port": "5665"} ), |
| 84 | + ( "host1", ["custom_host_var2", "custom_host_var1"], None, {"host": "10.0.2.10", "port": "5665"} ), |
| 85 | + ( "host1", ["var_does_not_exist"], None, {"host": "192.168.122.10", "port": "5665"} ), |
| 86 | + ( "host1", [], "custom_port_var", {"host": "192.168.122.10", "port": "5665"} ), |
| 87 | + |
| 88 | + ( "host2", [], None, {"host": "192.168.122.20", "port": "5665"} ), |
| 89 | + ( "host2", ["custom_host_var1", "custom_host_var2"], None, {"host": "10.0.1.20", "port": "5665"} ), |
| 90 | + ( "host2", ["custom_host_var2", "custom_host_var1"], None, {"host": "10.0.1.20", "port": "5665"} ), |
| 91 | + ( "host2", [], "custom_port_var", {"host": "192.168.122.20", "port": "6556"} ), |
| 92 | + |
| 93 | + ] |
| 94 | + |
| 95 | + for name, host_options, port_variable, expected in test_cases: |
| 96 | + self.assertEqual(get_best_endpoint_attrs(test_inventory, name, host_options, port_variable), expected) |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +## WIP |
| 101 | +#def test_get_best_endpoint_attrs(inventory, name, host_options=list(), port_variable=None) |
| 102 | +#def test_get_endpoints_from_zones(zones, name, inventory, upper_host_options=list(), middle_host_options=list(), lower_host_options=list(), port_variable=None) |
| 103 | +#def test_get_zones_from_hierarchy(hierarchy, groups, parent=None) |
0 commit comments