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:
Felix Matouschek
2023-07-17 14:20:39 +02:00
parent e1499d4aae
commit 68f5cc8e33
16 changed files with 505 additions and 40 deletions

18
tests/.kubeconfig Normal file
View File

@@ -0,0 +1,18 @@
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: http://localhost:12345
name: development
contexts:
- context:
cluster: development
user: developer
name: dev-frontend
current-context: dev-frontend
kind: Config
preferences: {}
users:
- name: developer
user:
token: ZDNffLzSlp8aSS0fht_tRnPMTOjxqgJGCyi_iy0ecUw

View File

@@ -0,0 +1,2 @@
[testgroup]
testhost ansible_connection="local" ansible_pipelining="yes" ansible_python_interpreter="/home/guido/Development/virtualenv/2.15/bin/python"

View File

View File

View File

View File

View File

View File

View File

44
tests/unit/conftest.py Normal file
View 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

View 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,
)

View File

@@ -0,0 +1,3 @@
pytest
PyYAML
kubernetes

View 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