test_load_balancer.py 166 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#    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
Jude Cross's avatar
Jude Cross committed
16
import random
17 18

import mock
19 20
from oslo_config import cfg
from oslo_config import fixture as oslo_fixture
21
from oslo_utils import uuidutils
22
from sqlalchemy.orm import exc as sa_exception
23

24
from octavia.api.drivers import exceptions as provider_exceptions
25
from octavia.common import constants
26
import octavia.common.context
27
from octavia.common import data_models
28
from octavia.common import exceptions
29
from octavia.network import base as network_base
30
from octavia.network import data_models as network_models
31 32 33 34
from octavia.tests.functional.api.v2 import base


class TestLoadBalancer(base.BaseAPITest):
35 36
    root_tag = 'loadbalancer'
    root_tag_list = 'loadbalancers'
37
    root_tag_links = 'loadbalancers_links'
38 39 40

    def _assert_request_matches_response(self, req, resp, **optionals):
        self.assertTrue(uuidutils.is_uuid_like(resp.get('id')))
41 42 43 44 45 46 47 48 49 50
        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'))
51 52 53 54 55 56 57 58 59
        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))
60 61 62

    def test_empty_list(self):
        response = self.get(self.LBS_PATH)
63 64
        api_list = response.json.get(self.root_tag_list)
        self.assertEqual([], api_list)
65 66 67 68

    def test_create(self, **optionals):
        lb_json = {'name': 'test1',
                   'vip_subnet_id': uuidutils.generate_uuid(),
69 70
                   'project_id': self.project_id,
                   'tags': ['test_tag1', 'test_tag2']
71 72 73 74
                   }
        lb_json.update(optionals)
        body = self._build_body(lb_json)
        response = self.post(self.LBS_PATH, body)
75 76
        api_lb = response.json.get(self.root_tag)
        self._assert_request_matches_response(lb_json, api_lb)
77
        return api_lb
78

79 80 81 82 83 84 85 86 87 88 89 90 91
    # 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

92 93 94 95 96 97 98 99 100 101 102
    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

103
    def test_create_without_vip(self):
104 105 106 107 108
        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: '
109
                   'vip_port_id, vip_network_id, vip_subnet_id.')
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        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)
192 193 194 195 196
        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:
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
            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)
215 216 217 218 219
        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:
220 221 222 223 224 225 226 227
            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'))

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    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])
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
        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)
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        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)
368 369
        # This test needs the provider driver to not supply the VIP port
        # so mocking noop to not supply a VIP port.
370 371 372 373
        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"
374 375 376
                ".get_port") as mock_get_port, mock.patch(
            "octavia.api.drivers.noop_driver.driver.NoopManager."
                "create_vip_port") as mock_provider:
377 378
            mock_get_network.return_value = network
            mock_get_port.return_value = port
379 380
            mock_provider.side_effect = (provider_exceptions.
                                         NotImplementedError())
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
            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'))

477 478 479 480 481 482 483 484 485 486 487 488
    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)
489 490 491 492 493
        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:
494 495 496 497 498 499 500 501 502 503
            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'))

504
    def test_create_neutron_failure(self):
505 506 507 508 509 510 511 512 513 514

        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)

515 516 517 518 519 520 521 522 523 524 525
        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)
526 527
        # This test needs the provider driver to not supply the VIP port
        # so mocking noop to not supply a VIP port.
528 529 530 531 532 533
        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"
534 535 536
                ".allocate_vip") as mock_allocate_vip, mock.patch(
            "octavia.api.drivers.noop_driver.driver.NoopManager."
                "create_vip_port") as mock_provider:
537 538 539 540
            mock_get_network.return_value = network
            mock_get_port.return_value = port
            mock_allocate_vip.side_effect = TestNeutronException(
                "octavia_msg", "neutron_msg", 409)
541 542
            mock_provider.side_effect = (provider_exceptions.
                                         NotImplementedError())
543 544 545 546 547
            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"))

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    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'))

671 672
    def test_create_with_long_name(self):
        lb_json = {'name': 'n' * 256,
673 674 675 676 677 678
                   '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'))
