From 8f44288d4a14b2460f38c544dd06a6fa2a10e34c Mon Sep 17 00:00:00 2001 From: Dawid Korzepa Date: Fri, 12 Jul 2024 13:24:07 +0200 Subject: [PATCH] [ISSUE-1186]: cr notes Co-authored-by: korzepadawwid Co-authored-by: mdutka-dell Signed-off-by: Dawid Korzepa --- tests/e2e-test-framework/framework/utils.py | 57 ++++++++++--------- .../tests/test_fake_attach.py | 33 ++--------- 2 files changed, 35 insertions(+), 55 deletions(-) diff --git a/tests/e2e-test-framework/framework/utils.py b/tests/e2e-test-framework/framework/utils.py index 09363fa0a..3797a3821 100644 --- a/tests/e2e-test-framework/framework/utils.py +++ b/tests/e2e-test-framework/framework/utils.py @@ -341,48 +341,49 @@ def get_pod_node_ip(self, pod_name: str, namespace: str) -> str: node = self.core_v1_api.read_node(name=node_name) return node.status.addresses[0].address - def get_events_by_reason( + def get_events_by_reason_for_cr( self, - plural: str, resource_name: str, - namespace: Optional[str] = None, - reason: Optional[str] = None, + reason: str, ) -> List[CoreV1Event]: """ - Retrieves events related to a specific resource by reason. + Retrieves a list of events filtered by the given resource name and reason. Args: - plural (str): The plural name of the resource. resource_name (str): The name of the resource. - namespace: (Optional[str], optional): The namespace of the resource. - reason (Optional[str], optional): The reason for filtering events. Defaults to None. + reason (str): The reason for filtering events. Returns: - List[CoreV1Event]: A list of events related to the resource by reason. + List[CoreV1Event]: A list of Kubernetes CoreV1Event objects. """ - if namespace: - cr = self.custom_objects_api.get_namespaced_custom_object( - const.CR_GROUP, - const.CR_VERSION, - namespace, - plural, - resource_name, - ) - else: - cr = self.custom_objects_api.get_cluster_custom_object( - const.CR_GROUP, const.CR_VERSION, plural, resource_name - ) - uid = cr["metadata"]["uid"] - field_selector = f"involvedObject.uid={uid}" + field_selector = f"involvedObject.name={resource_name},reason={reason}" events_list = self.core_v1_api.list_event_for_all_namespaces( field_selector=field_selector ).items - if reason: - events_list = [e for e in events_list if e.reason == reason] - return events_list + def event_in(self, resource_name: str, reason: str) -> bool: + """ + Checks if an event with the given resource name and reason exists in the Kubernetes API. + + Args: + resource_name (str): The name of the resource. + reason (str): The reason for the event. + + Returns: + bool: True if the event exists, False otherwise. + """ + events = self.get_events_by_reason_for_cr( + resource_name=resource_name, + reason=reason, + ) + if len(events) > 0: + logging.info(f"event {reason} found") + return True + logging.warning(f"event {reason} not found") + return False + def wait_volume( self, name: str, @@ -690,8 +691,8 @@ def recreate_pod(self, name: str, namespace: str) -> V1Pod: logging.info(f"pod {name} is ready") return pod - - def get_pod_node_ip(self, pod_name: str, namespace:str) -> str: + + def get_pod_node_ip(self, pod_name: str, namespace: str) -> str: """ Retrieves the IP address of the node associated with the given pod name and namespace. diff --git a/tests/e2e-test-framework/tests/test_fake_attach.py b/tests/e2e-test-framework/tests/test_fake_attach.py index 1d64c4aea..d3ff4f9d3 100644 --- a/tests/e2e-test-framework/tests/test_fake_attach.py +++ b/tests/e2e-test-framework/tests/test_fake_attach.py @@ -27,7 +27,7 @@ def setup_class( cls.drive_utils = drive_utils_executors cls.sts = STS(cls.namespace, cls.name, cls.replicas) cls.sts.delete() - cls.sts.create(storage_classes=[const.HDD_SC]) + cls.sts.create(storage_classes=[const.SSD_SC]) yield @@ -77,14 +77,10 @@ def test_5808_fake_attach_without_dr(self): ) logging.info(f"drive {drive_name} went {const.STATUS_OFFLINE}") - pod = self.utils.recreate_pod( - name=pod.metadata.name, namespace=self.namespace - ) + pod = self.utils.recreate_pod(pod) volume_name = volume["metadata"]["name"] - assert self.event_in( - plural="volumes", - resource_name=volume_name, - reason=const.FAKE_ATTACH_INVOLVED, + assert self.utils.event_in( + resource_name=volume_name, reason=const.FAKE_ATTACH_INVOLVED ), f"event {const.FAKE_ATTACH_INVOLVED} not found" self.drive_utils[node_ip].restore(host_num=host_num) @@ -95,25 +91,8 @@ def test_5808_fake_attach_without_dr(self): name=drive_name, expected_status=const.STATUS_ONLINE ) - self.utils.recreate_pod( - name=pod.metadata.name, namespace=self.namespace - ) - assert self.event_in( - plural="volumes", + self.utils.recreate_pod(pod) + assert self.utils.event_in( resource_name=volume_name, reason=const.FAKE_ATTACH_CLEARED, ), f"event {const.FAKE_ATTACH_CLEARED} not found" - - def event_in(self, plural: str, resource_name: str, reason: str) -> bool: - events = self.utils.get_events_by_reason( - plural=plural, - resource_name=resource_name, - reason=reason, - namespace=self.namespace, - ) - for event in events: - if event.reason == reason: - logging.info(f"event {reason} found") - return True - logging.warning(f"event {reason} not found") - return False