Coverage for manila/tests/test_network.py: 78%
124 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2015 Mirantis Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16import ddt
17from oslo_config import cfg
18from oslo_utils import importutils
20from manila import exception
21from manila import network
22from manila import test
24CONF = cfg.CONF
27@ddt.ddt
28class APITestCase(test.TestCase):
30 def setUp(self):
31 super(APITestCase, self).setUp()
32 self.mock_object(importutils, 'import_class')
34 def test_init_api_with_default_config_group_name(self):
35 network.API()
37 importutils.import_class.assert_called_once_with(
38 CONF.network_api_class)
39 importutils.import_class.return_value.assert_called_once_with(
40 config_group_name=None, label='user')
42 def test_init_api_with_custom_config_group_name(self):
43 group_name = 'FOO_GROUP_NAME'
45 network.API(config_group_name=group_name)
47 importutils.import_class.assert_called_once_with(
48 getattr(CONF, group_name).network_api_class)
49 importutils.import_class.return_value.assert_called_once_with(
50 config_group_name=group_name, label='user')
52 def test_init_api_with_custom_config_group_name_and_label(self):
53 group_name = 'FOO_GROUP_NAME'
54 label = 'custom_label'
56 network.API(config_group_name=group_name, label=label)
58 importutils.import_class.assert_called_once_with(
59 getattr(CONF, group_name).network_api_class)
60 importutils.import_class.return_value.assert_called_once_with(
61 config_group_name=group_name, label=label)
64@ddt.ddt
65class NetworkBaseAPITestCase(test.TestCase):
67 def setUp(self):
68 super(NetworkBaseAPITestCase, self).setUp()
69 self.db_driver = 'fake_driver'
70 self.mock_object(importutils, 'import_module')
72 def test_inherit_network_base_api_no_redefinitions(self):
73 class FakeNetworkAPI(network.NetworkBaseAPI):
74 pass
76 self.assertRaises(TypeError, FakeNetworkAPI)
78 def test_inherit_network_base_api_deallocate_not_redefined(self):
79 class FakeNetworkAPI(network.NetworkBaseAPI):
80 def allocate_network(self, *args, **kwargs):
81 pass
83 def manage_network_allocations(
84 self, context, allocations, share_server,
85 share_network=None):
86 pass
88 def unmanage_network_allocations(self, context, share_server_id):
89 pass
91 def include_network_info(self, share_network_subnet):
92 pass
94 self.assertRaises(TypeError, FakeNetworkAPI)
96 def test_inherit_network_base_api_allocate_not_redefined(self):
97 class FakeNetworkAPI(network.NetworkBaseAPI):
98 def deallocate_network(self, *args, **kwargs):
99 pass
101 def manage_network_allocations(
102 self, context, allocations, share_server,
103 share_network=None):
104 pass
106 def unmanage_network_allocations(self, context, share_server_id):
107 pass
109 def include_network_info(self, share_network_subnet):
110 pass
112 self.assertRaises(TypeError, FakeNetworkAPI)
114 def test_inherit_network_base_api(self):
115 class FakeNetworkAPI(network.NetworkBaseAPI):
116 def allocate_network(self, *args, **kwargs):
117 pass
119 def deallocate_network(self, *args, **kwargs):
120 pass
122 def manage_network_allocations(
123 self, context, allocations, share_server,
124 share_network=None):
125 pass
127 def unmanage_network_allocations(self, context, share_server_id):
128 pass
130 def include_network_info(self, share_network_subnet):
131 pass
133 result = FakeNetworkAPI()
135 self.assertTrue(hasattr(result, '_verify_share_network'))
136 self.assertTrue(hasattr(result, 'allocate_network'))
137 self.assertTrue(hasattr(result, 'deallocate_network'))
139 def test__verify_share_network_ok(self):
140 class FakeNetworkAPI(network.NetworkBaseAPI):
141 def allocate_network(self, *args, **kwargs):
142 pass
144 def deallocate_network(self, *args, **kwargs):
145 pass
147 def manage_network_allocations(
148 self, context, allocations, share_server,
149 share_network=None):
150 pass
152 def unmanage_network_allocations(self, context, share_server_id):
153 pass
155 def include_network_info(self, share_network_subnet):
156 pass
158 result = FakeNetworkAPI()
160 result._verify_share_network('foo_id', {'id': 'bar_id'})
162 def test__verify_share_network_fail(self):
163 class FakeNetworkAPI(network.NetworkBaseAPI):
164 def allocate_network(self, *args, **kwargs):
165 pass
167 def deallocate_network(self, *args, **kwargs):
168 pass
170 def manage_network_allocations(
171 self, context, allocations, share_server,
172 share_network=None):
173 pass
175 def unmanage_network_allocations(self, context, share_server_id):
176 pass
178 def include_network_info(self, share_network_subnet):
179 pass
181 result = FakeNetworkAPI()
183 self.assertRaises(
184 exception.NetworkBadConfigurationException,
185 result._verify_share_network, 'foo_id', None)
187 @ddt.data((True, False, set([6])), (False, True, set([4])),
188 (True, True, set([4, 6])), (False, False, set()))
189 @ddt.unpack
190 def test_enabled_ip_versions(self, network_plugin_ipv6_enabled,
191 network_plugin_ipv4_enabled,
192 enable_ip_versions):
193 class FakeNetworkAPI(network.NetworkBaseAPI):
194 def allocate_network(self, *args, **kwargs):
195 pass
197 def deallocate_network(self, *args, **kwargs):
198 pass
200 def manage_network_allocations(
201 self, context, allocations, share_server,
202 share_network=None):
203 pass
205 def unmanage_network_allocations(self, context, share_server_id):
206 pass
208 def include_network_info(self, share_network_subnet):
209 pass
211 network.CONF.set_default(
212 'network_plugin_ipv6_enabled', network_plugin_ipv6_enabled)
213 network.CONF.set_default(
214 'network_plugin_ipv4_enabled', network_plugin_ipv4_enabled)
216 result = FakeNetworkAPI()
218 if enable_ip_versions:
219 self.assertTrue(hasattr(result, 'enabled_ip_versions'))
220 self.assertEqual(enable_ip_versions, result.enabled_ip_versions)
221 else:
222 self.assertRaises(exception.NetworkBadConfigurationException,
223 getattr, result, 'enabled_ip_versions')