679 680 681

    def test_create_with_long_description(self):
        lb_json = {'description': 'n' * 256,
682 683 684 685 686 687
                   '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'))
688 689

    def test_create_with_nonuuid_vip_attributes(self):
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
        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'))
767

768
    def test_create_with_project_id(self):
769
        project_id = uuidutils.generate_uuid()
770
        api_lb = self.test_create(project_id=project_id)
771
        self.assertEqual(project_id, api_lb.get('project_id'))
772

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    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))
795 796
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
        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)
824
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
825 826 827 828
        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))
829 830
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
831 832 833 834 835 836 837 838
        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()):
839
            response = self.post(self.LBS_PATH, body, status=403)
840
        api_lb = response.json
841
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
842 843
        self.assertEqual(self.NOT_AUTHORIZED_BODY, api_lb)

844 845 846 847 848 849 850 851
    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)
852 853 854 855
        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)
856 857 858 859 860 861 862 863 864 865 866 867 868
        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)
869
        self.assertIn("Provider 'BOGUS' is not enabled.",
870 871
                      response.json.get('faultstring'))

872
    def test_create_flavor_bad_type(self, **optionals):
873 874 875
        lb_json = {'name': 'test1',
                   'vip_subnet_id': uuidutils.generate_uuid(),
                   'project_id': self.project_id,
876
                   'flavor_id': 'BOGUS'
877 878 879 880
                   }
        lb_json.update(optionals)
        body = self._build_body(lb_json)
        response = self.post(self.LBS_PATH, body, status=400)
881
        self.assertIn("Invalid input for field/attribute flavor_id. Value: "
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
                      "'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_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),
985 986
                      response.json.get('faultstring'))

987
    def test_get_all_admin(self):
988
        project_id = uuidutils.generate_uuid()
989
        lb1 = self.create_load_balancer(uuidutils.generate_uuid(),
990 991
                                        name='lb1', project_id=self.project_id,
                                        tags=['test_tag1'])
992
        lb2 = self.create_load_balancer(uuidutils.generate_uuid(),
993 994
                                        name='lb2', project_id=project_id,
                                        tags=['test_tag2'])
995
        lb3 = self.create_load_balancer(uuidutils.generate_uuid(),
996 997
                                        name='lb3', project_id=project_id,
                                        tags=['test_tag3'])
998 999 1000
        response = self.get(self.LBS_PATH)
        lbs = response.json.get(self.root_tag_list)
        self.assertEqual(3, len(lbs))
1001 1002 1003
        lb_id_names = [(lb.get('id'),
                        lb.get('name'),
                        lb.get('tags')) for lb in lbs]
1004 1005 1006
        lb1 = lb1.get(self.root_tag)
        lb2 = lb2.get(self.root_tag)
        lb3 = lb3.get(self.root_tag)
1007 1008 1009 1010 1011 1012
        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)
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

    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)

1024 1025 1026
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings',
                         auth_strategy=constants.KEYSTONE)
1027 1028
        with mock.patch.object(octavia.common.context.Context, 'project_id',
                               self.project_id):
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
            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)
1046
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
1047 1048 1049 1050 1051

        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)
1052

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    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)

1065 1066 1067
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings',
                         auth_strategy=constants.KEYSTONE)
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
        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)
1087
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104

        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))
1105 1106
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
1107 1108 1109
        LB_PROJECT_PATH = '{}?project_id={}'.format(self.LBS_PATH, project_id)
        with mock.patch.object(octavia.common.context.Context, 'project_id',
                               self.project_id):
1110
            response = self.get(LB_PROJECT_PATH, status=403)
1111
        api_lb = response.json
1112
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
1113 1114
        self.assertEqual(self.NOT_AUTHORIZED_BODY, api_lb)

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    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)
1127 1128
        response = self.get(self.LBS_PATH,
                            params={'project_id': project1_id})
1129
        lbs = response.json.get(self.root_tag_list)
1130 1131 1132 1133

        self.assertEqual(2, len(lbs))

        lb_id_names = [(lb.get('id'), lb.get('name')) for lb in lbs]
1134 1135 1136
        lb1 = lb1.get(self.root_tag)
        lb2 = lb2.get(self.root_tag)
        lb3 = lb3.get(self.root_tag)
