ocs_ci/ocs/ocp.py
def wait_for_resource(
        self, condition, resource_name='', selector=None, resource_count=0,
        timeout=60, sleep=3
    ):
        """
        Wait for a resource to reach to a desired condition

        Args:
            condition (str): The desired state the resource that is sampled
                from 'oc get <kind> <resource_name>' command
            resource_name (str): The name of the resource to wait
                for (e.g.my-pv1)
            selector (str): The resource selector to search with.
                Example: 'app=rook-ceph-mds'
            resource_count (int): How many resources expected to be
            timeout (int): Time in seconds to wait
            sleep (int): Sampling time in seconds

        Returns:
            bool: True in case all resources reached desired condition,
                False otherwise

        """
        log.info((
            f"Waiting for a resource(s) of kind {self._kind}"
            f" identified by name '{resource_name}'"
            f" and selector {selector}"
            f" to reach desired condition {condition}"))
        resource_name = resource_name if resource_name else self.resource_name

        # actual status of the resource we are waiting for, setting it to None
        # now prevents UnboundLocalError raised when waiting timeouts
        actual_status = None

        try:
            for sample in TimeoutSampler(
                timeout, sleep, self.get, resource_name, True, selector
            ):

                # Only 1 resource expected to be returned
                if resource_name:
                    status = self.get_resource_status(resource_name)
                    if status == condition:
                        return True
                    log.info((
                        f"status of {resource_name} was {status},"
                        f" but we were waiting for {condition}"))
                    actual_status = status
                # More than 1 resources returned
                elif sample.get('kind') == 'List':
                    in_condition = []
                    actual_status = []
                    sample = sample['items']
                    for item in sample:
                        try:
                            item_name = item.get('metadata').get('name')
                            status = self.get_resource_status(item_name)
                            actual_status.append(status)
                            if status == condition:
                                in_condition.append(item)
                        except CommandFailed as ex:
                            log.info(
                                f"Failed to get status of resource: {item_name}, "
                                f"Error: {ex}"
                            )
                        if resource_count:
                            if len(in_condition) == resource_count and (
                                len(sample) == len(in_condition)
                            ):
                                return True
                        elif len(sample) == len(in_condition):
                            return True
                    # preparing logging message with expected number of
                    # resource items we are waiting for
                    if resource_count > 0:
                        exp_num_str = f"all {resource_count}"
                    else:
                        exp_num_str = "all"
                    log.info((
                        f"status of {resource_name} item(s) were {actual_status},"
                        f" but we were waiting"
                        f" for {exp_num_str} of them to be {condition}"))
        except TimeoutExpiredError as ex:
            log.error(f"timeout expired: {ex}")
            log.error((
                f"Wait for {self._kind} resource {resource_name}"
                f" to reach desired condition {condition} failed,"
                f" last actual status was {actual_status}"))
            raise(ex)

        return False

        Wait for a resource to reach to a desired condition

        Args:
            condition (str): The desired state the resource that is sampled
                from 'oc get <kind> <resource_name>' command
            resource_name (str): The name of the resource to wait
                for (e.g.my-pv1)
            selector (str): The resource selector to search with.
                Example: 'app=rook-ceph-mds'
            resource_count (int): How many resources expected to be
            timeout (int): Time in seconds to wait
            sleep (int): Sampling time in seconds

        Returns:
            bool: True in case all resources reached desired condition,
                False otherwise

        
