diff --git a/test/units/modules/cloud/kubevirt/test_kubevirt_rs.py b/test/units/modules/cloud/kubevirt/test_kubevirt_rs.py index 1df8b307826..970e09724ba 100644 --- a/test/units/modules/cloud/kubevirt/test_kubevirt_rs.py +++ b/test/units/modules/cloud/kubevirt/test_kubevirt_rs.py @@ -29,6 +29,8 @@ def test_scale_rs_nowait(_replicas, _changed): res_inst = openshiftdynamic.ResourceInstance('', dict(kind=KIND, metadata={'name': _name}, spec={'replicas': 2})) openshiftdynamic.Resource.get.return_value = res_inst openshiftdynamic.Resource.search.return_value = [res_inst] + + # Final state, after patching the object KubernetesRawModule.patch_resource.return_value = dict(kind=KIND, metadata={'name': _name}, spec={'replicas': _replicas}), None @@ -38,3 +40,37 @@ def test_scale_rs_nowait(_replicas, _changed): # Verify result: assert result.value['changed'] == _changed + + +@pytest.mark.usefixtures("base_fixture") +@pytest.mark.parametrize("_replicas, _success", ((1, False), + (2, False), + (5, True),)) +def test_scale_rs_wait(_replicas, _success): + _name = 'test-rs' + # Desired state: + args = dict(name=_name, namespace='vms', replicas=5, wait=True) + set_module_args(args) + + # Mock pre-change state: + resource_args = dict(kind=KIND, **RESOURCE_DEFAULT_ARGS) + mymodule.KubeVirtVMIRS.find_supported_resource.return_value = openshiftdynamic.Resource(**resource_args) + res_inst = openshiftdynamic.ResourceInstance('', dict(kind=KIND, metadata={'name': _name}, spec={'replicas': 2})) + openshiftdynamic.Resource.get.return_value = res_inst + openshiftdynamic.Resource.search.return_value = [res_inst] + + # ~Final state, after patching the object (`replicas` match desired state) + KubernetesRawModule.patch_resource.return_value = dict(kind=KIND, name=_name, metadata={'name': _name}, + spec={'replicas': 5}), None + + # Final final state, as returned by resource.watch() + final_obj = dict(metadata=dict(name=_name), status=dict(readyReplicas=_replicas), **resource_args) + event = openshiftdynamic.ResourceInstance(None, final_obj) + openshiftdynamic.Resource.watch.return_value = [dict(object=event)] + + # Run code: + with pytest.raises(Exception) as result: + mymodule.KubeVirtVMIRS().execute_module() + + # Verify result: + assert result.value['success'] == _success diff --git a/test/units/modules/cloud/kubevirt/test_kubevirt_vm.py b/test/units/modules/cloud/kubevirt/test_kubevirt_vm.py index 6267d21e7ba..0a2a14c7b89 100644 --- a/test/units/modules/cloud/kubevirt/test_kubevirt_vm.py +++ b/test/units/modules/cloud/kubevirt/test_kubevirt_vm.py @@ -12,7 +12,7 @@ KIND = 'VirtulMachine' @pytest.mark.usefixtures("base_fixture") -def test_create_vm_with_multus(): +def test_create_vm_with_multus_nowait(): # Desired state: args = dict( state='present', name='testvm', @@ -64,3 +64,47 @@ def test_vm_is_absent(_wait): 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 + + +@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 diff --git a/test/units/utils/kubevirt_fixtures.py b/test/units/utils/kubevirt_fixtures.py index 744b6ca9020..ebcf5af9458 100644 --- a/test/units/utils/kubevirt_fixtures.py +++ b/test/units/utils/kubevirt_fixtures.py @@ -36,12 +36,14 @@ class AnsibleFailJson(Exception): def exit_json(*args, **kwargs): + kwargs['success'] = True if 'changed' not in kwargs: kwargs['changed'] = False raise AnsibleExitJson(**kwargs) def fail_json(*args, **kwargs): + kwargs['success'] = False raise AnsibleFailJson(**kwargs) @@ -59,6 +61,7 @@ def base_fixture(monkeypatch): openshift.dynamic.Resource.delete = MagicMock() openshift.dynamic.Resource.patch = MagicMock() openshift.dynamic.Resource.search = MagicMock() + openshift.dynamic.Resource.watch = MagicMock() # Globally mock some methods, since all tests will use this KubernetesRawModule.patch_resource = MagicMock() KubernetesRawModule.patch_resource.return_value = ({}, None)