1137 1138
        self.assertIn((lb1.get('id'), lb1.get('name')), lb_id_names)
        self.assertIn((lb2.get('id'), lb2.get('name')), lb_id_names)
1139 1140
        response = self.get(self.LBS_PATH,
                            params={'project_id': project2_id})
1141
        lbs = response.json.get(self.root_tag_list)
1142 1143 1144 1145
        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)

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    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])

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    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']:
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
            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)
1238

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    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'])

1254 1255 1256 1257
    def test_get_all_filter(self):
        lb1 = self.create_load_balancer(
            uuidutils.generate_uuid(),
            name='lb1',
1258 1259
            project_id=self.project_id,
            vip_address='10.0.0.1').get(self.root_tag)
1260 1261 1262 1263 1264 1265 1266 1267 1268
        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={
1269
            'id': lb1['id'], 'vip_address': lb1['vip_address']}).json
1270 1271 1272 1273
        self.assertEqual(1, len(lbs['loadbalancers']))
        self.assertEqual(lb1['id'],
                         lbs['loadbalancers'][0]['id'])

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
    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-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))

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
    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)

1378
    def test_get(self):
1379
        project_id = uuidutils.generate_uuid()
1380 1381 1382 1383 1384
        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)
1385 1386 1387 1388 1389
        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:
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
            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',
1400 1401
                                           admin_state_up=False,
                                           tags=['test_tag'])
1402
        lb_dict = lb.get(self.root_tag)
1403 1404
        response = self.get(
            self.LB_PATH.format(
1405
                lb_id=lb_dict.get('id'))).json.get(self.root_tag)
1406
        self.assertEqual('lb1', response.get('name'))
1407
        self.assertEqual(project_id, response.get('project_id'))
1408 1409
        self.assertEqual('desc1', response.get('description'))
        self.assertFalse(response.get('admin_state_up'))
1410 1411 1412 1413
        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'))
1414
        self.assertEqual(['test_tag'], response.get('tags'))
1415

1416
    def test_get_deleted_gives_404(self):
1417 1418 1419 1420 1421
        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)
1422 1423

        self.get(self.LB_PATH.format(lb_id=api_lb.get('id')), status=404)
1424

1425 1426 1427 1428
    def test_get_bad_lb_id(self):
        path = self.LB_PATH.format(lb_id='SEAN-CONNERY')
        self.get(path, status=404)

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
    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))
1454 1455
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
        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'))
1484
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510

    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))
1511 1512
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
1513 1514 1515
        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')),
1516
                                status=403)
1517
        api_lb = response.json
1518
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
1519 1520
        self.assertEqual(self.NOT_AUTHORIZED_BODY, api_lb)

1521 1522 1523 1524 1525 1526 1527 1528
    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)

1529
    def test_update(self):
1530
        project_id = uuidutils.generate_uuid()
1531 1532
        lb = self.create_load_balancer(uuidutils.generate_uuid(),
                                       name='lb1',
1533
                                       project_id=project_id,
1534
                                       description='desc1',
1535 1536
                                       admin_state_up=False,
                                       tags=['test_tag1'])
1537
        lb_dict = lb.get(self.root_tag)
1538
        lb_json = self._build_body({'name': 'lb2', 'tags': ['test_tag2']})
1539 1540 1541
        lb = self.set_lb_status(lb_dict.get('id'))
        response = self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
                            lb_json)
1542
        api_lb = response.json.get(self.root_tag)
1543
        self.assertIsNotNone(api_lb.get('vip_subnet_id'))
1544
        self.assertEqual('lb2', api_lb.get('name'))
1545
        self.assertEqual(['test_tag2'], api_lb.get('tags'))
1546
        self.assertEqual(project_id, api_lb.get('project_id'))
1547 1548 1549 1550
        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'))
1551 1552
        self.assert_correct_lb_status(api_lb.get('id'), constants.ONLINE,
                                      constants.PENDING_UPDATE)
1553 1554

    def test_update_with_vip(self):
1555
        project_id = uuidutils.generate_uuid()
