| 1 |
# Copyright (c) 2021, Cliff Hults <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
# |
| 5 |
# The API responses used in these tests were recorded from PVE version 6.2. |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import pytest |
| 10 |
from ansible.inventory.data import InventoryData |
| 11 |
|
| 12 |
from ansible_collections.community.general.plugins.inventory.icinga2 import InventoryModule |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture(scope="module") |
| 16 |
def inventory(): |
| 17 |
r = InventoryModule() |
| 18 |
r.inventory = InventoryData() |
| 19 |
return r |
| 20 |
|
| 21 |
|
| 22 |
def test_verify_file_bad_config(inventory): |
| 23 |
assert inventory.verify_file("foobar.icinga2.yml") is False |
| 24 |
|
| 25 |
|
| 26 |
def check_api(): |
| 27 |
return True |
| 28 |
|
| 29 |
|
| 30 |
# NOTE: when updating/adding replies to this function, |
| 31 |
# be sure to only add only the _contents_ of the 'data' dict in the API reply |
| 32 |
def query_hosts(hosts=None, attrs=None, joins=None, host_filter=None): |
| 33 |
# _get_hosts - list of dicts |
| 34 |
json_host_data = [ |
| 35 |
{ |
| 36 |
"attrs": { |
| 37 |
"address": "test-host1.home.local", |
| 38 |
"groups": ["home_servers", "servers_dell"], |
| 39 |
"display_name": "Test Host 1", |
| 40 |
"state": 0.0, |
| 41 |
"state_type": 1.0, |
| 42 |
}, |
| 43 |
"joins": {}, |
| 44 |
"meta": {}, |
| 45 |
"name": "test-host1", |
| 46 |
"type": "Host", |
| 47 |
}, |
| 48 |
{ |
| 49 |
"attrs": { |
| 50 |
"address": "test-host2.home.local", |
| 51 |
"display_name": "Test Host 2", |
| 52 |
"groups": ["home_servers", "servers_hp"], |
| 53 |
"state": 1.0, |
| 54 |
"state_type": 1.0, |
| 55 |
}, |
| 56 |
"joins": {}, |
| 57 |
"meta": {}, |
| 58 |
"name": "test-host2", |
| 59 |
"type": "Host", |
| 60 |
}, |
| 61 |
{ |
| 62 |
"attrs": { |
| 63 |
"address": "", |
| 64 |
"display_name": "Test Host 3", |
| 65 |
"groups": ["not_home_servers", "servers_hp"], |
| 66 |
"state": 1.0, |
| 67 |
"state_type": 1.0, |
| 68 |
}, |
| 69 |
"joins": {}, |
| 70 |
"meta": {}, |
| 71 |
"name": "test-host3.example.com", |
| 72 |
"type": "Host", |
| 73 |
}, |
| 74 |
] |
| 75 |
return json_host_data |
| 76 |
|
| 77 |
|
| 78 |
def get_option(option): |
| 79 |
if option == "groups": |
| 80 |
return {} |
| 81 |
elif option == "keyed_groups": |
| 82 |
return [] |
| 83 |
elif option == "compose": |
| 84 |
return {} |
| 85 |
elif option == "strict": |
| 86 |
return False |
| 87 |
elif option == "group_by_hostgroups": |
| 88 |
return True |
| 89 |
else: |
| 90 |
return None |
| 91 |
|
| 92 |
|
| 93 |
def test_populate(inventory, mocker): |
| 94 |
# module settings |
| 95 |
inventory.icinga2_user = "ansible" |
| 96 |
inventory.icinga2_password = "password" |
| 97 |
inventory.icinga2_url = "https://localhost:5665/v1" |
| 98 |
inventory.inventory_attr = "address" |
| 99 |
inventory.group_by_hostgroups = True |
| 100 |
|
| 101 |
# bypass authentication and API fetch calls |
| 102 |
inventory._check_api = mocker.MagicMock(side_effect=check_api) |
| 103 |
inventory._query_hosts = mocker.MagicMock(side_effect=query_hosts) |
| 104 |
inventory.get_option = mocker.MagicMock(side_effect=get_option) |
| 105 |
inventory._populate() |
| 106 |
|
| 107 |
# get different hosts |
| 108 |
host1_info = inventory.inventory.get_host("test-host1.home.local") |
| 109 |
print(host1_info) |
| 110 |
host2_info = inventory.inventory.get_host("test-host2.home.local") |
| 111 |
print(host2_info) |
| 112 |
host3_info = inventory.inventory.get_host("test-host3.example.com") |
| 113 |
assert inventory.inventory.get_host("test-host3.example.com") is not None |
| 114 |
print(host3_info) |
| 115 |
|
| 116 |
# check if host in the home_servers group |
| 117 |
assert "home_servers" in inventory.inventory.groups |
| 118 |
group1_data = inventory.inventory.groups["home_servers"] |
| 119 |
group1_test_data = [host1_info, host2_info] |
| 120 |
print(group1_data.hosts) |
| 121 |
print(group1_test_data) |
| 122 |
assert group1_data.hosts == group1_test_data |
| 123 |
# Test servers_hp group |
| 124 |
group2_data = inventory.inventory.groups["servers_hp"] |
| 125 |
group2_test_data = [host2_info, host3_info] |
| 126 |
print(group2_data.hosts) |
| 127 |
print(group2_test_data) |
| 128 |
assert group2_data.hosts == group2_test_data |
| 129 |
|
| 130 |
# check if host state rules apply properly |
| 131 |
assert host1_info.get_vars()["state"] == "on" |
| 132 |
assert host1_info.get_vars()["display_name"] == "Test Host 1" |
| 133 |
assert host2_info.get_vars()["state"] == "off" |
| 134 |
assert host3_info.get_vars().get("ansible_host") is None |
| 135 |
|
| 136 |
# Confirm attribute options switcher |
| 137 |
inventory.inventory_attr = "name" |
| 138 |
inventory._populate() |
| 139 |
assert inventory.inventory.get_host("test-host3.example.com") is not None |
| 140 |
host2_info = inventory.inventory.get_host("test-host2") |
| 141 |
assert host2_info is not None |
| 142 |
assert host2_info.get_vars().get("ansible_host") == "test-host2.home.local" |
| 143 |
|
| 144 |
# Confirm attribute options switcher |
| 145 |
inventory.inventory_attr = "display_name" |
| 146 |
inventory._populate() |
| 147 |
assert inventory.inventory.get_host("Test Host 3") is not None |
| 148 |
host2_info = inventory.inventory.get_host("Test Host 2") |
| 149 |
assert host2_info is not None |
| 150 |
assert host2_info.get_vars().get("ansible_host") == "test-host2.home.local" |