OpenStack Compute (Nova)
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.
 
 
 
 
 

152 lines
5.9 KiB

  1. # Copyright 2014 IBM Corp.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import datetime
  15. import mock
  16. from oslo_utils import timeutils
  17. from six.moves.urllib import parse
  18. from nova.tests.functional.api_sample_tests import test_servers
  19. import nova.tests.functional.api_samples_test_base as astb
  20. class SimpleTenantUsageSampleJsonTest(test_servers.ServersSampleBase):
  21. sample_dir = "os-simple-tenant-usage"
  22. def setUp(self):
  23. """setUp method for simple tenant usage."""
  24. super(SimpleTenantUsageSampleJsonTest, self).setUp()
  25. started = timeutils.utcnow()
  26. now = started + datetime.timedelta(hours=1)
  27. timeutils.set_time_override(started)
  28. self._post_server()
  29. timeutils.set_time_override(now)
  30. self.query = {
  31. 'start': str(started),
  32. 'end': str(now)
  33. }
  34. def tearDown(self):
  35. """tearDown method for simple tenant usage."""
  36. super(SimpleTenantUsageSampleJsonTest, self).tearDown()
  37. timeutils.clear_time_override()
  38. def test_get_tenants_usage(self):
  39. # Get api sample to get all tenants usage request.
  40. response = self._do_get('os-simple-tenant-usage?%s' % (
  41. parse.urlencode(self.query)))
  42. self._verify_response('simple-tenant-usage-get', {}, response, 200)
  43. def test_get_tenants_usage_with_detail(self):
  44. # Get all tenants usage information with detail.
  45. query = self.query.copy()
  46. query.update({'detailed': 1})
  47. response = self._do_get('os-simple-tenant-usage?%s' % (
  48. parse.urlencode(query)))
  49. self._verify_response('simple-tenant-usage-get-detail', {},
  50. response, 200)
  51. def test_get_tenant_usage_details(self):
  52. # Get api sample to get specific tenant usage request.
  53. tenant_id = astb.PROJECT_ID
  54. response = self._do_get('os-simple-tenant-usage/%s?%s' % (tenant_id,
  55. parse.urlencode(self.query)))
  56. self._verify_response('simple-tenant-usage-get-specific', {},
  57. response, 200)
  58. class SimpleTenantUsageV240Test(test_servers.ServersSampleBase):
  59. sample_dir = 'os-simple-tenant-usage'
  60. microversion = '2.40'
  61. scenarios = [('v2_40', {'api_major_version': 'v2.1'})]
  62. _use_project_id = False
  63. def setUp(self):
  64. super(SimpleTenantUsageV240Test, self).setUp()
  65. self.api.microversion = self.microversion
  66. self.project_id_0 = astb.PROJECT_ID
  67. self.project_id_1 = '0000000e737461636b20342065000000'
  68. started = timeutils.utcnow()
  69. now = started + datetime.timedelta(hours=1)
  70. timeutils.set_time_override(started)
  71. with mock.patch('oslo_utils.uuidutils.generate_uuid') as mock_uuids:
  72. # make uuids incrementing, so that sort order is deterministic
  73. uuid_format = '1f1deceb-17b5-4c04-84c7-e0d4499c8f%02d'
  74. mock_uuids.side_effect = [uuid_format % x for x in range(100)]
  75. self.project_id = self.project_id_0
  76. self.instance1_uuid = self._post_server(name='instance-1')
  77. self.instance2_uuid = self._post_server(name='instance-2')
  78. self.project_id = self.project_id_1
  79. self.instance3_uuid = self._post_server(name='instance-3')
  80. timeutils.set_time_override(now)
  81. self.query = {
  82. 'start': str(started),
  83. 'end': str(now),
  84. 'limit': '1',
  85. 'marker': self.instance1_uuid,
  86. }
  87. def tearDown(self):
  88. super(SimpleTenantUsageV240Test, self).tearDown()
  89. timeutils.clear_time_override()
  90. def test_get_tenants_usage(self):
  91. url = 'os-simple-tenant-usage?%s'
  92. response = self._do_get(url % (parse.urlencode(self.query)))
  93. template_name = 'simple-tenant-usage-get'
  94. self._verify_response(template_name, {}, response, 200)
  95. def test_get_tenants_usage_with_detail(self):
  96. query = self.query.copy()
  97. query.update({'detailed': 1})
  98. url = 'os-simple-tenant-usage?%s'
  99. response = self._do_get(url % (parse.urlencode(query)))
  100. template_name = 'simple-tenant-usage-get-detail'
  101. self._verify_response(template_name, {}, response, 200)
  102. def test_get_tenant_usage_details(self):
  103. tenant_id = self.project_id_0
  104. url = 'os-simple-tenant-usage/{tenant}?%s'.format(tenant=tenant_id)
  105. response = self._do_get(url % (parse.urlencode(self.query)))
  106. template_name = 'simple-tenant-usage-get-specific'
  107. subs = {'tenant_id': self.project_id_0}
  108. self._verify_response(template_name, subs, response, 200)
  109. def test_get_tenants_usage_end_marker(self):
  110. # When using the last server retrieved as a marker,
  111. # the subsequent usages list should be empty (bug #1796689).
  112. url = 'os-simple-tenant-usage?%s'
  113. query = dict(detailed=1,
  114. start=self.query['start'],
  115. end=self.query['end'])
  116. response = self._do_get(url % (parse.urlencode(query)))
  117. template_name = 'simple-tenant-usage-get-all'
  118. self._verify_response(template_name, {}, response, 200)
  119. last_server = response.json()['tenant_usages'][-1]['server_usages'][-1]
  120. query['marker'] = last_server['instance_id']
  121. response = self._do_get(url % (parse.urlencode(query)))
  122. self.assertEqual(0, len(response.json()['tenant_usages']))