diff options
author | vamahaja <vamahaja@redhat.com> | 2019-09-11 14:46:59 +0530 |
---|---|---|
committer | vponomar <vponomar@redhat.com> | 2019-09-19 11:45:44 +0000 |
commit | a214cf6781d48594afc84d08e865d59bdcbe8fe1 (patch) | |
tree | 484092c4cc0f318871ed4c4299e663edf0b0489e | |
parent | 764d7bd68ec0b4ea9229c381b3b4195367b44b83 (diff) |
Merge heketi node operation tests in one class and fix library
Fix consists of -
- Use "**kwargs" approach in the "heketi_node_list"
function as it is done in lots of other functions.
- Parse the CLI output in the "heketi_node_list" function
using regex instead of the splitting and stripping strings.
- Combine test cases related to the same feature into one
module - test_heketi_node_operations.py
- Remove redundant checks which already exist in common
libraries.
- Remove unnecessary logging.
Change-Id: I815ddfbbacb765140229e7630ec87a6bbaa6255b
Signed-off-by: vamahaja <vamahaja@redhat.com>
-rw-r--r-- | openshift-storage-libs/openshiftstoragelibs/heketi_ops.py | 37 | ||||
-rw-r--r-- | tests/functional/heketi/test_heketi_node_operations.py | 108 | ||||
-rw-r--r-- | tests/functional/heketi/test_node_enable_disable.py | 141 | ||||
-rw-r--r-- | tests/functional/heketi/test_node_info.py | 80 |
4 files changed, 128 insertions, 238 deletions
diff --git a/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py b/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py index 38e09cd5..7c7d98ec 100644 --- a/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py +++ b/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py @@ -18,19 +18,20 @@ from openshiftstoragelibs.utils import parse_prometheus_data from openshiftstoragelibs import waiter -HEKETI_BHV = re.compile(r"Id:(\S+)\s+Cluster:(\S+)\s+Name:(\S+)\s\[block\]") -HEKETI_OPERATIONS = re.compile(r"Id:(\S+)\s+Type:(\S+)\s+Status:(\S+)") +HEKETI_DC = g.config.get("cns", g.config.get("openshift"))[ + "heketi_config"]["heketi_dc_name"] HEKETI_COMMAND_TIMEOUT = g.config.get("common", {}).get( "heketi_command_timeout", 120) -TIMEOUT_PREFIX = "timeout %s " % HEKETI_COMMAND_TIMEOUT - MASTER_NODE = list(g.config["ocp_servers"]["master"].keys())[0] -HEKETI_DC = g.config.get("cns", g.config.get("openshift"))[ - "heketi_config"]["heketi_dc_name"] + +HEKETI_BHV = re.compile(r"Id:(\S+)\s+Cluster:(\S+)\s+Name:(\S+)\s\[block\]") +HEKETI_OPERATIONS = re.compile(r"Id:(\S+)\s+Type:(\S+)\s+Status:(\S+)") +HEKETI_NODES = re.compile(r"Id:(\S+)\s+Cluster:(\S+)") + GET_HEKETI_PODNAME_CMD = ( "oc get pods -l deploymentconfig=%s -o=custom-columns=:.metadata.name " - "--no-headers" % HEKETI_DC -) + "--no-headers" % HEKETI_DC) +TIMEOUT_PREFIX = "timeout %s " % HEKETI_COMMAND_TIMEOUT def cmd_run_on_heketi_pod(cmd, raise_on_error=True): @@ -948,22 +949,26 @@ def heketi_node_info(heketi_client_node, heketi_server_url, node_id, **kwargs): return out -def heketi_node_list(heketi_client_node, heketi_server_url, - heketi_user=None, heketi_secret=None): +def heketi_node_list(heketi_client_node, heketi_server_url, **kwargs): """Execute CLI 'heketi node list' command and parse its output. Args: heketi_client_node (str): Node on which cmd has to be executed heketi_server_url (str): Heketi server url to perform request to - heketi_user (str): Name of the user to perform request with - heketi_secret (str): Secret for 'heketi_user' + + Kwargs: + The keys, values in kwargs are: + - json : (bool) + - secret : (str)|None + - user : (str)|None Returns: list of strings which are node IDs + Raises: openshiftstoragelibs.exceptions.ExecutionError when command fails. """ heketi_server_url, json_arg, admin_key, user = _set_heketi_global_flags( - heketi_server_url, user=heketi_user, secret=heketi_secret) + heketi_server_url, **kwargs) cmd = "heketi-cli -s %s node list %s %s %s" % ( heketi_server_url, json_arg, admin_key, user) @@ -971,10 +976,8 @@ def heketi_node_list(heketi_client_node, heketi_server_url, out = heketi_cmd_run(heketi_client_node, cmd) heketi_node_id_list = [] - for line in out.strip().split("\n"): - # Line looks like this: 'Id:nodeIdString\tCluster:clusterIdString' - heketi_node_id_list.append( - line.strip().split("Cluster")[0].strip().split(":")[1]) + for node in HEKETI_NODES.findall(out.strip()): + heketi_node_id_list.append(node[0]) return heketi_node_id_list diff --git a/tests/functional/heketi/test_heketi_node_operations.py b/tests/functional/heketi/test_heketi_node_operations.py new file mode 100644 index 00000000..6386be6f --- /dev/null +++ b/tests/functional/heketi/test_heketi_node_operations.py @@ -0,0 +1,108 @@ +from glustolibs.gluster import peer_ops + +from openshiftstoragelibs import baseclass +from openshiftstoragelibs import heketi_ops +from openshiftstoragelibs import podcmd + + +class TestHeketiNodeOperations(baseclass.BaseClass): + """Class to test heketi node operations + """ + + @podcmd.GlustoPod() + def test_heketi_node_list(self): + """Test node list operation + """ + h_client, h_server = self.heketi_client_node, self.heketi_server_url + + # List heketi nodes + node_ips = [] + heketi_node_id_list = heketi_ops.heketi_node_list(h_client, h_server) + + for node_id in heketi_node_id_list: + node_info = heketi_ops.heketi_node_info( + h_client, h_server, node_id, json=True) + node_ips.append(node_info["hostnames"]["storage"]) + + # Compare the node listed in previous step + hostnames = [] + list_of_pools = peer_ops.get_pool_list('auto_get_gluster_endpoint') + self.assertTrue( + list_of_pools, + "Failed to get the pool list from gluster pods/nodes") + for pool in list_of_pools: + hostnames.append(pool["hostname"]) + self.assertEqual( + len(heketi_node_id_list), len(list_of_pools), + "Heketi volume list %s is not equal to gluster volume list %s" + % (node_ips, hostnames)) + + def test_heketi_node_info(self): + """Test heketi node info operation + """ + h_client, h_server = self.heketi_client_node, self.heketi_server_url + + # List heketi node + heketi_node_id_list = heketi_ops.heketi_node_list(h_client, h_server) + self.assertTrue(heketi_node_id_list, "Node Id list is empty.") + + for node_id in heketi_node_id_list: + node_info = heketi_ops.heketi_node_info( + h_client, h_server, node_id, json=True) + self.assertTrue(node_info, "Failed to retrieve the node info") + self.assertEqual( + node_info["id"], node_id, + "Failed to match node ID. Exp: %s, Act: %s" % ( + node_id, node_info["id"])) + + def test_heketi_node_states_enable_disable(self): + """Test node enable and disable functionality + """ + h_client, h_server = self.heketi_client_node, self.heketi_server_url + + node_list = heketi_ops.heketi_node_list(h_client, h_server) + online_hosts = [] + for node_id in node_list: + node_info = heketi_ops.heketi_node_info( + h_client, h_server, node_id, json=True) + if node_info["state"] == "online": + online_hosts.append(node_info) + + if len(online_hosts) < 3: + raise self.skipTest( + "This test can run only if online hosts are more than 2") + + # Disable n-3 nodes, in case we have n nodes + for node_info in online_hosts[3:]: + node_id = node_info["id"] + heketi_ops.heketi_node_disable(h_client, h_server, node_id) + self.addCleanup( + heketi_ops.heketi_node_enable, h_client, h_server, node_id) + + # Create volume when 3 nodes are online + vol_size = 1 + vol_info = heketi_ops.heketi_volume_create( + h_client, h_server, vol_size, json=True) + self.addCleanup( + heketi_ops.heketi_volume_delete, + h_client, h_server, vol_info['id']) + + node_id = online_hosts[0]['id'] + try: + heketi_ops.heketi_node_disable(h_client, h_server, node_id) + + # Try to create a volume, volume creation should fail + with self.assertRaises(AssertionError): + heketi_volume = heketi_ops.heketi_volume_create( + h_client, h_server, vol_size) + self.addCleanup( + heketi_ops.heketi_volume_delete, + h_client, h_server, heketi_volume["id"]) + finally: + # Enable heketi node + heketi_ops.heketi_node_enable(h_client, h_server, node_id) + + # Create volume when heketi node is enabled + vol_info = heketi_ops.heketi_volume_create( + h_client, h_server, vol_size, json=True) + heketi_ops.heketi_volume_delete(h_client, h_server, vol_info['id']) diff --git a/tests/functional/heketi/test_node_enable_disable.py b/tests/functional/heketi/test_node_enable_disable.py deleted file mode 100644 index 2d88ffb7..00000000 --- a/tests/functional/heketi/test_node_enable_disable.py +++ /dev/null @@ -1,141 +0,0 @@ -"""Test cases to disable and enable node in heketi.""" -from glusto.core import Glusto as g - -from openshiftstoragelibs.baseclass import BaseClass -from openshiftstoragelibs.heketi_ops import ( - heketi_node_disable, - heketi_node_enable, - heketi_node_info, - heketi_node_list, - heketi_volume_create, - heketi_volume_delete, -) - - -class TestHeketiNodeState(BaseClass): - """Test node enable and disable functionality.""" - - def enable_node(self, node_id): - """ - Enable node through heketi-cli. - - :param node_id: str node ID - """ - out = heketi_node_enable(self.heketi_client_node, - self.heketi_server_url, - node_id) - - self.assertNotEqual(out, False, - "Failed to enable node of" - " id %s" % node_id) - - def disable_node(self, node_id): - """ - Disable node through heketi-cli. - - :param node_id: str node ID - """ - out = heketi_node_disable(self.heketi_client_node, - self.heketi_server_url, - node_id) - - self.assertNotEqual(out, False, - "Failed to disable node of" - " id %s" % node_id) - - def get_node_info(self, node_id): - """ - Get node information from node_id. - - :param node_id: str node ID - :return node_info: list node information - """ - node_info = heketi_node_info( - self.heketi_client_node, self.heketi_server_url, - node_id, json=True) - self.assertNotEqual(node_info, False, - "Node info on %s failed" % node_id) - return node_info - - def get_online_nodes(self, node_list): - """ - Get online nodes information from node_list. - - :param node_list: list of node ID's - :return: list node information of online nodes - """ - online_hosts_info = [] - - for node in node_list: - node_info = self.get_node_info(node) - if node_info["state"] == "online": - online_hosts_info.append(node_info) - - return online_hosts_info - - def test_node_state(self): - """ - Test node enable and disable functionality. - - If we have 4 gluster servers, if we disable 1/4 nodes from heketi - and create a volume, the volume creation should be successful. - - If we disable 2/4 nodes from heketi-cli and create a volume - the volume creation should fail. - - If we enable back one gluster server and create a volume - the volume creation should be successful. - """ - g.log.info("Disable node in heketi") - node_list = heketi_node_list(self.heketi_client_node, - self.heketi_server_url) - self.assertTrue(node_list, "Failed to list heketi nodes") - g.log.info("Successfully got the list of nodes") - online_hosts = self.get_online_nodes(node_list) - - if len(online_hosts) < 3: - raise self.skipTest( - "This test can run only if online hosts are more " - "than 2") - # if we have n nodes, disable n-3 nodes - for node_info in online_hosts[3:]: - node_id = node_info["id"] - g.log.info("going to disable node id %s", node_id) - self.disable_node(node_id) - self.addCleanup(self.enable_node, node_id) - - vol_size = 1 - # create volume when 3 nodes are online - vol_info = heketi_volume_create(self.heketi_client_node, - self.heketi_server_url, vol_size, - json=True) - self.addCleanup( - heketi_volume_delete, self.heketi_client_node, - self.heketi_server_url, vol_info['id']) - - node_id = online_hosts[0]['id'] - g.log.info("going to disable node id %s", node_id) - self.disable_node(node_id) - self.addCleanup(self.enable_node, node_id) - - # try to create a volume, volume creation should fail - with self.assertRaises(AssertionError): - out = heketi_volume_create( - self.heketi_client_node, self.heketi_server_url, vol_size) - self.addCleanup( - heketi_volume_delete, self.heketi_client_node, - self.heketi_server_url, out["id"]) - self.assertFalse(True, "Volume creation didn't fail: %s" % out) - - g.log.info("Volume creation failed as expected.") - - # enable node - self.enable_node(node_id) - - # create volume when node is enabled - vol_info = heketi_volume_create(self.heketi_client_node, - self.heketi_server_url, vol_size, - json=True) - self.addCleanup( - heketi_volume_delete, self.heketi_client_node, - self.heketi_server_url, vol_info['id']) diff --git a/tests/functional/heketi/test_node_info.py b/tests/functional/heketi/test_node_info.py deleted file mode 100644 index 5bf7270f..00000000 --- a/tests/functional/heketi/test_node_info.py +++ /dev/null @@ -1,80 +0,0 @@ -from glusto.core import Glusto as g -from glustolibs.gluster.exceptions import ExecutionError -from glustolibs.gluster.peer_ops import get_pool_list - -from openshiftstoragelibs.baseclass import BaseClass -from openshiftstoragelibs import heketi_ops, podcmd - - -class TestHeketiVolume(BaseClass): - """ - Class to test heketi volume create - """ - - @podcmd.GlustoPod() - def test_to_get_list_of_nodes(self): - """ - Listing all nodes and compare the - node listed in previous step - """ - - # List all list - ip = [] - g.log.info("Listing the node id") - heketi_node_id_list = heketi_ops.heketi_node_list( - self.heketi_client_node, self.heketi_server_url) - - g.log.info("Successfully listed the node") - - if (len(heketi_node_id_list) == 0): - raise ExecutionError("Node list empty") - - for node_id in heketi_node_id_list: - g.log.info("Retrieve the node info") - node_info = heketi_ops.heketi_node_info( - self.heketi_client_node, self.heketi_server_url, - node_id, json=True) - self.assertTrue(node_info, ("Failed to " - "retrieve the node info")) - g.log.info("Successfully retrieved the node info %s" % node_id) - ip.append(node_info["hostnames"]["storage"]) - - # Compare the node listed in previous step - hostname = [] - - g.log.info("Get the pool list") - list_of_pools = get_pool_list('auto_get_gluster_endpoint') - self.assertTrue(list_of_pools, ("Failed to get the " - "pool list from gluster pods/nodes")) - g.log.info("Successfully got the pool list from gluster pods/nodes") - for pool in list_of_pools: - hostname.append(pool["hostname"]) - - if (len(heketi_node_id_list) != len(list_of_pools)): - raise ExecutionError( - "Heketi volume list %s is not equal " - "to gluster volume list %s" % ((ip), (hostname))) - g.log.info("The node IP's from node info and list" - " is : %s/n and pool list from gluster" - " pods/nodes is %s" % ((ip), (hostname))) - - def test_to_retrieve_node_info(self): - """ - List and retrieve node related info - """ - - # List all list - g.log.info("Listing the node id") - heketi_node_id_list = heketi_ops.heketi_node_list( - self.heketi_client_node, self.heketi_server_url) - self.assertTrue(heketi_node_id_list, ("Node Id list is empty.")) - g.log.info("Successfully listed the node") - - for node_id in heketi_node_id_list: - g.log.info("Retrieve the node info") - node_info = heketi_ops.heketi_node_info( - self.heketi_client_node, self.heketi_server_url, - node_id, json=True) - self.assertTrue(node_info, ("Failed to " - "retrieve the node info")) - g.log.info("Successfully retrieved the node info %s" % node_id) |