def test_pvc_creation_wait(self, mock_create_stream, _phase, _exception): _kind = 'PersistentVolumeClaim' # Desired state: args = dict(state='present', kind=_kind, wait=True, name='testvmi', namespace='vms', api_version='v1') set_module_args(args) # Mock pre-change state: Resource.get.return_value = None # Resource does NOT initially exist in cluster resource_args = dict(kind=_kind, **RESOURCE_DEFAULT_ARGS) K8sAnsibleMixin.find_resource.return_value = Resource(**resource_args) # Mock post-change state: stream_obj = dict( status=dict(phase=_phase), metadata=dict( annotations={'cdi.kubevirt.io/storage.pod.phase': 'Succeeded' }), method='create', changed=True, ) mock_watcher = MagicMock() mock_create_stream.return_value = (mock_watcher, [dict(object=stream_obj)]) # Actual test: with pytest.raises(_exception) as result: mymodule.KubeVirtVM().execute_module() if _exception is AnsibleExitJson: assert result.value[0]['method'] == 'create' and result.value[0][ 'changed'] == True
def test_resource_absent(self, _kind, _wait): # Desired state: args = dict(state='absent', kind=_kind, wait=_wait, name='testvmi', namespace='vms', api_version='v1') set_module_args(args) # Current state (mock): Resource.get.return_value = None # Resource does NOT exist in cluster resource_args = dict(kind=_kind, **RESOURCE_DEFAULT_ARGS) K8sAnsibleMixin.find_resource.return_value = Resource(**resource_args) # Actual test: with pytest.raises(AnsibleExitJson) as result: mymodule.KubeVirtVM().execute_module() assert result.value[0]['method'] == 'delete' and result.value[0][ 'changed'] == False
def test_stream_creation(self, mock_watch): _kind = 'PersistentVolumeClaim' # Desired state: args = dict(state='present', kind=_kind, wait=True, name='testvmi', namespace='vms', api_version='v1') set_module_args(args) # Mock pre-change state: Resource.get.return_value = None # Resource does NOT initially exist in cluster resource_args = dict(kind=_kind, **RESOURCE_DEFAULT_ARGS) K8sAnsibleMixin.find_resource.return_value = Resource(**resource_args) # Actual test: mock_watch.side_effect = KubernetesException("Test", value=42) with pytest.raises(AnsibleFailJson) as result: mymodule.KubeVirtVM().execute_module()