| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
from oslo_config import cfg |
| 16 |
from oslo_db.sqlalchemy import models |
| 17 |
import sqlalchemy as sa |
| 18 |
from sqlalchemy import orm |
| 19 |
|
| 20 |
from neutron_lib.api import attributes |
| 21 |
from neutron_lib import context |
| 22 |
from neutron_lib.db import utils |
| 23 |
from neutron_lib import exceptions as n_exc |
| 24 |
|
| 25 |
from neutron_lib.tests import _base as base |
| 26 |
|
| 27 |
|
| 28 |
class ModelBaseV2(orm.DeclarativeBase, models.ModelBase): |
| 29 |
pass |
| 30 |
|
| 31 |
|
| 32 |
class FakePort(ModelBaseV2): |
| 33 |
__tablename__ = 'fakeports' |
| 34 |
port_id = sa.Column(sa.String(36), primary_key=True) |
| 35 |
name = sa.Column(sa.String(64)) |
| 36 |
status = sa.Column(sa.String(16), nullable=False) |
| 37 |
|
| 38 |
|
| 39 |
class FakeRouter(ModelBaseV2): |
| 40 |
__tablename__ = 'fakerouters' |
| 41 |
router_id = sa.Column(sa.String(36), primary_key=True) |
| 42 |
gw_port_id = sa.Column(sa.String(36), sa.ForeignKey(FakePort.port_id)) |
| 43 |
gw_port = orm.relationship(FakePort, lazy='joined') |
| 44 |
|
| 45 |
|
| 46 |
class TestUtilsLegacyPolicies(base.BaseTestCase): |
| 47 |
|
| 48 |
def setUp(self): |
| 49 |
cfg.CONF.set_override('enforce_scope', False, group='oslo_policy') |
| 50 |
super().setUp() |
| 51 |
|
| 52 |
def test_get_sort_dirs(self): |
| 53 |
sorts = [(1, True), (2, False), (3, True)] |
| 54 |
self.assertEqual(['asc', 'desc', 'asc'], |
| 55 |
utils.get_sort_dirs(sorts)) |
| 56 |
|
| 57 |
def test_get_sort_dirs_reversed(self): |
| 58 |
sorts = [(1, True), (2, False), (3, True)] |
| 59 |
self.assertEqual(['desc', 'asc', 'desc'], |
| 60 |
utils.get_sort_dirs(sorts, page_reverse=True)) |
| 61 |
|
| 62 |
def test_get_and_validate_sort_keys(self): |
| 63 |
sorts = [('name', False), ('status', True)] |
| 64 |
self.assertEqual(['name', 'status'], |
| 65 |
utils.get_and_validate_sort_keys(sorts, FakePort)) |
| 66 |
|
| 67 |
def test_get_and_validate_sort_keys_bad_key_fails(self): |
| 68 |
sorts = [('master', True)] |
| 69 |
self.assertRaises(n_exc.BadRequest, |
| 70 |
utils.get_and_validate_sort_keys, sorts, FakePort) |
| 71 |
|
| 72 |
def test_get_and_validate_sort_keys_by_relationship_fails(self): |
| 73 |
sorts = [('gw_port', True)] |
| 74 |
self.assertRaises(n_exc.BadRequest, |
| 75 |
utils.get_and_validate_sort_keys, sorts, FakeRouter) |
| 76 |
|
| 77 |
def test_get_marker_obj(self): |
| 78 |
plugin = mock.Mock() |
| 79 |
plugin._get_myr.return_value = 'obj' |
| 80 |
obj = utils.get_marker_obj(plugin, 'ctx', 'myr', 10, mock.ANY) |
| 81 |
self.assertEqual('obj', obj) |
| 82 |
plugin._get_myr.assert_called_once_with('ctx', mock.ANY) |
| 83 |
|
| 84 |
def test_get_marker_obj_no_limit_and_marker(self): |
| 85 |
self.assertIsNone(utils.get_marker_obj( |
| 86 |
mock.Mock(), 'ctx', 'myr', 0, mock.ANY)) |
| 87 |
self.assertIsNone(utils.get_marker_obj( |
| 88 |
mock.Mock(), 'ctx', 'myr', 10, None)) |
| 89 |
|
| 90 |
@mock.patch.object(attributes, 'populate_project_info') |
| 91 |
def test_resource_fields(self, mock_populate): |
| 92 |
r = { |
| 93 |
'name': 'n', |
| 94 |
'id': '1', |
| 95 |
'desc': None |
| 96 |
} |
| 97 |
utils.resource_fields(r, ['name']) |
| 98 |
mock_populate.assert_called_once_with({'name': 'n'}) |
| 99 |
|
| 100 |
def test_model_query_scope_is_project_admin(self): |
| 101 |
ctx = context.Context( |
| 102 |
project_id='some project', |
| 103 |
is_admin=True, |
| 104 |
is_advsvc=False) |
| 105 |
model = mock.Mock(project_id='project') |
| 106 |
|
| 107 |
self.assertFalse( |
| 108 |
utils.model_query_scope_is_project(ctx, model)) |
| 109 |
|
| 110 |
# Ensure that project_id isn't mocked |
| 111 |
del model.project_id |
| 112 |
self.assertFalse( |
| 113 |
utils.model_query_scope_is_project(ctx, model)) |
| 114 |
|
| 115 |
def test_model_query_scope_is_project_advsvc(self): |
| 116 |
ctx = context.Context( |
| 117 |
project_id='some project', |
| 118 |
is_admin=False, |
| 119 |
is_advsvc=True) |
| 120 |
model = mock.Mock(project_id='project') |
| 121 |
|
| 122 |
self.assertFalse( |
| 123 |
utils.model_query_scope_is_project(ctx, model)) |
| 124 |
|
| 125 |
# Ensure that project_id isn't mocked |
| 126 |
del model.project_id |
| 127 |
self.assertFalse( |
| 128 |
utils.model_query_scope_is_project(ctx, model)) |
| 129 |
|
| 130 |
def test_model_query_scope_is_project_service_role(self): |
| 131 |
ctx = context.Context( |
| 132 |
project_id='some project', |
| 133 |
is_admin=False, |
| 134 |
roles=['service']) |
| 135 |
model = mock.Mock(project_id='project') |
| 136 |
|
| 137 |
self.assertFalse( |
| 138 |
utils.model_query_scope_is_project(ctx, model)) |
| 139 |
|
| 140 |
# Ensure that project_id isn't mocked |
| 141 |
del model.project_id |
| 142 |
self.assertFalse( |
| 143 |
utils.model_query_scope_is_project(ctx, model)) |
| 144 |
|
| 145 |
def test_model_query_scope_is_project_regular_user(self): |
| 146 |
ctx = context.Context( |
| 147 |
project_id='some project', |
| 148 |
is_admin=False, |
| 149 |
is_advsvc=False) |
| 150 |
model = mock.Mock(project_id='project') |
| 151 |
|
| 152 |
self.assertTrue( |
| 153 |
utils.model_query_scope_is_project(ctx, model)) |
| 154 |
|
| 155 |
# Ensure that project_id isn't mocked |
| 156 |
del model.project_id |
| 157 |
self.assertFalse( |
| 158 |
utils.model_query_scope_is_project(ctx, model)) |
| 159 |
|
| 160 |
def test_model_query_scope_is_project_system_scope(self): |
| 161 |
ctx = context.Context(system_scope='all') |
| 162 |
model = mock.Mock(project_id='project') |
| 163 |
|
| 164 |
self.assertTrue( |
| 165 |
utils.model_query_scope_is_project(ctx, model)) |
| 166 |
|
| 167 |
# Ensure that project_id isn't mocked |
| 168 |
del model.project_id |
| 169 |
self.assertFalse( |
| 170 |
utils.model_query_scope_is_project(ctx, model)) |
| 171 |
|
| 172 |
|
| 173 |
class TestUtilsWithScopeEnforcement(TestUtilsLegacyPolicies): |
| 174 |
|
| 175 |
def setUp(self): |
| 176 |
super().setUp() |
| 177 |
cfg.CONF.set_override('enforce_scope', True, group='oslo_policy') |