2019-02-13 11:17:01 +01:00
|
|
|
import pytest
|
|
|
|
|
2019-06-25 12:06:14 +02:00
|
|
|
openshiftdynamic = pytest.importorskip("openshift.dynamic")
|
2019-02-13 11:17:01 +01:00
|
|
|
|
2019-06-25 12:06:14 +02:00
|
|
|
from units.modules.utils import set_module_args
|
|
|
|
from units.utils.kubevirt_fixtures import base_fixture, RESOURCE_DEFAULT_ARGS, AnsibleExitJson
|
2019-02-13 11:17:01 +01:00
|
|
|
|
2019-06-25 12:06:14 +02:00
|
|
|
from ansible.module_utils.kubevirt import KubeVirtRawModule
|
2019-02-13 11:17:01 +01:00
|
|
|
from ansible.modules.cloud.kubevirt import kubevirt_vm as mymodule
|
|
|
|
|
|
|
|
KIND = 'VirtulMachine'
|
|
|
|
|
|
|
|
|
2019-06-25 12:06:14 +02:00
|
|
|
@pytest.mark.usefixtures("base_fixture")
|
2019-07-05 12:35:33 +02:00
|
|
|
def test_create_vm_with_multus_nowait():
|
2019-06-13 11:07:18 +02:00
|
|
|
# Desired state:
|
2019-02-13 11:17:01 +01:00
|
|
|
args = dict(
|
|
|
|
state='present', name='testvm',
|
2019-06-13 11:07:18 +02:00
|
|
|
namespace='vms',
|
2019-02-13 11:17:01 +01:00
|
|
|
interfaces=[
|
|
|
|
{'bridge': {}, 'name': 'default', 'network': {'pod': {}}},
|
|
|
|
{'bridge': {}, 'name': 'mynet', 'network': {'multus': {'networkName': 'mynet'}}},
|
|
|
|
],
|
|
|
|
wait=False,
|
|
|
|
)
|
|
|
|
set_module_args(args)
|
|
|
|
|
2019-06-13 11:07:18 +02:00
|
|
|
# State as "returned" by the "k8s cluster":
|
2019-02-13 11:17:01 +01:00
|
|
|
resource_args = dict(kind=KIND, **RESOURCE_DEFAULT_ARGS)
|
2019-06-13 11:07:18 +02:00
|
|
|
KubeVirtRawModule.find_supported_resource.return_value = openshiftdynamic.Resource(**resource_args)
|
2019-06-25 12:06:14 +02:00
|
|
|
openshiftdynamic.Resource.get.return_value = None # Object doesn't exist in the cluster
|
2019-02-13 11:17:01 +01:00
|
|
|
|
2019-06-25 12:06:14 +02:00
|
|
|
# Run code:
|
2019-02-13 11:17:01 +01:00
|
|
|
with pytest.raises(AnsibleExitJson) as result:
|
|
|
|
mymodule.KubeVirtVM().execute_module()
|
2019-06-25 12:06:14 +02:00
|
|
|
|
|
|
|
# Verify result:
|
2019-02-13 11:17:01 +01:00
|
|
|
assert result.value['changed']
|
2019-06-13 11:07:18 +02:00
|
|
|
assert result.value['method'] == 'create'
|
2019-02-13 11:17:01 +01:00
|
|
|
|
|
|
|
|
2019-06-25 12:06:14 +02:00
|
|
|
@pytest.mark.usefixtures("base_fixture")
|
2019-02-13 11:17:01 +01:00
|
|
|
@pytest.mark.parametrize("_wait", (False, True))
|
2019-06-25 12:06:14 +02:00
|
|
|
def test_vm_is_absent(_wait):
|
2019-02-13 11:17:01 +01:00
|
|
|
# Desired state:
|
|
|
|
args = dict(
|
|
|
|
state='absent', name='testvmi',
|
2019-06-13 11:07:18 +02:00
|
|
|
namespace='vms',
|
2019-02-13 11:17:01 +01:00
|
|
|
wait=_wait,
|
|
|
|
)
|
|
|
|
set_module_args(args)
|
|
|
|
|
2019-06-13 11:07:18 +02:00
|
|
|
# State as "returned" by the "k8s cluster":
|
2019-02-13 11:17:01 +01:00
|
|
|
resource_args = dict(kind=KIND, **RESOURCE_DEFAULT_ARGS)
|
2019-06-13 11:07:18 +02:00
|
|
|
KubeVirtRawModule.find_supported_resource.return_value = openshiftdynamic.Resource(**resource_args)
|
2019-06-25 12:06:14 +02:00
|
|
|
openshiftdynamic.Resource.get.return_value = None # Object doesn't exist in the cluster
|
2019-02-13 11:17:01 +01:00
|
|
|
|
2019-06-25 12:06:14 +02:00
|
|
|
# Run code:
|
2019-02-13 11:17:01 +01:00
|
|
|
with pytest.raises(AnsibleExitJson) as result:
|
|
|
|
mymodule.KubeVirtVM().execute_module()
|
2019-06-25 12:06:14 +02:00
|
|
|
|
|
|
|
# Verify result:
|
2019-06-13 11:07:18 +02:00
|
|
|
assert not result.value['kubevirt_vm']
|
2019-06-25 12:06:14 +02:00
|
|
|
assert result.value['method'] == 'delete'
|
|
|
|
# Note: nothing actually gets deleted, as we mock that there's not object in the cluster present,
|
|
|
|
# so if the method changes to something other than 'delete' at some point, that's fine
|
2019-07-05 12:35:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("base_fixture")
|
|
|
|
def test_vmpreset_create():
|
|
|
|
KIND = 'VirtulMachineInstancePreset'
|
|
|
|
# Desired state:
|
|
|
|
args = dict(state='present', name='testvmipreset', namespace='vms', memory='1024Mi', wait=False)
|
|
|
|
set_module_args(args)
|
|
|
|
|
|
|
|
# State as "returned" by the "k8s cluster":
|
|
|
|
resource_args = dict(kind=KIND, **RESOURCE_DEFAULT_ARGS)
|
|
|
|
KubeVirtRawModule.find_supported_resource.return_value = openshiftdynamic.Resource(**resource_args)
|
|
|
|
openshiftdynamic.Resource.get.return_value = None # Object doesn't exist in the cluster
|
|
|
|
|
|
|
|
# Run code:
|
|
|
|
with pytest.raises(AnsibleExitJson) as result:
|
|
|
|
mymodule.KubeVirtVM().execute_module()
|
|
|
|
|
|
|
|
# Verify result:
|
|
|
|
assert result.value['changed']
|
|
|
|
assert result.value['method'] == 'create'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("base_fixture")
|
|
|
|
def test_vmpreset_is_absent():
|
|
|
|
KIND = 'VirtulMachineInstancePreset'
|
|
|
|
# Desired state:
|
|
|
|
args = dict(state='absent', name='testvmipreset', namespace='vms')
|
|
|
|
set_module_args(args)
|
|
|
|
|
|
|
|
# State as "returned" by the "k8s cluster":
|
|
|
|
resource_args = dict(kind=KIND, **RESOURCE_DEFAULT_ARGS)
|
|
|
|
KubeVirtRawModule.find_supported_resource.return_value = openshiftdynamic.Resource(**resource_args)
|
|
|
|
openshiftdynamic.Resource.get.return_value = None # Object doesn't exist in the cluster
|
|
|
|
|
|
|
|
# Run code:
|
|
|
|
with pytest.raises(AnsibleExitJson) as result:
|
|
|
|
mymodule.KubeVirtVM().execute_module()
|
|
|
|
|
|
|
|
# Verify result:
|
|
|
|
assert not result.value['kubevirt_vm']
|
|
|
|
assert result.value['method'] == 'delete'
|
|
|
|
# Note: nothing actually gets deleted, as we mock that there's not object in the cluster present,
|
|
|
|
# so if the method changes to something other than 'delete' at some point, that's fine
|