Load Balancing as a Service (LBaaS) for OpenStack
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.
 
 
 

3786 lines
180 KiB

# Copyright 2014 Rackspace
#
# 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 random
from unittest import mock
from octavia_lib.api.drivers import exceptions as lib_exceptions
from oslo_config import cfg
from oslo_config import fixture as oslo_fixture
from oslo_utils import uuidutils
from sqlalchemy.orm import exc as sa_exception
from octavia.common import constants
import octavia.common.context
from octavia.common import data_models
from octavia.common import exceptions
from octavia.network import base as network_base
from octavia.network import data_models as network_models
from octavia.tests.functional.api.v2 import base
class TestLoadBalancer(base.BaseAPITest):
root_tag = 'loadbalancer'
root_tag_list = 'loadbalancers'
root_tag_links = 'loadbalancers_links'
def _assert_request_matches_response(self, req, resp, **optionals):
self.assertTrue(uuidutils.is_uuid_like(resp.get('id')))
req_name = req.get('name')
req_description = req.get('description')
if not req_name:
self.assertEqual('', resp.get('name'))
else:
self.assertEqual(req.get('name'), resp.get('name'))
if not req_description:
self.assertEqual('', resp.get('description'))
else:
self.assertEqual(req.get('description'), resp.get('description'))
self.assertEqual(constants.PENDING_CREATE,
resp.get('provisioning_status'))
self.assertEqual(constants.OFFLINE, resp.get('operating_status'))
self.assertEqual(req.get('admin_state_up', True),
resp.get('admin_state_up'))
self.assertIsNotNone(resp.get('created_at'))
self.assertIsNone(resp.get('updated_at'))
for key, value in optionals.items():
self.assertEqual(value, req.get(key))
def test_empty_list(self):
response = self.get(self.LBS_PATH)
api_list = response.json.get(self.root_tag_list)
self.assertEqual([], api_list)
def test_create(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'tags': ['test_tag1', 'test_tag2']
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
return api_lb
# Make sure the /v2.0 alias is maintained for the life of the v2 API
def test_create_v2_0(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, use_v2_0=True)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
return api_lb
def test_create_using_tenant_id(self):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'tenant_id': self.project_id
}
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
return api_lb
def test_create_without_vip(self):
lb_json = {'name': 'test1',
'project_id': self.project_id}
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
err_msg = ('Validation failure: VIP must contain one of: '
'vip_port_id, vip_network_id, vip_subnet_id.')
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_empty_vip(self):
lb_json = {'vip_subnet_id': '',
'project_id': self.project_id}
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
err_msg = ("Invalid input for field/attribute vip_subnet_id. "
"Value: ''. Value should be UUID format")
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_invalid_vip_subnet(self):
subnet_id = uuidutils.generate_uuid()
lb_json = {'vip_subnet_id': subnet_id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch("octavia.network.drivers.noop_driver.driver"
".NoopManager.get_subnet") as mock_get_subnet:
mock_get_subnet.side_effect = network_base.SubnetNotFound
response = self.post(self.LBS_PATH, body, status=400)
err_msg = 'Subnet {} not found.'.format(subnet_id)
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_invalid_vip_network_subnet(self):
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[])
subnet_id = uuidutils.generate_uuid()
lb_json = {
'vip_subnet_id': subnet_id,
'vip_network_id': network.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch("octavia.network.drivers.noop_driver.driver"
".NoopManager.get_network") as mock_get_network:
mock_get_network.return_value = network
response = self.post(self.LBS_PATH, body, status=400)
err_msg = 'Subnet {} not found.'.format(subnet_id)
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_vip_subnet_fills_network(self):
subnet = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=uuidutils.generate_uuid())
lb_json = {'vip_subnet_id': subnet.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch("octavia.network.drivers.noop_driver.driver"
".NoopManager.get_subnet") as mock_get_subnet:
mock_get_subnet.return_value = subnet
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(subnet.network_id, api_lb.get('vip_network_id'))
def test_create_with_vip_network_has_no_subnet(self):
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[])
lb_json = {
'vip_network_id': network.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch("octavia.network.drivers.noop_driver.driver"
".NoopManager.get_network") as mock_get_network:
mock_get_network.return_value = network
response = self.post(self.LBS_PATH, body, status=400)
err_msg = ("Validation failure: "
"Supplied network does not contain a subnet.")
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_vip_network_picks_subnet_ipv4(self):
network_id = uuidutils.generate_uuid()
subnet1 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=6)
subnet2 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=4)
network = network_models.Network(id=network_id,
subnets=[subnet1.id, subnet2.id])
lb_json = {'vip_network_id': network.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet:
mock_get_network.return_value = network
mock_get_subnet.side_effect = [subnet1, subnet2]
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet2.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network_id, api_lb.get('vip_network_id'))
def test_create_with_vip_network_picks_subnet_ipv6(self):
network_id = uuidutils.generate_uuid()
subnet = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=6)
network = network_models.Network(id=network_id,
subnets=[subnet.id])
lb_json = {'vip_network_id': network_id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet:
mock_get_network.return_value = network
mock_get_subnet.return_value = subnet
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network_id, api_lb.get('vip_network_id'))
def test_create_with_vip_network_picks_subnet_ipv4_avail_ips(self):
self.conf.config(
group='controller_worker',
loadbalancer_topology=constants.TOPOLOGY_ACTIVE_STANDBY)
network_id = uuidutils.generate_uuid()
subnet1 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=4)
subnet2 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=4)
subnet3 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=4)
network = network_models.Network(id=network_id,
subnets=[subnet1.id, subnet2.id,
subnet3.id])
subnet_ip_availability = [{'subnet_id': subnet1.id, 'used_ips': 254,
'total_ips': 254}, {'subnet_id': subnet2.id,
'used_ips': 128, 'total_ips': 254},
{'subnet_id': subnet3.id, 'used_ips': 254,
'total_ips': 254}]
ip_avail = network_models.Network_IP_Availability(
network_id=network.id,
subnet_ip_availability=subnet_ip_availability)
lb_json = {'vip_network_id': network.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network_ip_availability") as (
mock_get_network_ip_availability):
mock_get_network.return_value = network
mock_get_subnet.side_effect = [subnet1, subnet2, subnet3]
mock_get_network_ip_availability.return_value = ip_avail
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet2.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network_id, api_lb.get('vip_network_id'))
def test_create_with_vip_network_not_enough_avail_ips(self):
self.conf.config(
group='controller_worker',
loadbalancer_topology=constants.TOPOLOGY_ACTIVE_STANDBY)
network_id = uuidutils.generate_uuid()
subnet1 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=4)
subnet2 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=4)
network = network_models.Network(id=network_id,
subnets=[subnet1.id, subnet2.id])
subnet_ip_availability = [{'subnet_id': subnet1.id, 'used_ips': 254,
'total_ips': 254}, {'subnet_id': subnet2.id,
'used_ips': 254, 'total_ips': 254}]
ip_avail = network_models.Network_IP_Availability(
network_id=network.id,
subnet_ip_availability=subnet_ip_availability)
lb_json = {'vip_network_id': network.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network_ip_availability") as (
mock_get_network_ip_availability):
mock_get_network.return_value = network
mock_get_subnet.side_effect = [subnet1, subnet2]
mock_get_network_ip_availability.return_value = ip_avail
response = self.post(self.LBS_PATH, body, status=400)
err_msg = ('Validation failure: Subnet(s) in the supplied network do '
'not contain enough available IPs.')
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_vip_network_and_address(self):
ip_address = '198.51.100.10'
network_id = uuidutils.generate_uuid()
subnet1 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
cidr='2001:DB8::/32',
ip_version=6)
subnet2 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
cidr='198.51.100.0/24',
ip_version=4)
network = network_models.Network(id=network_id,
subnets=[subnet1.id, subnet2.id])
lb_json = {'vip_network_id': network.id,
'vip_address': ip_address,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet:
mock_get_network.return_value = network
mock_get_subnet.side_effect = [subnet1, subnet2]
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet2.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network.id, api_lb.get('vip_network_id'))
self.assertEqual(ip_address, api_lb.get('vip_address'))
def test_create_with_vip_network_and_address_no_subnet_match(self):
ip_address = '198.51.100.10'
network_id = uuidutils.generate_uuid()
subnet1 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
cidr='2001:DB8::/32',
ip_version=6)
subnet2 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
cidr='203.0.113.0/24',
ip_version=4)
network = network_models.Network(id=network_id,
subnets=[subnet1.id, subnet2.id])
lb_json = {'vip_network_id': network.id,
'vip_address': ip_address,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet:
mock_get_network.return_value = network
mock_get_subnet.side_effect = [subnet1, subnet2]
response = self.post(self.LBS_PATH, body, status=400)
err_msg = ('Validation failure: Supplied network does not contain a '
'subnet for VIP address specified.')
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_vip_network_and_address_ipv6(self):
ip_address = '2001:DB8::10'
network_id = uuidutils.generate_uuid()
subnet1 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
cidr='2001:DB8::/32',
ip_version=6)
subnet2 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
cidr='198.51.100.0/24',
ip_version=4)
network = network_models.Network(id=network_id,
subnets=[subnet1.id, subnet2.id])
lb_json = {'vip_network_id': network.id,
'vip_address': ip_address,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet:
mock_get_network.return_value = network
mock_get_subnet.side_effect = [subnet1, subnet2]
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet1.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network.id, api_lb.get('vip_network_id'))
self.assertEqual(ip_address, api_lb.get('vip_address'))
# Note: This test is using the unique local address range to
# validate that we handle a fully expaned IP address properly.
# This is not possible with the documentation/testnet range.
def test_create_with_vip_network_and_address_full_ipv6(self):
ip_address = 'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
network_id = uuidutils.generate_uuid()
subnet1 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
cidr='fc00::/7',
ip_version=6)
subnet2 = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
cidr='198.51.100.0/24',
ip_version=4)
network = network_models.Network(id=network_id,
subnets=[subnet1.id, subnet2.id])
lb_json = {'vip_network_id': network.id,
'vip_address': ip_address,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet:
mock_get_network.return_value = network
mock_get_subnet.side_effect = [subnet1, subnet2]
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet1.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network.id, api_lb.get('vip_network_id'))
self.assertEqual(ip_address, api_lb.get('vip_address'))
def test_create_with_vip_port_1_fixed_ip(self):
ip_address = '198.51.100.1'
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
fixed_ip = network_models.FixedIP(subnet_id=subnet.id,
ip_address=ip_address)
port = network_models.Port(id=uuidutils.generate_uuid(),
fixed_ips=[fixed_ip],
network_id=network.id)
lb_json = {
'name': 'test1', 'description': 'test1_desc',
'vip_port_id': port.id, 'admin_state_up': False,
'project_id': self.project_id}
body = self._build_body(lb_json)
# This test needs the provider driver to not supply the VIP port
# so mocking noop to not supply a VIP port.
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port, mock.patch(
"octavia.api.drivers.noop_driver.driver.NoopManager."
"create_vip_port") as mock_provider:
mock_get_network.return_value = network
mock_get_port.return_value = port
mock_provider.side_effect = lib_exceptions.NotImplementedError()
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(ip_address, api_lb.get('vip_address'))
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network.id, api_lb.get('vip_network_id'))
self.assertEqual(port.id, api_lb.get('vip_port_id'))
def test_create_with_vip_port_2_fixed_ip(self):
ip_address = '198.51.100.1'
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
fixed_ip = network_models.FixedIP(subnet_id=subnet.id,
ip_address=ip_address)
fixed_ip_2 = network_models.FixedIP(
subnet_id=uuidutils.generate_uuid(), ip_address='203.0.113.5')
port = network_models.Port(id=uuidutils.generate_uuid(),
fixed_ips=[fixed_ip, fixed_ip_2],
network_id=network.id)
lb_json = {
'name': 'test1', 'description': 'test1_desc',
'vip_port_id': port.id, 'admin_state_up': False,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port:
mock_get_network.return_value = network
mock_get_port.return_value = port
response = self.post(self.LBS_PATH, body, status=400)
err_msg = ("Validation failure: "
"VIP port's subnet could not be determined. Please "
"specify either a VIP subnet or address.")
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_vip_port_and_address(self):
ip_address = '198.51.100.1'
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
fixed_ip = network_models.FixedIP(subnet_id=subnet.id,
ip_address=ip_address)
port = network_models.Port(id=uuidutils.generate_uuid(),
fixed_ips=[fixed_ip],
network_id=network.id)
lb_json = {
'name': 'test1', 'description': 'test1_desc',
'vip_port_id': port.id, 'vip_address': ip_address,
'admin_state_up': False, 'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port:
mock_get_network.return_value = network
mock_get_port.return_value = port
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(ip_address, api_lb.get('vip_address'))
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network.id, api_lb.get('vip_network_id'))
self.assertEqual(port.id, api_lb.get('vip_port_id'))
def test_create_with_vip_port_and_bad_address(self):
ip_address = '198.51.100.1'
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
fixed_ip = network_models.FixedIP(subnet_id=subnet.id,
ip_address=ip_address)
port = network_models.Port(id=uuidutils.generate_uuid(),
fixed_ips=[fixed_ip],
network_id=network.id)
lb_json = {
'name': 'test1', 'description': 'test1_desc',
'vip_port_id': port.id, 'vip_address': '203.0.113.7',
'admin_state_up': False, 'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port:
mock_get_network.return_value = network
mock_get_port.return_value = port
response = self.post(self.LBS_PATH, body, status=400)
err_msg = ("Validation failure: "
"Specified VIP address not found on the specified VIP "
"port.")
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_vip_full(self):
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
port = network_models.Port(id=uuidutils.generate_uuid(),
network_id=network.id)
lb_json = {
'name': 'test1', 'description': 'test1_desc',
'vip_address': '10.0.0.1', 'vip_subnet_id': subnet.id,
'vip_network_id': network.id, 'vip_port_id': port.id,
'admin_state_up': False, 'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port:
mock_get_network.return_value = network
mock_get_port.return_value = port
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual('10.0.0.1', api_lb.get('vip_address'))
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network.id, api_lb.get('vip_network_id'))
self.assertEqual(port.id, api_lb.get('vip_port_id'))
def test_create_neutron_failure(self):
class TestNeutronException(network_base.AllocateVIPException):
def __init__(self, message, orig_msg, orig_code):
super(TestNeutronException, self).__init__(
message, orig_msg=orig_msg, orig_code=orig_code,
)
def __str__(self):
return repr(self.message)
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
port = network_models.Port(id=uuidutils.generate_uuid(),
network_id=network.id)
lb_json = {
'name': 'test1', 'description': 'test1_desc',
'vip_address': '10.0.0.1', 'vip_subnet_id': subnet.id,
'vip_network_id': network.id, 'vip_port_id': port.id,
'admin_state_up': False, 'project_id': self.project_id}
body = self._build_body(lb_json)
# This test needs the provider driver to not supply the VIP port
# so mocking noop to not supply a VIP port.
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".allocate_vip") as mock_allocate_vip, mock.patch(
"octavia.api.drivers.noop_driver.driver.NoopManager."
"create_vip_port") as mock_provider:
mock_get_network.return_value = network
mock_get_port.return_value = port
mock_allocate_vip.side_effect = TestNeutronException(
"octavia_msg", "neutron_msg", 409)
mock_provider.side_effect = lib_exceptions.NotImplementedError()
response = self.post(self.LBS_PATH, body, status=409)
# Make sure the faultstring contains the neutron error and not
# the octavia error message
self.assertIn("neutron_msg", response.json.get("faultstring"))
def test_create_with_qos(self):
subnet = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=uuidutils.generate_uuid())
qos_policy_id = uuidutils.generate_uuid()
# Test with specific vip_qos_policy_id
lb_json = {'vip_subnet_id': subnet.id,
'project_id': self.project_id,
'vip_qos_policy_id': qos_policy_id}
body = self._build_body(lb_json)
with mock.patch("octavia.network.drivers.noop_driver.driver"
".NoopManager.get_subnet") as mock_get_subnet:
with mock.patch("octavia.common.validate."
"qos_policy_exists") as mock_get_qos:
mock_get_subnet.return_value = subnet
mock_get_qos.return_value = qos_policy_id
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(qos_policy_id, api_lb.get('vip_qos_policy_id'))
def test_create_with_qos_vip_port(self):
# Test with vip_port_id which applied qos_policy
subnet = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=uuidutils.generate_uuid())
port_qos_policy_id = uuidutils.generate_uuid()
ip_address = '192.168.50.50'
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
fixed_ip = network_models.FixedIP(subnet_id=subnet.id,
ip_address=ip_address)
port = network_models.Port(id=uuidutils.generate_uuid(),
fixed_ips=[fixed_ip],
network_id=network.id,
qos_policy_id=port_qos_policy_id)
lb_json = {'vip_port_id': port.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver."
"NoopManager.get_network") as m_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".allocate_vip") as mock_allocate_vip, mock.patch(
"octavia.common.validate."
"qos_policy_exists") as m_get_qos:
m_get_qos.return_value = port_qos_policy_id
mock_allocate_vip.return_value = data_models.Vip(
ip_address=ip_address, subnet_id=subnet.id,
network_id=network.id, port_id=port.id)
m_get_network.return_value = network
mock_get_port.return_value = port
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(port.id, api_lb.get('vip_port_id'))
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network.id, api_lb.get('vip_network_id'))
self.assertEqual(port_qos_policy_id, api_lb.get(
'vip_qos_policy_id'))
def test_create_with_qos_vip_port_and_vip_qos(self):
subnet = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=uuidutils.generate_uuid())
port_qos_policy_id = uuidutils.generate_uuid()
new_qos_policy_id = uuidutils.generate_uuid()
ip_address = '192.168.50.50'
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
fixed_ip = network_models.FixedIP(subnet_id=subnet.id,
ip_address=ip_address)
port = network_models.Port(id=uuidutils.generate_uuid(),
fixed_ips=[fixed_ip],
network_id=network.id,
qos_policy_id=port_qos_policy_id)
lb_json = {'vip_port_id': port.id,
'project_id': self.project_id,
'vip_qos_policy_id': new_qos_policy_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver."
"NoopManager.get_network") as m_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".allocate_vip") as mock_allocate_vip, mock.patch(
"octavia.common.validate."
"qos_policy_exists") as m_get_qos:
m_get_qos.return_value = mock.ANY
mock_allocate_vip.return_value = data_models.Vip(
ip_address=ip_address, subnet_id=subnet.id,
network_id=network.id, port_id=port.id)
m_get_network.return_value = network
mock_get_port.return_value = port
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(port.id, api_lb.get('vip_port_id'))
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network.id, api_lb.get('vip_network_id'))
self.assertEqual(new_qos_policy_id, api_lb.get(
'vip_qos_policy_id'))
def test_create_with_non_exist_qos_policy_id(self):
subnet = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=uuidutils.generate_uuid())
qos_policy_id = uuidutils.generate_uuid()
lb_json = {'vip_subnet_id': subnet.id,
'project_id': self.project_id,
'vip_qos_policy_id': qos_policy_id}
body = self._build_body(lb_json)
with mock.patch("octavia.network.drivers.noop_driver.driver"
".NoopManager.get_subnet") as mock_get_subnet:
with mock.patch("octavia.network.drivers.noop_driver."
"driver.NoopManager."
"get_qos_policy") as mock_get_qos:
mock_get_subnet.return_value = subnet
mock_get_qos.side_effect = Exception()
response = self.post(self.LBS_PATH, body, status=400)
err_msg = "qos_policy %s not found." % qos_policy_id
self.assertEqual(err_msg, response.json.get('faultstring'))
def test_create_with_long_name(self):
lb_json = {'name': 'n' * 256,
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id}
response = self.post(self.LBS_PATH, self._build_body(lb_json),
status=400)
self.assertIn('Invalid input for field/attribute name',
response.json.get('faultstring'))
def test_create_with_long_description(self):
lb_json = {'description': 'n' * 256,
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id}
response = self.post(self.LBS_PATH, self._build_body(lb_json),
status=400)
self.assertIn('Invalid input for field/attribute description',
response.json.get('faultstring'))
def test_create_with_nonuuid_vip_attributes(self):
lb_json = {'vip_subnet_id': 'HI',
'project_id': self.project_id}
response = self.post(self.LBS_PATH, self._build_body(lb_json),
status=400)
self.assertIn('Invalid input for field/attribute vip_subnet_id',
response.json.get('faultstring'))
def test_create_with_allowed_network_id(self):
network_id = uuidutils.generate_uuid()
self.conf.config(group="networking", valid_vip_networks=network_id)
subnet = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id,
ip_version=4)
network = network_models.Network(id=network_id, subnets=[subnet.id])
lb_json = {'vip_network_id': network.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet:
mock_get_network.return_value = network
mock_get_subnet.return_value = subnet
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
self.assertEqual(subnet.id, api_lb.get('vip_subnet_id'))
self.assertEqual(network_id, api_lb.get('vip_network_id'))
def test_create_with_disallowed_network_id(self):
network_id1 = uuidutils.generate_uuid()
network_id2 = uuidutils.generate_uuid()
self.conf.config(group="networking", valid_vip_networks=network_id1)
subnet = network_models.Subnet(id=uuidutils.generate_uuid(),
network_id=network_id2,
ip_version=4)
network = network_models.Network(id=network_id2, subnets=[subnet.id])
lb_json = {'vip_network_id': network.id,
'project_id': self.project_id}
body = self._build_body(lb_json)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_subnet") as mock_get_subnet:
mock_get_network.return_value = network
mock_get_subnet.return_value = subnet
response = self.post(self.LBS_PATH, body, status=400)
self.assertIn("Supplied VIP network_id is not allowed",
response.json.get('faultstring'))
def test_create_with_disallowed_vip_objects(self):
self.conf.config(group="networking", allow_vip_network_id=False)
self.conf.config(group="networking", allow_vip_subnet_id=False)
self.conf.config(group="networking", allow_vip_port_id=False)
lb_json = {'vip_network_id': uuidutils.generate_uuid(),
'project_id': self.project_id}
response = self.post(self.LBS_PATH, self._build_body(lb_json),
status=400)
self.assertIn('use of vip_network_id is disallowed',
response.json.get('faultstring'))
lb_json = {'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id}
response = self.post(self.LBS_PATH, self._build_body(lb_json),
status=400)
self.assertIn('use of vip_subnet_id is disallowed',
response.json.get('faultstring'))
lb_json = {'vip_port_id': uuidutils.generate_uuid(),
'project_id': self.project_id}
response = self.post(self.LBS_PATH, self._build_body(lb_json),
status=400)
self.assertIn('use of vip_port_id is disallowed',
response.json.get('faultstring'))
def test_create_with_project_id(self):
project_id = uuidutils.generate_uuid()
api_lb = self.test_create(project_id=project_id)
self.assertEqual(project_id, api_lb.get('project_id'))
def test_create_no_project_id(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid()
}
lb_json.update(optionals)
body = self._build_body(lb_json)
self.post(self.LBS_PATH, body, status=400)
def test_create_context_project_id(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid()
}
lb_json.update(optionals)
body = self._build_body(lb_json)
with mock.patch.object(octavia.common.context.Context, 'project_id',
self.project_id):
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
def test_create_authorized(self, **optionals):
self.conf = self.useFixture(oslo_fixture.Config(cfg.CONF))
auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
project_id = uuidutils.generate_uuid()
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': project_id
}
lb_json.update(optionals)
body = self._build_body(lb_json)
with mock.patch.object(octavia.common.context.Context, 'project_id',
project_id):
override_credentials = {
'service_user_id': None,
'user_domain_id': None,
'is_admin_project': True,
'service_project_domain_id': None,
'service_project_id': None,
'roles': ['load-balancer_member'],
'user_id': None,
'is_admin': False,
'service_user_domain_id': None,
'project_domain_id': None,
'service_roles': [],
'project_id': project_id}
with mock.patch(
"oslo_context.context.RequestContext.to_policy_values",
return_value=override_credentials):
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self.conf.config(group='api_settings', auth_strategy=auth_strategy)
self._assert_request_matches_response(lb_json, api_lb)
def test_create_not_authorized(self, **optionals):
self.conf = self.useFixture(oslo_fixture.Config(cfg.CONF))
auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': uuidutils.generate_uuid()
}
lb_json.update(optionals)
body = self._build_body(lb_json)
with mock.patch.object(octavia.common.context.Context, 'project_id',
uuidutils.generate_uuid()):
response = self.post(self.LBS_PATH, body, status=403)
api_lb = response.json
self.conf.config(group='api_settings', auth_strategy=auth_strategy)
self.assertEqual(self.NOT_AUTHORIZED_BODY, api_lb)
def test_create_provider_octavia(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'provider': constants.OCTAVIA
}
lb_json.update(optionals)
body = self._build_body(lb_json)
with mock.patch('oslo_messaging.get_rpc_transport'):
with mock.patch('oslo_messaging.Target'):
with mock.patch('oslo_messaging.RPCClient'):
response = self.post(self.LBS_PATH, body)
api_lb = response.json.get(self.root_tag)
self._assert_request_matches_response(lb_json, api_lb)
return api_lb
def test_create_provider_bogus(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'provider': 'BOGUS'
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
self.assertIn("Provider 'BOGUS' is not enabled.",
response.json.get('faultstring'))
def test_create_flavor_bad_type(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'flavor_id': 'BOGUS'
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
self.assertIn("Invalid input for field/attribute flavor_id. Value: "
"'BOGUS'. Value should be UUID format",
response.json.get('faultstring'))
def test_create_flavor_invalid(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'flavor_id': uuidutils.generate_uuid()
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
self.assertIn("Validation failure: Invalid flavor_id.",
response.json.get('faultstring'))
def test_create_flavor_disabled(self, **optionals):
fp = self.create_flavor_profile('test1', 'noop_driver',
'{"image": "ubuntu"}')
flavor = self.create_flavor('name1', 'description',
fp.get('id'), False)
test_flavor_id = flavor.get('id')
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'flavor_id': test_flavor_id,
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
ref_faultstring = ('The selected flavor is not allowed in this '
'deployment: {}'.format(test_flavor_id))
self.assertEqual(ref_faultstring, response.json.get('faultstring'))
def test_create_flavor_missing(self, **optionals):
fp = self.create_flavor_profile('test1', 'noop_driver',
'{"image": "ubuntu"}')
flavor = self.create_flavor('name1', 'description', fp.get('id'), True)
test_flavor_id = flavor.get('id')
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'flavor_id': test_flavor_id
}
lb_json.update(optionals)
body = self._build_body(lb_json)
with mock.patch('octavia.db.repositories.FlavorRepository.'
'get_flavor_metadata_dict',
side_effect=sa_exception.NoResultFound):
response = self.post(self.LBS_PATH, body, status=400)
self.assertIn("Validation failure: Invalid flavor_id.",
response.json.get('faultstring'))
def test_create_flavor_no_provider(self, **optionals):
fp = self.create_flavor_profile('test1', 'noop_driver',
'{"image": "ubuntu"}')
flavor = self.create_flavor('name1', 'description', fp.get('id'), True)
test_flavor_id = flavor.get('id')
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'flavor_id': test_flavor_id,
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=201)
api_lb = response.json.get(self.root_tag)
self.assertEqual('noop_driver', api_lb.get('provider'))
self.assertEqual(test_flavor_id, api_lb.get('flavor_id'))
def test_create_with_availability_zone(self, **optionals):
zone_name = 'nova'
azp = self.create_availability_zone_profile(
'test1', 'noop_driver', '{"compute_zone": "%s"}' % zone_name)
az = self.create_availability_zone(zone_name, 'description',
azp.get('id'), True)
api_lb = self.test_create(availability_zone=az.get('name'))
self.assertEqual(zone_name, api_lb.get('availability_zone'))
def test_create_az_disabled(self, **optionals):
zone_name = 'nova'
azp = self.create_availability_zone_profile(
'test1', 'noop_driver', '{"compute_zone": "%s"}' % zone_name)
az = self.create_availability_zone(zone_name, 'description',
azp.get('id'), False)
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'availability_zone': az.get('name'),
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
ref_faultstring = ('The selected availability_zone is not allowed in '
'this deployment: {}'.format(zone_name))
self.assertEqual(ref_faultstring, response.json.get('faultstring'))
def test_create_az_missing(self, **optionals):
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'availability_zone': 'bogus-az',
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
ref_faultstring = 'Validation failure: Invalid availability zone.'
self.assertEqual(ref_faultstring, response.json.get('faultstring'))
@mock.patch('octavia.api.drivers.utils.call_provider')
def test_create_az_unsupported(self, mock_provider):
zone_name = 'nova'
azp = self.create_availability_zone_profile(
'test1', 'noop_driver', '{"compute_zone": "%s"}' % zone_name)
az = self.create_availability_zone(zone_name, 'description',
azp.get('id'), True)
mock_provider.side_effect = NotImplementedError
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'availability_zone': az.get('name'),
}
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=501)
ref_faultstring = ("Provider \'noop_driver\' does not support a "
"requested action: This provider does not support "
"availability zones.")
self.assertEqual(ref_faultstring, response.json.get('faultstring'))
def test_matching_providers(self, **optionals):
fp = self.create_flavor_profile('test1', 'noop_driver',
'{"image": "ubuntu"}')
flavor = self.create_flavor('name1', 'description', fp.get('id'), True)
test_flavor_id = flavor.get('id')
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'flavor_id': test_flavor_id,
'provider': 'noop_driver'
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=201)
api_lb = response.json.get(self.root_tag)
self.assertEqual('noop_driver', api_lb.get('provider'))
self.assertEqual(test_flavor_id, api_lb.get('flavor_id'))
def test_conflicting_providers(self, **optionals):
fp = self.create_flavor_profile('test1', 'noop_driver',
'{"image": "ubuntu"}')
flavor = self.create_flavor('name1', 'description', fp.get('id'), True)
test_flavor_id = flavor.get('id')
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id,
'flavor_id': test_flavor_id,
'provider': 'noop_driver-alt'
}
lb_json.update(optionals)
body = self._build_body(lb_json)
response = self.post(self.LBS_PATH, body, status=400)
self.assertIn("Flavor '{}' is not compatible with provider "
"'noop_driver-alt'".format(test_flavor_id),
response.json.get('faultstring'))
def test_get_all_admin(self):
project_id = uuidutils.generate_uuid()
lb1 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1', project_id=self.project_id,
tags=['test_tag1'])
lb2 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb2', project_id=project_id,
tags=['test_tag2'])
lb3 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb3', project_id=project_id,
tags=['test_tag3'])
response = self.get(self.LBS_PATH)
lbs = response.json.get(self.root_tag_list)
self.assertEqual(3, len(lbs))
lb_id_names = [(lb.get('id'),
lb.get('name'),
lb.get('tags')) for lb in lbs]
lb1 = lb1.get(self.root_tag)
lb2 = lb2.get(self.root_tag)
lb3 = lb3.get(self.root_tag)
self.assertIn((lb1.get('id'), lb1.get('name'), lb1.get('tags')),
lb_id_names)
self.assertIn((lb2.get('id'), lb2.get('name'), lb2.get('tags')),
lb_id_names)
self.assertIn((lb3.get('id'), lb3.get('name'), lb3.get('tags')),
lb_id_names)
def test_get_all_non_admin(self):
project_id = uuidutils.generate_uuid()
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1', project_id=project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb2', project_id=project_id)
lb3 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb3', project_id=self.project_id)
lb3 = lb3.get(self.root_tag)
auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
self.conf.config(group='api_settings',
auth_strategy=constants.KEYSTONE)
with mock.patch.object(octavia.common.context.Context, 'project_id',
self.project_id):
override_credentials = {
'service_user_id': None,
'user_domain_id': None,
'is_admin_project': True,
'service_project_domain_id': None,
'service_project_id': None,
'roles': ['load-balancer_member'],
'user_id': None,
'is_admin': False,
'service_user_domain_id': None,
'project_domain_id': None,
'service_roles': [],
'project_id': self.project_id}
with mock.patch(
"oslo_context.context.RequestContext.to_policy_values",
return_value=override_credentials):
response = self.get(self.LBS_PATH)
self.conf.config(group='api_settings', auth_strategy=auth_strategy)
lbs = response.json.get(self.root_tag_list)
self.assertEqual(1, len(lbs))
lb_id_names = [(lb.get('id'), lb.get('name')) for lb in lbs]
self.assertIn((lb3.get('id'), lb3.get('name')), lb_id_names)
def test_get_all_non_admin_global_observer(self):
project_id = uuidutils.generate_uuid()
lb1 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1', project_id=project_id)
lb2 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb2', project_id=project_id)
lb3 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb3', project_id=self.project_id)
lb1 = lb1.get(self.root_tag)
lb2 = lb2.get(self.root_tag)
lb3 = lb3.get(self.root_tag)
auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
self.conf.config(group='api_settings',
auth_strategy=constants.KEYSTONE)
with mock.patch.object(octavia.common.context.Context, 'project_id',
self.project_id):
override_credentials = {
'service_user_id': None,
'user_domain_id': None,
'is_admin_project': True,
'service_project_domain_id': None,
'service_project_id': None,
'roles': ['load-balancer_global_observer'],
'user_id': None,
'is_admin': False,
'service_user_domain_id': None,
'project_domain_id': None,
'service_roles': [],
'project_id': self.project_id}
with mock.patch(
"oslo_context.context.RequestContext.to_policy_values",
return_value=override_credentials):
response = self.get(self.LBS_PATH)
self.conf.config(group='api_settings', auth_strategy=auth_strategy)
lbs = response.json.get(self.root_tag_list)
self.assertEqual(3, len(lbs))
lb_id_names = [(lb.get('id'), lb.get('name')) for lb in lbs]
self.assertIn((lb1.get('id'), lb1.get('name')), lb_id_names)
self.assertIn((lb2.get('id'), lb2.get('name')), lb_id_names)
self.assertIn((lb3.get('id'), lb3.get('name')), lb_id_names)
def test_get_all_not_authorized(self):
project_id = uuidutils.generate_uuid()
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1', project_id=self.project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb2', project_id=project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb3', project_id=project_id)
self.conf = self.useFixture(oslo_fixture.Config(cfg.CONF))
auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
LB_PROJECT_PATH = '{}?project_id={}'.format(self.LBS_PATH, project_id)
with mock.patch.object(octavia.common.context.Context, 'project_id',
self.project_id):
response = self.get(LB_PROJECT_PATH, status=403)
api_lb = response.json
self.conf.config(group='api_settings', auth_strategy=auth_strategy)
self.assertEqual(self.NOT_AUTHORIZED_BODY, api_lb)
def test_get_all_by_project_id(self):
project1_id = uuidutils.generate_uuid()
project2_id = uuidutils.generate_uuid()
lb1 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=project1_id)
lb2 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb2',
project_id=project1_id)
lb3 = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb3',
project_id=project2_id)
response = self.get(self.LBS_PATH,
params={'project_id': project1_id})
lbs = response.json.get(self.root_tag_list)
self.assertEqual(2, len(lbs))
lb_id_names = [(lb.get('id'), lb.get('name')) for lb in lbs]
lb1 = lb1.get(self.root_tag)
lb2 = lb2.get(self.root_tag)
lb3 = lb3.get(self.root_tag)
self.assertIn((lb1.get('id'), lb1.get('name')), lb_id_names)
self.assertIn((lb2.get('id'), lb2.get('name')), lb_id_names)
response = self.get(self.LBS_PATH,
params={'project_id': project2_id})
lbs = response.json.get(self.root_tag_list)
lb_id_names = [(lb.get('id'), lb.get('name')) for lb in lbs]
self.assertEqual(1, len(lbs))
self.assertIn((lb3.get('id'), lb3.get('name')), lb_id_names)
def test_get_all_sorted(self):
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=self.project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb2',
project_id=self.project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb3',
project_id=self.project_id)
response = self.get(self.LBS_PATH,
params={'sort': 'name:desc'})
lbs_desc = response.json.get(self.root_tag_list)
response = self.get(self.LBS_PATH,
params={'sort': 'name:asc'})
lbs_asc = response.json.get(self.root_tag_list)
self.assertEqual(3, len(lbs_desc))
self.assertEqual(3, len(lbs_asc))
lb_id_names_desc = [(lb.get('id'), lb.get('name')) for lb in lbs_desc]
lb_id_names_asc = [(lb.get('id'), lb.get('name')) for lb in lbs_asc]
self.assertEqual(lb_id_names_asc, list(reversed(lb_id_names_desc)))
def test_get_all_limited(self):
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=self.project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb2',
project_id=self.project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb3',
project_id=self.project_id)
# First two -- should have 'next' link
first_two = self.get(self.LBS_PATH, params={'limit': 2}).json
objs = first_two[self.root_tag_list]
links = first_two[self.root_tag_links]
self.assertEqual(2, len(objs))
self.assertEqual(1, len(links))
self.assertEqual('next', links[0]['rel'])
# Third + off the end -- should have previous link
third = self.get(self.LBS_PATH, params={
'limit': 2,
'marker': first_two[self.root_tag_list][1]['id']}).json
objs = third[self.root_tag_list]
links = third[self.root_tag_links]
self.assertEqual(1, len(objs))
self.assertEqual(1, len(links))
self.assertEqual('previous', links[0]['rel'])
# Middle -- should have both links
middle = self.get(self.LBS_PATH, params={
'limit': 1,
'marker': first_two[self.root_tag_list][0]['id']}).json
objs = middle[self.root_tag_list]
links = middle[self.root_tag_links]
self.assertEqual(1, len(objs))
self.assertEqual(2, len(links))
self.assertItemsEqual(['previous', 'next'], [l['rel'] for l in links])
def test_get_all_fields_filter(self):
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=self.project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb2',
project_id=self.project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
name='lb3',
project_id=self.project_id)
lbs = self.get(self.LBS_PATH, params={
'fields': ['id', 'project_id']}).json
for lb in lbs['loadbalancers']:
self.assertIn(u'id', lb)
self.assertIn(u'project_id', lb)
self.assertNotIn(u'description', lb)
def test_get_one_fields_filter(self):
lb1 = self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb1', project_id=self.project_id).get(self.root_tag)
lb = self.get(
self.LB_PATH.format(lb_id=lb1.get('id')),
params={'fields': ['id', 'project_id']}).json.get(self.root_tag)
self.assertIn(u'id', lb)
self.assertIn(u'project_id', lb)
self.assertNotIn(u'description', lb)
def test_get_all_admin_state_up_filter(self):
self.create_load_balancer(uuidutils.generate_uuid(),
admin_state_up=True,
name='lb1',
project_id=self.project_id)
self.create_load_balancer(uuidutils.generate_uuid(),
admin_state_up=False,
name='lb2',
project_id=self.project_id)
lbs = self.get(self.LBS_PATH, params={'admin_state_up': 'false'}).json
self.assertEqual(1, len(lbs['loadbalancers']))
self.assertFalse(lbs['loadbalancers'][0]['admin_state_up'])
self.assertEqual('lb2', lbs['loadbalancers'][0]['name'])
def test_get_all_filter(self):
lb1 = self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb1',
project_id=self.project_id,
vip_address='10.0.0.1').get(self.root_tag)
self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb2',
project_id=self.project_id).get(self.root_tag)
self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb3',
project_id=self.project_id).get(self.root_tag)
lbs = self.get(self.LBS_PATH, params={
'id': lb1['id'], 'vip_address': lb1['vip_address']}).json
self.assertEqual(1, len(lbs['loadbalancers']))
self.assertEqual(lb1['id'],
lbs['loadbalancers'][0]['id'])
def test_get_all_tags_filter(self):
lb1 = self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb1',
project_id=self.project_id,
vip_address='10.0.0.1',
tags=['test_tag1', 'test_tag2']
).get(self.root_tag)
lb2 = self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb2',
project_id=self.project_id,
tags=['test_tag2', 'test_tag3']
).get(self.root_tag)
lb3 = self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb3',
project_id=self.project_id,
tags=['test_tag4', 'test_tag5']
).get(self.root_tag)
lbs = self.get(
self.LBS_PATH,
params={'tags': 'test_tag2'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(2, len(lbs))
self.assertEqual(
[lb1.get('id'), lb2.get('id')],
[lb.get('id') for lb in lbs]
)
lbs = self.get(
self.LBS_PATH,
params={'tags': ['test_tag2', 'test_tag3']}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(1, len(lbs))
self.assertEqual(
[lb2.get('id')],
[lb.get('id') for lb in lbs]
)
lbs = self.get(
self.LBS_PATH,
params={'tags': ['test_tag2,test_tag3']}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(1, len(lbs))
self.assertEqual(
[lb2.get('id')],
[lb.get('id') for lb in lbs]
)
lbs = self.get(
self.LBS_PATH,
params={'tags-any': 'test_tag2'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(2, len(lbs))
self.assertEqual(
[lb1.get('id'), lb2.get('id')],
[lb.get('id') for lb in lbs]
)
lbs = self.get(
self.LBS_PATH,
params={'not-tags': 'test_tag2'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(1, len(lbs))
self.assertEqual(
[lb3.get('id')],
[lb.get('id') for lb in lbs]
)
lbs = self.get(
self.LBS_PATH,
params={'not-tags-any': ['test_tag2', 'test_tag4']}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(0, len(lbs))
lbs = self.get(
self.LBS_PATH,
params={'tags': 'test_tag2',
'tags-any': ['test_tag1', 'test_tag3']}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(2, len(lbs))
self.assertEqual(
[lb1.get('id'), lb2.get('id')],
[lb.get('id') for lb in lbs]
)
lbs = self.get(
self.LBS_PATH,
params={'tags': 'test_tag2', 'not-tags': 'test_tag2'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(0, len(lbs))
def test_get_all_tags_mixed_filters(self):
lb1 = self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb1',
project_id=self.project_id,
vip_address='10.0.0.1',
tags=['test_tag1', 'test_tag2']
).get(self.root_tag)
self.create_load_balancer(
uuidutils.generate_uuid(),
name='lb2',
project_id=self.project_id,
tags=['test_tag2', 'test_tag3']
).get(self.root_tag)
lbs = self.get(
self.LBS_PATH,
params={'name': 'lb1', 'tags': 'test_tag2',
'vip_address': '10.0.0.1'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(1, len(lbs))
self.assertEqual(lb1.get('id'), lbs[0].get('id'))
lbs = self.get(
self.LBS_PATH,
params={'tags': 'test_tag2', 'vip_address': '10.0.0.1'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(1, len(lbs))
self.assertEqual(lb1.get('id'), lbs[0].get('id'))
lbs = self.get(
self.LBS_PATH,
params={'name': 'lb1', 'tags': 'test_tag1',
'vip_address': '10.0.0.1'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(1, len(lbs))
self.assertEqual(lb1.get('id'), lbs[0].get('id'))
lbs = self.get(
self.LBS_PATH,
params={'name': 'lb1', 'tags': 'test_tag3',
'vip_address': '10.0.0.1'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(0, len(lbs))
lbs = self.get(
self.LBS_PATH,
params={'name': 'lb1', 'tags': 'test_tag3',
'vip_address': '10.0.0.1'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(0, len(lbs))
lbs = self.get(
self.LBS_PATH,
params={'name': 'bogus-lb', 'tags': 'test_tag2',
'vip_address': '10.0.0.1'}
).json.get(self.root_tag_list)
self.assertIsInstance(lbs, list)
self.assertEqual(0, len(lbs))
def test_get_all_hides_deleted(self):
api_lb = self.create_load_balancer(
uuidutils.generate_uuid()).get(self.root_tag)
response = self.get(self.LBS_PATH)
objects = response.json.get(self.root_tag_list)
self.assertEqual(len(objects), 1)
self.set_object_status(self.lb_repo, api_lb.get('id'),
provisioning_status=constants.DELETED)
response = self.get(self.LBS_PATH)
objects = response.json.get(self.root_tag_list)
self.assertEqual(len(objects), 0)
def test_get(self):
project_id = uuidutils.generate_uuid()
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
port = network_models.Port(id=uuidutils.generate_uuid(),
network_id=network.id)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port:
mock_get_network.return_value = network
mock_get_port.return_value = port
lb = self.create_load_balancer(subnet.id,
vip_address='10.0.0.1',
vip_network_id=network.id,
vip_port_id=port.id,
name='lb1',
project_id=project_id,
description='desc1',
admin_state_up=False,
tags=['test_tag'])
lb_dict = lb.get(self.root_tag)
response = self.get(
self.LB_PATH.format(
lb_id=lb_dict.get('id'))).json.get(self.root_tag)
self.assertEqual('lb1', response.get('name'))
self.assertEqual(project_id, response.get('project_id'))
self.assertEqual('desc1', response.get('description'))
self.assertFalse(response.get('admin_state_up'))
self.assertEqual('10.0.0.1', response.get('vip_address'))
self.assertEqual(subnet.id, response.get('vip_subnet_id'))
self.assertEqual(network.id, response.get('vip_network_id'))
self.assertEqual(port.id, response.get('vip_port_id'))
self.assertEqual(['test_tag'], response.get('tags'))
def test_get_deleted_gives_404(self):
api_lb = self.create_load_balancer(
uuidutils.generate_uuid()).get(self.root_tag)
self.set_object_status(self.lb_repo, api_lb.get('id'),
provisioning_status=constants.DELETED)
self.get(self.LB_PATH.format(lb_id=api_lb.get('id')), status=404)
def test_get_bad_lb_id(self):
path = self.LB_PATH.format(lb_id='SEAN-CONNERY')
self.get(path, status=404)
def test_get_authorized(self):
project_id = uuidutils.generate_uuid()
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
port = network_models.Port(id=uuidutils.generate_uuid(),
network_id=network.id)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port:
mock_get_network.return_value = network
mock_get_port.return_value = port
lb = self.create_load_balancer(subnet.id,
vip_address='10.0.0.1',
vip_network_id=network.id,
vip_port_id=port.id,
name='lb1',
project_id=project_id,
description='desc1',
admin_state_up=False)
lb_dict = lb.get(self.root_tag)
self.conf = self.useFixture(oslo_fixture.Config(cfg.CONF))
auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
with mock.patch.object(octavia.common.context.Context, 'project_id',
project_id):
override_credentials = {
'service_user_id': None,
'user_domain_id': None,
'is_admin_project': True,
'service_project_domain_id': None,
'service_project_id': None,
'roles': ['load-balancer_member'],
'user_id': None,
'is_admin': False,
'service_user_domain_id': None,
'project_domain_id': None,
'service_roles': [],
'project_id': project_id}
with mock.patch(
"oslo_context.context.RequestContext.to_policy_values",
return_value=override_credentials):
response = self.get(self.LB_PATH.format(
lb_id=lb_dict.get('id'))).json.get(self.root_tag)
self.assertEqual('lb1', response.get('name'))
self.assertEqual(project_id, response.get('project_id'))
self.assertEqual('desc1', response.get('description'))
self.assertFalse(response.get('admin_state_up'))
self.assertEqual('10.0.0.1', response.get('vip_address'))
self.assertEqual(subnet.id, response.get('vip_subnet_id'))
self.assertEqual(network.id, response.get('vip_network_id'))
self.assertEqual(port.id, response.get('vip_port_id'))
self.conf.config(group='api_settings', auth_strategy=auth_strategy)
def test_get_not_authorized(self):
project_id = uuidutils.generate_uuid()
subnet = network_models.Subnet(id=uuidutils.generate_uuid())
network = network_models.Network(id=uuidutils.generate_uuid(),
subnets=[subnet])
port = network_models.Port(id=uuidutils.generate_uuid(),
network_id=network.id)
with mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_network") as mock_get_network, mock.patch(
"octavia.network.drivers.noop_driver.driver.NoopManager"
".get_port") as mock_get_port:
mock_get_network.return_value = network
mock_get_port.return_value = port
lb = self.create_load_balancer(subnet.id,
vip_address='10.0.0.1',
vip_network_id=network.id,
vip_port_id=port.id,
name='lb1',
project_id=project_id,
description='desc1',
admin_state_up=False)
lb_dict = lb.get(self.root_tag)
self.conf = self.useFixture(oslo_fixture.Config(cfg.CONF))
auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
with mock.patch.object(octavia.common.context.Context, 'project_id',
uuidutils.generate_uuid()):
response = self.get(self.LB_PATH.format(lb_id=lb_dict.get('id')),
status=403)
api_lb = response.json
self.conf.config(group='api_settings', auth_strategy=auth_strategy)
self.assertEqual(self.NOT_AUTHORIZED_BODY, api_lb)
def test_create_over_quota(self):
self.start_quota_mock(data_models.LoadBalancer)
lb_json = {'name': 'test1',
'vip_subnet_id': uuidutils.generate_uuid(),
'project_id': self.project_id}
body = self._build_body(lb_json)
self.post(self.LBS_PATH, body, status=403)
def test_update(self):
project_id = uuidutils.generate_uuid()
lb = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=project_id,
description='desc1',
admin_state_up=False,
tags=['test_tag1'])
lb_dict = lb.get(self.root_tag)
lb_json = self._build_body({'name': 'lb2', 'tags': ['test_tag2']})
lb = self.set_lb_status(lb_dict.get('id'))
response = self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
lb_json)
api_lb = response.json.get(self.root_tag)
self.assertIsNotNone(api_lb.get('vip_subnet_id'))
self.assertEqual('lb2', api_lb.get('name'))
self.assertEqual(['test_tag2'], api_lb.get('tags'))
self.assertEqual(project_id, api_lb.get('project_id'))
self.assertEqual('desc1', api_lb.get('description'))
self.assertFalse(api_lb.get('admin_state_up'))
self.assertIsNotNone(api_lb.get('created_at'))
self.assertIsNotNone(api_lb.get('updated_at'))
self.assert_correct_lb_status(api_lb.get('id'), constants.ONLINE,
constants.PENDING_UPDATE)
def test_update_with_vip(self):
project_id = uuidutils.generate_uuid()
lb = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=project_id,
description='desc1',
admin_state_up=False)
lb_dict = lb.get(self.root_tag)
lb_json = self._build_body({'vip_subnet_id': '1234'})
lb = self.set_lb_status(lb_dict.get('id'))
self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
lb_json, status=400)
def test_update_with_qos(self):
project_id = uuidutils.generate_uuid()
lb = self.create_load_balancer(
uuidutils.generate_uuid(), name='lb1',
project_id=project_id,
vip_qos_policy_id=uuidutils.generate_uuid())
lb_dict = lb.get(self.root_tag)
self.set_lb_status(lb_dict.get('id'))
lb_json = self._build_body(
{'vip_qos_policy_id': uuidutils.generate_uuid()})
self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
lb_json, status=200)
def test_update_with_bad_qos(self):
project_id = uuidutils.generate_uuid()
vip_qos_policy_id = uuidutils.generate_uuid()
lb = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=project_id,
vip_qos_policy_id=vip_qos_policy_id)
lb_dict = lb.get(self.root_tag)
lb_json = self._build_body({'vip_qos_policy_id': 'BAD'})
self.set_lb_status(lb_dict.get('id'))
self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
lb_json, status=400)
def test_update_with_qos_ext_disabled(self):
project_id = uuidutils.generate_uuid()
lb = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=project_id)
lb_dict = lb.get(self.root_tag)
self.set_lb_status(lb_dict.get('id'))
vip_qos_policy_id = uuidutils.generate_uuid()
lb_json = self._build_body({'vip_qos_policy_id': vip_qos_policy_id})
with mock.patch("octavia.network.drivers.noop_driver.driver"
".NoopManager.qos_enabled", return_value=False):
self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
lb_json, status=400)
def test_update_bad_lb_id(self):
path = self.LB_PATH.format(lb_id='SEAN-CONNERY')
self.put(path, body={}, status=404)
def test_update_pending_create(self):
project_id = uuidutils.generate_uuid()
lb = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=project_id,
description='desc1',
admin_state_up=False)
lb_dict = lb.get(self.root_tag)
lb_json = self._build_body({'name': 'Roberto'})
self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
lb_json, status=409)
def test_update_authorized(self):
project_id = uuidutils.generate_uuid()
lb = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=project_id,
description='desc1',
admin_state_up=False)
lb_dict = lb.get(self.root_tag)
lb_json = self._build_body({'name': 'lb2'})
lb = self.set_lb_status(lb_dict.get('id'))
self.conf = self.useFixture(oslo_fixture.Config(cfg.CONF))
auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
with mock.patch.object(octavia.common.context.Context, 'project_id',
project_id):
override_credentials = {
'service_user_id': None,
'user_domain_id': None,
'is_admin_project': True,
'service_project_domain_id': None,
'service_project_id': None,
'roles': ['load-balancer_member'],
'user_id': None,
'is_admin': False,
'service_user_domain_id': None,
'project_domain_id': None,
'service_roles': [],
'project_id': project_id}
with mock.patch(
"oslo_context.context.RequestContext.to_policy_values",
return_value=override_credentials):
response = self.put(
self.LB_PATH.format(lb_id=lb_dict.get('id')), lb_json)
api_lb = response.json.get(self.root_tag)
self.conf.config(group='api_settings', auth_strategy=auth_strategy)
self.assertIsNotNone(api_lb.get('vip_subnet_id'))
self.assertEqual('lb2', api_lb.get('name'))
self.assertEqual(project_id, api_lb.get('project_id'))
self.assertEqual('desc1', api_lb.get('description'))
self.assertFalse(api_lb.get('admin_state_up'))
self.assertIsNotNone(api_lb.get('created_at'))
self.assertIsNotNone(api_lb.get('updated_at'))
self.assert_correct_lb_status(api_lb.get('id'), constants.ONLINE,
constants.PENDING_UPDATE)
def test_update_not_authorized(self):
project_id = uuidutils.generate_uuid()
lb = self.create_load_balancer(uuidutils.generate_uuid(),
name='lb1',
project_id=project_id,
description='desc1',
admin_state_up=False)
lb_dict = lb.get<