# Copyright 2015 Servionica LLC
# 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.
from nova.api.openstack.compute import loadbalancer as lb_v2
from nova.loadbalancer.manager import LoadBalancer
from nova import exception
from nova import test
from nova.tests.unit.api.openstack import fakes
import mock
FAKE_LB_HOSTS = {
"node 1": {
"hypervisor_hostname": "compute1.students.dev",
"cpu_used_percent": 14.00,
"ram_total": 2048,
"ram_used": 1024,
"ram_used_percent": 50.00,
"suspend_state": "active",
"mac_to_wake": "00:00:00:00:00:00",
"vcpus": 4
},
"node 2": {
"hypervisor_hostname": "compute2.students.dev",
"cpu_used_percent": 23.00,
"ram_total": 4096,
"ram_used": 1024,
"ram_used_percent": 25.00,
"suspend_state": "active",
"mac_to_wake": "00:00:00:00:00:00",
"vcpus": 8
}
}
def fake_get_nodes():
return [host for _, host in FAKE_LB_HOSTS.iteritems()]
class LbControllerTestV2(test.NoDBTestCase):
"""Test of the OpenStack API /loadbalancer application controller
"""
_prefix = "/v2/fake"
Controller = lb_v2.Controller
fake_request = fakes.HTTPRequest
_fake = "/fake"
def setUp(self):
super(LbControllerTestV2, self).setUp()
self.flags(osapi_compute_extension=[])
fakes.stub_out_networking(self.stubs)
fakes.stub_out_rate_limiting(self.stubs)
self.stubs.Set(LoadBalancer, "get_nodes", fake_get_nodes)
self.controller = self.Controller()
@mock.patch('nova.loadbalancer.manager.LoadBalancer.get_nodes',
return_value=[{
"hypervisor_hostname": "compute1.students.dev",
"cpu_used_percent": 14.00,
"ram_total": 2048,
"ram_used": 1024,
"ram_used_percent": 50.00,
"suspend_state": "active",
"mac_to_wake": "00:00:00:00:00:00",
"vcpus": 4
},
{
"hypervisor_hostname": "compute2.students.dev",
"cpu_used_percent": 23.00,
"ram_total": 4096,
"ram_used": 1024,
"ram_used_percent": 25.00,
"suspend_state": "active",
"mac_to_wake": "00:00:00:00:00:00",
"vcpus": 8
}])
def test_get_nodes(self, mock):
request = self.fake_request.blank(self._prefix + '/loadbalancer')
request.method = 'GET'
host_list = self.controller.index(request)
expected = [{
"hypervisor_hostname": "compute1.students.dev",
"cpu_used_percent": 14.00,
"ram_total": 2048,
"ram_used": 1024,
"ram_used_percent": 50.00,
"suspend_state": "active",
"mac_to_wake": "00:00:00:00:00:00",
"vcpus": 4
}, {
"hypervisor_hostname": "compute2.students.dev",
"cpu_used_percent": 23.00,
"ram_total": 4096,
"ram_used": 1024,
"ram_used_percent": 25.00,
"suspend_state": "active",
"mac_to_wake": "00:00:00:00:00:00",
"vcpus": 8
}]
self.assertEqual(host_list, expected)
@mock.patch('nova.loadbalancer.manager.LoadBalancer.suspend_host')
def test_suspend_host(self, mock):
request = self.fake_request.blank(self._prefix +
'/loadbalancer/action')
request.method = 'POST'
body = {"suspend_host": {"host": "compute1"}}
response = self.controller.suspend_host(request, body)
self.assertEqual(body, response)