From 70ded8d3fbfe0b9962d7b4d83edee25044c5ab9b Mon Sep 17 00:00:00 2001 From: vamahaja Date: Wed, 25 Sep 2019 20:15:46 +0530 Subject: Add library to get heketi block volumes by name prefix This change required due to - 1. Get block volume by prefix is common step which is used in two places for now and will be used in other places too. 2. Hence add library "heketi_blockvolume_list_by_name_prefix" in heketi_ops.py. 3. And use added library and update code from class "GlusterStabilityTestSetup" and "TestGlusterBlockStability". Change-Id: I9e60d58d5c84380104081335745270b3d21ff071 Signed-off-by: vamahaja --- .../openshiftstoragelibs/heketi_ops.py | 32 +++++++++++++++ .../test_gluster_block_stability.py | 43 ++++----------------- .../test_restart_gluster_services.py | 45 ++++------------------ 3 files changed, 48 insertions(+), 72 deletions(-) diff --git a/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py b/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py index 251104d3..c28c98d2 100644 --- a/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py +++ b/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py @@ -1244,6 +1244,38 @@ def heketi_blockvolume_list(heketi_client_node, heketi_server_url, **kwargs): return out +def heketi_blockvolume_list_by_name_prefix( + heketi_client_node, heketi_server_url, prefix, **kwargs): + """Get block volume id, cluster and name by name prefix. + + Args: + heketi_client_node (str): Node on which cmd has to be executed. + heketi_server_url (str): Heketi server url. + prefix (str): Block volume name prefix. + + Kwargs: + The keys, values in kwargs are: + - secret : (str)|None + - user : (str)|None + + Returns: + list: tuple of bv id, cluster id and name + + Raises: + exceptions.AssertionError: if command fails to execute on + heketi server. + """ + # Delete json key from kwargs + kwargs.pop("json", None) + + block_vols = heketi_blockvolume_list( + heketi_client_node, heketi_server_url, **kwargs) + + block_vol_regex = re.compile( + r"Id:(\S+)\s+Cluster:(\S+)\s+Name:(%s_\S+)" % prefix) + return block_vol_regex.findall(block_vols.strip()) + + def verify_volume_name_prefix(hostname, prefix, namespace, pvc_name, heketi_server_url, **kwargs): """Check whether heketi volume is present with volname prefix or not. diff --git a/tests/functional/gluster_stability/test_gluster_block_stability.py b/tests/functional/gluster_stability/test_gluster_block_stability.py index 5cbf3783..13b8cc85 100644 --- a/tests/functional/gluster_stability/test_gluster_block_stability.py +++ b/tests/functional/gluster_stability/test_gluster_block_stability.py @@ -20,6 +20,7 @@ from openshiftstoragelibs.heketi_ops import ( heketi_blockvolume_delete, heketi_blockvolume_info, heketi_blockvolume_list, + heketi_blockvolume_list_by_name_prefix, heketi_node_info, heketi_node_list, ) @@ -70,8 +71,6 @@ from openshiftstoragelibs.openshift_version import ( from openshiftstoragelibs import utils from openshiftstoragelibs.waiter import Waiter -HEKETI_BLOCK_VOLUME_REGEX = "^Id:(.*).Cluster:(.*).Name:%s_(.*)$" - @ddt.ddt class TestGlusterBlockStability(GlusterBlockBaseClass): @@ -92,36 +91,7 @@ class TestGlusterBlockStability(GlusterBlockBaseClass): "Skipping this test case as multipath validation " "is not supported in OCS 3.9") - def get_heketi_block_volumes(self, vol_prefix): - """Get list of heketi block volumes filtered by prefix - - Args: - vol_prefix (str): volume name prefix used for the block PVC - - Returns: - A tuple containing two lists of heketi block volume IDs and names - """ - - heketi_cmd_out = heketi_blockvolume_list( - self.heketi_client_node, self.heketi_server_url, - secret=self.heketi_cli_key, user=self.heketi_cli_user - ) - heketi_block_volume_ids, heketi_block_volume_names = [], [] - - for block_vol in heketi_cmd_out.split("\n"): - heketi_vol_match = re.search( - HEKETI_BLOCK_VOLUME_REGEX % vol_prefix, block_vol.strip()) - if heketi_vol_match: - heketi_block_volume_ids.append( - (heketi_vol_match.group(1)).strip()) - heketi_block_volume_names.append( - (heketi_vol_match.group(3)).strip()) - - return (sorted(heketi_block_volume_ids), sorted( - heketi_block_volume_names)) - def bulk_app_pods_creation_with_block_pv(self, app_pod_count): - prefix = "autotest-%s" % utils.get_random_str() self.create_storage_class(sc_name_prefix=prefix, create_vol_name_prefix=True, set_hacount=3) @@ -139,16 +109,19 @@ class TestGlusterBlockStability(GlusterBlockBaseClass): iqn, _, ini_node = self.verify_iscsi_sessions_and_multipath( pvc_name, dc_name) - heketi_block_volume_ids, heketi_block_volume_names = ( - self.get_heketi_block_volumes(vol_prefix=prefix)) + h_blockvol_list = heketi_blockvolume_list_by_name_prefix( + self.heketi_client_node, self.heketi_server_url, self.prefix) # validate block volumes listed by heketi and pvs + heketi_blockvolume_ids = sorted([bv[0] for bv in h_blockvol_list]) match_pv_and_heketi_block_volumes( - self.node, heketi_block_volume_ids, pvc_prefix=prefix) + self.node, heketi_blockvolume_ids, pvc_prefix=prefix) # validate block volumes listed by heketi and gluster + heketi_blockvolume_names = sorted([ + bv[1].replace("%s_" % prefix, "") for bv in h_blockvol_list]) match_heketi_and_gluster_block_volumes_by_prefix( - heketi_block_volume_names, block_vol_prefix=(prefix + "_")) + heketi_blockvolume_names, "%s_" % prefix) def initiator_side_failures(self): self.create_storage_class() diff --git a/tests/functional/gluster_stability/test_restart_gluster_services.py b/tests/functional/gluster_stability/test_restart_gluster_services.py index 84bf0e37..3d760f27 100644 --- a/tests/functional/gluster_stability/test_restart_gluster_services.py +++ b/tests/functional/gluster_stability/test_restart_gluster_services.py @@ -1,5 +1,4 @@ from datetime import datetime -import re import time from unittest import skip @@ -15,7 +14,7 @@ from openshiftstoragelibs.gluster_ops import ( wait_to_heal_complete, ) from openshiftstoragelibs.heketi_ops import ( - heketi_blockvolume_list, + heketi_blockvolume_list_by_name_prefix, heketi_server_operation_cleanup, heketi_server_operations_list, ) @@ -87,35 +86,6 @@ class GlusterStabilityTestSetup(GlusterBlockBaseClass): for pvc_name in self.pvc_list: self.addCleanup(oc_delete, self.oc_node, "pvc", pvc_name) - def get_heketi_block_volumes(self): - """lists heketi block volumes - - Returns: - list : list of ids of heketi block volumes - """ - heketi_cmd_out = heketi_blockvolume_list( - self.heketi_client_node, self.heketi_server_url) - - self.assertTrue(heketi_cmd_out, "failed to get block volume list") - - heketi_block_volume_ids = [] - heketi_block_volume_names = [] - for block_vol in heketi_cmd_out.split("\n"): - heketi_vol_match = re.search( - HEKETI_BLOCK_VOLUME_REGEX % self.prefix, block_vol.strip() - ) - if heketi_vol_match: - heketi_block_volume_ids.append( - (heketi_vol_match.group(1)).strip() - ) - heketi_block_volume_names.append( - (heketi_vol_match.group(3)).strip() - ) - - return (sorted(heketi_block_volume_ids), sorted( - heketi_block_volume_names) - ) - def validate_volumes_and_blocks(self): """Validates PVC and block volumes generated through heketi and OCS """ @@ -141,18 +111,19 @@ class GlusterStabilityTestSetup(GlusterBlockBaseClass): match_pvc_and_pv(self.oc_node, self.prefix) # get list of block volumes using heketi - heketi_block_volume_ids, heketi_block_volume_names = ( - self.get_heketi_block_volumes() - ) + h_blockvol_list = heketi_blockvolume_list_by_name_prefix( + self.heketi_client_node, self.heketi_server_url, self.prefix) # validate block volumes listed by heketi and pvs + heketi_blockvolume_ids = sorted([bv[0] for bv in h_blockvol_list]) match_pv_and_heketi_block_volumes( - self.oc_node, heketi_block_volume_ids, self.prefix - ) + self.oc_node, heketi_blockvolume_ids, self.prefix) # validate block volumes listed by heketi and gluster + heketi_blockvolume_names = sorted([ + bv[1].replace("%s_" % self.prefix, "") for bv in h_blockvol_list]) match_heketi_and_gluster_block_volumes_by_prefix( - heketi_block_volume_names, "%s_" % self.prefix) + heketi_blockvolume_names, "%s_" % self.prefix) def get_io_time(self): """Gets last io time of io pod by listing log file directory -- cgit