Wait for a resource to reach to a desired condition
Wait for a resource to reach to a desired condition
def wait_for_resource(
        self, condition, resource_name='', selector=None, resource_count=0,
        timeout=60, sleep=3
    ):
        log.info((
            f"Waiting for a resource(s) of kind {self._kind}"
            f" identified by name '{resource_name}'"
            f" and selector {selector}"
            f" to reach desired condition {condition}"))
        resource_name = resource_name if resource_name else self.resource_name
        actual_status = None
        try:
            for sample in TimeoutSampler(
                timeout, sleep, self.get, resource_name, True, selector
            ):
                if resource_name:
                    status = self.get_resource_status(resource_name)
                    if status == condition:
                        return True
                    log.info((
                        f"status of {resource_name} was {status},"
                        f" but we were waiting for {condition}"))
                    actual_status = status
                elif sample.get('kind') == 'List':
                    in_condition = []
                    actual_status = []
                    sample = sample['items']
                    for item in sample:
                        try:
                            item_name = item.get('metadata').get('name')
                            status = self.get_resource_status(item_name)
                            actual_status.append(status)
                            if status == condition:
                                in_condition.append(item)
                        except CommandFailed as ex:
                            log.info(
                                f"Failed to get status of resource: {item_name}, "
                                f"Error: {ex}"
                            )
                        if resource_count:
                            if len(in_condition) == resource_count and (
                                len(sample) == len(in_condition)
                            ):
                                return True
                        elif len(sample) == len(in_condition):
                            return True
                    if resource_count > 0:
                        exp_num_str = f"all {resource_count}"
                    else:
                        exp_num_str = "all"
                    log.info((
                        f"status of {resource_name} item(s) were {actual_status},"
                        f" but we were waiting"
                        f" for {exp_num_str} of them to be {condition}"))
        except TimeoutExpiredError as ex:
            log.error(f"timeout expired: {ex}")
            log.error((
                f"Wait for {self._kind} resource {resource_name}"
                f" to reach desired condition {condition} failed,"
                f" last actual status was {actual_status}"))
            raise(ex)
        return False
def wait_for_resource ( self , condition , resource_name = '' , selector = None , resource_count = 0 , timeout = 60 , sleep = 3 ) : log . info ( ( f"Waiting for a resource(s) of kind {self._kind}" f" identified by name '{resource_name}'" f" and selector {selector}" f" to reach desired condition {condition}" ) ) resource_name = resource_name if resource_name else self . resource_name actual_status = None try : for sample in TimeoutSampler ( timeout , sleep , self . get , resource_name , True , selector ) : if resource_name : status = self . get_resource_status ( resource_name ) if status == condition : return True log . info ( ( f"status of {resource_name} was {status}," f" but we were waiting for {condition}" ) ) actual_status = status elif sample . get ( 'kind' ) == 'List' : in_condition = [ ] actual_status = [ ] sample = sample [ 'items' ] for item in sample : try : item_name = item . get ( 'metadata' ) . get ( 'name' ) status = self . get_resource_status ( item_name ) actual_status . append ( status ) if status == condition : in_condition . append ( item ) except CommandFailed as ex : log . info ( f"Failed to get status of resource: {item_name}, " f"Error: {ex}" ) if resource_count : if len ( in_condition ) == resource_count and ( len ( sample ) == len ( in_condition ) ) : return True elif len ( sample ) == len ( in_condition ) : return True if resource_count > 0 : exp_num_str = f"all {resource_count}" else : exp_num_str = "all" log . info ( ( f"status of {resource_name} item(s) were {actual_status}," f" but we were waiting" f" for {exp_num_str} of them to be {condition}" ) ) except TimeoutExpiredError as ex : log . error ( f"timeout expired: {ex}" ) log . error ( ( f"Wait for {self._kind} resource {resource_name}" f" to reach desired condition {condition} failed," f" last actual status was {actual_status}" ) ) raise ( ex ) return False
Wait for a resource to reach to a desired condition
Wait for a resource to reach to a desired condition
"""
        Wait for a resource to reach to a desired condition

        Args:
            condition (str): The desired state the resource that is sampled
                from 'oc get <kind> <resource_name>' command
            resource_name (str): The name of the resource to wait
                for (e.g.my-pv1)
            selector (str): The resource selector to search with.
                Example: 'app=rook-ceph-mds'
            resource_count (int): How many resources expected to be
            timeout (int): Time in seconds to wait
            sleep (int): Sampling time in seconds

        Returns:
            bool: True in case all resources reached desired condition,
                False otherwise

        """ # actual status of the resource we are waiting for, setting it to None # now prevents UnboundLocalError raised when waiting timeouts # Only 1 resource expected to be returned # More than 1 resources returned # preparing logging message with expected number of # resource items we are waiting for
True in case all resources reached desired condition,
False otherwise
True in case all resources reached desired condition False otherwise
bool
self
condition
The desired state the resource that is sampled
from 'oc get  ' command
The desired state the resource that is sampled from ' oc get ' command
resource_name
The name of the resource to wait
for
The name of the resource to wait for
selector
The resource selector to search with.
Example: 'app=rook-ceph-mds'
The resource selector to search with . Example : ' app = rook - ceph - mds '
resource_count
None
timeout
Time in seconds to wait
Time in seconds to wait
sleep
Sampling time in seconds
Sampling time in seconds