OpenStack Image Management (Glance)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ft_utils.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright 2018 Verizon Wireless
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import six
  16. import time
  17. from oslo_serialization import jsonutils
  18. import requests
  19. from six.moves import http_client as http
  20. def verify_image_hashes_and_status(
  21. test_obj, image_id, checksum=None, os_hash_value=None, status=None,
  22. os_hash_algo='sha512'):
  23. """Makes image-detail request and checks response.
  24. :param test_obj: The test object; expected to have _url() and
  25. _headers() defined on it
  26. :param image_id: Image id to use in the request
  27. :param checksum: Expected checksum (default: None)
  28. :param os_hash_value: Expected multihash value (default: None)
  29. :param status: Expected status (default: None)
  30. :param os_hash_algo: Expected value of os_hash_algo; only checked when
  31. os_hash_value is not None (default: 'sha512')
  32. """
  33. path = test_obj._url('/v2/images/%s' % image_id)
  34. response = requests.get(path, headers=test_obj._headers())
  35. test_obj.assertEqual(http.OK, response.status_code)
  36. image = jsonutils.loads(response.text)
  37. test_obj.assertEqual(checksum, image['checksum'])
  38. if os_hash_value:
  39. # make sure we're using the hashing_algorithm we expect
  40. test_obj.assertEqual(six.text_type(os_hash_algo),
  41. image['os_hash_algo'])
  42. test_obj.assertEqual(os_hash_value, image['os_hash_value'])
  43. test_obj.assertEqual(status, image['status'])
  44. def wait_for_status(request_path, request_headers, status='active',
  45. max_sec=10, delay_sec=0.2, start_delay_sec=None):
  46. """
  47. Performs a time-bounded wait for the entity at the request_path to
  48. reach the requested status.
  49. :param request_path: path to use to make the request
  50. :param request_headers: headers to use when making the request
  51. :param status: the status to wait for (default: 'active')
  52. :param max_sec: the maximum number of seconds to wait (default: 10)
  53. :param delay_sec: seconds to sleep before the next request is
  54. made (default: 0.2)
  55. :param start_delay_sec: seconds to wait before making the first
  56. request (default: None)
  57. :raises Exception: if the entity fails to reach the status within
  58. the requested time or if the server returns something
  59. other than a 200 response
  60. """
  61. start_time = time.time()
  62. done_time = start_time + max_sec
  63. if start_delay_sec:
  64. time.sleep(start_delay_sec)
  65. while time.time() <= done_time:
  66. resp = requests.get(request_path, headers=request_headers)
  67. if resp.status_code != http.OK:
  68. raise Exception("Received {} response from server".format(
  69. resp.status_code))
  70. entity = jsonutils.loads(resp.text)
  71. if entity['status'] == status:
  72. return
  73. time.sleep(delay_sec)
  74. entity_id = request_path.rsplit('/', 1)[1]
  75. msg = "Entity {0} failed to reach status '{1}' within {2} sec"
  76. raise Exception(msg.format(entity_id, status, max_sec))