| 1 |
# Copyright (c) 2025 Stéphane Graber <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import pytest |
| 8 |
from ansible.inventory.data import InventoryData |
| 9 |
from ansible.parsing.dataloader import DataLoader |
| 10 |
from ansible.template import Templar |
| 11 |
|
| 12 |
from ansible_collections.community.general.plugins.inventory.incus import ( |
| 13 |
InventoryModule, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture(scope="module") |
| 18 |
def inventory(): |
| 19 |
plugin = InventoryModule() |
| 20 |
plugin.inventory = InventoryData() |
| 21 |
plugin.templar = Templar(loader=DataLoader()) |
| 22 |
return plugin |
| 23 |
|
| 24 |
|
| 25 |
def test_verify_file_yml(tmp_path, inventory): |
| 26 |
file = tmp_path / "foobar.incus.yml" |
| 27 |
file.touch() |
| 28 |
assert inventory.verify_file(str(file)) is True |
| 29 |
|
| 30 |
|
| 31 |
def test_verify_file_yaml(tmp_path, inventory): |
| 32 |
file = tmp_path / "foobar.incus.yaml" |
| 33 |
file.touch() |
| 34 |
assert inventory.verify_file(str(file)) is True |
| 35 |
|
| 36 |
|
| 37 |
def test_verify_file_bad_config_yml(inventory): |
| 38 |
assert inventory.verify_file("foobar.incus.yml") is False |
| 39 |
|
| 40 |
|
| 41 |
def test_verify_file_bad_config_yaml(inventory): |
| 42 |
assert inventory.verify_file("foobar.incus.yaml") is False |
| 43 |
|
| 44 |
|
| 45 |
def test_verify_file_bad_config(inventory): |
| 46 |
assert inventory.verify_file("foobar.wrongcloud.yml") is False |
| 47 |
|
| 48 |
|
| 49 |
def _build_get_option(options): |
| 50 |
def _get_option(option): |
| 51 |
return options.get(option, False) |
| 52 |
|
| 53 |
return _get_option |
| 54 |
|
| 55 |
|
| 56 |
def _make_host(name): |
| 57 |
entry = {} |
| 58 |
entry["name"] = name |
| 59 |
|
| 60 |
for prop in ( |
| 61 |
"architecture", |
| 62 |
"config", |
| 63 |
"description", |
| 64 |
"devices", |
| 65 |
"ephemeral", |
| 66 |
"expanded_config", |
| 67 |
"expanded_devices", |
| 68 |
"location", |
| 69 |
"profiles", |
| 70 |
"status", |
| 71 |
"type", |
| 72 |
): |
| 73 |
entry[prop] = "" |
| 74 |
|
| 75 |
return entry |
| 76 |
|
| 77 |
|
| 78 |
def run_incus(*args): |
| 79 |
if args == ("project", "list", "r1:"): |
| 80 |
return [{"name": "default"}] |
| 81 |
|
| 82 |
if args == ("project", "list", "r2:"): |
| 83 |
return [{"name": "foo"}] |
| 84 |
|
| 85 |
if args == ("list", "r1:", "--project", "default", "status=running"): |
| 86 |
return [_make_host("c1")] |
| 87 |
|
| 88 |
if args == ("list", "r2:", "--project", "foo", "status=running"): |
| 89 |
return [_make_host("c2")] |
| 90 |
|
| 91 |
if args == ("list", "r3:", "--project", "proj1", "status=running"): |
| 92 |
return [_make_host("c3")] |
| 93 |
|
| 94 |
if args == ("list", "r3:", "--project", "proj2", "status=running"): |
| 95 |
return [_make_host("c4"), _make_host("c5")] |
| 96 |
|
| 97 |
return [] |
| 98 |
|
| 99 |
|
| 100 |
def test_build_inventory(inventory, mocker): |
| 101 |
get_option = _build_get_option( |
| 102 |
{ |
| 103 |
"default_groups": True, |
| 104 |
"remotes": ["r1", "r2", "r3:proj1", "r3:proj2"], |
| 105 |
"filters": ["status=running"], |
| 106 |
"host_fqdn": True, |
| 107 |
"host_domain": "example.net", |
| 108 |
}, |
| 109 |
) |
| 110 |
inventory.get_option = mocker.MagicMock(side_effect=get_option) |
| 111 |
inventory._run_incus = mocker.MagicMock(side_effect=run_incus) |
| 112 |
inventory.populate() |
| 113 |
|
| 114 |
c1 = inventory.inventory.get_host("c1.default.r1.example.net") |
| 115 |
assert c1 |
| 116 |
assert "ansible_incus_status" in c1.get_vars() |
| 117 |
|
| 118 |
c2 = inventory.inventory.get_host("c2.foo.r2.example.net") |
| 119 |
assert c2 |
| 120 |
assert "ansible_incus_status" in c2.get_vars() |
| 121 |
|
| 122 |
c3 = inventory.inventory.get_host("c3.proj1.r3.example.net") |
| 123 |
assert c3 |
| 124 |
assert "ansible_incus_status" in c3.get_vars() |
| 125 |
|
| 126 |
c4 = inventory.inventory.get_host("c4.proj2.r3.example.net") |
| 127 |
assert c4 |
| 128 |
assert "ansible_incus_status" in c4.get_vars() |
| 129 |
|
| 130 |
c5 = inventory.inventory.get_host("c5.proj2.r3.example.net") |
| 131 |
assert c5 |
| 132 |
assert "ansible_incus_status" in c5.get_vars() |
| 133 |
|
| 134 |
assert len(inventory.inventory.groups["all"].hosts) == 5 |
| 135 |
assert len(inventory.inventory.groups["incus"].child_groups) == 3 |
| 136 |
assert len(inventory.inventory.groups["incus_r1"].child_groups) == 1 |
| 137 |
assert len(inventory.inventory.groups["incus_r2"].child_groups) == 1 |
| 138 |
assert len(inventory.inventory.groups["incus_r3"].child_groups) == 2 |
| 139 |
assert len(inventory.inventory.groups["incus_r3_proj1"].hosts) == 1 |
| 140 |
assert len(inventory.inventory.groups["incus_r3_proj2"].hosts) == 2 |
| 141 |
|
| 142 |
|
| 143 |
def test_build_inventory_fqdn_from_host_domain(inventory, mocker): |
| 144 |
get_option = _build_get_option( |
| 145 |
{ |
| 146 |
"default_groups": True, |
| 147 |
"remotes": ["r1", "r2", "r3:proj1", "r3:proj2"], |
| 148 |
"filters": ["status=running"], |
| 149 |
"host_fqdn": False, |
| 150 |
"host_domain": "example.net", |
| 151 |
}, |
| 152 |
) |
| 153 |
inventory.get_option = mocker.MagicMock(side_effect=get_option) |
| 154 |
inventory._run_incus = mocker.MagicMock(side_effect=run_incus) |
| 155 |
inventory.populate() |
| 156 |
|
| 157 |
c1 = inventory.inventory.get_host("c1.example.net") |
| 158 |
assert c1 |
| 159 |
assert "ansible_incus_status" in c1.get_vars() |
| 160 |
|
| 161 |
c2 = inventory.inventory.get_host("c2.example.net") |
| 162 |
assert c2 |
| 163 |
assert "ansible_incus_status" in c2.get_vars() |
| 164 |
|
| 165 |
c3 = inventory.inventory.get_host("c3.example.net") |
| 166 |
assert c3 |
| 167 |
assert "ansible_incus_status" in c3.get_vars() |
| 168 |
|
| 169 |
c4 = inventory.inventory.get_host("c4.example.net") |
| 170 |
assert c4 |
| 171 |
assert "ansible_incus_status" in c4.get_vars() |
| 172 |
|
| 173 |
c5 = inventory.inventory.get_host("c5.example.net") |
| 174 |
assert c5 |
| 175 |
assert "ansible_incus_status" in c5.get_vars() |
| 176 |
|
| 177 |
assert len(inventory.inventory.groups["all"].hosts) == 5 |
| 178 |
assert len(inventory.inventory.groups["incus"].child_groups) == 3 |
| 179 |
assert len(inventory.inventory.groups["incus_r1"].child_groups) == 1 |
| 180 |
assert len(inventory.inventory.groups["incus_r2"].child_groups) == 1 |
| 181 |
assert len(inventory.inventory.groups["incus_r3"].child_groups) == 2 |
| 182 |
assert len(inventory.inventory.groups["incus_r3_proj1"].hosts) == 1 |
| 183 |
assert len(inventory.inventory.groups["incus_r3_proj2"].hosts) == 2 |