mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-05-06 13:22:38 +00:00
Update Ansible GitHub workflows
The workflows committed in the first commit were missing the installation of required dependencies and other fixes. Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
44
tests/unit/conftest.py
Normal file
44
tests/unit/conftest.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
import sys
|
||||
from io import BytesIO
|
||||
|
||||
import pytest
|
||||
|
||||
import ansible.module_utils.basic
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.module_utils.common._collections_compat import MutableMapping
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def stdin(mocker, request):
|
||||
old_args = ansible.module_utils.basic._ANSIBLE_ARGS
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
old_argv = sys.argv
|
||||
sys.argv = ["ansible_unittest"]
|
||||
|
||||
if isinstance(request.param, string_types):
|
||||
args = request.param
|
||||
elif isinstance(request.param, MutableMapping):
|
||||
if "ANSIBLE_MODULE_ARGS" not in request.param:
|
||||
request.param = {"ANSIBLE_MODULE_ARGS": request.param}
|
||||
if "_ansible_remote_tmp" not in request.param["ANSIBLE_MODULE_ARGS"]:
|
||||
request.param["ANSIBLE_MODULE_ARGS"]["_ansible_remote_tmp"] = "/tmp"
|
||||
if "_ansible_keep_remote_files" not in request.param["ANSIBLE_MODULE_ARGS"]:
|
||||
request.param["ANSIBLE_MODULE_ARGS"]["_ansible_keep_remote_files"] = False
|
||||
args = json.dumps(request.param)
|
||||
else:
|
||||
raise Exception("Malformed data to the stdin pytest fixture")
|
||||
|
||||
fake_stdin = BytesIO(to_bytes(args, errors="surrogate_or_strict"))
|
||||
mocker.patch("ansible.module_utils.basic.sys.stdin", mocker.MagicMock())
|
||||
mocker.patch("ansible.module_utils.basic.sys.stdin.buffer", fake_stdin)
|
||||
|
||||
yield fake_stdin
|
||||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = old_args
|
||||
sys.argv = old_argv
|
||||
147
tests/unit/modules/test_module_kubevirt_vm.py
Normal file
147
tests/unit/modules/test_module_kubevirt_vm.py
Normal file
@@ -0,0 +1,147 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# 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
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
|
||||
from unittest.mock import patch, ANY
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s import runner
|
||||
from ansible_collections.kubernetes.kubevirt.plugins.modules import kubevirt_vm
|
||||
from ansible_collections.kubernetes.kubevirt.tests.unit.utils.ansible_module_mock import (
|
||||
AnsibleFailJson,
|
||||
AnsibleExitJson,
|
||||
exit_json,
|
||||
fail_json,
|
||||
set_module_args,
|
||||
get_api_client
|
||||
)
|
||||
|
||||
FIXTURE1 = {
|
||||
"apiVersion": "kubevirt.io/v1",
|
||||
"kind": "VirtualMachine",
|
||||
"metadata": {
|
||||
"name": "testvm",
|
||||
"namespace": "default",
|
||||
"labels": {
|
||||
"environment": "staging",
|
||||
"service": "loadbalancer"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"running": True,
|
||||
"template": {
|
||||
"metadata": {
|
||||
"labels": {
|
||||
"environment": "staging",
|
||||
"service": "loadbalancer"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"domain": {
|
||||
"devices": {}
|
||||
},
|
||||
"terminationGracePeriodSeconds": 180
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
METADATA = '''apiVersion: kubevirt.io/v1
|
||||
kind: VirtualMachine
|
||||
metadata:
|
||||
name: "testvm"
|
||||
namespace: "default"
|
||||
labels:
|
||||
environment: staging
|
||||
service: loadbalancer
|
||||
spec:
|
||||
running: True
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
environment: staging
|
||||
service: loadbalancer
|
||||
spec:
|
||||
domain:
|
||||
devices: {}
|
||||
terminationGracePeriodSeconds: 180'''
|
||||
|
||||
FIXTURE2 = {
|
||||
'name': 'testvm',
|
||||
'namespace': 'default',
|
||||
'state': 'present',
|
||||
'labels': {
|
||||
'service': 'loadbalancer',
|
||||
'environment': 'staging'
|
||||
},
|
||||
'api_version': 'kubevirt.io/v1', 'running': True, 'termination_grace_period': 180, 'wait': False, 'wait_sleep': 5, 'wait_timeout': 120, 'force': False,
|
||||
'generate_name': None, 'annotations': None, 'instancetype': None, 'preference': None, 'infer_from_volume': None, 'clear_revision_name': None,
|
||||
'interfaces': None, 'networks': None, 'volumes': None, 'kubeconfig': None, 'context': None, 'host': None, 'api_key': None, 'username': None,
|
||||
'password': None, 'validate_certs': None, 'ca_cert': None, 'client_cert': None, 'client_key': None, 'proxy': None, 'no_proxy': None, 'proxy_headers': None,
|
||||
'persist_config': None, 'impersonate_user': None, 'impersonate_groups': None, 'delete_options': None,
|
||||
'resource_definition': METADATA,
|
||||
'wait_condition': {
|
||||
'type': 'Ready',
|
||||
'status': True
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TestCreateVM(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.mock_module_helper = patch.multiple(
|
||||
basic.AnsibleModule,
|
||||
exit_json=exit_json,
|
||||
fail_json=fail_json
|
||||
)
|
||||
self.mock_module_helper.start()
|
||||
|
||||
self.mock_runner = patch.multiple(
|
||||
runner,
|
||||
get_api_client=get_api_client
|
||||
)
|
||||
self.mock_runner.start()
|
||||
|
||||
# Stop the patch after test execution
|
||||
# like tearDown but executed also when the setup failed
|
||||
self.addCleanup(self.mock_module_helper.stop)
|
||||
self.addCleanup(self.mock_runner.stop)
|
||||
|
||||
def test_module_fail_when_required_args_missing(self):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
set_module_args({})
|
||||
kubevirt_vm.main()
|
||||
|
||||
def test_create(self):
|
||||
set_module_args(
|
||||
{
|
||||
"name": "testvm",
|
||||
"namespace": "default",
|
||||
"state": "present",
|
||||
"labels": {
|
||||
"service": "loadbalancer",
|
||||
"environment": "staging"
|
||||
}
|
||||
}
|
||||
)
|
||||
with patch.object(runner, "perform_action") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
{
|
||||
"method": "create",
|
||||
"changed": True,
|
||||
"result": "success"
|
||||
}
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
kubevirt_vm.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
ANY,
|
||||
FIXTURE1,
|
||||
FIXTURE2,
|
||||
)
|
||||
3
tests/unit/requirements.txt
Normal file
3
tests/unit/requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
pytest
|
||||
PyYAML
|
||||
kubernetes
|
||||
51
tests/unit/utils/ansible_module_mock.py
Normal file
51
tests/unit/utils/ansible_module_mock.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
# This module maock the AnsibleModule class for more information please visite
|
||||
# https://docs.ansible.com/ansible/latest/dev_guide/testing_units_modules.html#module-argument-processing
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils.common.text.converters import to_bytes
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
"""prepare arguments so that they will be picked up during module creation"""
|
||||
args = json.dumps({"ANSIBLE_MODULE_ARGS": args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args)
|
||||
|
||||
|
||||
class AnsibleExitJson(Exception):
|
||||
"""Exception class to be raised by module.exit_json and caught by the test case"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class AnsibleFailJson(Exception):
|
||||
"""Exception class to be raised by module.fail_json and caught by the test case"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def exit_json(*args, **kwargs):
|
||||
"""function to patch over exit_json; package return data into an exception"""
|
||||
if "changed" not in kwargs:
|
||||
kwargs["changed"] = False
|
||||
raise AnsibleExitJson(kwargs)
|
||||
|
||||
|
||||
def fail_json(*args, **kwargs):
|
||||
"""function to patch over fail_json; package return data into an exception"""
|
||||
kwargs["failed"] = True
|
||||
raise AnsibleFailJson(kwargs)
|
||||
|
||||
|
||||
def get_api_client(*args, **kwargs):
|
||||
"""function to patch over get_api_client """
|
||||
pass
|
||||
Reference in New Issue
Block a user