Convert v3 hypervisor plugin to v2.1

This patch convert v3 hypervisor plugin to v2.1 and makes v2
unit tests share between v2 and v2.1.

Unit tests has been modified in optimized way.

The differences between v2 and v3 are described on the wiki page
https://wiki.openstack.org/wiki/NovaAPIv2tov3.

Partially implements blueprint v2-on-v3-api

Change-Id: I23ffc10d4c0170cb43ef8aeebf69ae2794a08827
This commit is contained in:
Ghanshyam 2014-09-01 13:36:41 +09:00
parent 10a6c6cb0b
commit 2529fbe1aa
8 changed files with 271 additions and 764 deletions

View File

@ -1,9 +1,11 @@
{
"hypervisor": {
"hypervisor_hostname": "fake-mini",
"id": 1,
"state": "up",
"status": "enabled",
"servers": []
}
"hypervisors": [
{
"hypervisor_hostname": "fake-mini",
"id": 1,
"state": "up",
"status": "enabled",
"servers": []
}
]
}

View File

@ -62,7 +62,7 @@ class HypervisorsController(object):
}
if servers is not None:
hyp_dict['servers'] = [dict(name=serv['name'], id=serv['uuid'])
hyp_dict['servers'] = [dict(name=serv['name'], uuid=serv['uuid'])
for serv in servers]
# Add any additional info
@ -123,33 +123,35 @@ class HypervisorsController(object):
return dict(hypervisor=self._view_hypervisor(hyp, False,
uptime=uptime))
@extensions.expected_errors(400)
def search(self, req):
@extensions.expected_errors(404)
def search(self, req, id):
context = req.environ['nova.context']
authorize(context)
query = req.GET.get('query', None)
if not query:
msg = _("Need parameter 'query' to specify "
"which hypervisor to filter on")
raise webob.exc.HTTPBadRequest(explanation=msg)
hypervisors = self.host_api.compute_node_search_by_hypervisor(
context, query)
return dict(hypervisors=[self._view_hypervisor(hyp, False)
for hyp in hypervisors])
context, id)
if hypervisors:
return dict(hypervisors=[self._view_hypervisor(hyp, False)
for hyp in hypervisors])
else:
msg = _("No hypervisor matching '%s' could be found.") % id
raise webob.exc.HTTPNotFound(explanation=msg)
@extensions.expected_errors(404)
def servers(self, req, id):
context = req.environ['nova.context']
authorize(context)
try:
compute_node = self.host_api.compute_node_get(context, id)
except (ValueError, exception.ComputeHostNotFound):
msg = _("Hypervisor with ID '%s' could not be found.") % id
compute_nodes = self.host_api.compute_node_search_by_hypervisor(
context, id)
if not compute_nodes:
msg = _("No hypervisor matching '%s' could be found.") % id
raise webob.exc.HTTPNotFound(explanation=msg)
instances = self.host_api.instance_get_all_by_host(context,
compute_node['service']['host'])
return dict(hypervisor=self._view_hypervisor(compute_node, False,
instances))
hypervisors = []
for compute_node in compute_nodes:
instances = self.host_api.instance_get_all_by_host(context,
compute_node['service']['host'])
hyp = self._view_hypervisor(compute_node, False, instances)
hypervisors.append(hyp)
return dict(hypervisors=hypervisors)
@extensions.expected_errors(())
def statistics(self, req):
@ -170,9 +172,9 @@ class Hypervisors(extensions.V3APIExtensionBase):
resources = [extensions.ResourceExtension(ALIAS,
HypervisorsController(),
collection_actions={'detail': 'GET',
'search': 'GET',
'statistics': 'GET'},
member_actions={'uptime': 'GET',
'search': 'GET',
'servers': 'GET'})]
return resources

View File

