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.

test_api.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright 2012 OpenStack Foundation
  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. """Version-independent api tests"""
  16. import httplib2
  17. from oslo_serialization import jsonutils
  18. from six.moves import http_client
  19. from glance.tests import functional
  20. def _generate_v2_versions(url):
  21. version_list = []
  22. version_list.extend([
  23. {
  24. 'id': 'v2.6',
  25. 'status': 'CURRENT',
  26. 'links': [{'rel': 'self', 'href': url % '2'}],
  27. },
  28. {
  29. 'id': 'v2.5',
  30. 'status': 'SUPPORTED',
  31. 'links': [{'rel': 'self', 'href': url % '2'}],
  32. },
  33. {
  34. 'id': 'v2.4',
  35. 'status': 'SUPPORTED',
  36. 'links': [{'rel': 'self', 'href': url % '2'}],
  37. },
  38. {
  39. 'id': 'v2.3',
  40. 'status': 'SUPPORTED',
  41. 'links': [{'rel': 'self', 'href': url % '2'}],
  42. },
  43. {
  44. 'id': 'v2.2',
  45. 'status': 'SUPPORTED',
  46. 'links': [{'rel': 'self', 'href': url % '2'}],
  47. },
  48. {
  49. 'id': 'v2.1',
  50. 'status': 'SUPPORTED',
  51. 'links': [{'rel': 'self', 'href': url % '2'}],
  52. },
  53. {
  54. 'id': 'v2.0',
  55. 'status': 'SUPPORTED',
  56. 'links': [{'rel': 'self', 'href': url % '2'}],
  57. }
  58. ])
  59. v2_versions = {'versions': version_list}
  60. return v2_versions
  61. def _generate_all_versions(url):
  62. v2 = _generate_v2_versions(url)
  63. all_versions = {'versions': v2['versions']}
  64. return all_versions
  65. class TestApiVersions(functional.FunctionalTest):
  66. def test_version_configurations(self):
  67. """Test that versioning is handled properly through all channels"""
  68. self.start_servers(**self.__dict__.copy())
  69. url = 'http://127.0.0.1:%d/v%%s/' % self.api_port
  70. versions = _generate_all_versions(url)
  71. # Verify version choices returned.
  72. path = 'http://%s:%d' % ('127.0.0.1', self.api_port)
  73. http = httplib2.Http()
  74. response, content_json = http.request(path, 'GET')
  75. self.assertEqual(http_client.MULTIPLE_CHOICES, response.status)
  76. content = jsonutils.loads(content_json.decode())
  77. self.assertEqual(versions, content)
  78. def test_v2_api_configuration(self):
  79. self.api_server.enable_v2_api = True
  80. self.start_servers(**self.__dict__.copy())
  81. url = 'http://127.0.0.1:%d/v%%s/' % self.api_port
  82. versions = _generate_v2_versions(url)
  83. # Verify version choices returned.
  84. path = 'http://%s:%d' % ('127.0.0.1', self.api_port)
  85. http = httplib2.Http()
  86. response, content_json = http.request(path, 'GET')
  87. self.assertEqual(http_client.MULTIPLE_CHOICES, response.status)
  88. content = jsonutils.loads(content_json.decode())
  89. self.assertEqual(versions, content)
  90. class TestApiPaths(functional.FunctionalTest):
  91. def setUp(self):
  92. super(TestApiPaths, self).setUp()
  93. self.start_servers(**self.__dict__.copy())
  94. url = 'http://127.0.0.1:%d/v%%s/' % self.api_port
  95. self.versions = _generate_all_versions(url)
  96. images = {'images': []}
  97. self.images_json = jsonutils.dumps(images)
  98. def test_get_root_path(self):
  99. """Assert GET / with `no Accept:` header.
  100. Verify version choices returned.
  101. Bug lp:803260 no Accept header causes a 500 in glance-api
  102. """
  103. path = 'http://%s:%d' % ('127.0.0.1', self.api_port)
  104. http = httplib2.Http()
  105. response, content_json = http.request(path, 'GET')
  106. self.assertEqual(http_client.MULTIPLE_CHOICES, response.status)
  107. content = jsonutils.loads(content_json.decode())
  108. self.assertEqual(self.versions, content)
  109. def test_get_root_path_with_unknown_header(self):
  110. """Assert GET / with Accept: unknown header
  111. Verify version choices returned. Verify message in API log about
  112. unknown accept header.
  113. """
  114. path = 'http://%s:%d/' % ('127.0.0.1', self.api_port)
  115. http = httplib2.Http()
  116. headers = {'Accept': 'unknown'}
  117. response, content_json = http.request(path, 'GET', headers=headers)
  118. self.assertEqual(http_client.MULTIPLE_CHOICES, response.status)
  119. content = jsonutils.loads(content_json.decode())
  120. self.assertEqual(self.versions, content)
  121. def test_get_va1_images_path(self):
  122. """Assert GET /va.1/images with no Accept: header
  123. Verify version choices returned
  124. """
  125. path = 'http://%s:%d/va.1/images' % ('127.0.0.1', self.api_port)
  126. http = httplib2.Http()
  127. response, content_json = http.request(path, 'GET')
  128. self.assertEqual(http_client.MULTIPLE_CHOICES, response.status)
  129. content = jsonutils.loads(content_json.decode())
  130. self.assertEqual(self.versions, content)
  131. def test_get_versions_path(self):
  132. """Assert GET /versions with no Accept: header
  133. Verify version choices returned
  134. """
  135. path = 'http://%s:%d/versions' % ('127.0.0.1', self.api_port)
  136. http = httplib2.Http()
  137. response, content_json = http.request(path, 'GET')
  138. self.assertEqual(http_client.OK, response.status)
  139. content = jsonutils.loads(content_json.decode())
  140. self.assertEqual(self.versions, content)
  141. def test_get_versions_choices(self):
  142. """Verify version choices returned"""
  143. path = 'http://%s:%d/v10' % ('127.0.0.1', self.api_port)
  144. http = httplib2.Http()
  145. response, content_json = http.request(path, 'GET')
  146. self.assertEqual(http_client.MULTIPLE_CHOICES, response.status)
  147. content = jsonutils.loads(content_json.decode())
  148. self.assertEqual(self.versions, content)