Add time-bounded wait-for-status to func tests

Replace strategy of time.sleep(s_value) waiting for an image to
reach 'active' with a time-bounded wait-for-status loop function.
That way we can have a more generous s_value, which seems to
sometimes be needed in the gates, without having to use that much
time on every run.

I picked the initial s_value as 4x what was in the current code,
though given that some of the other gate tests run >1 hour, we could
easily make it 600 sec without lengthening the current minimum
time-to-verification of a patch.

Closes-bug: #1767142
Change-Id: Ib771079348d22212c7b6d90fa89e7fd4161422d3
This commit is contained in:
Brian Rosmaita 2018-04-26 19:04:58 -04:00
parent 65f7e49537
commit b2179479db
2 changed files with 70 additions and 3 deletions

View File

@ -0,0 +1,56 @@
# Copyright 2018 Verizon Wireless
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import time
from oslo_serialization import jsonutils
import requests
from six.moves import http_client as http
def wait_for_status(request_path, request_headers, status='active',
max_sec=10, delay_sec=0.2, start_delay_sec=None):
"""
Performs a time-bounded wait for the entity at the request_path to
reach the requested status.
:param request_path: path to use to make the request
:param request_headers: headers to use when making the request
:param status: the status to wait for (default: 'active')
:param max_sec: the maximum number of seconds to wait (default: 10)
:param delay_sec: seconds to sleep before the next request is
made (default: 0.2)
:param start_delay_sec: seconds to wait before making the first
request (default: None)
:raises Exception: if the entity fails to reach the status within
the requested time or if the server returns something
other than a 200 response
"""
start_time = time.time()
done_time = start_time + max_sec
if start_delay_sec:
time.sleep(start_delay_sec)
while time.time() <= done_time:
resp = requests.get(request_path, headers=request_headers)
if resp.status_code != http.OK:
raise Exception("Received {} response from server".format(
resp.status_code))
entity = jsonutils.loads(resp.text)
if entity['status'] == status:
return
time.sleep(delay_sec)
entity_id = request_path.rsplit('/', 1)[1]
msg = "Entity {0} failed to reach status '{1}' within {2} sec"
raise Exception(msg.format(entity_id, status, max_sec))

View File

@ -15,7 +15,6 @@
import os
import signal
import time
import uuid
from oslo_serialization import jsonutils
@ -27,6 +26,7 @@ from six.moves import range
from six.moves import urllib
from glance.tests import functional
from glance.tests.functional import ft_utils as func_utils
from glance.tests import utils as test_utils
@ -247,7 +247,12 @@ class TestImages(functional.FunctionalTest):
# Verify image is in active state and checksum is set
# NOTE(abhishekk): As import is a async call we need to provide
# some timelap to complete the call.
time.sleep(0.5)
path = self._url('/v2/images/%s' % image_id)
func_utils.wait_for_status(request_path=path,
request_headers=self._headers(),
status='active',
max_sec=2,
delay_sec=0.2)
_verify_image_checksum_and_status(
checksum='8f113e38d28a79a5a451b16048cc2b72',
status='active')
@ -380,7 +385,13 @@ class TestImages(functional.FunctionalTest):
# Verify image is in active state and checksum is set
# NOTE(abhishekk): As import is a async call we need to provide
# some timelap to complete the call.
time.sleep(5)
path = self._url('/v2/images/%s' % image_id)
func_utils.wait_for_status(request_path=path,
request_headers=self._headers(),
status='active',
max_sec=20,
delay_sec=0.2,
start_delay_sec=1)
_verify_image_checksum_and_status(
checksum='bcd65f8922f61a9e6a20572ad7aa2bdd',
status='active')