@ -12,105 +12,94 @@
# License for the specific language governing permissions and limitations
# under the License.
from nova.api.openstack.compute.contrib import hypervisors
import copy
import mock
from nova.api.openstack.compute.contrib import hypervisors as hypervisors_v2
from nova.api.openstack.compute.plugins.v3 import hypervisors \
as hypervisors_v21
from nova.api.openstack import extensions
from nova import db
from nova import exception
from nova import test
from nova.tests.api.openstack.compute.contrib import test_hypervisors
from nova.tests.api.openstack import fakes
class ExtendedHypervisorsTest(test_hypervisors.HypervisorsTest):
def fake_compute_node_get(context, compute_id):
for hyper in test_hypervisors.TEST_HYPERS:
if hyper['id'] == compute_id:
return hyper
raise exception.ComputeHostNotFound(host=compute_id)
def fake_compute_node_get_all(context):
return test_hypervisors.TEST_HYPERS
class ExtendedHypervisorsTestV21(test.NoDBTestCase):
DETAIL_HYPERS_DICTS = copy.deepcopy(test_hypervisors.TEST_HYPERS)
del DETAIL_HYPERS_DICTS[0]['service_id']
del DETAIL_HYPERS_DICTS[1]['service_id']
DETAIL_HYPERS_DICTS[0].update({'state': 'up',
'status': 'enabled',
'service': dict(id=1, host='compute1',
disabled_reason=None)})
DETAIL_HYPERS_DICTS[1].update({'state': 'up',
'status': 'enabled',
'service': dict(id=2, host='compute2',
disabled_reason=None)})
def _set_up_controller(self):
self.controller = hypervisors_v21.HypervisorsController()
self.controller.servicegroup_api.service_is_up = mock.MagicMock(
return_value=True)
def _get_request(self):
return fakes.HTTPRequestV3.blank('/os-hypervisors',
use_admin_context=True)
def setUp(self):
super(ExtendedHypervisorsTest, self).setUp()
self.ext_mgr.extensions['os-extended-hypervisors'] = True
self.controller = hypervisors.HypervisorsController(self.ext_mgr)
super(ExtendedHypervisorsTestV21, self).setUp()
self._set_up_controller()
self.stubs.Set(db, 'compute_node_get_all', fake_compute_node_get_all)
self.stubs.Set(db, 'compute_node_get',
fake_compute_node_get)
def test_view_hypervisor_detail_noservers(self):
result = self.controller._view_hypervisor(
test_hypervisors.TEST_HYPERS[0], True)
self.assertEqual(result, dict(
id=1,
hypervisor_hostname="hyper1",
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='1.1.1.1',
service=dict(id=1, host='compute1')))
self.assertEqual(result, self.DETAIL_HYPERS_DICTS[0])
def test_detail(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/detail',
use_admin_context=True)
req = self._get_request()
result = self.controller.detail(req)
self.assertEqual(result, dict(hypervisors=[
dict(id=1,
service=dict(id=1, host="compute1"),
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper1",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='1.1.1.1'),
dict(id=2,
service=dict(id=2, host="compute2"),
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper2",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='2.2.2.2')]))
self.assertEqual(result, dict(hypervisors=self.DETAIL_HYPERS_DICTS))
def test_show_withid(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1',
use_admin_context=True)
req = self._get_request()
result = self.controller.show(req, '1')
self.assertEqual(result, dict(hypervisor=dict(
id=1,
service=dict(id=1, host="compute1"),
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper1",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='1.1.1.1')))
self.assertEqual(result, dict(hypervisor=self.DETAIL_HYPERS_DICTS[0]))
class ExtendedHypervisorsTestV2(ExtendedHypervisorsTestV21):
DETAIL_HYPERS_DICTS = copy.deepcopy(test_hypervisors.TEST_HYPERS)
del DETAIL_HYPERS_DICTS[0]['service_id']
del DETAIL_HYPERS_DICTS[1]['service_id']
DETAIL_HYPERS_DICTS[0].update({'service': dict(id=1, host='compute1')})
DETAIL_HYPERS_DICTS[1].update({'service': dict(id=2, host='compute2')})
def _set_up_controller(self):
self.ext_mgr = extensions.ExtensionManager()
self.ext_mgr.extensions = {}
self.ext_mgr.extensions['os-extended-hypervisors'] = True
self.controller = hypervisors_v2.HypervisorsController(self.ext_mgr)
def _get_request(self):
return fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/detail',
use_admin_context=True)

View File

@ -16,10 +16,13 @@ import copy
import mock
from nova.api.openstack.compute.contrib import hypervisors
from nova.api.openstack.compute.contrib import hypervisors as hypervisors_v2
from nova.api.openstack.compute.plugins.v3 import hypervisors \
as hypervisors_v21
from nova.api.openstack import extensions
from nova import test
from nova.tests.api.openstack.compute.contrib import test_hypervisors
TEST_HYPER = dict(test_hypervisors.TEST_HYPERS[0],
service=dict(id=1,
host="compute1",
@ -32,10 +35,9 @@ TEST_HYPER = dict(test_hypervisors.TEST_HYPERS[0],
)
class HypervisorStatusTest(test_hypervisors.HypervisorsTest):
class HypervisorStatusTestV21(test.NoDBTestCase):
def _prepare_extension(self):
self.ext_mgr.extensions['os-hypervisor-status'] = True
self.controller = hypervisors.HypervisorsController(self.ext_mgr)
self.controller = hypervisors_v21.HypervisorsController()
self.controller.servicegroup_api.service_is_up = mock.MagicMock(
return_value=True)
@ -78,3 +80,13 @@ class HypervisorStatusTest(test_hypervisors.HypervisorsTest):
result = self.controller._view_hypervisor(hyper, True)
self.assertEqual('disabled', result['status'],)
self.assertEqual('fake', result['service']['disabled_reason'])
class HypervisorStatusTestV2(HypervisorStatusTestV21):
def _prepare_extension(self):
ext_mgr = extensions.ExtensionManager()
ext_mgr.extensions = {}
ext_mgr.extensions['os-hypervisor-status'] = True
self.controller = hypervisors_v2.HypervisorsController(ext_mgr)
self.controller.servicegroup_api.service_is_up = mock.MagicMock(
return_value=True)

View File

@ -13,10 +13,15 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
from lxml import etree
import mock
from webob import exc
from nova.api.openstack.compute.contrib import hypervisors
from nova.api.openstack.compute.contrib import hypervisors as hypervisors_v2
from nova.api.openstack.compute.plugins.v3 import hypervisors \
as hypervisors_v21
from nova.api.openstack import extensions
from nova import context
from nova import db
@ -34,6 +39,7 @@ TEST_HYPERS = [
topic="compute_topic",
report_count=5,
disabled=False,
disabled_reason=None,
availability_zone="nova"),
vcpus=4,
memory_mb=10 * 1024,
@ -59,6 +65,7 @@ TEST_HYPERS = [
topic="compute_topic",
report_count=5,
disabled=False,
disabled_reason=None,
availability_zone="nova"),
vcpus=4,
memory_mb=10 * 1024,
@ -131,13 +138,41 @@ def fake_instance_get_all_by_host(context, host):
return results
class HypervisorsTest(test.NoDBTestCase):
class HypervisorsTestV21(test.NoDBTestCase):
DETAIL_HYPERS_DICTS = copy.deepcopy(TEST_HYPERS)
del DETAIL_HYPERS_DICTS[0]['service_id']
del DETAIL_HYPERS_DICTS[1]['service_id']
DETAIL_HYPERS_DICTS[0].update({'state': 'up',
'status': 'enabled',
'service': dict(id=1, host='compute1',
disabled_reason=None)})
DETAIL_HYPERS_DICTS[1].update({'state': 'up',
'status': 'enabled',
'service': dict(id=2, host='compute2',
disabled_reason=None)})
INDEX_HYPER_DICTS = [
dict(id=1, hypervisor_hostname="hyper1",
state='up', status='enabled'),
dict(id=2, hypervisor_hostname="hyper2",
state='up', status='enabled')]
NO_SERVER_HYPER_DICTS = copy.deepcopy(INDEX_HYPER_DICTS)
NO_SERVER_HYPER_DICTS[0].update({'servers': []})
NO_SERVER_HYPER_DICTS[1].update({'servers': []})
def _get_request(self, use_admin_context):
return fakes.HTTPRequestV3.blank('/os-hypervisors',
use_admin_context=use_admin_context)
def _set_up_controller(self):
self.controller = hypervisors_v21.HypervisorsController()
self.controller.servicegroup_api.service_is_up = mock.MagicMock(
return_value=True)
def setUp(self):
super(HypervisorsTest, self).setUp()
self.context = context.get_admin_context()
self.ext_mgr = extensions.ExtensionManager()
self.ext_mgr.extensions = {}
self.controller = hypervisors.HypervisorsController(self.ext_mgr)
super(HypervisorsTestV21, self).setUp()
self._set_up_controller()
self.stubs.Set(db, 'compute_node_get_all', fake_compute_node_get_all)
self.stubs.Set(db, 'compute_node_search_by_hypervisor',
@ -152,149 +187,69 @@ class HypervisorsTest(test.NoDBTestCase):
def test_view_hypervisor_nodetail_noservers(self):
result = self.controller._view_hypervisor(TEST_HYPERS[0], False)
self.assertEqual(result, dict(id=1, hypervisor_hostname="hyper1"))
self.assertEqual(result, self.INDEX_HYPER_DICTS[0])
def test_view_hypervisor_detail_noservers(self):
result = self.controller._view_hypervisor(TEST_HYPERS[0], True)
self.assertEqual(result, dict(
id=1,
hypervisor_hostname="hyper1",
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
service=dict(id=1, host='compute1')))
self.assertEqual(result, self.DETAIL_HYPERS_DICTS[0])
def test_view_hypervisor_servers(self):
result = self.controller._view_hypervisor(TEST_HYPERS[0], False,
TEST_SERVERS)
expected_dict = copy.deepcopy(self.INDEX_HYPER_DICTS[0])
expected_dict.update({'servers': [
dict(name="inst1", uuid="uuid1"),
dict(name="inst2", uuid="uuid2"),
dict(name="inst3", uuid="uuid3"),
dict(name="inst4", uuid="uuid4")]})
self.assertEqual(result, dict(
id=1,
hypervisor_hostname="hyper1",
servers=[
dict(name="inst1", uuid="uuid1"),
dict(name="inst2", uuid="uuid2"),
dict(name="inst3", uuid="uuid3"),
dict(name="inst4", uuid="uuid4")]))
self.assertEqual(result, expected_dict)
def test_index(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors',
use_admin_context=True)
req = self._get_request(True)
result = self.controller.index(req)
self.assertEqual(result, dict(hypervisors=[
dict(id=1, hypervisor_hostname="hyper1"),
dict(id=2, hypervisor_hostname="hyper2")]))
self.assertEqual(result, dict(hypervisors=self.INDEX_HYPER_DICTS))
def test_index_non_admin(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors',
use_admin_context=False)
req = self._get_request(False)
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.index, req)
def test_detail(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/detail',
use_admin_context=True)
req = self._get_request(True)
result = self.controller.detail(req)
self.assertEqual(result, dict(hypervisors=[
dict(id=1,
service=dict(id=1, host="compute1"),
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper1",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100),
dict(id=2,
service=dict(id=2, host="compute2"),
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper2",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100)]))
self.assertEqual(result, dict(hypervisors=self.DETAIL_HYPERS_DICTS))
def test_detail_non_admin(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/detail',
use_admin_context=False)
req = self._get_request(False)
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.detail, req)
def test_show_noid(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/3',
use_admin_context=True)
req = self._get_request(True)
self.assertRaises(exc.HTTPNotFound, self.controller.show, req, '3')
def test_show_non_integer_id(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/abc',
use_admin_context=True)
req = self._get_request(True)
self.assertRaises(exc.HTTPNotFound, self.controller.show, req, 'abc')
def test_show_withid(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1',
use_admin_context=True)
req = self._get_request(True)
result = self.controller.show(req, '1')
self.assertEqual(result, dict(hypervisor=dict(
id=1,
service=dict(id=1, host="compute1"),
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper1",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100)))
self.assertEqual(result, dict(hypervisor=self.DETAIL_HYPERS_DICTS[0]))
def test_show_non_admin(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1',
use_admin_context=False)
req = self._get_request(False)
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.show, req, '1')
def test_uptime_noid(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/3',
use_admin_context=True)
self.assertRaises(exc.HTTPNotFound, self.controller.show, req, '3')
req = self._get_request(True)
self.assertRaises(exc.HTTPNotFound, self.controller.uptime, req, '3')
def test_uptime_notimplemented(self):
def fake_get_host_uptime(context, hyp):
@ -303,8 +258,7 @@ class HypervisorsTest(test.NoDBTestCase):
self.stubs.Set(self.controller.host_api, 'get_host_uptime',
fake_get_host_uptime)
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1',
use_admin_context=True)
req = self._get_request(True)
self.assertRaises(exc.HTTPNotImplemented,
self.controller.uptime, req, '1')
@ -315,38 +269,30 @@ class HypervisorsTest(test.NoDBTestCase):
self.stubs.Set(self.controller.host_api, 'get_host_uptime',
fake_get_host_uptime)
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1',
use_admin_context=True)
req = self._get_request(True)
result = self.controller.uptime(req, '1')
self.assertEqual(result, dict(hypervisor=dict(
id=1,
hypervisor_hostname="hyper1",
uptime="fake uptime")))
expected_dict = copy.deepcopy(self.INDEX_HYPER_DICTS[0])
expected_dict.update({'uptime': "fake uptime"})
self.assertEqual(result, dict(hypervisor=expected_dict))
def test_uptime_non_integer_id(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/abc/uptime',
use_admin_context=True)
req = self._get_request(True)
self.assertRaises(exc.HTTPNotFound, self.controller.uptime, req, 'abc')
def test_uptime_non_admin(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1',
use_admin_context=False)
req = self._get_request(False)
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.uptime, req, '1')
def test_search(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/hyper/search',
use_admin_context=True)
req = self._get_request(True)
result = self.controller.search(req, 'hyper')
self.assertEqual(result, dict(hypervisors=[
dict(id=1, hypervisor_hostname="hyper1"),
dict(id=2, hypervisor_hostname="hyper2")]))
self.assertEqual(result, dict(hypervisors=self.INDEX_HYPER_DICTS))
def test_search_non_admin(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/hyper/search',
use_admin_context=False)
req = self._get_request(False)
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.search, req, '1')
@ -356,32 +302,22 @@ class HypervisorsTest(test.NoDBTestCase):
return []
self.stubs.Set(db, 'compute_node_search_by_hypervisor',
fake_compute_node_search_by_hypervisor_return_empty)
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/a/search',
use_admin_context=True)
req = self._get_request(True)
self.assertRaises(exc.HTTPNotFound, self.controller.search, req, 'a')
def test_servers(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/hyper/servers',
use_admin_context=True)
req = self._get_request(True)
result = self.controller.servers(req, 'hyper')
self.assertEqual(result, dict(hypervisors=[
dict(id=1,
hypervisor_hostname="hyper1",
servers=[
dict(name="inst1", uuid="uuid1"),
dict(name="inst3", uuid="uuid3")]),
dict(id=2,
hypervisor_hostname="hyper2",
servers=[
dict(name="inst2", uuid="uuid2"),
dict(name="inst4", uuid="uuid4")])]))
expected_dict = copy.deepcopy(self.INDEX_HYPER_DICTS)
expected_dict[0].update({'servers': [
dict(name="inst1", uuid="uuid1"),
dict(name="inst3", uuid="uuid3")]})
expected_dict[1].update({'servers': [
dict(name="inst2", uuid="uuid2"),
dict(name="inst4", uuid="uuid4")]})
def test_servers_non_admin(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/hyper/servers',
use_admin_context=False)
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.servers, req, '1')
self.assertEqual(result, dict(hypervisors=expected_dict))
def test_servers_non_id(self):
def fake_compute_node_search_by_hypervisor_return_empty(context,
@ -390,12 +326,16 @@ class HypervisorsTest(test.NoDBTestCase):
self.stubs.Set(db, 'compute_node_search_by_hypervisor',
fake_compute_node_search_by_hypervisor_return_empty)
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/115/servers',
use_admin_context=True)
req = self._get_request(True)
self.assertRaises(exc.HTTPNotFound,
self.controller.servers,
req, '115')
def test_servers_non_admin(self):
req = self._get_request(False)
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.servers, req, '1')
def test_servers_with_non_integer_hypervisor_id(self):
def fake_compute_node_search_by_hypervisor_return_empty(context,
hypervisor_re):
@ -403,8 +343,7 @@ class HypervisorsTest(test.NoDBTestCase):
self.stubs.Set(db, 'compute_node_search_by_hypervisor',
fake_compute_node_search_by_hypervisor_return_empty)
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/abc/servers',
use_admin_context=True)
req = self._get_request(True)
self.assertRaises(exc.HTTPNotFound,
self.controller.servers, req, 'abc')
@ -413,17 +352,12 @@ class HypervisorsTest(test.NoDBTestCase):
return []
self.stubs.Set(db, 'instance_get_all_by_host',
fake_instance_get_all_by_host_return_empty)
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1/servers',
use_admin_context=True)
req = self._get_request(True)
result = self.controller.servers(req, '1')
self.assertEqual(dict(hypervisors=[
dict(id=1, hypervisor_hostname="hyper1"),
dict(id=2, hypervisor_hostname="hyper2")]),
result)
self.assertEqual(result, dict(hypervisors=self.NO_SERVER_HYPER_DICTS))
def test_statistics(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/statistics',
use_admin_context=True)
req = self._get_request(True)
result = self.controller.statistics(req)
self.assertEqual(result, dict(hypervisor_statistics=dict(
@ -441,12 +375,49 @@ class HypervisorsTest(test.NoDBTestCase):
disk_available_least=200)))
def test_statistics_non_admin(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/statistics',
use_admin_context=False)
req = self._get_request(False)
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.statistics, req)
class HypervisorsTestV2(HypervisorsTestV21):
DETAIL_HYPERS_DICTS = copy.deepcopy(
HypervisorsTestV21.DETAIL_HYPERS_DICTS)
del DETAIL_HYPERS_DICTS[0]['state']
del DETAIL_HYPERS_DICTS[1]['state']
del DETAIL_HYPERS_DICTS[0]['status']
del DETAIL_HYPERS_DICTS[1]['status']
del DETAIL_HYPERS_DICTS[0]['service']['disabled_reason']
del DETAIL_HYPERS_DICTS[1]['service']['disabled_reason']
del DETAIL_HYPERS_DICTS[0]['host_ip']
del DETAIL_HYPERS_DICTS[1]['host_ip']
INDEX_HYPER_DICTS = copy.deepcopy(HypervisorsTestV21.INDEX_HYPER_DICTS)
del INDEX_HYPER_DICTS[0]['state']
del INDEX_HYPER_DICTS[1]['state']
del INDEX_HYPER_DICTS[0]['status']
del INDEX_HYPER_DICTS[1]['status']
NO_SERVER_HYPER_DICTS = copy.deepcopy(
HypervisorsTestV21.NO_SERVER_HYPER_DICTS)
del NO_SERVER_HYPER_DICTS[0]['state']
del NO_SERVER_HYPER_DICTS[1]['state']
del NO_SERVER_HYPER_DICTS[0]['status']
del NO_SERVER_HYPER_DICTS[1]['status']
del NO_SERVER_HYPER_DICTS[0]['servers']
del NO_SERVER_HYPER_DICTS[1]['servers']
def _get_request(self, use_admin_context):
return fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/statistics',
use_admin_context=use_admin_context)
def _set_up_controller(self):
self.context = context.get_admin_context()
self.ext_mgr = extensions.ExtensionManager()
self.ext_mgr.extensions = {}
self.controller = hypervisors_v2.HypervisorsController(self.ext_mgr)
class HypervisorsSerializersTest(test.NoDBTestCase):
def compare_to_exemplar(self, exemplar, hyper):
# Check attributes
@ -479,7 +450,7 @@ class HypervisorsSerializersTest(test.NoDBTestCase):
self.assertEqual(len(required_children), 0)
def test_index_serializer(self):
serializer = hypervisors.HypervisorIndexTemplate()
serializer = hypervisors_v2.HypervisorIndexTemplate()
exemplar = dict(hypervisors=[
dict(hypervisor_hostname="hyper1",
id=1),
@ -495,7 +466,7 @@ class HypervisorsSerializersTest(test.NoDBTestCase):
self.compare_to_exemplar(exemplar['hypervisors'][idx], hyper)
def test_detail_serializer(self):
serializer = hypervisors.HypervisorDetailTemplate()
serializer = hypervisors_v2.HypervisorDetailTemplate()
exemplar = dict(hypervisors=[
dict(hypervisor_hostname="hyper1",
id=1,
@ -543,7 +514,7 @@ class HypervisorsSerializersTest(test.NoDBTestCase):
self.compare_to_exemplar(exemplar['hypervisors'][idx], hyper)
def test_show_serializer(self):
serializer = hypervisors.HypervisorTemplate()
serializer = hypervisors_v2.HypervisorTemplate()
exemplar = dict(hypervisor=dict(
hypervisor_hostname="hyper1",
id=1,
@ -570,7 +541,7 @@ class HypervisorsSerializersTest(test.NoDBTestCase):
self.compare_to_exemplar(exemplar['hypervisor'], tree)
def test_uptime_serializer(self):
serializer = hypervisors.HypervisorUptimeTemplate()
serializer = hypervisors_v2.HypervisorUptimeTemplate()
exemplar = dict(hypervisor=dict(
hypervisor_hostname="hyper1",
id=1,
@ -582,7 +553,7 @@ class HypervisorsSerializersTest(test.NoDBTestCase):
self.compare_to_exemplar(exemplar['hypervisor'], tree)
def test_servers_serializer(self):
serializer = hypervisors.HypervisorServersTemplate()
serializer = hypervisors_v2.HypervisorServersTemplate()
exemplar = dict(hypervisors=[
dict(hypervisor_hostname="hyper1",
id=1,
@ -608,7 +579,7 @@ class HypervisorsSerializersTest(test.NoDBTestCase):
self.compare_to_exemplar(exemplar['hypervisors'][idx], hyper)
def test_statistics_serializer(self):
serializer = hypervisors.HypervisorStatisticsTemplate()
serializer = hypervisors_v2.HypervisorStatisticsTemplate()
exemplar = dict(hypervisor_statistics=dict(
count=2,
vcpus=8,

View File

@ -1,471 +0,0 @@
# Copyright (c) 2012 OpenStack Foundation
# 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 copy
import mock
from webob import exc
from nova.api.openstack.compute.plugins.v3 import hypervisors
from nova import db
from nova.db.sqlalchemy import api as db_api
from nova import exception
from nova import test
from nova.tests.api.openstack import fakes
TEST_HYPERS = [
dict(id=1,
service_id=1,
service=dict(id=1,
host="compute1",
binary="nova-compute",
topic="compute_topic",
report_count=5,
disabled=False,
disabled_reason=None,
availability_zone="nova"),
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper1",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='1.1.1.1'),
dict(id=2,
service_id=2,
service=dict(id=2,
host="compute2",
binary="nova-compute",
topic="compute_topic",
report_count=5,
disabled=False,
disabled_reason=None,
availability_zone="nova"),
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper2",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='2.2.2.2')]
TEST_SERVERS = [dict(name="inst1", uuid="uuid1", host="compute1"),
dict(name="inst2", uuid="uuid2", host="compute2"),
dict(name="inst3", uuid="uuid3", host="compute1"),
dict(name="inst4", uuid="uuid4", host="compute2")]
@db_api.require_admin_context
def fake_compute_node_get_all(context):
return TEST_HYPERS
def fake_compute_node_search_by_hypervisor(context, hypervisor_re):
return TEST_HYPERS
def fake_compute_node_get(context, compute_id):
for hyper in TEST_HYPERS:
if hyper['id'] == compute_id:
return hyper
raise exception.ComputeHostNotFound(host=compute_id)
def fake_compute_node_statistics(context):
result = dict(
count=0,
vcpus=0,
memory_mb=0,
local_gb=0,
vcpus_used=0,
memory_mb_used=0,
local_gb_used=0,
free_ram_mb=0,
free_disk_gb=0,
current_workload=0,
running_vms=0,
disk_available_least=0,
)
for hyper in TEST_HYPERS:
for key in result:
if key == 'count':
result[key] += 1
else:
result[key] += hyper[key]
return result
def fake_instance_get_all_by_host(context, host):
results = []
for inst in TEST_SERVERS:
if inst['host'] == host:
results.append(inst)
return results
class HypervisorsTest(test.NoDBTestCase):
def setUp(self):
super(HypervisorsTest, self).setUp()
self.controller = hypervisors.HypervisorsController()
self.controller.servicegroup_api.service_is_up = mock.MagicMock(
return_value=True)
self.stubs.Set(db, 'compute_node_get_all', fake_compute_node_get_all)
self.stubs.Set(db, 'compute_node_search_by_hypervisor',
fake_compute_node_search_by_hypervisor)
self.stubs.Set(db, 'compute_node_get',
fake_compute_node_get)
self.stubs.Set(db, 'compute_node_statistics',
fake_compute_node_statistics)
self.stubs.Set(db, 'instance_get_all_by_host',
fake_instance_get_all_by_host)
def test_view_hypervisor_nodetail_noservers(self):
result = self.controller._view_hypervisor(TEST_HYPERS[0], False)
self.assertEqual(dict(id=1, hypervisor_hostname="hyper1",
state='up', status='enabled'),
result)
def test_view_hypervisor_detail_noservers(self):
result = self.controller._view_hypervisor(TEST_HYPERS[0], True)
self.assertEqual(result, dict(
id=1,
hypervisor_hostname="hyper1",
state='up',
status='enabled',
vcpus=4,
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='1.1.1.1',
service=dict(id=1, host='compute1', disabled_reason=None)))
def test_view_hypervisor_servers(self):
result = self.controller._view_hypervisor(TEST_HYPERS[0], False,
TEST_SERVERS)
self.assertEqual(result, dict(
id=1,
hypervisor_hostname="hyper1",
state='up',
status='enabled',
servers=[
dict(name="inst1", id="uuid1"),
dict(name="inst2", id="uuid2"),
dict(name="inst3", id="uuid3"),
dict(name="inst4", id="uuid4")]))
def test_view_hypervisor_service_status(self):
result = self.controller._view_hypervisor(TEST_HYPERS[0], False)
self.assertEqual('up', result['state'])
self.assertEqual('enabled', result['status'])
self.controller.servicegroup_api.service_is_up.return_value = False
result = self.controller._view_hypervisor(TEST_HYPERS[0], False)
self.assertEqual('down', result['state'])
self.assertEqual('enabled', result['status'])
hyper = copy.deepcopy(TEST_HYPERS[0])
hyper['service']['disabled'] = True
result = self.controller._view_hypervisor(hyper, False)
self.assertEqual('down', result['state'])
self.assertEqual('disabled', result['status'])
def test_index(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors',
use_admin_context=True)
result = self.controller.index(req)
self.assertEqual(result, dict(hypervisors=[
dict(id=1,
hypervisor_hostname="hyper1",
state='up',
status='enabled'),
dict(id=2,
hypervisor_hostname="hyper2",
state='up',
status='enabled')]))
def test_index_non_admin(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors')
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.index, req)
def test_detail(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/detail',
use_admin_context=True)
result = self.controller.detail(req)
self.assertEqual(result, dict(hypervisors=[
dict(id=1,
service=dict(
id=1, host="compute1", disabled_reason=None),
vcpus=4,
state='up',
status='enabled',
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper1",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='1.1.1.1'),
dict(id=2,
service=dict(id=2, host="compute2",
disabled_reason=None),
vcpus=4,
state='up',
status='enabled',
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper2",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='2.2.2.2')]))
def test_detail_non_admin(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/detail')
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.detail, req)
def test_show_noid(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/3',
use_admin_context=True)
self.assertRaises(exc.HTTPNotFound, self.controller.show, req, '3')
def test_show_non_integer_id(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/abc',
use_admin_context=True)
self.assertRaises(exc.HTTPNotFound, self.controller.show, req, 'abc')
def test_show_withid(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/1',
use_admin_context=True)
result = self.controller.show(req, '1')
self.assertEqual(result, dict(hypervisor=dict(
id=1,
service=dict(id=1, host="compute1", disabled_reason=None),
vcpus=4,
state='up',
status='enabled',
memory_mb=10 * 1024,
local_gb=250,
vcpus_used=2,
memory_mb_used=5 * 1024,
local_gb_used=125,
hypervisor_type="xen",
hypervisor_version=3,
hypervisor_hostname="hyper1",
free_ram_mb=5 * 1024,
free_disk_gb=125,
current_workload=2,
running_vms=2,
cpu_info='cpu_info',
disk_available_least=100,
host_ip='1.1.1.1')))
def test_show_non_admin(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/1')
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.show, req, '1')
def test_uptime_noid(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/3',
use_admin_context=True)
self.assertRaises(exc.HTTPNotFound, self.controller.show, req, '3')
def test_uptime_notimplemented(self):
def fake_get_host_uptime(context, hyp):
raise exc.HTTPNotImplemented()
self.stubs.Set(self.controller.host_api, 'get_host_uptime',
fake_get_host_uptime)
req = fakes.HTTPRequestV3.blank('/os-hypervisors/1',
use_admin_context=True)
self.assertRaises(exc.HTTPNotImplemented,
self.controller.uptime, req, '1')
def test_uptime_implemented(self):
def fake_get_host_uptime(context, hyp):
return "fake uptime"
self.stubs.Set(self.controller.host_api, 'get_host_uptime',
fake_get_host_uptime)
req = fakes.HTTPRequestV3.blank('/os-hypervisors/1',
use_admin_context=True)
result = self.controller.uptime(req, '1')
self.assertEqual(result, dict(hypervisor=dict(
id=1,
hypervisor_hostname="hyper1",
state='up',
status='enabled',
uptime="fake uptime")))
def test_uptime_non_integer_id(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/abc/uptime',
use_admin_context=True)
self.assertRaises(exc.HTTPNotFound, self.controller.uptime, req, 'abc')
def test_uptime_non_admin(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/1/uptime')
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.uptime, req, '1')
def test_search(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/search?query=hyper',
use_admin_context=True)
result = self.controller.search(req)
self.assertEqual(result, dict(hypervisors=[
dict(id=1, hypervisor_hostname="hyper1",
state='up', status='enabled'),
dict(id=2, hypervisor_hostname="hyper2",
state='up', status='enabled')]))
def test_search_non_exist(self):
def fake_compute_node_search_by_hypervisor_return_empty(context,
hypervisor_re):
return []
self.stubs.Set(db, 'compute_node_search_by_hypervisor',
fake_compute_node_search_by_hypervisor_return_empty)
req = fakes.HTTPRequestV3.blank('/os-hypervisors/search?query=a',
use_admin_context=True)
result = self.controller.search(req)
self.assertEqual(result, dict(hypervisors=[]))
def test_search_without_query(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/search',
use_admin_context=True)
self.assertRaises(exc.HTTPBadRequest, self.controller.search, req)
def test_servers(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/1/servers',
use_admin_context=True)
result = self.controller.servers(req, '1')
self.assertEqual(result, dict(hypervisor=
dict(id=1,
hypervisor_hostname="hyper1",
state='up',
status='enabled',
servers=[
dict(name="inst1", id="uuid1"),
dict(name="inst3", id="uuid3")])))
def test_servers_non_id(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/3/servers',
use_admin_context=True)
self.assertRaises(exc.HTTPNotFound, self.controller.servers, req, '3')
def test_servers_non_admin(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/1/servers')
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.servers, req, '1')
def test_servers_return_empty(self):
def fake_instance_get_all_by_host_return_empty(context, hypervisor_re):
return []
self.stubs.Set(db, 'instance_get_all_by_host',
fake_instance_get_all_by_host_return_empty)
req = fakes.HTTPRequestV3.blank('/os-hypervisors/1/servers',
use_admin_context=True)
result = self.controller.servers(req, '1')
self.assertEqual(result, dict(hypervisor=
dict(id=1,
hypervisor_hostname="hyper1",
state='up',
status='enabled',
servers=[])))
def test_servers_with_non_integer_hypervisor_id(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/abc/servers',
use_admin_context=True)
self.assertRaises(exc.HTTPNotFound,
self.controller.servers, req, 'abc')
def test_statistics(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/statistics',
use_admin_context=True)
result = self.controller.statistics(req)
self.assertEqual(result, dict(hypervisor_statistics=dict(
count=2,
vcpus=8,
memory_mb=20 * 1024,
local_gb=500,
vcpus_used=4,
memory_mb_used=10 * 1024,
local_gb_used=250,
free_ram_mb=10 * 1024,
free_disk_gb=250,
current_workload=4,
running_vms=4,
disk_available_least=200)))
def test_statistics_non_admin(self):
req = fakes.HTTPRequestV3.blank('/os-hypervisors/statistics')
self.assertRaises(exception.PolicyNotAuthorized,
self.controller.statistics, req)

View File

@ -1,9 +1,11 @@
{
"hypervisor": {
"hypervisor_hostname": "fake-mini",
"id": 1,
"state": "up",
"status": "enabled",
"servers": []
}
"hypervisors": [
{
"hypervisor_hostname": "fake-mini",
"id": 1,
"state": "up",
"status": "enabled",
"servers": []
}
]
}

View File

@ -25,11 +25,11 @@ class HypervisorsSampleJsonTests(api_sample_base.ApiSampleTestBaseV3):
self._verify_response('hypervisors-list-resp', {}, response, 200)
def test_hypervisors_search(self):
response = self._do_get('os-hypervisors/search?query=fake')
response = self._do_get('os-hypervisors/fake/search')
self._verify_response('hypervisors-search-resp', {}, response, 200)
def test_hypervisors_servers(self):
response = self._do_get('os-hypervisors/1/servers')
response = self._do_get('os-hypervisors/fake/servers')
self._verify_response('hypervisors-servers-resp', {}, response, 200)
def test_hypervisors_detail(self):