diff --git a/tests/integration/targets/cargo/tasks/main.yml b/tests/integration/targets/cargo/tasks/main.yml index 89f13960a6..d9048f77bf 100644 --- a/tests/integration/targets/cargo/tasks/main.yml +++ b/tests/integration/targets/cargo/tasks/main.yml @@ -20,4 +20,4 @@ environment: "{{ cargo_environment }}" when: has_cargo | default(false) - import_tasks: test_rustup_cargo.yml - when: rustup_cargo_bin | default(false) + when: (rustup_cargo_bin | default(false)) is truthy diff --git a/tests/unit/plugins/inventory/test_cobbler.py b/tests/unit/plugins/inventory/test_cobbler.py index a09001ad62..7bc2b847c6 100644 --- a/tests/unit/plugins/inventory/test_cobbler.py +++ b/tests/unit/plugins/inventory/test_cobbler.py @@ -16,11 +16,6 @@ def inventory(): return InventoryModule() -def test_init_cache(inventory): - inventory._init_cache() - assert inventory._cache[inventory.cache_key] == {} - - def test_verify_file(tmp_path, inventory): file = tmp_path / "foobar.cobbler.yml" file.touch() diff --git a/tests/unit/plugins/inventory/test_opennebula.py b/tests/unit/plugins/inventory/test_opennebula.py index 52ea934043..21d485e4bb 100644 --- a/tests/unit/plugins/inventory/test_opennebula.py +++ b/tests/unit/plugins/inventory/test_opennebula.py @@ -11,14 +11,40 @@ __metaclass__ = type from collections import OrderedDict import json +import os import pytest +from ansible import constants as C from ansible.inventory.data import InventoryData -from ansible.parsing.dataloader import DataLoader -from ansible.template import Templar +from ansible.inventory.manager import InventoryManager +from ansible.module_utils.common.text.converters import to_native + from ansible_collections.community.general.plugins.inventory.opennebula import InventoryModule -from ansible_collections.community.general.tests.unit.compat.mock import create_autospec +from ansible_collections.community.general.tests.unit.mock.loader import DictDataLoader +from ansible_collections.community.general.tests.unit.mock.path import mock_unfrackpath_noop + + +original_exists = os.path.exists +original_access = os.access + + +def exists_mock(path, exists=True): + def exists(f): + if to_native(f) == path: + return exists + return original_exists(f) + + return exists + + +def access_mock(path, can_access=True): + def access(f, m, *args, **kwargs): + if to_native(f) == path: + return can_access + return original_access(f, m, *args, **kwargs) # pragma: no cover + + return access class HistoryEntry(object): @@ -239,18 +265,6 @@ options_base_test = { 'filter_by_label': None, } -options_constructable_test = options_base_test.copy() -options_constructable_test.update({ - 'compose': {'is_linux': "GUEST_OS == 'linux'"}, - 'filter_by_label': 'bench', - 'groups': { - 'benchmark_clients': "TGROUP.endswith('clients')", - 'lin': 'is_linux == True' - }, - 'keyed_groups': [{'key': 'TGROUP', 'prefix': 'tgroup'}], - -}) - # given a dictionary `opts_dict`, return a function that behaves like ansible's inventory get_options def mk_get_options(opts_dict): @@ -266,24 +280,41 @@ def test_get_connection_info(inventory, mocker): assert (auth.username and auth.password) -def test_populate_constructable_templating(inventory, mocker): - # bypass API fetch call - inventory._get_vm_pool = mocker.MagicMock(side_effect=get_vm_pool_json) - inventory.get_option = mocker.MagicMock(side_effect=mk_get_options(options_constructable_test)) +def test_populate_constructable_templating(mocker): + 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)) # the templating engine is needed for the constructable groups/vars # so give that some fake data and instantiate it. - fake_config_filepath = '/fake/opennebula.yml' - fake_cache = {fake_config_filepath: options_constructable_test.copy()} - fake_cache[fake_config_filepath]['plugin'] = 'community.general.opennebula' - dataloader = create_autospec(DataLoader, instance=True) - dataloader._FILE_CACHE = fake_cache - inventory.templar = Templar(loader=dataloader) - - inventory._populate() + C.INVENTORY_ENABLED = ['community.general.opennebula'] + inventory_file = {inventory_filename: r''' +--- +plugin: community.general.opennebula +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 +compose: + is_linux: GUEST_OS == 'linux' +filter_by_label: bench +groups: + benchmark_clients: TGROUP.endswith('clients') + lin: is_linux == true +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 options_constructable_test asks ansible to filter it out + # 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', @@ -291,31 +322,31 @@ def test_populate_constructable_templating(inventory, mocker): 'terraform_demo_srv_00', 'bs-windows', ]) - assert set(inventory.inventory.hosts) == set(['terraform_demo_00', 'terraform_demo_01', 'terraform_demo_srv_00']) + assert set(im._inventory.hosts) == set(['terraform_demo_00', 'terraform_demo_01', 'terraform_demo_srv_00']) - host_demo00 = inventory.inventory.get_host('terraform_demo_00') - host_demo01 = inventory.inventory.get_host('terraform_demo_01') - host_demosrv = inventory.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 inventory.inventory.groups - assert 'lin' in inventory.inventory.groups - assert inventory.inventory.groups['benchmark_clients'].hosts == [host_demo00, host_demo01] - assert inventory.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 inventory.inventory.groups - assert 'foo' in inventory.inventory.groups - assert inventory.inventory.groups['bench'].hosts == [host_demo00, host_demo01, host_demosrv] - assert inventory.inventory.groups['serv'].hosts == [host_demosrv] - assert inventory.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 # test `keyed_groups` - assert inventory.inventory.groups['tgroup_bench_clients'].hosts == [host_demo00, host_demo01] - assert inventory.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):