Skip to content

Commit 2f619ac

Browse files
committed
Improve/fix tests.
1 parent 5e08cce commit 2f619ac

File tree

2 files changed

+74
-47
lines changed

2 files changed

+74
-47
lines changed

tests/unit/plugins/inventory/test_cobbler.py

-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ def inventory():
1616
return InventoryModule()
1717

1818

19-
def test_init_cache(inventory):
20-
inventory._init_cache()
21-
assert inventory._cache[inventory.cache_key] == {}
22-
23-
2419
def test_verify_file(tmp_path, inventory):
2520
file = tmp_path / "foobar.cobbler.yml"
2621
file.touch()

tests/unit/plugins/inventory/test_opennebula.py

+74-42
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,41 @@
1111

1212
from collections import OrderedDict
1313
import json
14+
import os
1415

1516
import pytest
1617

18+
from ansible import constants as C
1719
from ansible.inventory.data import InventoryData
18-
from ansible.parsing.dataloader import DataLoader
20+
from ansible.inventory.manager import InventoryManager
21+
from ansible.module_utils.common.text.converters import to_native
1922
from ansible.template import Templar
23+
2024
from ansible_collections.community.general.plugins.inventory.opennebula import InventoryModule
21-
from ansible_collections.community.general.tests.unit.compat.mock import create_autospec
25+
from ansible_collections.community.general.tests.unit.mock.loader import DictDataLoader
26+
from ansible_collections.community.general.tests.unit.mock.path import mock_unfrackpath_noop
27+
28+
29+
original_exists = os.path.exists
30+
original_access = os.access
31+
32+
33+
def exists_mock(path, exists=True):
34+
def exists(f):
35+
if to_native(f) == path:
36+
return exists
37+
return original_exists(f)
38+
39+
return exists
40+
41+
42+
def access_mock(path, can_access=True):
43+
def access(f, m, *args, **kwargs):
44+
if to_native(f) == path:
45+
return can_access
46+
return original_access(f, m, *args, **kwargs) # pragma: no cover
47+
48+
return access
2249

2350

2451
class HistoryEntry(object):
@@ -239,18 +266,6 @@ def get_vm_pool():
239266
'filter_by_label': None,
240267
}
241268

242-
options_constructable_test = options_base_test.copy()
243-
options_constructable_test.update({
244-
'compose': {'is_linux': "GUEST_OS == 'linux'"},
245-
'filter_by_label': 'bench',
246-
'groups': {
247-
'benchmark_clients': "TGROUP.endswith('clients')",
248-
'lin': 'is_linux == True'
249-
},
250-
'keyed_groups': [{'key': 'TGROUP', 'prefix': 'tgroup'}],
251-
252-
})
253-
254269

255270
# given a dictionary `opts_dict`, return a function that behaves like ansible's inventory get_options
256271
def mk_get_options(opts_dict):
@@ -266,56 +281,73 @@ def test_get_connection_info(inventory, mocker):
266281
assert (auth.username and auth.password)
267282

268283

269-
def test_populate_constructable_templating(inventory, mocker):
270-
# bypass API fetch call
271-
inventory._get_vm_pool = mocker.MagicMock(side_effect=get_vm_pool_json)
272-
inventory.get_option = mocker.MagicMock(side_effect=mk_get_options(options_constructable_test))
284+
def test_populate_constructable_templating(mocker):
285+
inventory_filename = '/fake/opennebula.yml'
286+
287+
mocker.patch.object(InventoryModule, '_get_vm_pool', side_effect=get_vm_pool_json)
288+
mocker.patch('ansible_collections.community.general.plugins.inventory.opennebula.HAS_PYONE', True)
289+
mocker.patch('ansible.inventory.manager.unfrackpath', mock_unfrackpath_noop)
290+
mocker.patch('os.path.exists', exists_mock(inventory_filename))
291+
mocker.patch('os.access', access_mock(inventory_filename))
273292

