Coverage for ocp_resources/benchmark.py: 0%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-30 10:48 +0200

1from typing import Any, Optional 

2 

3from ocp_resources.utils.constants import NOT_FOUND_ERROR_EXCEPTION_DICT, TIMEOUT_30SEC 

4from ocp_resources.resource import NamespacedResource 

5from timeout_sampler import TimeoutSampler 

6 

7 

8class Benchmark(NamespacedResource): 

9 """ 

10 Benchmark resource 

11 Defined by https://github.com/cloud-bulldozer/benchmark-operator 

12 

13 The benchmark-operator monitors a namespace for `Benchmark` resources. 

14 When a new `Benchmark` is created, the benchmark-operator creates and starts the pods or VMs necessary, 

15 and triggers the benchmark run. 

16 """ 

17 

18 api_group = NamespacedResource.ApiGroup.RIPSAW_CLOUDBULLDOZER_IO 

19 

20 class Status: 

21 NONE: str = "None" # None state is valid for newly created benchmark resources 

22 

23 class Workload: 

24 class Kind: 

25 VM: str = "vm" 

26 POD: str = "pod" 

27 

28 def _wait_for_instance_key(self, parent: str, key: str) -> Any: 

29 """ 

30 Wait for key to exist in parent attribute of instance 

31 

32 Args: 

33 parent (str): An attribute of `self.instance` that should contain key. 

34 key (str): A dictionary entry within parent 

35 

36 Returns: 

37 Any: Value of key if found, otherwise None 

38 """ 

39 samples = TimeoutSampler( 

40 wait_timeout=TIMEOUT_30SEC, 

41 sleep=1, 

42 func=lambda: getattr(self.instance, parent, None), 

43 exceptions_dict=NOT_FOUND_ERROR_EXCEPTION_DICT, 

44 ) 

45 for sample in samples: 

46 if sample: 

47 return sample.get(key) 

48 

49 @property 

50 def uuid(self) -> str: 

51 """ 

52 Returns: 

53 str: UUID string from resource instance 

54 """ 

55 return self._wait_for_instance_key(parent="status", key="uuid") 

56 

57 @property 

58 def suuid(self) -> str: 

59 """ 

60 Returns: 

61 str: (short)UUID string from resource instance 

62 """ 

63 return self._wait_for_instance_key(parent="status", key="suuid") 

64 

65 @property 

66 def workload_kind(self) -> str: 

67 """ 

68 Retrieve the value of spec.workload.args.kind 

69 

70 Not all Benchmarks have a 'kind' defined, this was added for vms. The default is 'pod' 

71 

72 Returns: 

73 str: Value representing workload kind 

74 

75 """ 

76 return self.workload_arg(arg="kind", default="pod") 

77 

78 def workload_arg(self, arg: str, default: Optional[Any] = None) -> Any: 

79 """ 

80 Retrieve the value of spec.workload.args[arg] 

81 

82 To provide a similar usage as .get(), a default can be defined if needed. 

83 

84 Args: 

85 arg (str): Argument to retrieve from spec.workload.args 

86 default (Any): Default value to return if arg is not found in workload args 

87 

88 Returns: 

89 Any: Value of workload arg or 'default' if workload arg does not exist 

90 """ 

91 if (workload := self._wait_for_instance_key(parent="spec", key="workload")) and isinstance(workload, dict): 

92 return workload.get("args", {}).get(arg, default) 

93 return default