Exemplo n.º 1
0
def if_block_exists(mnode, volname, blockname):
    """Check if block already exists

    Args:
        mnode (str): Node on which commands has to be executed
        volname (str): Name of the volume.
        blockname(str): block name

    Returns:
        bool : True if block exists. False Otherwise
    """
    # Get block list for the volname
    ret, out, err = block_list(mnode, volname)
    if ret != 0:
        g.log.error("Failed to get block list for the volume %s: %s", volname,
                    err)
        return False

    # Check if block exists for the volname
    block_list_dict = g.load_json_string(out)
    if blockname in block_list_dict['blocks']:
        return True
    else:
        g.log.error("Block '%s' doesn't exists on volume %s", blockname,
                    volname)
        return False
def match_heketi_and_gluster_block_volumes_by_prefix(gluster_pod,
                                                     heketi_block_volumes,
                                                     block_vol_prefix,
                                                     hostname=None):
    """Match block volumes from heketi and gluster. This function can't
       be used for block volumes with custom prefixes

    Args:
        gluster_pod (podcmd | str): gluster pod class object has gluster
                                    pod and ocp master node or gluster
                                    pod name
        heketi_block_volumes (list): list of heketi block volumes with
                                     which gluster block volumes need to
                                     be matched
        block_vol_prefix (str): block volume prefix by which the block
                                volumes needs to be filtered
        hostname (str): ocp master node on which oc command gets executed

    """
    gluster_pod = _get_gluster_pod(gluster_pod, hostname)

    gluster_vol_list = get_volume_list(gluster_pod)

    gluster_vol_block_list = []
    for gluster_vol in gluster_vol_list[1:]:
        ret, out, err = block_list(gluster_pod, gluster_vol)
        try:
            if ret != 0 and json.loads(out)["RESULT"] == "FAIL":
                msg = "failed to get block volume list with error: %s" % err
                g.log.error(msg)
                raise AssertionError(msg)
        except Exception as e:
            g.log.error(e)
            raise

        gluster_vol_block_list.extend([
            block_vol.replace(block_vol_prefix, "")
            for block_vol in json.loads(out)["blocks"]
            if block_vol.startswith(block_vol_prefix)
        ])

    if cmp(sorted(gluster_vol_block_list), heketi_block_volumes) != 0:
        err_msg = "Gluster and Heketi Block volume list match failed"
        err_msg += "\nGluster Volumes: %s, " % gluster_vol_block_list
        err_msg += "\nBlock volumes %s" % heketi_block_volumes
        err_msg += "\nDifference: %s" % (set(gluster_vol_block_list)
                                         ^ set(heketi_block_volumes))
        raise AssertionError(err_msg)
def match_heketi_and_gluster_block_volumes_by_prefix(
        heketi_block_volumes, block_vol_prefix):
    """Match block volumes from heketi and gluster. This function can't
       be used for block volumes with custom prefixes

    Args:
        heketi_block_volumes (list): list of heketi block volumes with
                                     which gluster block volumes need to
                                     be matched
        block_vol_prefix (str): block volume prefix by which the block
                                volumes needs to be filtered
    """
    gluster_vol_list = get_volume_list("auto_get_gluster_endpoint")

    gluster_vol_block_list = []
    for gluster_vol in gluster_vol_list[1:]:
        ret, out, err = block_list("auto_get_gluster_endpoint", gluster_vol)
        try:
            if ret != 0 and json.loads(out)["RESULT"] == "FAIL":
                msg = "failed to get block volume list with error: %s" % err
                g.log.error(msg)
                raise AssertionError(msg)
        except Exception as e:
            g.log.error(e)
            raise

        gluster_vol_block_list.extend([
            block_vol.replace(block_vol_prefix, "")
            for block_vol in json.loads(out)["blocks"]
            if block_vol.startswith(block_vol_prefix)
        ])

    vol_difference = set(gluster_vol_block_list) ^ set(heketi_block_volumes)
    if vol_difference:
        err_msg = "Gluster and Heketi Block volume list match failed"
        err_msg += "\nGluster Volumes: %s, " % gluster_vol_block_list
        err_msg += "\nBlock volumes %s" % heketi_block_volumes
        err_msg += "\nDifference: %s" % vol_difference
        raise AssertionError(err_msg)
def match_heketi_and_gluster_block_volumes_by_prefix(
        heketi_block_volumes, block_vol_prefix):
    """Match block volumes from heketi and gluster. This function can't
       be used for block volumes with custom prefixes

    Args:
        heketi_block_volumes (list): list of heketi block volumes with
                                     which gluster block volumes need to
                                     be matched
        block_vol_prefix (str): block volume prefix by which the block
                                volumes needs to be filtered
    """
    gluster_vol_list = get_volume_list("auto_get_gluster_endpoint")

    gluster_vol_block_list = []
    for gluster_vol in gluster_vol_list[1:]:
        ret, out, err = block_list("auto_get_gluster_endpoint", gluster_vol)
        try:
            if ret != 0 and json.loads(out)["RESULT"] == "FAIL":
                msg = "failed to get block volume list with error: %s" % err
                g.log.error(msg)
                raise AssertionError(msg)
        except Exception as e:
            g.log.error(e)
            raise

        gluster_vol_block_list.extend([
            block_vol.replace(block_vol_prefix, "")
            for block_vol in json.loads(out)["blocks"]
            if block_vol.startswith(block_vol_prefix)
        ])

    if cmp(sorted(gluster_vol_block_list), heketi_block_volumes) != 0:
        err_msg = "Gluster and Heketi Block volume list match failed"
        err_msg += "\nGluster Volumes: %s, " % gluster_vol_block_list
        err_msg += "\nBlock volumes %s" % heketi_block_volumes
        err_msg += "\nDifference: %s" % (set(gluster_vol_block_list) ^
                                         set(heketi_block_volumes))
        raise AssertionError(err_msg)
Exemplo n.º 5
0
def get_block_list(mnode, volname):
    """ Get list of all blocks for the volume

    Args:
        mnode (str): Node on which commands has to be executed
        volname (str): Name of the volume.

    Returns:
        list of block names if successful in getting block list, None if
        block list command errors
    """
    # get block list for the volname
    ret, out, err = block_list(mnode, volname)
    if ret != 0:
        g.log.error("Failed to get block list for the volume %s: %s", volname,
                    err)
        return None

    block_list_dict = g.load_json_string(out)
    blocknames = block_list_dict.get('blocks', None)
    if blocknames is None:
        g.log.error("No blocks present on volume %s", volname)

    return blocknames