274293
# the templating engine is needed for the constructable groups/vars
275294
# so give that some fake data and instantiate it.
276-
fake_config_filepath = '/fake/opennebula.yml'
277-
fake_cache = {fake_config_filepath: options_constructable_test.copy()}
278-
fake_cache[fake_config_filepath]['plugin'] = 'community.general.opennebula'
279-
dataloader = create_autospec(DataLoader, instance=True)
280-
dataloader._FILE_CACHE = fake_cache
281-
inventory.templar = Templar(loader=dataloader)
282-
283-
inventory._populate()
295+
C.INVENTORY_ENABLED = ['community.general.opennebula']
296+
inventory_file = {inventory_filename: r'''
297+
---
298+
plugin: community.general.opennebula
299+
api_url: https://opennebula:2633/RPC2
300+
api_username: username
301+
api_password: password
302+
api_authfile: '~/.one/one_auth'
303+
hostname: v4_first_ip
304+
group_by_labels: true
305+
compose:
306+
is_linux: GUEST_OS == 'linux'
307+
filter_by_label: bench
308+
groups:
309+
benchmark_clients: TGROUP.endswith('clients')
310+
lin: is_linux == true
311+
keyed_groups:
312+
- key: TGROUP
313+
prefix: tgroup
314+
'''}
315+
im = InventoryManager(loader=DictDataLoader(inventory_file), sources=inventory_filename)
284316

285317
# note the vm_pool (and json data file) has four hosts,
286-
# but options_constructable_test asks ansible to filter it out
318+
# but the options above asks ansible to filter one out
287319
assert len(get_vm_pool_json().VM) == 4
288320
assert set([vm.NAME for vm in get_vm_pool_json().VM]) == set([
289321
'terraform_demo_00',
290322
'terraform_demo_01',
291323
'terraform_demo_srv_00',
292324
'bs-windows',
293325
])
294-
assert set(inventory.inventory.hosts) == set(['terraform_demo_00', 'terraform_demo_01', 'terraform_demo_srv_00'])
326+
assert set(im._inventory.hosts) == set(['terraform_demo_00', 'terraform_demo_01', 'terraform_demo_srv_00'])
295327

296-
host_demo00 = inventory.inventory.get_host('terraform_demo_00')
297-
host_demo01 = inventory.inventory.get_host('terraform_demo_01')
298-
host_demosrv = inventory.inventory.get_host('terraform_demo_srv_00')
328+
host_demo00 = im._inventory.get_host('terraform_demo_00')
329+
host_demo01 = im._inventory.get_host('terraform_demo_01')
330+
host_demosrv = im._inventory.get_host('terraform_demo_srv_00')
299331

300-
assert 'benchmark_clients' in inventory.inventory.groups
301-
assert 'lin' in inventory.inventory.groups
302-
assert inventory.inventory.groups['benchmark_clients'].hosts == [host_demo00, host_demo01]
303-
assert inventory.inventory.groups['lin'].hosts == [host_demo00, host_demo01, host_demosrv]
332+
assert 'benchmark_clients' in im._inventory.groups
333+
assert 'lin' in im._inventory.groups
334+
assert im._inventory.groups['benchmark_clients'].hosts == [host_demo00, host_demo01]
335+
assert im._inventory.groups['lin'].hosts == [host_demo00, host_demo01, host_demosrv]
304336

305337
# test group by label:
306-
assert 'bench' in inventory.inventory.groups
307-
assert 'foo' in inventory.inventory.groups
308-
assert inventory.inventory.groups['bench'].hosts == [host_demo00, host_demo01, host_demosrv]
309-
assert inventory.inventory.groups['serv'].hosts == [host_demosrv]
310-
assert inventory.inventory.groups['foo'].hosts == [host_demo00, host_demo01]
338+
assert 'bench' in im._inventory.groups
339+
assert 'foo' in im._inventory.groups
340+
assert im._inventory.groups['bench'].hosts == [host_demo00, host_demo01, host_demosrv]
341+
assert im._inventory.groups['serv'].hosts == [host_demosrv]
342+
assert im._inventory.groups['foo'].hosts == [host_demo00, host_demo01]
311343

312344
# test `compose` transforms GUEST_OS=Linux to is_linux == True
313345
assert host_demo00.get_vars()['GUEST_OS'] == 'linux'
314346
assert host_demo00.get_vars()['is_linux'] is True
315347

316348
# test `keyed_groups`
317-
assert inventory.inventory.groups['tgroup_bench_clients'].hosts == [host_demo00, host_demo01]
318-
assert inventory.inventory.groups['tgroup_bench_server'].hosts == [host_demosrv]
349+
assert im._inventory.groups['tgroup_bench_clients'].hosts == [host_demo00, host_demo01]
350+
assert im._inventory.groups['tgroup_bench_server'].hosts == [host_demosrv]
319351

320352

321353
def test_populate(inventory, mocker):

0 commit comments

Comments
 (0)