/nova/tests/unit/api/openstack/compute/contrib/test_extended_hypervisors.py

http://github.com/openstack/nova
Python | 116 lines | 79 code | 24 blank | 13 comment | 4 complexity | 012d51072f2ce71abcd00709342765ab MD5 | raw file
  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 copy
  15. import mock
  16. from nova.api.openstack.compute.contrib import hypervisors as hypervisors_v2
  17. from nova.api.openstack.compute.plugins.v3 import hypervisors \
  18. as hypervisors_v21
  19. from nova.api.openstack import extensions
  20. from nova import exception
  21. from nova import objects
  22. from nova import test
  23. from nova.tests.unit.api.openstack.compute.contrib import test_hypervisors
  24. from nova.tests.unit.api.openstack import fakes
  25. def fake_compute_node_get(context, compute_id):
  26. for hyper in test_hypervisors.TEST_HYPERS_OBJ:
  27. if hyper.id == int(compute_id):
  28. return hyper
  29. raise exception.ComputeHostNotFound(host=compute_id)
  30. def fake_compute_node_get_all(context):
  31. return test_hypervisors.TEST_HYPERS_OBJ
  32. @classmethod
  33. def fake_service_get_by_compute_host(cls, context, host):
  34. for service in test_hypervisors.TEST_SERVICES:
  35. if service.host == host:
  36. return service
  37. class ExtendedHypervisorsTestV21(test.NoDBTestCase):
  38. DETAIL_HYPERS_DICTS = copy.deepcopy(test_hypervisors.TEST_HYPERS)
  39. del DETAIL_HYPERS_DICTS[0]['service_id']
  40. del DETAIL_HYPERS_DICTS[1]['service_id']
  41. del DETAIL_HYPERS_DICTS[0]['host']
  42. del DETAIL_HYPERS_DICTS[1]['host']
  43. DETAIL_HYPERS_DICTS[0].update({'state': 'up',
  44. 'status': 'enabled',
  45. 'service': dict(id=1, host='compute1',
  46. disabled_reason=None)})
  47. DETAIL_HYPERS_DICTS[1].update({'state': 'up',
  48. 'status': 'enabled',
  49. 'service': dict(id=2, host='compute2',
  50. disabled_reason=None)})
  51. def _set_up_controller(self):
  52. self.controller = hypervisors_v21.HypervisorsController()
  53. self.controller.servicegroup_api.service_is_up = mock.MagicMock(
  54. return_value=True)
  55. def _get_request(self):
  56. return fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/detail',
  57. use_admin_context=True)
  58. def setUp(self):
  59. super(ExtendedHypervisorsTestV21, self).setUp()
  60. self._set_up_controller()
  61. self.stubs.Set(self.controller.host_api, 'compute_node_get_all',
  62. fake_compute_node_get_all)
  63. self.stubs.Set(self.controller.host_api, 'compute_node_get',
  64. fake_compute_node_get)
  65. self.stubs.Set(objects.Service, 'get_by_compute_host',
  66. fake_service_get_by_compute_host)
  67. def test_view_hypervisor_detail_noservers(self):
  68. result = self.controller._view_hypervisor(
  69. test_hypervisors.TEST_HYPERS_OBJ[0],
  70. test_hypervisors.TEST_SERVICES[0], True)
  71. self.assertEqual(result, self.DETAIL_HYPERS_DICTS[0])
  72. def test_detail(self):
  73. req = self._get_request()
  74. result = self.controller.detail(req)
  75. self.assertEqual(result, dict(hypervisors=self.DETAIL_HYPERS_DICTS))
  76. def test_show_withid(self):
  77. req = self._get_request()
  78. result = self.controller.show(req, '1')
  79. self.assertEqual(result, dict(hypervisor=self.DETAIL_HYPERS_DICTS[0]))
  80. class ExtendedHypervisorsTestV2(ExtendedHypervisorsTestV21):
  81. DETAIL_HYPERS_DICTS = copy.deepcopy(test_hypervisors.TEST_HYPERS)
  82. del DETAIL_HYPERS_DICTS[0]['service_id']
  83. del DETAIL_HYPERS_DICTS[1]['service_id']
  84. del DETAIL_HYPERS_DICTS[0]['host']
  85. del DETAIL_HYPERS_DICTS[1]['host']
  86. DETAIL_HYPERS_DICTS[0].update({'service': dict(id=1, host='compute1')})
  87. DETAIL_HYPERS_DICTS[1].update({'service': dict(id=2, host='compute2')})
  88. def _set_up_controller(self):
  89. self.ext_mgr = extensions.ExtensionManager()
  90. self.ext_mgr.extensions = {}
  91. self.ext_mgr.extensions['os-extended-hypervisors'] = True
  92. self.controller = hypervisors_v2.HypervisorsController(self.ext_mgr)