| 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 |
import re |
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
from oslo_config import cfg |
| 17 |
from oslo_db import options |
| 18 |
from oslotest import base |
| 19 |
|
| 20 |
from neutron_lib.api import attributes |
| 21 |
from neutron_lib.api.definitions import port |
| 22 |
from neutron_lib.callbacks import registry |
| 23 |
from neutron_lib.db import model_base |
| 24 |
from neutron_lib.db import resource_extend |
| 25 |
from neutron_lib import fixture |
| 26 |
from neutron_lib.placement import client as place_client |
| 27 |
from neutron_lib.plugins import directory |
| 28 |
from neutron_lib.tests.unit.api import test_attributes |
| 29 |
|
| 30 |
|
| 31 |
class PluginDirectoryFixtureTestCase(base.BaseTestCase): |
| 32 |
|
| 33 |
def setUp(self): |
| 34 |
super().setUp() |
| 35 |
self.directory = mock.Mock() |
| 36 |
self.useFixture(fixture.PluginDirectoryFixture( |
| 37 |
plugin_directory=self.directory)) |
| 38 |
|
| 39 |
def test_fixture(self): |
| 40 |
directory.add_plugin('foo', 'foo') |
| 41 |
self.assertTrue(self.directory.add_plugin.called) |
| 42 |
|
| 43 |
|
| 44 |
class CallbackRegistryFixtureTestCase(base.BaseTestCase): |
| 45 |
|
| 46 |
def setUp(self): |
| 47 |
super().setUp() |
| 48 |
self.manager = mock.Mock() |
| 49 |
self.useFixture(fixture.CallbackRegistryFixture( |
| 50 |
callback_manager=self.manager)) |
| 51 |
|
| 52 |
def test_fixture(self): |
| 53 |
registry.publish('a', 'b', self, payload=mock.ANY) |
| 54 |
self.assertTrue(self.manager.publish.called) |
| 55 |
|
| 56 |
|
| 57 |
class SqlFixtureTestCase(base.BaseTestCase): |
| 58 |
|
| 59 |
def setUp(self): |
| 60 |
super().setUp() |
| 61 |
options.set_defaults( |
| 62 |
cfg.CONF, |
| 63 |
connection='sqlite://') |
| 64 |
self.fixture = fixture.StaticSqlFixture() |
| 65 |
self.useFixture(self.fixture) |
| 66 |
|
| 67 |
def test_fixture(self): |
| 68 |
self.assertIsNotNone(model_base.BASEV2.metadata.sorted_tables) |
| 69 |
self.assertIsNotNone(self.fixture.engine) |
| 70 |
|
| 71 |
|
| 72 |
class APIDefinitionFixtureTestCase(base.BaseTestCase): |
| 73 |
|
| 74 |
def _test_all_api_definitions_fixture(self, global_cleanup=True): |
| 75 |
apis = fixture.APIDefinitionFixture.all_api_definitions_fixture() |
| 76 |
apis.backup_global_resources = global_cleanup |
| 77 |
apis.setUp() |
| 78 |
|
| 79 |
asserteq = self.assertNotEqual if global_cleanup else self.assertEqual |
| 80 |
asserteq({}, apis._orig_resources) |
| 81 |
|
| 82 |
for r in test_attributes.TestCoreResources.CORE_DEFS: |
| 83 |
attributes.RESOURCES[r.COLLECTION_NAME]['_test_'] = {} |
| 84 |
r.RESOURCE_ATTRIBUTE_MAP['_test_'] = {} |
| 85 |
|
| 86 |
apis.cleanUp() |
| 87 |
for r in test_attributes.TestCoreResources.CORE_DEFS: |
| 88 |
self.assertNotIn('_test_', r.RESOURCE_ATTRIBUTE_MAP) |
| 89 |
global_assert = (self.assertNotIn |
| 90 |
if global_cleanup else self.assertIn) |
| 91 |
global_assert('_test_', attributes.RESOURCES[r.COLLECTION_NAME]) |
| 92 |
# cleanup |
| 93 |
if not global_cleanup: |
| 94 |
del attributes.RESOURCES[r.COLLECTION_NAME]['_test_'] |
| 95 |
|
| 96 |
def test_all_api_definitions_fixture_no_global_backup(self): |
| 97 |
self._test_all_api_definitions_fixture(global_cleanup=False) |
| 98 |
|
| 99 |
def test_all_api_definitions_fixture_with_global_backup(self): |
| 100 |
self._test_all_api_definitions_fixture(global_cleanup=True) |
| 101 |
|
| 102 |
def test_global_resources_reference_updated(self): |
| 103 |
resources_ref = attributes.RESOURCES |
| 104 |
apis = fixture.APIDefinitionFixture() |
| 105 |
|
| 106 |
apis.setUp() |
| 107 |
attributes.RESOURCES['test_resource'] = {} |
| 108 |
self.assertIn('test_resource', resources_ref) |
| 109 |
attributes.RESOURCES[port.COLLECTION_NAME]['test_port_attr'] = {} |
| 110 |
self.assertIn('test_port_attr', |
| 111 |
attributes.RESOURCES[port.COLLECTION_NAME]) |
| 112 |
apis.cleanUp() |
| 113 |
|
| 114 |
self.assertNotIn('test_port_attr', |
| 115 |
attributes.RESOURCES[port.COLLECTION_NAME]) |
| 116 |
self.assertNotIn('test_resource', resources_ref) |
| 117 |
|
| 118 |
def test_api_def_reference_updated(self): |
| 119 |
api_def_ref = port.RESOURCE_ATTRIBUTE_MAP |
| 120 |
apis = fixture.APIDefinitionFixture() |
| 121 |
|
| 122 |
apis.setUp() |
| 123 |
port.RESOURCE_ATTRIBUTE_MAP[port.COLLECTION_NAME]['test_attr'] = {} |
| 124 |
self.assertIn('test_attr', api_def_ref[port.COLLECTION_NAME]) |
| 125 |
apis.cleanUp() |
| 126 |
|
| 127 |
self.assertNotIn('test_attr', |
| 128 |
port.RESOURCE_ATTRIBUTE_MAP[port.COLLECTION_NAME]) |
| 129 |
self.assertNotIn('test_attr', api_def_ref[port.COLLECTION_NAME]) |
| 130 |
|
| 131 |
|
| 132 |
class PlacementAPIClientFixtureTestCase(base.BaseTestCase): |
| 133 |
|
| 134 |
def _create_client_and_fixture(self): |
| 135 |
placement_client = place_client.PlacementAPIClient(mock.Mock()) |
| 136 |
placement_fixture = self.useFixture( |
| 137 |
fixture.PlacementAPIClientFixture(placement_client)) |
| 138 |
return placement_client, placement_fixture |
| 139 |
|
| 140 |
def test_post(self): |
| 141 |
p_client, p_fixture = self._create_client_and_fixture() |
| 142 |
p_client.create_resource_provider('resource') |
| 143 |
p_fixture.mock_post.assert_called_once() |
| 144 |
|
| 145 |
def test_put(self): |
| 146 |
inventory = {'total': 42} |
| 147 |
p_client, p_fixture = self._create_client_and_fixture() |
| 148 |
p_client.update_resource_provider_inventory('resource', inventory, |
| 149 |
'class_name', 1) |
| 150 |
p_fixture.mock_put.assert_called_once() |
| 151 |
|
| 152 |
def test_delete(self): |
| 153 |
p_client, p_fixture = self._create_client_and_fixture() |
| 154 |
p_client.delete_resource_provider('resource') |
| 155 |
p_fixture.mock_delete.assert_called_once() |
| 156 |
|
| 157 |
def test_get(self): |
| 158 |
p_client, p_fixture = self._create_client_and_fixture() |
| 159 |
p_client.list_aggregates('resource') |
| 160 |
p_fixture.mock_get.assert_called_once() |
| 161 |
|
| 162 |
|
| 163 |
class DBResourceExtendFixtureTestCase(base.BaseTestCase): |
| 164 |
|
| 165 |
def test_fixture_backup(self): |
| 166 |
fake_methods = { |
| 167 |
'a': 'A', |
| 168 |
'b': 'B' |
| 169 |
} |
| 170 |
orig_methods = resource_extend._resource_extend_functions |
| 171 |
self.assertNotEqual(fake_methods, orig_methods) |
| 172 |
|
| 173 |
db_fixture = fixture.DBResourceExtendFixture( |
| 174 |
extended_functions=fake_methods) |
| 175 |
db_fixture.setUp() |
| 176 |
|
| 177 |
resource_extend.register_funcs('C', (lambda x: x,)) |
| 178 |
self.assertNotEqual( |
| 179 |
orig_methods, resource_extend._resource_extend_functions) |
| 180 |
|
| 181 |
db_fixture.cleanUp() |
| 182 |
self.assertEqual( |
| 183 |
orig_methods, resource_extend._resource_extend_functions) |
| 184 |
|
| 185 |
|
| 186 |
class WarningsFixture(base.BaseTestCase): |
| 187 |
|
| 188 |
@mock.patch.object(fixture.warnings, 'filterwarnings') |
| 189 |
def test_fixture_regex(self, mock_filterwarnings): |
| 190 |
module_re = ['^neutron\\.'] |
| 191 |
warn_fixture = fixture.WarningsFixture(module_re=module_re) |
| 192 |
warn_fixture.setUp() |
| 193 |
call_re = mock_filterwarnings.mock_calls[0][2]['module'] |
| 194 |
self.assertEqual('^neutron_lib\\.|^neutron\\.', call_re) |
| 195 |
self.assertIsNotNone(re.compile(call_re)) |
| 196 |
self.assertIsNotNone(re.search(call_re, 'neutron.db.blah')) |
| 197 |
self.assertIsNotNone(re.search(call_re, 'neutron_lib.db.blah')) |
| 198 |
self.assertIsNone(re.search( |
| 199 |
call_re, 'neutron_dynamic_routing.db.blah')) |