Skip to content

Commit

Permalink
Convert v3 hypervisor plugin to v2.1
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gmannos committed Sep 2, 2014
1 parent 10a6c6c commit 2529fbe
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 764 deletions.
16 changes: 9 additions & 7 deletions doc/v3/api_samples/os-hypervisors/hypervisors-servers-resp.json
@@ -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": []
}
]
}
42 changes: 22 additions & 20 deletions nova/api/openstack/compute/plugins/v3/hypervisors.py
Expand Up @@ -62,7 +62,7 @@ def _view_hypervisor(self, hypervisor, detail, servers=None, **kwargs):
}

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
Expand Down Expand Up @@ -123,33 +123,35 @@ def uptime(self, req, id):
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):
Expand All @@ -170,9 +172,9 @@ def get_resources(self):
resources = [extensions.ResourceExtension(ALIAS,
HypervisorsController(),
collection_actions={'detail': 'GET',
'search': 'GET',
'statistics': 'GET'},
member_actions={'uptime': 'GET',
'search': 'GET',
'servers': 'GET'})]

return resources
Expand Down
157 changes: 73 additions & 84 deletions nova/tests/api/openstack/compute/contrib/test_extended_hypervisors.py
Expand Up @@ -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)
22 changes: 17 additions & 5 deletions nova/tests/api/openstack/compute/contrib/test_hypervisor_status.py
Expand Up @@ -16,10 +16,13 @@

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",
Expand All @@ -32,10 +35,9 @@
)


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)

Expand Down Expand Up @@ -78,3 +80,13 @@ def test_view_hypervisor_detail_status(self):
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)

0 comments on commit 2529fbe

Please sign in to comment.