1556 1557
        lb = self.create_load_balancer(uuidutils.generate_uuid(),
                                       name='lb1',
1558
                                       project_id=project_id,
1559 1560
                                       description='desc1',
                                       admin_state_up=False)
1561
        lb_dict = lb.get(self.root_tag)
1562 1563 1564 1565 1566
        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)

1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
    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)

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
    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)

1607 1608 1609 1610 1611
    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):
1612
        project_id = uuidutils.generate_uuid()
1613 1614
        lb = self.create_load_balancer(uuidutils.generate_uuid(),
                                       name='lb1',
1615
                                       project_id=project_id,
1616 1617
                                       description='desc1',
                                       admin_state_up=False)
1618
        lb_dict = lb.get(self.root_tag)
1619 1620 1621 1622
        lb_json = self._build_body({'name': 'Roberto'})
        self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
                 lb_json, status=409)

1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
    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))
1635 1636
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
        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)
1658
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
1659
        self.assertIsNotNone(api_lb.get('vip_subnet_id'))
1660
        self.assertEqual('lb2', api_lb.get('name'))
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
        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(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))
1681 1682
        auth_strategy = self.conf.conf.api_settings.get('auth_strategy')
        self.conf.config(group='api_settings', auth_strategy=constants.TESTING)
1683 1684 1685
        with mock.patch.object(octavia.common.context.Context, 'project_id',
                               uuidutils.generate_uuid()):
            response = self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
1686
                                lb_json, status=403)
1687
        api_lb = response.json
1688
        self.conf.config(group='api_settings', auth_strategy=auth_strategy)
1689 1690 1691 1692
        self.assertEqual(self.NOT_AUTHORIZED_BODY, api_lb)
        self.assert_correct_lb_status(lb_dict.get('id'), constants.ONLINE,
                                      constants.ACTIVE)

1693
    def test_delete_pending_create(self):
1694
        project_id = uuidutils.generate_uuid()
1695 1696
        lb = self.create_load_balancer(uuidutils.generate_uuid(),
                                       name='lb1',
1697
                                       project_id=project_id,
1698 1699
                                       description='desc1',
                                       admin_state_up=False)
1700
        lb_dict = lb.get(self.root_tag)
1701 1702 1703
        self.delete(self.LB_PATH.format(lb_id=lb_dict.get('id')), status=409)

    def test_update_pending_update(self):
1704
        project_id = uuidutils.generate_uuid()
1705 1706
        lb = self.create_load_balancer(uuidutils.generate_uuid(),
                                       name='lb1',
1707
                                       project_id=project_id,
1708 1709
                                       description='desc1',
                                       admin_state_up=False)
1710
        lb_dict = lb.get(self.root_tag)
1711 1712 1713 1714 1715 1716 1717
        lb_json = self._build_body({'name': 'Bob'})
        lb = self.set_lb_status(lb_dict.get('id'))
        self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')), lb_json)
        self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')),
                 lb_json, status=409)

    def test_delete_pending_update(self):
1718
        project_id = uuidutils.generate_uuid()
1719 1720
        lb = self.create_load_balancer(uuidutils.generate_uuid(),
                                       name='lb1',
1721
                                       project_id=project_id,
1722 1723 1724
                                       description='desc1',
                                       admin_state_up=False)
        lb_json = self._build_body({'name': 'Steve'})
1725
        lb_dict = lb.get(self.root_tag)
1726
        lb = self.set_lb_status(lb_dict.get('id'))
1727
        self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')), lb_json)
1728 1729 1730
        self.delete(self.LB_PATH.format(lb_id=lb_dict.get('id')), status=409)

    def test_delete_with_error_status(self):
1731
        project_id = uuidutils.generate_uuid()
1732 1733
        lb = self.create_load_balancer(uuidutils.generate_uuid(),
                                       name='lb1',
1734
                                       project_id=project_id,
1735 1736
                                       description='desc1',
                                       admin_state_up=False)
1737
        lb_dict = lb.get(self.root_tag)
1738 1739 1740 1741
        lb = self.set_lb_status(lb_dict.get('id'), status=constants.ERROR)
        self.delete(self.LB_PATH.format(lb_id=lb_dict.get('id')), status=204)

    def test_update_pending_delete(self):