Skip to content

Commit

Permalink
Test Automation for space reclaim with storageclass annotations. (red…
Browse files Browse the repository at this point in the history
…-hat-storage#10364)


Signed-off-by: Parag Kamble <pakamble@redhat.com>
  • Loading branch information
paraggit authored Sep 5, 2024
1 parent 7cef9c8 commit c4c25e4
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ocs_ci/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ def create_storage_class(
volume_binding_mode="Immediate",
allow_volume_expansion=True,
kernelMountOptions=None,
annotations=None,
):
"""
Create a storage class
Expand All @@ -769,6 +770,7 @@ def create_storage_class(
pod attachment.
allow_volume_expansion(bool): True to create sc with volume expansion
kernelMountOptions (str): Mount option for security context
annotations(dict): dict of annotations to be added to the storageclass.
Returns:
OCS: An OCS instance for the storage class
"""
Expand Down Expand Up @@ -819,6 +821,9 @@ def create_storage_class(
f"csi.storage.k8s.io/{key}-secret-namespace"
] = config.ENV_DATA["cluster_namespace"]

if annotations:
sc_data["metadata"]["annotations"] = annotations

sc_data["parameters"]["clusterID"] = config.ENV_DATA["cluster_namespace"]
sc_data["reclaimPolicy"] = reclaim_policy
sc_data["volumeBindingMode"] = volume_binding_mode
Expand Down Expand Up @@ -5146,3 +5151,35 @@ def wait_for_reclaim_space_job(reclaim_space_job):
raise UnexpectedBehaviour(
f"ReclaimSpaceJob {reclaim_space_job.name} is not successful. Yaml output: {reclaim_space_job.get()}"
)


def get_rbd_image_info(rbd_pool, rbd_image_name):
"""
Get RBD image information. (e.g provisioned size, used size, image , )
Args:
rbd_pool(str) : pool name
rbd_image_name(str) : name of rbd image
Returns:
dict : rbd image information e.g, provisioned size, used size etc.
"""
ct_pod = pod.get_ceph_tools_pod()

cmd = f"rbd du -p {rbd_pool} {rbd_image_name}"

cmd_out = ct_pod.exec_ceph_cmd(ceph_cmd=cmd, format="json")

data = next(
(volume for volume in cmd_out["images"] if volume["name"] == rbd_image_name),
None,
)

if data:
# Conversion constant: 1 GiB = 1024^3 bytes
bytes_in_gib = 1024**3

data["provisioned_size_gib"] = data["provisioned_size"] / bytes_in_gib
data["used_size_gib"] = data["used_size"] / bytes_in_gib

return data
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ def factory(
volume_binding_mode="Immediate",
allow_volume_expansion=True,
kernelMountOptions=None,
annotations=None,
):
"""
Args:
Expand All @@ -868,6 +869,7 @@ def factory(
till pod attachment.
allow_volume_expansion (bool): True to Allows volume expansion
kernelMountOptions (str): Mount option for security context
annotations (dict): dict of annotation to be added to the storageclass.
Returns:
object: helpers.create_storage_class instance with links to
Expand Down Expand Up @@ -906,6 +908,7 @@ def factory(
volume_binding_mode=volume_binding_mode,
allow_volume_expansion=allow_volume_expansion,
kernelMountOptions=kernelMountOptions,
annotations=annotations,
)
assert sc_obj, f"Failed to create {interface} storage class"
sc_obj.secret = secret
Expand Down
113 changes: 113 additions & 0 deletions tests/functional/storageclass/test_storageclass_reclaim_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import logging

from ocs_ci.ocs import constants
from ocs_ci.helpers.helpers import get_rbd_image_info
from ocs_ci.ocs.exceptions import UnexpectedBehaviour
from ocs_ci.utility.retry import retry
from ocs_ci.framework.pytest_customization.marks import (
green_squad,
polarion_id,
tier1,
)

log = logging.getLogger(__name__)


@green_squad
@polarion_id("OCS-6197")
class TestStorageClassReclamespace:
@retry(UnexpectedBehaviour, tries=5, delay=10)
def wait_till_expected_image_size(self, pvc_obj, expected_size):
"""
Waiting till rbd image size is became expected size.
"""
rbd_image_name = pvc_obj.get_rbd_image_name
image_size = get_rbd_image_info(self.pool_name, rbd_image_name).get(
"used_size_gib"
)

if image_size != expected_size:
raise UnexpectedBehaviour(
f"RBD image {rbd_image_name} size is not expected as {expected_size}GiB"
)
log.info(f" RBD Image { rbd_image_name} is size of {image_size}GiB")
return True

@tier1
def test_storageclass_reclaimspace(
self, storageclass_factory, multi_pvc_factory, pod_factory
):
"""
Test Space Reclaim Operation with Storageclass annotation.
Steps:
1. Create a RBD storageclass with reclaimspace annotations.
"reclaimspace.csiaddons.openshift.io/schedule: */3 * * * *".
2. Create a 3 PVC
4. Create a pod w.r.t each PVC
5. Write a 2.0GiB on block device mounted in the pod.
6. destroy all pods
7. wait for reclaimspace operation to be complete
6. verify reclaimspace Job ran successfully for the storageclass.
"""

# Storegeclass ReclaimSpace annotations.
reclaimspace_annotations = {
"reclaimspace.csiaddons.openshift.io/schedule": "*/3 * * * *"
}

# Creating StorageClass with reclaimspace annotations.
self.sc_obj = storageclass_factory(
interface=constants.CEPHBLOCKPOOL, annotations=reclaimspace_annotations
)
self.pool_name = self.sc_obj.data["parameters"]["pool"]

# Create a PVC's with volume block mode
pvc_objs = multi_pvc_factory(
size=5,
storageclass=self.sc_obj,
num_of_pvc=3,
access_modes=[f"{constants.ACCESS_MODE_RWO}-Block"],
wait_each=True,
)

# Create POds
self.pod_objs = []

# Create pods
for pvc in pvc_objs:
pod_obj = pod_factory(
pvc=pvc,
status=constants.STATUS_RUNNING,
raw_block_pv=True,
)
self.pod_objs.append(pod_obj)

# Writing data to the block device
for pod_obj, pvc_obj in zip(self.pod_objs, pvc_objs):
storage_path = pod_obj.get_storage_path("block")
log.info("Writing 2.0GiB of data to the block device")
pod_obj.exec_cmd_on_pod(
f"dd if=/dev/zero of={storage_path} bs=1M count=2048 oflag=direct > /dev/null 2>&1 &",
shell=True,
)

# Wait until all writes are complete and the RBD image shows the expected size.
for pvc_obj in pvc_objs:
assert self.wait_till_expected_image_size(
pvc_obj, 2.0
), f"RBD Image '{pvc_obj.get_rbd_image_name}' expected size of '2.0 GiB' does not match the actual size."

# Delete all pods
for pod_obj in self.pod_objs:
pod_obj.delete()
pod_obj.ocp.wait_for_delete(resource_name=pod_obj.name)

# Wait till reclaim space operations is complete
for pvc_obj in pvc_objs:
assert self.wait_till_expected_image_size(
pvc_obj, 0.0
), f"RBD Image '{pvc_obj.get_rbd_image_name}' expected size of '0.0 GiB' does not match the actual size."

log.info("ReclaimSpace JOB has ran successfully. ")

0 comments on commit c4c25e4

Please sign in to comment.