mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-05-06 13:22:38 +00:00
tests: Add basic unit tests for inventory plugin
Add a test file for the inventory plugin with some basic unit tests. This file can be used to add more tests in the future. Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
52
tests/unit/plugins/inventory/test_kubevirt.py
Normal file
52
tests/unit/plugins/inventory/test_kubevirt.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2024 Red Hat, Inc.
|
||||
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible_collections.kubevirt.core.plugins.inventory.kubevirt import InventoryModule
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def inventory():
|
||||
return InventoryModule()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"file_name,expected",
|
||||
[
|
||||
("inventory.kubevirt.yml", True),
|
||||
("inventory.kubevirt.yaml", True),
|
||||
("something.kubevirt.yml", True),
|
||||
("something.kubevirt.yaml", True),
|
||||
("inventory.somethingelse.yml", False),
|
||||
("inventory.somethingelse.yaml", False),
|
||||
("something.somethingelse.yml", False),
|
||||
("something.somethingelse.yaml", False),
|
||||
],
|
||||
)
|
||||
def test_verify_file(tmp_path, inventory, file_name, expected):
|
||||
file = tmp_path / file_name
|
||||
file.touch()
|
||||
assert inventory.verify_file(str(file)) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"file_name",
|
||||
[
|
||||
"inventory.kubevirt.yml",
|
||||
"inventory.kubevirt.yaml",
|
||||
"something.kubevirt.yml",
|
||||
"something.kubevirt.yaml",
|
||||
"inventory.somethingelse.yml",
|
||||
"inventory.somethingelse.yaml",
|
||||
"something.somethingelse.yml",
|
||||
"something.somethingelse.yaml",
|
||||
],
|
||||
)
|
||||
def test_verify_file_bad_config(inventory, file_name):
|
||||
assert inventory.verify_file(file_name) is False
|
||||
@@ -21,41 +21,47 @@ from ansible_collections.kubevirt.core.tests.unit.utils.ansible_module_mock impo
|
||||
set_module_args,
|
||||
)
|
||||
|
||||
FIND_ARGS_DEFAULT = {
|
||||
"kind": "VirtualMachine",
|
||||
"api_version": "kubevirt.io/v1",
|
||||
"name": None,
|
||||
"namespace": None,
|
||||
"label_selectors": [],
|
||||
"field_selectors": [],
|
||||
"wait": False,
|
||||
"wait_sleep": 5,
|
||||
"wait_timeout": 120,
|
||||
"condition": {"type": "Ready", "status": True},
|
||||
}
|
||||
|
||||
FIND_ARGS_NAME_NAMESPACE = {
|
||||
"kind": "VirtualMachine",
|
||||
"api_version": "kubevirt.io/v1",
|
||||
"name": "testvm",
|
||||
"namespace": "default",
|
||||
"label_selectors": [],
|
||||
"field_selectors": [],
|
||||
"wait": False,
|
||||
"wait_sleep": 5,
|
||||
"wait_timeout": 120,
|
||||
"condition": {"type": "Ready", "status": True},
|
||||
}
|
||||
@pytest.fixture(scope="module")
|
||||
def find_args_default():
|
||||
return {
|
||||
"kind": "VirtualMachine",
|
||||
"api_version": "kubevirt.io/v1",
|
||||
"name": None,
|
||||
"namespace": None,
|
||||
"label_selectors": [],
|
||||
"field_selectors": [],
|
||||
"wait": False,
|
||||
"wait_sleep": 5,
|
||||
"wait_timeout": 120,
|
||||
"condition": {"type": "Ready", "status": True},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def find_args_name_namespace():
|
||||
return {
|
||||
"kind": "VirtualMachine",
|
||||
"api_version": "kubevirt.io/v1",
|
||||
"name": "testvm",
|
||||
"namespace": "default",
|
||||
"label_selectors": [],
|
||||
"field_selectors": [],
|
||||
"wait": False,
|
||||
"wait_sleep": 5,
|
||||
"wait_timeout": 120,
|
||||
"condition": {"type": "Ready", "status": True},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"module_args,find_args",
|
||||
[
|
||||
({}, FIND_ARGS_DEFAULT),
|
||||
({"name": "testvm", "namespace": "default"}, FIND_ARGS_NAME_NAMESPACE),
|
||||
({}, "find_args_default"),
|
||||
({"name": "testvm", "namespace": "default"}, "find_args_name_namespace"),
|
||||
],
|
||||
)
|
||||
def test_module(monkeypatch, mocker, module_args, find_args):
|
||||
def test_module(request, monkeypatch, mocker, module_args, find_args):
|
||||
monkeypatch.setattr(AnsibleModule, "exit_json", exit_json)
|
||||
monkeypatch.setattr(kubevirt_vm_info, "get_api_client", lambda _: None)
|
||||
|
||||
@@ -70,4 +76,4 @@ def test_module(monkeypatch, mocker, module_args, find_args):
|
||||
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
kubevirt_vm_info.main()
|
||||
find.assert_called_once_with(**find_args)
|
||||
find.assert_called_once_with(**request.getfixturevalue(find_args))
|
||||
|
||||
Reference in New Issue
Block a user