mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-05-08 06:12:39 +00:00
Merge pull request #24 from 0xFelix/info_module
kubevirt_vm_info: Add info module for VirtualMachines
This commit is contained in:
68
tests/integration/targets/kubevirt_vm_info/playbook.yml
Normal file
68
tests/integration/targets/kubevirt_vm_info/playbook.yml
Normal file
@@ -0,0 +1,68 @@
|
||||
---
|
||||
- name: Create VM
|
||||
connection: local
|
||||
gather_facts: false
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: Create a VirtualMachine
|
||||
kubevirt.core.kubevirt_vm:
|
||||
name: testvm
|
||||
namespace: default
|
||||
instancetype:
|
||||
name: u1.small
|
||||
preference:
|
||||
name: centos.9.stream
|
||||
spec:
|
||||
domain:
|
||||
devices: {}
|
||||
volumes:
|
||||
- containerDisk:
|
||||
image: quay.io/containerdisks/centos-stream:9
|
||||
name: containerdisk
|
||||
state: present
|
||||
wait: true
|
||||
wait_timeout: 600
|
||||
|
||||
- name: Describe created VM
|
||||
connection: local
|
||||
gather_facts: false
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: Describe a VirtualMachine
|
||||
kubevirt.core.kubevirt_vm_info:
|
||||
name: testvm
|
||||
namespace: default
|
||||
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 VirtualMachine
|
||||
kubevirt.core.kubevirt_vm:
|
||||
name: testvm
|
||||
namespace: default
|
||||
state: absent
|
||||
wait: true
|
||||
|
||||
- name: Verify VM deletion
|
||||
connection: local
|
||||
gather_facts: false
|
||||
hosts: localhost
|
||||
tasks:
|
||||
- name: Delete a VirtualMachine
|
||||
kubevirt.core.kubevirt_vm:
|
||||
name: testvm
|
||||
namespace: default
|
||||
state: absent
|
||||
register: delete
|
||||
- name: Assert module reported no changes
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- not delete.changed
|
||||
10
tests/integration/targets/kubevirt_vm_info/runme.sh
Executable file
10
tests/integration/targets/kubevirt_vm_info/runme.sh
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eux
|
||||
set -o pipefail
|
||||
|
||||
{
|
||||
export ANSIBLE_CALLBACKS_ENABLED=profile_tasks
|
||||
ansible-playbook playbook.yml "$@"
|
||||
} || {
|
||||
exit 1
|
||||
}
|
||||
93
tests/unit/modules/test_module_kubevirt_vm_info.py
Normal file
93
tests/unit/modules/test_module_kubevirt_vm_info.py
Normal file
@@ -0,0 +1,93 @@
|
||||
# -*- 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
|
||||
|
||||
from unittest import TestCase
|
||||
from unittest.mock import patch
|
||||
|
||||
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_vm_info,
|
||||
)
|
||||
from ansible_collections.kubevirt.core.tests.unit.utils.ansible_module_mock import (
|
||||
AnsibleExitJson,
|
||||
exit_json,
|
||||
fail_json,
|
||||
set_module_args,
|
||||
get_api_client,
|
||||
)
|
||||
|
||||
FIXTURE1 = {
|
||||
"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},
|
||||
}
|
||||
|
||||
FIXTURE2 = {
|
||||
"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},
|
||||
}
|
||||
|
||||
|
||||
class TestDescribeVM(TestCase):
|
||||
def setUp(self):
|
||||
self.mock_module_helper = patch.multiple(
|
||||
AnsibleModule, exit_json=exit_json, fail_json=fail_json
|
||||
)
|
||||
self.mock_module_helper.start()
|
||||
|
||||
self.mock_main = patch.multiple(kubevirt_vm_info, get_api_client=get_api_client)
|
||||
self.mock_main.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_main.stop)
|
||||
|
||||
def run_module(self, fixture):
|
||||
with patch.object(K8sService, "find") as mock_find_command:
|
||||
mock_find_command.return_value = {
|
||||
"api_found": True,
|
||||
"failed": False,
|
||||
"resources": [],
|
||||
} # successful execution
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
kubevirt_vm_info.main()
|
||||
mock_find_command.assert_called_once_with(
|
||||
**fixture,
|
||||
)
|
||||
|
||||
def test_describe_without_args(self):
|
||||
set_module_args({})
|
||||
self.run_module(FIXTURE1)
|
||||
|
||||
def test_describe_with_args(self):
|
||||
set_module_args(
|
||||
{
|
||||
"name": "testvm",
|
||||
"namespace": "default",
|
||||
}
|
||||
)
|
||||
self.run_module(FIXTURE2)
|
||||
Reference in New Issue
Block a user