Coverage for ocp_resources/virtual_machine_snapshot.py: 0%
33 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 10:48 +0200
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 10:48 +0200
1# -*- coding: utf-8 -*-
3from kubernetes.dynamic.exceptions import ResourceNotFoundError
5from ocp_resources.utils.constants import PROTOCOL_ERROR_EXCEPTION_DICT, TIMEOUT_4MINUTES
6from ocp_resources.resource import NamespacedResource
7from timeout_sampler import TimeoutSampler, TimeoutWatch
8from ocp_resources.virtual_machine import VirtualMachine
11class VirtualMachineSnapshot(NamespacedResource):
12 """
13 VirtualMachineSnapshot object.
14 """
16 api_group = NamespacedResource.ApiGroup.SNAPSHOT_KUBEVIRT_IO
18 def __init__(
19 self,
20 name=None,
21 namespace=None,
22 vm_name=None,
23 client=None,
24 teardown=True,
25 yaml_file=None,
26 delete_timeout=TIMEOUT_4MINUTES,
27 **kwargs,
28 ):
29 super().__init__(
30 name=name,
31 namespace=namespace,
32 client=client,
33 teardown=teardown,
34 yaml_file=yaml_file,
35 delete_timeout=delete_timeout,
36 **kwargs,
37 )
38 self.vm_name = vm_name
40 def to_dict(self) -> None:
41 super().to_dict()
42 if not self.kind_dict and not self.yaml_file:
43 spec = self.res.setdefault("spec", {})
44 spec.setdefault("source", {})["apiGroup"] = NamespacedResource.ApiGroup.KUBEVIRT_IO
45 spec["source"]["kind"] = VirtualMachine.kind
46 spec["source"]["name"] = self.vm_name
48 def wait_ready_to_use(self, status=True, timeout=TIMEOUT_4MINUTES):
49 """
50 Wait for VirtualMachineSnapshot to be in readyToUse status
52 Args:
53 status: Expected status: True for a ready to use VirtualMachineSnapshot, False otherwise.
54 timeout (int): Time to wait for the resource.
56 Raises:
57 TimeoutExpiredError: If timeout reached.
58 """
59 self.logger.info(f"Wait for {self.kind} {self.name} status to be {'' if status else 'not '}ready to use")
61 timeout_watcher = TimeoutWatch(timeout=timeout)
62 for sample in TimeoutSampler(
63 wait_timeout=timeout,
64 sleep=1,
65 func=lambda: self.exists,
66 ):
67 if sample:
68 break
70 samples = TimeoutSampler(
71 wait_timeout=timeout_watcher.remaining_time(),
72 sleep=1,
73 exceptions_dict=PROTOCOL_ERROR_EXCEPTION_DICT,
74 func=lambda: self.instance.get("status", {}).get("readyToUse") == status,
75 )
76 for sample in samples:
77 if sample:
78 return
80 def wait_snapshot_done(self, timeout=TIMEOUT_4MINUTES):
81 """
82 Wait for the the snapshot to be done. This check 2 parameters, the snapshot status to be readyToUse
83 and the VM status snapshotInProgress to be None.
85 Args:
86 timeout (int): Time to wait.
88 Raises:
89 TimeoutExpiredError: If timeout reached.
90 """
91 self.wait_ready_to_use(timeout=timeout)
93 vm = VirtualMachine(
94 client=self.client,
95 namespace=self.namespace,
96 name=self.vm_name,
97 )
99 if vm.exists:
100 return vm.wait_for_status_none(status="snapshotInProgress", timeout=timeout)
101 raise ResourceNotFoundError(f"VirtualMachine: {vm.name} not found")