1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-30 15:38:53 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -21,4 +21,4 @@ def test_verify_file(tmp_path, inventory):
def test_verify_file_bad_config(inventory):
assert inventory.verify_file('foobar.cobbler.yml') is False
assert inventory.verify_file("foobar.cobbler.yml") is False

View file

@ -20,7 +20,7 @@ def inventory():
def test_verify_file_bad_config(inventory):
assert inventory.verify_file('foobar.icinga2.yml') is False
assert inventory.verify_file("foobar.icinga2.yml") is False
def check_api():
@ -33,58 +33,58 @@ def query_hosts(hosts=None, attrs=None, joins=None, host_filter=None):
# _get_hosts - list of dicts
json_host_data = [
{
'attrs': {
'address': 'test-host1.home.local',
'groups': ['home_servers', 'servers_dell'],
'display_name': 'Test Host 1',
'state': 0.0,
'state_type': 1.0
"attrs": {
"address": "test-host1.home.local",
"groups": ["home_servers", "servers_dell"],
"display_name": "Test Host 1",
"state": 0.0,
"state_type": 1.0,
},
'joins': {},
'meta': {},
'name': 'test-host1',
'type': 'Host'
"joins": {},
"meta": {},
"name": "test-host1",
"type": "Host",
},
{
'attrs': {
'address': 'test-host2.home.local',
'display_name': 'Test Host 2',
'groups': ['home_servers', 'servers_hp'],
'state': 1.0,
'state_type': 1.0
"attrs": {
"address": "test-host2.home.local",
"display_name": "Test Host 2",
"groups": ["home_servers", "servers_hp"],
"state": 1.0,
"state_type": 1.0,
},
'joins': {},
'meta': {},
'name': 'test-host2',
'type': 'Host'
"joins": {},
"meta": {},
"name": "test-host2",
"type": "Host",
},
{
'attrs': {
'address': '',
'display_name': 'Test Host 3',
'groups': ['not_home_servers', 'servers_hp'],
'state': 1.0,
'state_type': 1.0
"attrs": {
"address": "",
"display_name": "Test Host 3",
"groups": ["not_home_servers", "servers_hp"],
"state": 1.0,
"state_type": 1.0,
},
'joins': {},
'meta': {},
'name': 'test-host3.example.com',
'type': 'Host'
}
"joins": {},
"meta": {},
"name": "test-host3.example.com",
"type": "Host",
},
]
return json_host_data
def get_option(option):
if option == 'groups':
if option == "groups":
return {}
elif option == 'keyed_groups':
elif option == "keyed_groups":
return []
elif option == 'compose':
elif option == "compose":
return {}
elif option == 'strict':
elif option == "strict":
return False
elif option == 'group_by_hostgroups':
elif option == "group_by_hostgroups":
return True
else:
return None
@ -92,8 +92,8 @@ def get_option(option):
def test_populate(inventory, mocker):
# module settings
inventory.icinga2_user = 'ansible'
inventory.icinga2_password = 'password'
inventory.icinga2_user = "ansible"
inventory.icinga2_password = "password"
inventory.icinga2_url = "https://localhost:5665/v1"
inventory.inventory_attr = "address"
inventory.group_by_hostgroups = True
@ -105,46 +105,46 @@ def test_populate(inventory, mocker):
inventory._populate()
# get different hosts
host1_info = inventory.inventory.get_host('test-host1.home.local')
host1_info = inventory.inventory.get_host("test-host1.home.local")
print(host1_info)
host2_info = inventory.inventory.get_host('test-host2.home.local')
host2_info = inventory.inventory.get_host("test-host2.home.local")
print(host2_info)
host3_info = inventory.inventory.get_host('test-host3.example.com')
assert inventory.inventory.get_host('test-host3.example.com') is not None
host3_info = inventory.inventory.get_host("test-host3.example.com")
assert inventory.inventory.get_host("test-host3.example.com") is not None
print(host3_info)
# check if host in the home_servers group
assert 'home_servers' in inventory.inventory.groups
group1_data = inventory.inventory.groups['home_servers']
assert "home_servers" in inventory.inventory.groups
group1_data = inventory.inventory.groups["home_servers"]
group1_test_data = [host1_info, host2_info]
print(group1_data.hosts)
print(group1_test_data)
assert group1_data.hosts == group1_test_data
# Test servers_hp group
group2_data = inventory.inventory.groups['servers_hp']
group2_data = inventory.inventory.groups["servers_hp"]
group2_test_data = [host2_info, host3_info]
print(group2_data.hosts)
print(group2_test_data)
assert group2_data.hosts == group2_test_data
# check if host state rules apply properly
assert host1_info.get_vars()['state'] == 'on'
assert host1_info.get_vars()['display_name'] == "Test Host 1"
assert host2_info.get_vars()['state'] == 'off'
assert host3_info.get_vars().get('ansible_host') is None
assert host1_info.get_vars()["state"] == "on"
assert host1_info.get_vars()["display_name"] == "Test Host 1"
assert host2_info.get_vars()["state"] == "off"
assert host3_info.get_vars().get("ansible_host") is None
# Confirm attribute options switcher
inventory.inventory_attr = "name"
inventory._populate()
assert inventory.inventory.get_host('test-host3.example.com') is not None
host2_info = inventory.inventory.get_host('test-host2')
assert inventory.inventory.get_host("test-host3.example.com") is not None
host2_info = inventory.inventory.get_host("test-host2")
assert host2_info is not None
assert host2_info.get_vars().get('ansible_host') == 'test-host2.home.local'
assert host2_info.get_vars().get("ansible_host") == "test-host2.home.local"
# Confirm attribute options switcher
inventory.inventory_attr = "display_name"
inventory._populate()
assert inventory.inventory.get_host('Test Host 3') is not None
host2_info = inventory.inventory.get_host('Test Host 2')
assert inventory.inventory.get_host("Test Host 3") is not None
host2_info = inventory.inventory.get_host("Test Host 2")
assert host2_info is not None
assert host2_info.get_vars().get('ansible_host') == 'test-host2.home.local'
assert host2_info.get_vars().get("ansible_host") == "test-host2.home.local"

View file

@ -1,4 +1,3 @@
# Copyright (c) 2024 Vladimir Botka <vbotka@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
@ -19,51 +18,51 @@ def inventory():
inv = InventoryModule()
inv.inventory = InventoryData()
inv.templar = Templar(None)
inv.jails = load_txt_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_jails.txt')
inv.js_ok = load_yml_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_jails.yml')
inv.jails_dhcp = load_txt_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp.txt')
inv.js_dhcp_ok = load_yml_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp.yml')
inv.jails_dhcp_nr = load_txt_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp_not_running.txt')
inv.js_dhcp_nr_ok = load_yml_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp_not_running.yml')
prpts_101 = load_txt_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_properties_test_101.txt')
prpts_102 = load_txt_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_properties_test_102.txt')
prpts_103 = load_txt_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_properties_test_103.txt')
inv.prpts = {'test_101': prpts_101, 'test_102': prpts_102, 'test_103': prpts_103}
inv.ps_ok = load_yml_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_properties.yml')
inv.ok = load_yml_data('tests/unit/plugins/inventory/fixtures/iocage/iocage_inventory.yml')
inv.jails = load_txt_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_jails.txt")
inv.js_ok = load_yml_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_jails.yml")
inv.jails_dhcp = load_txt_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp.txt")
inv.js_dhcp_ok = load_yml_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp.yml")
inv.jails_dhcp_nr = load_txt_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp_not_running.txt")
inv.js_dhcp_nr_ok = load_yml_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp_not_running.yml")
prpts_101 = load_txt_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_properties_test_101.txt")
prpts_102 = load_txt_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_properties_test_102.txt")
prpts_103 = load_txt_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_properties_test_103.txt")
inv.prpts = {"test_101": prpts_101, "test_102": prpts_102, "test_103": prpts_103}
inv.ps_ok = load_yml_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_properties.yml")
inv.ok = load_yml_data("tests/unit/plugins/inventory/fixtures/iocage/iocage_inventory.yml")
return inv
def load_txt_data(path):
with open(path, 'r') as f:
with open(path, "r") as f:
s = f.read()
return s
def load_yml_data(path):
with open(path, 'r') as f:
with open(path, "r") as f:
d = yaml.safe_load(f)
return d
def get_option(option):
groups = {}
groups['test'] = make_trusted("inventory_hostname.startswith('test')")
groups["test"] = make_trusted("inventory_hostname.startswith('test')")
if option == 'groups':
if option == "groups":
return groups
elif option == 'keyed_groups':
elif option == "keyed_groups":
return []
elif option == 'compose':
elif option == "compose":
return {}
elif option == 'strict':
elif option == "strict":
return False
else:
return None
def test_verify_file_bad_config(inventory):
assert inventory.verify_file('foobar.iocage.yml') is False
assert inventory.verify_file("foobar.iocage.yml") is False
def test_verify_file(tmp_path, inventory):
@ -73,53 +72,61 @@ def test_verify_file(tmp_path, inventory):
def test_get_jails(inventory):
# jails
results = {'_meta': {'hostvars': {}}}
results = {"_meta": {"hostvars": {}}}
inventory.get_jails(inventory.jails, results)
assert results == inventory.js_ok
# jails_dhcp
results = {'_meta': {'hostvars': {}}}
results = {"_meta": {"hostvars": {}}}
inventory.get_jails(inventory.jails_dhcp, results)
assert results == inventory.js_dhcp_ok
# jails_dhcp_not_running
results = {'_meta': {'hostvars': {}}}
results = {"_meta": {"hostvars": {}}}
inventory.get_jails(inventory.jails_dhcp_nr, results)
assert results == inventory.js_dhcp_nr_ok
def test_get_properties(inventory):
results = {'_meta': {'hostvars': {}}}
results = {"_meta": {"hostvars": {}}}
inventory.get_jails(inventory.jails, results)
for hostname, host_vars in results['_meta']['hostvars'].items():
for hostname, host_vars in results["_meta"]["hostvars"].items():
inventory.get_properties(inventory.prpts[hostname], results, hostname)
assert results == inventory.ps_ok
def test_populate(inventory, mocker):
results = {'_meta': {'hostvars': {}}}
results = {"_meta": {"hostvars": {}}}
inventory.get_jails(inventory.jails, results)
for hostname, host_vars in results['_meta']['hostvars'].items():
for hostname, host_vars in results["_meta"]["hostvars"].items():
inventory.get_properties(inventory.prpts[hostname], results, hostname)
inventory.get_option = mocker.MagicMock(side_effect=get_option)
inventory.populate(results)
# test
hosts = ('test_101', 'test_102', 'test_103')
vars = ('iocage_basejail', 'iocage_boot', 'iocage_ip4', 'iocage_ip6', 'iocage_properties',
'iocage_release', 'iocage_state', 'iocage_template', 'iocage_type')
hosts = ("test_101", "test_102", "test_103")
vars = (
"iocage_basejail",
"iocage_boot",
"iocage_ip4",
"iocage_ip6",
"iocage_properties",
"iocage_release",
"iocage_state",
"iocage_template",
"iocage_type",
)
# test host_vars
for host in hosts:
h = inventory.inventory.get_host(host)
for var in vars:
assert inventory.ok['all']['children']['test']['hosts'][host][var] == h.get_vars()[var]
assert inventory.ok["all"]["children"]["test"]["hosts"][host][var] == h.get_vars()[var]
# test groups
test_101_info = inventory.inventory.get_host('test_101')
test_102_info = inventory.inventory.get_host('test_102')
test_103_info = inventory.inventory.get_host('test_103')
g = inventory.inventory.groups['test']
test_101_info = inventory.inventory.get_host("test_101")
test_102_info = inventory.inventory.get_host("test_102")
test_103_info = inventory.inventory.get_host("test_103")
g = inventory.inventory.groups["test"]
assert g.hosts == [test_101_info, test_102_info, test_103_info]

View file

@ -6,7 +6,7 @@ from __future__ import annotations
import pytest
linode_apiv4 = pytest.importorskip('linode_api4')
linode_apiv4 = pytest.importorskip("linode_api4")
from ansible.errors import AnsibleError
from ansible.parsing.dataloader import DataLoader
@ -23,10 +23,10 @@ def inventory():
def test_missing_access_token_lookup(inventory):
loader = DataLoader()
inventory._options = {'access_token': None}
inventory._options = {"access_token": None}
with pytest.raises(AnsibleError) as error_message:
inventory._build_client(loader)
assert 'Could not retrieve Linode access token' in error_message
assert "Could not retrieve Linode access token" in error_message
def test_verify_file_yml(tmp_path, inventory):

View file

@ -12,24 +12,43 @@ from ansible_collections.community.general.plugins.inventory.lxd import Inventor
HOST_COMPARATIVE_DATA = {
'ansible_connection': 'ssh', 'ansible_host': '10.98.143.199', 'ansible_lxd_os': 'ubuntu', 'ansible_lxd_release': 'focal',
'ansible_lxd_profile': ['default'], 'ansible_lxd_state': 'running', 'ansible_lxd_location': 'Berlin',
'ansible_lxd_vlan_ids': {'my-macvlan': 666}, 'inventory_hostname': 'vlantest', 'inventory_hostname_short': 'vlantest'}
"ansible_connection": "ssh",
"ansible_host": "10.98.143.199",
"ansible_lxd_os": "ubuntu",
"ansible_lxd_release": "focal",
"ansible_lxd_profile": ["default"],
"ansible_lxd_state": "running",
"ansible_lxd_location": "Berlin",
"ansible_lxd_vlan_ids": {"my-macvlan": 666},
"inventory_hostname": "vlantest",
"inventory_hostname_short": "vlantest",
}
GROUP_COMPARATIVE_DATA = {
'all': [], 'ungrouped': [], 'testpattern': ['vlantest'], 'vlan666': ['vlantest'], 'locationBerlin': ['vlantest'],
'osUbuntu': ['vlantest'], 'releaseFocal': ['vlantest'], 'releaseBionic': [], 'profileDefault': ['vlantest'],
'profileX11': [], 'netRangeIPv4': ['vlantest'], 'netRangeIPv6': ['vlantest']}
"all": [],
"ungrouped": [],
"testpattern": ["vlantest"],
"vlan666": ["vlantest"],
"locationBerlin": ["vlantest"],
"osUbuntu": ["vlantest"],
"releaseFocal": ["vlantest"],
"releaseBionic": [],
"profileDefault": ["vlantest"],
"profileX11": [],
"netRangeIPv4": ["vlantest"],
"netRangeIPv6": ["vlantest"],
}
GROUP_Config = {
'testpattern': {'type': 'pattern', 'attribute': 'test'},
'vlan666': {'type': 'vlanid', 'attribute': 666},
'locationBerlin': {'type': 'location', 'attribute': 'Berlin'},
'osUbuntu': {'type': 'os', 'attribute': 'ubuntu'},
'releaseFocal': {'type': 'release', 'attribute': 'focal'},
'releaseBionic': {'type': 'release', 'attribute': 'bionic'},
'profileDefault': {'type': 'profile', 'attribute': 'default'},
'profileX11': {'type': 'profile', 'attribute': 'x11'},
'netRangeIPv4': {'type': 'network_range', 'attribute': '10.98.143.0/24'},
'netRangeIPv6': {'type': 'network_range', 'attribute': 'fd42:bd00:7b11:2167:216:3eff::/96'}}
"testpattern": {"type": "pattern", "attribute": "test"},
"vlan666": {"type": "vlanid", "attribute": 666},
"locationBerlin": {"type": "location", "attribute": "Berlin"},
"osUbuntu": {"type": "os", "attribute": "ubuntu"},
"releaseFocal": {"type": "release", "attribute": "focal"},
"releaseBionic": {"type": "release", "attribute": "bionic"},
"profileDefault": {"type": "profile", "attribute": "default"},
"profileX11": {"type": "profile", "attribute": "x11"},
"netRangeIPv4": {"type": "network_range", "attribute": "10.98.143.0/24"},
"netRangeIPv6": {"type": "network_range", "attribute": "fd42:bd00:7b11:2167:216:3eff::/96"},
}
@pytest.fixture
@ -38,13 +57,13 @@ def inventory():
inv.inventory = InventoryData()
# Test Values
inv.data = inv.load_json_data('tests/unit/plugins/inventory/fixtures/lxd_inventory.atd') # Load Test Data
inv.data = inv.load_json_data("tests/unit/plugins/inventory/fixtures/lxd_inventory.atd") # Load Test Data
inv.groupby = GROUP_Config
inv.prefered_instance_network_interface = 'eth'
inv.prefered_instance_network_family = 'inet'
inv.filter = 'running'
inv.prefered_instance_network_interface = "eth"
inv.prefered_instance_network_family = "inet"
inv.filter = "running"
inv.dump_data = False
inv.type_filter = 'both'
inv.type_filter = "both"
return inv
@ -56,7 +75,7 @@ def test_verify_file(tmp_path, inventory):
def test_verify_file_bad_config(inventory):
assert inventory.verify_file('foobar.lxd.yml') is False
assert inventory.verify_file("foobar.lxd.yml") is False
def test_build_inventory_hosts(inventory):
@ -64,7 +83,7 @@ def test_build_inventory_hosts(inventory):
After the inventory plugin has run with the test data, the result of the host is checked."""
inventory._populate()
generated_data = inventory.inventory.get_host('vlantest').get_vars()
generated_data = inventory.inventory.get_host("vlantest").get_vars()
eq = True
for key, value in HOST_COMPARATIVE_DATA.items():
@ -94,7 +113,7 @@ def test_build_inventory_groups_with_no_groupselection(inventory):
inventory.groupby = None
inventory._populate()
generated_data = inventory.inventory.get_groups_dict()
group_comparative_data = {'all': [], 'ungrouped': []}
group_comparative_data = {"all": [], "ungrouped": []}
eq = True
print(f"data: {generated_data}")

View file

@ -47,14 +47,14 @@ def access_mock(path, can_access=True):
class HistoryEntry:
def __init__(self):
self.SEQ = '384'
self.HOSTNAME = 'sam-691-sam'
self.HID = '10'
self.CID = '0'
self.DS_ID = '100'
self.VM_MAD = 'kvm'
self.TM_MAD = '3par'
self.ACTION = '0'
self.SEQ = "384"
self.HOSTNAME = "sam-691-sam"
self.HID = "10"
self.CID = "0"
self.DS_ID = "100"
self.VM_MAD = "kvm"
self.TM_MAD = "3par"
self.ACTION = "0"
class HistoryRecords:
@ -76,191 +76,223 @@ def test_verify_file(tmp_path, inventory):
def test_verify_file_bad_config(inventory):
assert inventory.verify_file('foobar.opennebula.yml') is False
assert inventory.verify_file("foobar.opennebula.yml") is False
def get_vm_pool_json():
with open('tests/unit/plugins/inventory/fixtures/opennebula_inventory.json', 'r') as json_file:
with open("tests/unit/plugins/inventory/fixtures/opennebula_inventory.json", "r") as json_file:
jsondata = json.load(json_file)
data = type('pyone.bindings.VM_POOLSub', (object,), {'VM': []})()
data = type("pyone.bindings.VM_POOLSub", (object,), {"VM": []})()
for fake_server in jsondata:
data.VM.append(type('pyone.bindings.VMType90Sub', (object,), fake_server)())
data.VM.append(type("pyone.bindings.VMType90Sub", (object,), fake_server)())
return data
def get_vm_pool():
data = type('pyone.bindings.VM_POOLSub', (object,), {'VM': []})()
data = type("pyone.bindings.VM_POOLSub", (object,), {"VM": []})()
vm = type('pyone.bindings.VMType90Sub', (object,), {
'DEPLOY_ID': 'one-7157',
'ETIME': 0,
'GID': 132,
'GNAME': 'CSApparelVDC',
'HISTORY_RECORDS': HistoryRecords(),
'ID': 7157,
'LAST_POLL': 1632762935,
'LCM_STATE': 3,
'MONITORING': {},
'NAME': 'sam-691-sam',
'RESCHED': 0,
'SNAPSHOTS': [],
'STATE': 3,
'STIME': 1632755245,
'TEMPLATE': OrderedDict({
'NIC': OrderedDict({
'AR_ID': '0',
'BRIDGE': 'onebr80',
'BRIDGE_TYPE': 'linux',
'CLUSTER_ID': '0',
'IP': '172.22.4.187',
'MAC': '02:00:ac:16:04:bb',
'MTU': '8192',
'NAME': 'NIC0',
'NETWORK': 'Private Net CSApparel',
'NETWORK_ID': '80',
'NETWORK_UNAME': 'CSApparelVDC-admin',
'NIC_ID': '0',
'PHYDEV': 'team0',
'SECURITY_GROUPS': '0',
'TARGET': 'one-7157-0',
'VLAN_ID': '480',
'VN_MAD': '802.1Q'
})
}),
'USER_TEMPLATE': OrderedDict({
'HYPERVISOR': 'kvm',
'INPUTS_ORDER': '',
'LOGO': 'images/logos/centos.png',
'MEMORY_UNIT_COST': 'MB',
'SCHED_REQUIREMENTS': 'CLUSTER_ID="0"'
})
})()
vm = type(
"pyone.bindings.VMType90Sub",
(object,),
{
"DEPLOY_ID": "one-7157",
"ETIME": 0,
"GID": 132,
"GNAME": "CSApparelVDC",
"HISTORY_RECORDS": HistoryRecords(),
"ID": 7157,
"LAST_POLL": 1632762935,
"LCM_STATE": 3,
"MONITORING": {},
"NAME": "sam-691-sam",
"RESCHED": 0,
"SNAPSHOTS": [],
"STATE": 3,
"STIME": 1632755245,
"TEMPLATE": OrderedDict(
{
"NIC": OrderedDict(
{
"AR_ID": "0",
"BRIDGE": "onebr80",
"BRIDGE_TYPE": "linux",
"CLUSTER_ID": "0",
"IP": "172.22.4.187",
"MAC": "02:00:ac:16:04:bb",
"MTU": "8192",
"NAME": "NIC0",
"NETWORK": "Private Net CSApparel",
"NETWORK_ID": "80",
"NETWORK_UNAME": "CSApparelVDC-admin",
"NIC_ID": "0",
"PHYDEV": "team0",
"SECURITY_GROUPS": "0",
"TARGET": "one-7157-0",
"VLAN_ID": "480",
"VN_MAD": "802.1Q",
}
)
}
),
"USER_TEMPLATE": OrderedDict(
{
"HYPERVISOR": "kvm",
"INPUTS_ORDER": "",
"LOGO": "images/logos/centos.png",
"MEMORY_UNIT_COST": "MB",
"SCHED_REQUIREMENTS": 'CLUSTER_ID="0"',
}
),
},
)()
data.VM.append(vm)
vm = type('pyone.bindings.VMType90Sub', (object,), {
'DEPLOY_ID': 'one-327',
'ETIME': 0,
'GID': 0,
'GNAME': 'oneadmin',
'HISTORY_RECORDS': [],
'ID': 327,
'LAST_POLL': 1632763543,
'LCM_STATE': 3,
'MONITORING': {},
'NAME': 'zabbix-327',
'RESCHED': 0,
'SNAPSHOTS': [],
'STATE': 3,
'STIME': 1575410106,
'TEMPLATE': OrderedDict({
'NIC': [
OrderedDict({
'AR_ID': '0',
'BRIDGE': 'onerb.103',
'BRIDGE_TYPE': 'linux',
'IP': '185.165.1.1',
'IP6_GLOBAL': '2000:a001::b9ff:feae:aa0d',
'IP6_LINK': 'fe80::b9ff:feae:aa0d',
'MAC': '02:00:b9:ae:aa:0d',
'NAME': 'NIC0',
'NETWORK': 'Public',
'NETWORK_ID': '7',
'NIC_ID': '0',
'PHYDEV': 'team0',
'SECURITY_GROUPS': '0',
'TARGET': 'one-327-0',
'VLAN_ID': '100',
'VN_MAD': '802.1Q'
}),
OrderedDict({
'AR_ID': '0',
'BRIDGE': 'br0',
'BRIDGE_TYPE': 'linux',
'CLUSTER_ID': '0',
'IP': '192.168.1.1',
'MAC': '02:00:c0:a8:3b:01',
'NAME': 'NIC1',
'NETWORK': 'Management',
'NETWORK_ID': '11',
'NIC_ID': '1',
'SECURITY_GROUPS': '0',
'TARGET': 'one-327-1',
'VN_MAD': 'bridge'
})
]
}),
'USER_TEMPLATE': OrderedDict({
'HYPERVISOR': 'kvm',
'INPUTS_ORDER': '',
'LABELS': 'Oracle Linux',
'LOGO': 'images/logos/centos.png',
'MEMORY_UNIT_COST': 'MB',
'SAVED_TEMPLATE_ID': '29'
})
})()
vm = type(
"pyone.bindings.VMType90Sub",
(object,),
{
"DEPLOY_ID": "one-327",
"ETIME": 0,
"GID": 0,
"GNAME": "oneadmin",
"HISTORY_RECORDS": [],
"ID": 327,
"LAST_POLL": 1632763543,
"LCM_STATE": 3,
"MONITORING": {},
"NAME": "zabbix-327",
"RESCHED": 0,
"SNAPSHOTS": [],
"STATE": 3,
"STIME": 1575410106,
"TEMPLATE": OrderedDict(
{
"NIC": [
OrderedDict(
{
"AR_ID": "0",
"BRIDGE": "onerb.103",
"BRIDGE_TYPE": "linux",
"IP": "185.165.1.1",
"IP6_GLOBAL": "2000:a001::b9ff:feae:aa0d",
"IP6_LINK": "fe80::b9ff:feae:aa0d",
"MAC": "02:00:b9:ae:aa:0d",
"NAME": "NIC0",
"NETWORK": "Public",
"NETWORK_ID": "7",
"NIC_ID": "0",
"PHYDEV": "team0",
"SECURITY_GROUPS": "0",
"TARGET": "one-327-0",
"VLAN_ID": "100",
"VN_MAD": "802.1Q",
}
),
OrderedDict(
{
"AR_ID": "0",
"BRIDGE": "br0",
"BRIDGE_TYPE": "linux",
"CLUSTER_ID": "0",
"IP": "192.168.1.1",
"MAC": "02:00:c0:a8:3b:01",
"NAME": "NIC1",
"NETWORK": "Management",
"NETWORK_ID": "11",
"NIC_ID": "1",
"SECURITY_GROUPS": "0",
"TARGET": "one-327-1",
"VN_MAD": "bridge",
}
),
]
}
),
"USER_TEMPLATE": OrderedDict(
{
"HYPERVISOR": "kvm",
"INPUTS_ORDER": "",
"LABELS": "Oracle Linux",
"LOGO": "images/logos/centos.png",
"MEMORY_UNIT_COST": "MB",
"SAVED_TEMPLATE_ID": "29",
}
),
},
)()
data.VM.append(vm)
vm = type('pyone.bindings.VMType90Sub', (object,), {
'DEPLOY_ID': 'one-107',
'ETIME': 0,
'GID': 0,
'GNAME': 'oneadmin',
'HISTORY_RECORDS': [],
'ID': 107,
'LAST_POLL': 1632764186,
'LCM_STATE': 3,
'MONITORING': {},
'NAME': 'gitlab-107',
'RESCHED': 0,
'SNAPSHOTS': [],
'STATE': 3,
'STIME': 1572485522,
'TEMPLATE': OrderedDict({
'NIC': OrderedDict({
'AR_ID': '0',
'BRIDGE': 'onerb.103',
'BRIDGE_TYPE': 'linux',
'IP': '185.165.1.3',
'IP6_GLOBAL': '2000:a001::b9ff:feae:aa03',
'IP6_LINK': 'fe80::b9ff:feae:aa03',
'MAC': '02:00:b9:ae:aa:03',
'NAME': 'NIC0',
'NETWORK': 'Public',
'NETWORK_ID': '7',
'NIC_ID': '0',
'PHYDEV': 'team0',
'SECURITY_GROUPS': '0',
'TARGET': 'one-107-0',
'VLAN_ID': '100',
'VN_MAD': '802.1Q'
})
}),
'USER_TEMPLATE': OrderedDict({
'HYPERVISOR': 'kvm',
'INPUTS_ORDER': '',
'LABELS': 'Gitlab,Centos',
'LOGO': 'images/logos/centos.png',
'MEMORY_UNIT_COST': 'MB',
'SCHED_REQUIREMENTS': 'ID="0" | ID="1" | ID="2"',
'SSH_PORT': '8822'
})
})()
vm = type(
"pyone.bindings.VMType90Sub",
(object,),
{
"DEPLOY_ID": "one-107",
"ETIME": 0,
"GID": 0,
"GNAME": "oneadmin",
"HISTORY_RECORDS": [],
"ID": 107,
"LAST_POLL": 1632764186,
"LCM_STATE": 3,
"MONITORING": {},
"NAME": "gitlab-107",
"RESCHED": 0,
"SNAPSHOTS": [],
"STATE": 3,
"STIME": 1572485522,
"TEMPLATE": OrderedDict(
{
"NIC": OrderedDict(
{
"AR_ID": "0",
"BRIDGE": "onerb.103",
"BRIDGE_TYPE": "linux",
"IP": "185.165.1.3",
"IP6_GLOBAL": "2000:a001::b9ff:feae:aa03",
"IP6_LINK": "fe80::b9ff:feae:aa03",
"MAC": "02:00:b9:ae:aa:03",
"NAME": "NIC0",
"NETWORK": "Public",
"NETWORK_ID": "7",
"NIC_ID": "0",
"PHYDEV": "team0",
"SECURITY_GROUPS": "0",
"TARGET": "one-107-0",
"VLAN_ID": "100",
"VN_MAD": "802.1Q",
}
)
}
),
"USER_TEMPLATE": OrderedDict(
{
"HYPERVISOR": "kvm",
"INPUTS_ORDER": "",
"LABELS": "Gitlab,Centos",
"LOGO": "images/logos/centos.png",
"MEMORY_UNIT_COST": "MB",
"SCHED_REQUIREMENTS": 'ID="0" | ID="1" | ID="2"',
"SSH_PORT": "8822",
}
),
},
)()
data.VM.append(vm)
return data
options_base_test = {
'api_url': 'https://opennebula:2633/RPC2',
'api_username': 'username',
'api_password': 'password',
'api_authfile': '~/.one/one_auth',
'hostname': 'v4_first_ip',
'group_by_labels': True,
'filter_by_label': None,
"api_url": "https://opennebula:2633/RPC2",
"api_username": "username",
"api_password": "password",
"api_authfile": "~/.one/one_auth",
"hostname": "v4_first_ip",
"group_by_labels": True,
"filter_by_label": None,
}
@ -268,6 +300,7 @@ options_base_test = {
def mk_get_options(opts_dict):
def inner(opt):
return opts_dict.get(opt, False)
return inner
@ -275,22 +308,23 @@ def test_get_connection_info(inventory, mocker):
inventory.get_option = mocker.MagicMock(side_effect=mk_get_options(options_base_test))
auth = inventory._get_connection_info()
assert (auth.username and auth.password)
assert auth.username and auth.password
def test_populate_constructable_templating(mocker):
inventory_filename = '/fake/opennebula.yml'
inventory_filename = "/fake/opennebula.yml"
mocker.patch.object(InventoryModule, '_get_vm_pool', side_effect=get_vm_pool_json)
mocker.patch('ansible_collections.community.general.plugins.inventory.opennebula.HAS_PYONE', True)
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop)
mocker.patch('os.path.exists', exists_mock(inventory_filename))
mocker.patch('os.access', access_mock(inventory_filename))
mocker.patch.object(InventoryModule, "_get_vm_pool", side_effect=get_vm_pool_json)
mocker.patch("ansible_collections.community.general.plugins.inventory.opennebula.HAS_PYONE", True)
mocker.patch("ansible.inventory.manager.unfrackpath", mock_unfrackpath_noop)
mocker.patch("os.path.exists", exists_mock(inventory_filename))
mocker.patch("os.access", access_mock(inventory_filename))
# the templating engine is needed for the constructable groups/vars
# so give that some fake data and instantiate it.
C.INVENTORY_ENABLED = ['community.general.opennebula']
inventory_file = {inventory_filename: r'''
C.INVENTORY_ENABLED = ["community.general.opennebula"]
inventory_file = {
inventory_filename: r"""
---
plugin: community.general.opennebula
api_url: https://opennebula:2633/RPC2
@ -308,43 +342,46 @@ groups:
keyed_groups:
- key: TGROUP
prefix: tgroup
'''}
"""
}
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename)
# note the vm_pool (and json data file) has four hosts,
# but the options above asks ansible to filter one out
assert len(get_vm_pool_json().VM) == 4
assert set(vm.NAME for vm in get_vm_pool_json().VM) == set([
'terraform_demo_00',
'terraform_demo_01',
'terraform_demo_srv_00',
'bs-windows',
])
assert set(im._inventory.hosts) == set(['terraform_demo_00', 'terraform_demo_01', 'terraform_demo_srv_00'])
assert set(vm.NAME for vm in get_vm_pool_json().VM) == set(
[
"terraform_demo_00",
"terraform_demo_01",
"terraform_demo_srv_00",
"bs-windows",
]
)
assert set(im._inventory.hosts) == set(["terraform_demo_00", "terraform_demo_01", "terraform_demo_srv_00"])
host_demo00 = im._inventory.get_host('terraform_demo_00')
host_demo01 = im._inventory.get_host('terraform_demo_01')
host_demosrv = im._inventory.get_host('terraform_demo_srv_00')
host_demo00 = im._inventory.get_host("terraform_demo_00")
host_demo01 = im._inventory.get_host("terraform_demo_01")
host_demosrv = im._inventory.get_host("terraform_demo_srv_00")
assert 'benchmark_clients' in im._inventory.groups
assert 'lin' in im._inventory.groups
assert im._inventory.groups['benchmark_clients'].hosts == [host_demo00, host_demo01]
assert im._inventory.groups['lin'].hosts == [host_demo00, host_demo01, host_demosrv]
assert "benchmark_clients" in im._inventory.groups
assert "lin" in im._inventory.groups
assert im._inventory.groups["benchmark_clients"].hosts == [host_demo00, host_demo01]
assert im._inventory.groups["lin"].hosts == [host_demo00, host_demo01, host_demosrv]
# test group by label:
assert 'bench' in im._inventory.groups
assert 'foo' in im._inventory.groups
assert im._inventory.groups['bench'].hosts == [host_demo00, host_demo01, host_demosrv]
assert im._inventory.groups['serv'].hosts == [host_demosrv]
assert im._inventory.groups['foo'].hosts == [host_demo00, host_demo01]
assert "bench" in im._inventory.groups
assert "foo" in im._inventory.groups
assert im._inventory.groups["bench"].hosts == [host_demo00, host_demo01, host_demosrv]
assert im._inventory.groups["serv"].hosts == [host_demosrv]
assert im._inventory.groups["foo"].hosts == [host_demo00, host_demo01]
# test `compose` transforms GUEST_OS=Linux to is_linux == True
assert host_demo00.get_vars()['GUEST_OS'] == 'linux'
assert host_demo00.get_vars()['is_linux'] is True
assert host_demo00.get_vars()["GUEST_OS"] == "linux"
assert host_demo00.get_vars()["is_linux"] is True
# test `keyed_groups`
assert im._inventory.groups['tgroup_bench_clients'].hosts == [host_demo00, host_demo01]
assert im._inventory.groups['tgroup_bench_server'].hosts == [host_demosrv]
assert im._inventory.groups["tgroup_bench_clients"].hosts == [host_demo00, host_demo01]
assert im._inventory.groups["tgroup_bench_server"].hosts == [host_demosrv]
def test_populate(inventory, mocker):
@ -354,35 +391,35 @@ def test_populate(inventory, mocker):
inventory._populate()
# get different hosts
host_sam = inventory.inventory.get_host('sam-691-sam')
host_zabbix = inventory.inventory.get_host('zabbix-327')
host_gitlab = inventory.inventory.get_host('gitlab-107')
host_sam = inventory.inventory.get_host("sam-691-sam")
host_zabbix = inventory.inventory.get_host("zabbix-327")
host_gitlab = inventory.inventory.get_host("gitlab-107")
# test if groups exists
assert 'Gitlab' in inventory.inventory.groups
assert 'Centos' in inventory.inventory.groups
assert 'Oracle_Linux' in inventory.inventory.groups
assert "Gitlab" in inventory.inventory.groups
assert "Centos" in inventory.inventory.groups
assert "Oracle_Linux" in inventory.inventory.groups
# check if host_zabbix is in Oracle_Linux group
group_oracle_linux = inventory.inventory.groups['Oracle_Linux']
group_oracle_linux = inventory.inventory.groups["Oracle_Linux"]
assert group_oracle_linux.hosts == [host_zabbix]
# check if host_gitlab is in Gitlab and Centos group
group_gitlab = inventory.inventory.groups['Gitlab']
group_centos = inventory.inventory.groups['Centos']
group_gitlab = inventory.inventory.groups["Gitlab"]
group_centos = inventory.inventory.groups["Centos"]
assert group_gitlab.hosts == [host_gitlab]
assert group_centos.hosts == [host_gitlab]
# check IPv4 address
assert '172.22.4.187' == host_sam.get_vars()['v4_first_ip']
assert "172.22.4.187" == host_sam.get_vars()["v4_first_ip"]
# check IPv6 address
assert '2000:a001::b9ff:feae:aa0d' == host_zabbix.get_vars()['v6_first_ip']
assert "2000:a001::b9ff:feae:aa0d" == host_zabbix.get_vars()["v6_first_ip"]
# check ansible_hosts
assert '172.22.4.187' == host_sam.get_vars()['ansible_host']
assert '185.165.1.1' == host_zabbix.get_vars()['ansible_host']
assert '185.165.1.3' == host_gitlab.get_vars()['ansible_host']
assert "172.22.4.187" == host_sam.get_vars()["ansible_host"]
assert "185.165.1.1" == host_zabbix.get_vars()["ansible_host"]
assert "185.165.1.3" == host_gitlab.get_vars()["ansible_host"]
# check for custom ssh port
assert '8822' == host_gitlab.get_vars()['ansible_port']
assert "8822" == host_gitlab.get_vars()["ansible_port"]

View file

@ -12,129 +12,127 @@ from ansible.inventory.data import InventoryData
from ansible_collections.community.general.plugins.inventory.xen_orchestra import InventoryModule
objects = {
'vms': {
'0e64588-2bea-2d82-e922-881654b0a48f':
{
'type': 'VM',
'addresses': {},
'CPUs': {'max': 4, 'number': 4},
'memory': {'dynamic': [1073741824, 2147483648], 'static': [536870912, 4294967296], 'size': 2147483648},
'name_description': '',
'name_label': 'XCP-NG lab 2',
'os_version': {},
'parent': 'd3af89b2-d846-0874-6acb-031ccf11c560',
'power_state': 'Running',
'tags': [],
'id': '0e645898-2bea-2d82-e922-881654b0a48f',
'uuid': '0e645898-2bea-2d82-e922-881654b0a48f',
'$pool': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'$poolId': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'$container': '222d8594-9426-468a-ad69-7a6f02330fa3'
},
'b0d25e70-019d-6182-2f7c-b0f5d8ef9331':
{
'type': 'VM',
'addresses': {'0/ipv4/0': '192.168.1.55', '1/ipv4/0': '10.0.90.1'},
'CPUs': {'max': 4, 'number': 4},
'mainIpAddress': '192.168.1.55',
'memory': {'dynamic': [2147483648, 2147483648], 'static': [134217728, 2147483648], 'size': 2147483648},
'name_description': '',
'name_label': 'XCP-NG lab 3',
'os_version': {'name': 'FreeBSD 11.3-STABLE', 'uname': '11.3-STABLE', 'distro': 'FreeBSD'},
'power_state': 'Halted',
'tags': [],
'id': 'b0d25e70-019d-6182-2f7c-b0f5d8ef9331',
'uuid': 'b0d25e70-019d-6182-2f7c-b0f5d8ef9331',
'$pool': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'$poolId': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'$container': 'c96ec4dd-28ac-4df4-b73c-4371bd202728',
}
},
'pools': {
'3d315997-73bd-5a74-8ca7-289206cb03ab': {
'master': '222d8594-9426-468a-ad69-7a6f02330fa3',
'tags': [],
'name_description': '',
'name_label': 'Storage Lab',
'cpus': {'cores': 120, 'sockets': 6},
'id': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'type': 'pool',
'uuid': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'$pool': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'$poolId': '3d315997-73bd-5a74-8ca7-289206cb03ab'
}
},
'hosts': {
'c96ec4dd-28ac-4df4-b73c-4371bd202728': {
'type': 'host',
'uuid': 'c96ec4dd-28ac-4df4-b73c-4371bd202728',
'enabled': True,
'CPUs': {
'cpu_count': '40',
'socket_count': '2',
'vendor': 'GenuineIntel',
'speed': '1699.998',
'modelname': 'Intel(R) Xeon(R) CPU E5-2650L v2 @ 1.70GHz',
'family': '6',
'model': '62',
'stepping': '4'
},
'address': '172.16.210.14',
'build': 'release/stockholm/master/7',
'cpus': {'cores': 40, 'sockets': 2},
'hostname': 'r620-s1',
'name_description': 'Default install',
'name_label': 'R620-S1',
'memory': {'usage': 45283590144, 'size': 137391292416},
'power_state': 'Running',
'tags': [],
'version': '8.2.0',
'productBrand': 'XCP-ng',
'id': 'c96ec4dd-28ac-4df4-b73c-4371bd202728',
'$pool': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'$poolId': '3d315997-73bd-5a74-8ca7-289206cb03ab'
"vms": {
"0e64588-2bea-2d82-e922-881654b0a48f": {
"type": "VM",
"addresses": {},
"CPUs": {"max": 4, "number": 4},
"memory": {"dynamic": [1073741824, 2147483648], "static": [536870912, 4294967296], "size": 2147483648},
"name_description": "",
"name_label": "XCP-NG lab 2",
"os_version": {},
"parent": "d3af89b2-d846-0874-6acb-031ccf11c560",
"power_state": "Running",
"tags": [],
"id": "0e645898-2bea-2d82-e922-881654b0a48f",
"uuid": "0e645898-2bea-2d82-e922-881654b0a48f",
"$pool": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"$poolId": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"$container": "222d8594-9426-468a-ad69-7a6f02330fa3",
},
'222d8594-9426-468a-ad69-7a6f02330fa3': {
'type': 'host',
'uuid': '222d8594-9426-468a-ad69-7a6f02330fa3',
'enabled': True,
'CPUs': {
'cpu_count': '40',
'socket_count': '2',
'vendor': 'GenuineIntel',
'speed': '1700.007',
'modelname': 'Intel(R) Xeon(R) CPU E5-2650L v2 @ 1.70GHz',
'family': '6',
'model': '62',
'stepping': '4'
},
'address': '172.16.210.16',
'build': 'release/stockholm/master/7',
'cpus': {'cores': 40, 'sockets': 2},
'hostname': 'r620-s2',
'name_description': 'Default install',
'name_label': 'R620-S2',
'memory': {'usage': 10636521472, 'size': 137391292416},
'power_state': 'Running',
'tags': ['foo', 'bar', 'baz'],
'version': '8.2.0',
'productBrand': 'XCP-ng',
'id': '222d8594-9426-468a-ad69-7a6f02330fa3',
'$pool': '3d315997-73bd-5a74-8ca7-289206cb03ab',
'$poolId': '3d315997-73bd-5a74-8ca7-289206cb03ab'
"b0d25e70-019d-6182-2f7c-b0f5d8ef9331": {
"type": "VM",
"addresses": {"0/ipv4/0": "192.168.1.55", "1/ipv4/0": "10.0.90.1"},
"CPUs": {"max": 4, "number": 4},
"mainIpAddress": "192.168.1.55",
"memory": {"dynamic": [2147483648, 2147483648], "static": [134217728, 2147483648], "size": 2147483648},
"name_description": "",
"name_label": "XCP-NG lab 3",
"os_version": {"name": "FreeBSD 11.3-STABLE", "uname": "11.3-STABLE", "distro": "FreeBSD"},
"power_state": "Halted",
"tags": [],
"id": "b0d25e70-019d-6182-2f7c-b0f5d8ef9331",
"uuid": "b0d25e70-019d-6182-2f7c-b0f5d8ef9331",
"$pool": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"$poolId": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"$container": "c96ec4dd-28ac-4df4-b73c-4371bd202728",
},
},
"pools": {
"3d315997-73bd-5a74-8ca7-289206cb03ab": {
"master": "222d8594-9426-468a-ad69-7a6f02330fa3",
"tags": [],
"name_description": "",
"name_label": "Storage Lab",
"cpus": {"cores": 120, "sockets": 6},
"id": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"type": "pool",
"uuid": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"$pool": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"$poolId": "3d315997-73bd-5a74-8ca7-289206cb03ab",
}
}
},
"hosts": {
"c96ec4dd-28ac-4df4-b73c-4371bd202728": {
"type": "host",
"uuid": "c96ec4dd-28ac-4df4-b73c-4371bd202728",
"enabled": True,
"CPUs": {
"cpu_count": "40",
"socket_count": "2",
"vendor": "GenuineIntel",
"speed": "1699.998",
"modelname": "Intel(R) Xeon(R) CPU E5-2650L v2 @ 1.70GHz",
"family": "6",
"model": "62",
"stepping": "4",
},
"address": "172.16.210.14",
"build": "release/stockholm/master/7",
"cpus": {"cores": 40, "sockets": 2},
"hostname": "r620-s1",
"name_description": "Default install",
"name_label": "R620-S1",
"memory": {"usage": 45283590144, "size": 137391292416},
"power_state": "Running",
"tags": [],
"version": "8.2.0",
"productBrand": "XCP-ng",
"id": "c96ec4dd-28ac-4df4-b73c-4371bd202728",
"$pool": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"$poolId": "3d315997-73bd-5a74-8ca7-289206cb03ab",
},
"222d8594-9426-468a-ad69-7a6f02330fa3": {
"type": "host",
"uuid": "222d8594-9426-468a-ad69-7a6f02330fa3",
"enabled": True,
"CPUs": {
"cpu_count": "40",
"socket_count": "2",
"vendor": "GenuineIntel",
"speed": "1700.007",
"modelname": "Intel(R) Xeon(R) CPU E5-2650L v2 @ 1.70GHz",
"family": "6",
"model": "62",
"stepping": "4",
},
"address": "172.16.210.16",
"build": "release/stockholm/master/7",
"cpus": {"cores": 40, "sockets": 2},
"hostname": "r620-s2",
"name_description": "Default install",
"name_label": "R620-S2",
"memory": {"usage": 10636521472, "size": 137391292416},
"power_state": "Running",
"tags": ["foo", "bar", "baz"],
"version": "8.2.0",
"productBrand": "XCP-ng",
"id": "222d8594-9426-468a-ad69-7a6f02330fa3",
"$pool": "3d315997-73bd-5a74-8ca7-289206cb03ab",
"$poolId": "3d315997-73bd-5a74-8ca7-289206cb03ab",
},
},
}
def get_option(option):
if option == 'groups':
if option == "groups":
return {}
elif option == 'keyed_groups':
elif option == "keyed_groups":
return []
elif option == 'compose':
elif option == "compose":
return {}
elif option == 'strict':
elif option == "strict":
return False
else:
return None
@ -152,55 +150,57 @@ def inventory():
def test_verify_file_bad_config(inventory):
assert inventory.verify_file('foobar.xen_orchestra.yml') is False
assert inventory.verify_file("foobar.xen_orchestra.yml") is False
def test_populate(inventory, mocker):
inventory.host_entry_name_type = 'uuid'
inventory.vm_entry_name_type = 'uuid'
inventory.host_entry_name_type = "uuid"
inventory.vm_entry_name_type = "uuid"
inventory.get_option = mocker.MagicMock(side_effect=get_option)
inventory._populate(objects)
actual = sorted(inventory.inventory.hosts.keys())
expected = sorted(['c96ec4dd-28ac-4df4-b73c-4371bd202728', '222d8594-9426-468a-ad69-7a6f02330fa3',
'0e64588-2bea-2d82-e922-881654b0a48f', 'b0d25e70-019d-6182-2f7c-b0f5d8ef9331'])
expected = sorted(
[
"c96ec4dd-28ac-4df4-b73c-4371bd202728",
"222d8594-9426-468a-ad69-7a6f02330fa3",
"0e64588-2bea-2d82-e922-881654b0a48f",
"b0d25e70-019d-6182-2f7c-b0f5d8ef9331",
]
)
assert actual == expected
# Host with ip assertions
host_with_ip = inventory.inventory.get_host(
'b0d25e70-019d-6182-2f7c-b0f5d8ef9331')
host_with_ip = inventory.inventory.get_host("b0d25e70-019d-6182-2f7c-b0f5d8ef9331")
host_with_ip_vars = host_with_ip.vars
assert host_with_ip_vars['ansible_host'] == '192.168.1.55'
assert host_with_ip_vars['power_state'] == 'halted'
assert host_with_ip_vars['type'] == 'VM'
assert host_with_ip_vars["ansible_host"] == "192.168.1.55"
assert host_with_ip_vars["power_state"] == "halted"
assert host_with_ip_vars["type"] == "VM"
assert host_with_ip in inventory.inventory.groups['with_ip'].hosts
assert host_with_ip in inventory.inventory.groups["with_ip"].hosts
# Host without ip
host_without_ip = inventory.inventory.get_host(
'0e64588-2bea-2d82-e922-881654b0a48f')
host_without_ip = inventory.inventory.get_host("0e64588-2bea-2d82-e922-881654b0a48f")
host_without_ip_vars = host_without_ip.vars
assert host_without_ip_vars['ansible_host'] is None
assert host_without_ip_vars['power_state'] == 'running'
assert host_without_ip_vars["ansible_host"] is None
assert host_without_ip_vars["power_state"] == "running"
assert host_without_ip in inventory.inventory.groups['without_ip'].hosts
assert host_without_ip in inventory.inventory.groups["without_ip"].hosts
assert host_with_ip in inventory.inventory.groups['xo_host_r620_s1'].hosts
assert host_without_ip in inventory.inventory.groups['xo_host_r620_s2'].hosts
assert host_with_ip in inventory.inventory.groups["xo_host_r620_s1"].hosts
assert host_without_ip in inventory.inventory.groups["xo_host_r620_s2"].hosts
r620_s1 = inventory.inventory.get_host(
'c96ec4dd-28ac-4df4-b73c-4371bd202728')
r620_s2 = inventory.inventory.get_host(
'222d8594-9426-468a-ad69-7a6f02330fa3')
r620_s1 = inventory.inventory.get_host("c96ec4dd-28ac-4df4-b73c-4371bd202728")
r620_s2 = inventory.inventory.get_host("222d8594-9426-468a-ad69-7a6f02330fa3")
assert r620_s1.vars['address'] == '172.16.210.14'
assert r620_s1.vars['tags'] == []
assert r620_s2.vars['address'] == '172.16.210.16'
assert r620_s2.vars['tags'] == ['foo', 'bar', 'baz']
assert r620_s1.vars["address"] == "172.16.210.14"
assert r620_s1.vars["tags"] == []
assert r620_s2.vars["address"] == "172.16.210.16"
assert r620_s2.vars["tags"] == ["foo", "bar", "baz"]
storage_lab = inventory.inventory.groups['xo_pool_storage_lab']
storage_lab = inventory.inventory.groups["xo_pool_storage_lab"]
# Check that hosts are in their corresponding pool
assert r620_s1 in storage_lab.hosts