Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a new test case for reclaim space job and cronjob on rwop pvc #10096

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions ocs_ci/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5057,3 +5057,67 @@ def upgrade_multus_holder_design():
delete_csi_holder_pods()
delete_csi_holder_daemonsets()
verify_csi_holder_pods_do_not_exist()


def wait_for_reclaim_space_cronjob(reclaim_space_cron_job, schedule):
"""
Wait for reclaim space cronjbo

Args:
reclaim_space_cron_job (obj): The reclaim space cron job
schedule (str): Reclaim space cron job schedule

Raises:
UnexpectedBehaviour: In case reclaim space cron job doesn't reach the desired state
"""

try:
for reclaim_space_cron_job_yaml in TimeoutSampler(
timeout=120, sleep=5, func=reclaim_space_cron_job.get
):
result = reclaim_space_cron_job_yaml["spec"]["schedule"]
if result == f"@{schedule}":
logger.info(
f"ReclaimSpaceCronJob {reclaim_space_cron_job.name} succeeded"
)
break
else:
logger.info(
f"Waiting for the @{schedule} result of the ReclaimSpaceCronJob {reclaim_space_cron_job.name}. "
f"Present value of result is {result}"
)
except TimeoutExpiredError:
raise UnexpectedBehaviour(
f"ReclaimSpaceJob {reclaim_space_cron_job.name} is not successful. "
f"Yaml output: {reclaim_space_cron_job.get()}"
)


def wait_for_reclaim_space_job(reclaim_space_job):
"""
Wait for reclaim space cronjbo

Args:
reclaim_space_job (obj): The reclaim space job

Raises:
UnexpectedBehaviour: In case reclaim space job doesn't reach the Succeeded state
"""

try:
for reclaim_space_job_yaml in TimeoutSampler(
timeout=120, sleep=5, func=reclaim_space_job.get
):
result = reclaim_space_job_yaml.get("status", {}).get("result")
if result == "Succeeded":
logger.info(f"ReclaimSpaceJob {reclaim_space_job.name} succeeded")
break
else:
logger.info(
f"Waiting for the Succeeded result of the ReclaimSpaceJob {reclaim_space_job.name}. "
f"Present value of result is {result}"
)
except TimeoutExpiredError:
raise UnexpectedBehaviour(
f"ReclaimSpaceJob {reclaim_space_job.name} is not successful. Yaml output: {reclaim_space_job.get()}"
)
Comment on lines +5062 to +5123
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the 2 functions are almost identical. Can you club them to 1 function and call them with a parameter to distinguish between reclaim_space_cron_job_yaml and reclaim_space_job_yaml?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#10111 - a Github issue was opened for addressing your comment later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these 2 methods look visually identical but the content is somewhat different, the same as a subject of "reclaim space" action, since it does not look good, and does not help in readability, I would not suggest merging them.

For reference, the one that is merged can look like:

def wait_for_reclaim_space_job(reclaim_space_job, job_type="cronjob", schedule=None):
    """
    Wait for reclaim space job to reach the desired state
    Args:
        reclaim_space_job (obj): The reclaim space job object
        job_type (str): Type of the job, either "cronjob" or "job"
        schedule (str, optional): Reclaim space cron job schedule if job_type is "cronjob"
    Raises:
        UnexpectedBehaviour: In case reclaim space job doesn't reach the desired state
    """

    try:
        for reclaim_space_job_yaml in TimeoutSampler(
            timeout=120, sleep=5, func=reclaim_space_job.get
        ):
            if job_type == "cronjob":
                if schedule is None:
                    raise ValueError("Schedule must be provided for cronjob type")
                result = reclaim_space_job_yaml["spec"]["schedule"]
                desired_state = f"@{schedule}"
                success_message = f"ReclaimSpaceCronJob {reclaim_space_job.name} succeeded"
            else:
                result = reclaim_space_job_yaml.get("status", {}).get("result")
                desired_state = "Succeeded"
                success_message = f"ReclaimSpaceJob {reclaim_space_job.name} succeeded"
            
            if result == desired_state:
                logger.info(success_message)
                break
            else:
                logger.info(
                    f"Waiting for the {desired_state} result of the {job_type} {reclaim_space_job.name}. "
                    f"Present value of result is {result}"
                )
    except TimeoutExpiredError:
        raise UnexpectedBehaviour(
            f"{job_type} {reclaim_space_job.name} is not successful. "
            f"Yaml output: {reclaim_space_job.get()}"
        ) 

13 changes: 13 additions & 0 deletions tests/functional/pv/pv_services/test_rwop_pvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,16 @@ def test_rwop_create_pod_on_different_node(self, pod_factory, interface):
f"Pod {self.pod_obj.name} using RWOP pvc {self.pvc_obj.name} is not in Pending state"
f"Pod {second_pod_obj.name} using RWOP pvc {self.pvc_obj.name} is not in Pending state"
)

def test_rwop_pvc_reclaim_space(self):
"""
Test to verify creation of reclaim space job and reclaim space cron job on RWOP pvc
"""

schedule = "weekly"
reclaim_space_job = self.pvc_obj.create_reclaim_space_cronjob(schedule)
helpers.wait_for_reclaim_space_cronjob(reclaim_space_job, schedule)

reclaim_space_job = self.pvc_obj.create_reclaim_space_job()

helpers.wait_for_reclaim_space_job(reclaim_space_job)
Loading