mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-05-08 06:12:39 +00:00
feat: Add kubevirt_vmi_info module
Add the kubevirt_vmi_info module which allows to fetch information about VirtualMachineInstance(s). Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
13
tests/integration/targets/kubevirt_vmi_info/generate.yml
Normal file
13
tests/integration/targets/kubevirt_vmi_info/generate.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
---
|
||||
- name: Generate test files
|
||||
connection: local
|
||||
gather_facts: false
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: Generate test files
|
||||
ansible.builtin.template:
|
||||
src: "{{ item }}.yml.j2"
|
||||
dest: "{{ item }}.yml"
|
||||
mode: "0644"
|
||||
with_items:
|
||||
- playbook
|
||||
67
tests/integration/targets/kubevirt_vmi_info/playbook.yml.j2
Normal file
67
tests/integration/targets/kubevirt_vmi_info/playbook.yml.j2
Normal file
@@ -0,0 +1,67 @@
|
||||
---
|
||||
- name: Create VM
|
||||
connection: local
|
||||
gather_facts: false
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: Create a VM
|
||||
kubevirt.core.kubevirt_vm:
|
||||
name: testvm
|
||||
namespace: {{ NAMESPACE }}
|
||||
instancetype:
|
||||
name: u1.small
|
||||
preference:
|
||||
name: centos.stream9
|
||||
spec:
|
||||
domain:
|
||||
devices: {}
|
||||
volumes:
|
||||
- containerDisk:
|
||||
image: quay.io/containerdisks/centos-stream:9
|
||||
name: containerdisk
|
||||
wait: true
|
||||
wait_timeout: 600
|
||||
|
||||
- name: Describe created VMI
|
||||
connection: local
|
||||
gather_facts: false
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: Describe a VMI
|
||||
kubevirt.core.kubevirt_vmi_info:
|
||||
name: testvm
|
||||
namespace: {{ NAMESPACE }}
|
||||
register: describe
|
||||
- name: Assert module reported no changes
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- not describe.changed
|
||||
- describe.resources | length == 1
|
||||
|
||||
- name: Delete VM
|
||||
connection: local
|
||||
gather_facts: false
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: Delete a VM
|
||||
kubevirt.core.kubevirt_vm:
|
||||
name: testvm
|
||||
namespace: {{ NAMESPACE }}
|
||||
state: absent
|
||||
wait: true
|
||||
|
||||
- name: Verify VM deletion
|
||||
connection: local
|
||||
gather_facts: false
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: Delete a VM
|
||||
kubevirt.core.kubevirt_vm:
|
||||
name: testvm
|
||||
namespace: {{ NAMESPACE }}
|
||||
state: absent
|
||||
register: delete
|
||||
- name: Assert module reported no changes
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- not delete.changed
|
||||
19
tests/integration/targets/kubevirt_vmi_info/runme.sh
Executable file
19
tests/integration/targets/kubevirt_vmi_info/runme.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eux
|
||||
|
||||
export ANSIBLE_CALLBACKS_ENABLED=ansible.posix.profile_tasks
|
||||
|
||||
NAMESPACE="test-kubevirt-vmi-info-$(tr -dc '[:lower:]' < /dev/urandom | head -c 5)"
|
||||
|
||||
cleanup() {
|
||||
ansible localhost -m kubernetes.core.k8s -a "name=${NAMESPACE} api_version=v1 kind=Namespace state=absent"
|
||||
rm -rf playbook.yml
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# Prepare the test environment
|
||||
ansible localhost -m kubernetes.core.k8s -a "name=${NAMESPACE} api_version=v1 kind=Namespace state=present"
|
||||
ansible-playbook -e "NAMESPACE=${NAMESPACE}" generate.yml
|
||||
|
||||
# Run the tests
|
||||
ansible-playbook playbook.yml "$@"
|
||||
@@ -1,3 +1,4 @@
|
||||
plugins/inventory/kubevirt.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm_info.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vmi_info.py validate-modules:missing-gplv3-license
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
plugins/inventory/kubevirt.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm_info.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vmi_info.py validate-modules:missing-gplv3-license
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
plugins/inventory/kubevirt.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm_info.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vmi_info.py validate-modules:missing-gplv3-license
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
plugins/inventory/kubevirt.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm_info.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vmi_info.py validate-modules:missing-gplv3-license
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
plugins/inventory/kubevirt.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vm_info.py validate-modules:missing-gplv3-license
|
||||
plugins/modules/kubevirt_vmi_info.py validate-modules:missing-gplv3-license
|
||||
|
||||
81
tests/unit/plugins/modules/test_kubevirt_vmi_info.py
Normal file
81
tests/unit/plugins/modules/test_kubevirt_vmi_info.py
Normal file
@@ -0,0 +1,81 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2024 Red Hat, Inc.
|
||||
# Apache License 2.0 (see LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
|
||||
K8sService,
|
||||
)
|
||||
from ansible_collections.kubevirt.core.plugins.modules import (
|
||||
kubevirt_vmi_info,
|
||||
)
|
||||
from ansible_collections.kubevirt.core.plugins.module_utils import (
|
||||
info,
|
||||
)
|
||||
from ansible_collections.kubevirt.core.tests.unit.utils.ansible_module_mock import (
|
||||
AnsibleExitJson,
|
||||
exit_json,
|
||||
set_module_args,
|
||||
)
|
||||
|
||||
FIND_ARGS_DEFAULT = {
|
||||
"kind": "VirtualMachineInstance",
|
||||
"api_version": "kubevirt.io/v1",
|
||||
"name": None,
|
||||
"namespace": None,
|
||||
"label_selectors": [],
|
||||
"field_selectors": [],
|
||||
"wait": None,
|
||||
"wait_sleep": 5,
|
||||
"wait_timeout": 120,
|
||||
"condition": {"type": "Ready", "status": True},
|
||||
}
|
||||
|
||||
FIND_ARGS_NAME_NAMESPACE = FIND_ARGS_DEFAULT | {
|
||||
"name": "testvm",
|
||||
"namespace": "default",
|
||||
}
|
||||
|
||||
FIND_ARGS_LABEL_SELECTOR = FIND_ARGS_DEFAULT | {
|
||||
"label_selectors": ["app=test"],
|
||||
}
|
||||
|
||||
FIND_ARGS_FIELD_SELECTOR = FIND_ARGS_DEFAULT | {
|
||||
"field_selectors": ["app=test"],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"module_args,find_args",
|
||||
[
|
||||
({}, FIND_ARGS_DEFAULT),
|
||||
({"name": "testvm", "namespace": "default"}, FIND_ARGS_NAME_NAMESPACE),
|
||||
({"label_selectors": "app=test"}, FIND_ARGS_LABEL_SELECTOR),
|
||||
({"field_selectors": "app=test"}, FIND_ARGS_FIELD_SELECTOR),
|
||||
],
|
||||
)
|
||||
def test_module(mocker, module_args, find_args):
|
||||
mocker.patch.object(AnsibleModule, "exit_json", exit_json)
|
||||
mocker.patch.object(info, "get_api_client")
|
||||
|
||||
find = mocker.patch.object(
|
||||
K8sService,
|
||||
"find",
|
||||
return_value={
|
||||
"api_found": True,
|
||||
"failed": False,
|
||||
"resources": [],
|
||||
},
|
||||
)
|
||||
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
set_module_args(module_args)
|
||||
kubevirt_vmi_info.main()
|
||||
|
||||
find.assert_called_once_with(**find_args)
|
||||
Reference in New Issue
Block a user