def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None, ip_version=None, client=None, **kwargs): """Wrapper utility that returns a test subnet.""" # allow tests to use admin client if not client: client = cls.client # The cidr and mask_bits depend on the ip version. ip_version = ip_version if ip_version is not None else cls._ip_version gateway_not_set = gateway == '' if ip_version == 4: cidr = cidr or netaddr.IPNetwork(CONF.network.tenant_network_cidr) mask_bits = mask_bits or CONF.network.tenant_network_mask_bits elif ip_version == 6: cidr = (cidr or netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr)) mask_bits = mask_bits or CONF.network.tenant_network_v6_mask_bits # Find a cidr that is not in use yet and create a subnet with it for subnet_cidr in cidr.subnet(mask_bits): if gateway_not_set: gateway_ip = str(netaddr.IPAddress(subnet_cidr) + 1) else: gateway_ip = gateway try: body = client.create_subnet(network_id=network['id'], cidr=str(subnet_cidr), ip_version=ip_version, gateway_ip=gateway_ip, **kwargs) break except lib_exc.BadRequest as e: is_overlapping_cidr = 'overlaps with another subnet' in str(e) if not is_overlapping_cidr: raise else: message = 'Available CIDR for subnet creation could not be found' raise exceptions.BuildErrorException(message) subnet = body['subnet'] cls.subnets.append(subnet) return subnet
def wait_for_server_termination(self, server_id, ignore_error=False): """Waits for server to reach termination.""" build_interval = CONF.boto.build_interval while True: try: rs = rest_client.RestClient(self.auth_provider, 'compute', 'RegionOne') ( resp, body, ) = rs.get('servers/%s' % str(server_id)) body = json.loads(body) except lib_exc.NotFound: return server_status = body['server']['status'] if server_status == 'ERROR' and not ignore_error: raise exceptions.BuildErrorException(server_id=server_id) time.sleep(build_interval)
def wait_for_server_status(self, server_id, status, ready_wait=True, extra_timeout=0, raise_on_error=True): """Waits for a server to reach a given status.""" build_timeout = CONF.compute.build_timeout build_interval = CONF.boto.build_interval def _get_task_state(body): return body.get('OS-EXT-STS:task_state', None) rs = rest_client.RestClient(self.auth_provider, "compute", "RegionOne") resp, body = rs.get("servers/%s" % str(server_id)) body = json.loads(body) old_status = server_status = body['server']['status'] old_task_state = task_state = _get_task_state(body) start_time = int(time.time()) timeout = build_timeout + extra_timeout while True: if status == 'BUILD' and server_status != 'UNKNOWN': return if server_status == status: if ready_wait: if status == 'BUILD': return if str(task_state) == "None": time.sleep(CONF.compute.ready_wait) return else: return time.sleep(build_interval) resp, body = rs.get("servers/%s" % str(server_id)) body = json.loads(body) server_status = body['server']['status'] task_state = _get_task_state(body) if (server_status != old_status) or (task_state != old_task_state): oldstatus = '/'.join((old_status, str(old_task_state))) serverstatus = '/'.join((server_status, str(task_state))) waitsec = (time.time() - start_time) LOG.info( _LI('State transtion %(oldstatus)s => %(serverstatus)s' 'after %(waitsec)d second wait') % { 'oldstatus': oldstatus, 'serverstatus': serverstatus, 'waitsec': waitsec }) if (server_status == 'ERROR') and raise_on_error: if 'fault' in body: raise exceptions.BuildErrorException(body['fault'], server_id=server_id) else: raise exceptions.BuildErrorException(server_id=server_id) timed_out = int(time.time()) - start_time >= timeout if timed_out: expected_task_state = 'None' if ready_wait else 'n/a' message = ('Server %(server_id)s failed to reach %(status)s ' 'status and task state "%(expected_task_state)s" ' 'within the required time (%(timeout)s s).' % { 'server_id': server_id, 'status': status, 'expected_task_state': expected_task_state, 'timeout': timeout }) message += ' Current status: %s.' % server_status message += ' Current task state: %s.' % task_state caller = misc_utils.find_test_caller() if caller: message = '(%s) %s' % (caller, message) raise exceptions.TimeoutException(message) old_status = server_status old_task_state = task_state
def wait_for_server_status(client, server_id, status, ready_wait=True, extra_timeout=0, raise_on_error=True): """Waits for a server to reach a given status.""" def _get_task_state(body): return body.get('OS-EXT-STS:task_state', None) # NOTE(afazekas): UNKNOWN status possible on ERROR # or in a very early stage. body = client.get_server(server_id) old_status = server_status = body['status'] old_task_state = task_state = _get_task_state(body) start_time = int(time.time()) timeout = client.build_timeout + extra_timeout while True: # NOTE(afazekas): Now the BUILD status only reached # between the UNKNOWN->ACTIVE transition. # TODO(afazekas): enumerate and validate the stable status set if status == 'BUILD' and server_status != 'UNKNOWN': return if server_status == status: if ready_wait: if status == 'BUILD': return # NOTE(afazekas): The instance is in "ready for action state" # when no task in progress # NOTE(afazekas): Converted to string bacuse of the XML # responses if str(task_state) == "None": # without state api extension 3 sec usually enough time.sleep(CONF.compute.ready_wait) return else: return time.sleep(client.build_interval) body = client.get_server(server_id) server_status = body['status'] task_state = _get_task_state(body) if (server_status != old_status) or (task_state != old_task_state): LOG.info('State transition "%s" ==> "%s" after %d second wait', '/'.join((old_status, str(old_task_state))), '/'.join((server_status, str(task_state))), time.time() - start_time) if (server_status == 'ERROR') and raise_on_error: if 'fault' in body: raise exceptions.BuildErrorException(body['fault'], server_id=server_id) else: raise exceptions.BuildErrorException(server_id=server_id) timed_out = int(time.time()) - start_time >= timeout if timed_out: expected_task_state = 'None' if ready_wait else 'n/a' message = ('Server %(server_id)s failed to reach %(status)s ' 'status and task state "%(expected_task_state)s" ' 'within the required time (%(timeout)s s).' % {'server_id': server_id, 'status': status, 'expected_task_state': expected_task_state, 'timeout': timeout}) message += ' Current status: %s.' % server_status message += ' Current task state: %s.' % task_state caller = misc_utils.find_test_caller() if caller: message = '(%s) %s' % (caller, message) raise exceptions.TimeoutException(message) old_status = server_status old_task_state = task_state