2020-08-18 13:10:09 +00:00
|
|
|
"""Test inventory plugin proxmox."""
|
|
|
|
# Copyright (c) 2020, Robert Kaussow <mail@thegeeklab.de>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
|
2023-01-31 19:09:29 +00:00
|
|
|
__metaclass__ = type
|
|
|
|
|
2020-08-18 13:10:09 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
proxmox = pytest.importorskip("proxmoxer")
|
|
|
|
|
|
|
|
from plugins.inventory.proxmox import InventoryModule
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def inventory():
|
|
|
|
return InventoryModule()
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_names(inventory):
|
|
|
|
nodes = [{"status": "online", "type": "node", "id": "node/testnode", "node": "testnode"}]
|
|
|
|
pools = [{"poolid": "testpool"}]
|
|
|
|
|
|
|
|
assert ["testnode"] == inventory._get_names(nodes, "node")
|
|
|
|
assert ["testpool"] == inventory._get_names(pools, "pool")
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_variables(inventory):
|
|
|
|
pve_list = [{
|
|
|
|
"status": "running",
|
|
|
|
"vmid": "100",
|
|
|
|
"name": "test",
|
|
|
|
}]
|
|
|
|
|
|
|
|
variables = {
|
|
|
|
"test": {
|
|
|
|
"proxmox_status": "running",
|
|
|
|
"proxmox_vmid": "100",
|
|
|
|
"proxmox_name": "test",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert variables == inventory._get_variables(pve_list, "qemu")
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_ip_address(inventory, mocker):
|
|
|
|
networks = {
|
|
|
|
"result": [{
|
|
|
|
"ip-addresses": [{
|
|
|
|
"ip-address": "10.0.0.1",
|
|
|
|
"prefix": 26,
|
|
|
|
"ip-address-type": "ipv4"
|
|
|
|
}],
|
|
|
|
"name": "eth0"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
inventory.client = mocker.MagicMock()
|
|
|
|
inventory.client.nodes.return_value.get.return_value = networks
|
|
|
|
|
2023-01-31 19:09:29 +00:00
|
|
|
assert inventory._get_ip_address("qemu", None, None) == "10.0.0.1"
|
2020-08-18 13:10:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_exclude(inventory, mocker):
|
|
|
|
|
|
|
|
def get_option(name, *args, **kwargs):
|
|
|
|
if name == "exclude_state":
|
|
|
|
return ["stopped"]
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
inventory.get_option = mocker.MagicMock(side_effect=get_option)
|
|
|
|
|
|
|
|
pve_list = [{
|
|
|
|
"status": "running",
|
|
|
|
"vmid": "100",
|
|
|
|
"name": "test",
|
|
|
|
}, {
|
|
|
|
"status": "stopped",
|
|
|
|
"vmid": "101",
|
|
|
|
"name": "stop",
|
|
|
|
}]
|
|
|
|
|
|
|
|
filtered = [{
|
|
|
|
"status": "running",
|
|
|
|
"vmid": "100",
|
|
|
|
"name": "test",
|
|
|
|
}]
|
|
|
|
|
|
|
|
assert filtered == inventory._exclude(pve_list)
|