Coverage for manila/tests/network/neutron/test_neutron_plugin.py: 99%

830 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2013 OpenStack Foundation 

2# Copyright 2015 Mirantis, Inc. 

3# All Rights Reserved 

4# 

5# Licensed under the Apache License, Version 2.0 (the "License"); you may 

6# not use this file except in compliance with the License. You may obtain 

7# a copy of the License at 

8# 

9# http://www.apache.org/licenses/LICENSE-2.0 

10# 

11# Unless required by applicable law or agreed to in writing, software 

12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

14# License for the specific language governing permissions and limitations 

15# under the License. 

16 

17import copy 

18import time 

19from unittest import mock 

20 

21import ddt 

22from oslo_config import cfg 

23 

24from manila.common import constants 

25from manila import context 

26from manila.db import api as db_api 

27from manila import exception 

28from manila.network.neutron import api as neutron_api 

29from manila.network.neutron import constants as neutron_constants 

30from manila.network.neutron import neutron_network_plugin as plugin 

31from manila.share import utils as share_utils 

32from manila import test 

33from manila.tests import utils as test_utils 

34 

35CONF = cfg.CONF 

36 

37fake_neutron_port = { 

38 "status": "ACTIVE", 

39 "allowed_address_pairs": [], 

40 "admin_state_up": True, 

41 "network_id": "test_net_id", 

42 "tenant_id": "fake_tenant_id", 

43 "extra_dhcp_opts": [], 

44 "device_owner": "test", 

45 "binding:capabilities": {"port_filter": True}, 

46 "mac_address": "test_mac", 

47 "fixed_ips": [ 

48 {"subnet_id": "test_subnet_id", "ip_address": "203.0.113.100"}, 

49 ], 

50 "id": "test_port_id", 

51 "security_groups": ["fake_sec_group_id"], 

52 "device_id": "fake_device_id", 

53} 

54 

55fake_neutron_network = { 

56 'admin_state_up': True, 

57 'availability_zone_hints': [], 

58 'availability_zones': ['nova'], 

59 'description': '', 

60 'id': 'fake net id', 

61 'ipv4_address_scope': None, 

62 'ipv6_address_scope': None, 

63 'name': 'test_neutron_network', 

64 'port_security_enabled': True, 

65 'provider:network_type': 'vxlan', 

66 'provider:physical_network': None, 

67 'provider:segmentation_id': 1234, 

68 'router:external': False, 

69 'shared': False, 

70 'status': 'ACTIVE', 

71 'subnets': ['fake subnet id', 

72 'fake subnet id 2'], 

73} 

74 

75fake_ip_version = 4 

76 

77fake_neutron_subnet = { 

78 'cidr': '10.0.0.0/24', 

79 'ip_version': fake_ip_version, 

80 'gateway_ip': '10.0.0.1', 

81} 

82 

83fake_share_network_subnet = { 

84 'id': 'fake nw subnet id', 

85 'neutron_subnet_id': fake_neutron_network['subnets'][0], 

86 'neutron_net_id': fake_neutron_network['id'], 

87 'network_type': 'fake_network_type', 

88 'segmentation_id': 1234, 

89 'ip_version': 4, 

90 'cidr': 'fake_cidr', 

91 'gateway': 'fake_gateway', 

92 'mtu': 1509, 

93} 

94 

95fake_share_network = { 

96 'id': 'fake nw info id', 

97 'project_id': 'fake project id', 

98 'status': 'test_subnet_status', 

99 'name': 'fake name', 

100 'description': 'fake description', 

101 'security_services': [], 

102 'subnets': [fake_share_network_subnet], 

103} 

104 

105fake_share_server = { 

106 'id': 'fake nw info id', 

107 'status': 'test_server_status', 

108 'host': 'fake@host', 

109 'network_allocations': [], 

110 'shares': [], 

111} 

112 

113fake_network_allocation = { 

114 'id': fake_neutron_port['id'], 

115 'share_server_id': fake_share_server['id'], 

116 'ip_address': fake_neutron_port['fixed_ips'][0]['ip_address'], 

117 'mac_address': fake_neutron_port['mac_address'], 

118 'status': constants.STATUS_ACTIVE, 

119 'label': 'user', 

120 'network_type': fake_share_network_subnet['network_type'], 

121 'segmentation_id': fake_share_network_subnet['segmentation_id'], 

122 'ip_version': fake_share_network_subnet['ip_version'], 

123 'cidr': fake_share_network_subnet['cidr'], 

124 'gateway': fake_share_network_subnet['gateway'], 

125 'mtu': 1509, 

126 'share_network_subnet_id': fake_share_network_subnet['id'], 

127} 

128 

129fake_nw_info = { 

130 'segments': [ 

131 { 

132 'provider:network_type': 'vlan', 

133 'provider:physical_network': 'net1', 

134 'provider:segmentation_id': 3926, 

135 }, 

136 { 

137 'provider:network_type': 'vlan', 

138 'provider:physical_network': 'net2', 

139 'provider:segmentation_id': 1249, 

140 }, 

141 { 

142 'provider:network_type': 'vxlan', 

143 'provider:physical_network': None, 

144 'provider:segmentation_id': 2000, 

145 }, 

146 ], 

147 'mtu': 1509, 

148} 

149 

150fake_neutron_network_multi = { 

151 'admin_state_up': True, 

152 'availability_zone_hints': [], 

153 'availability_zones': ['nova'], 

154 'description': '', 

155 'id': 'fake net id', 

156 'ipv4_address_scope': None, 

157 'ipv6_address_scope': None, 

158 'name': 'test_neutron_network', 

159 'port_security_enabled': True, 

160 'router:external': False, 

161 'shared': False, 

162 'status': 'ACTIVE', 

163 'subnets': ['fake subnet id', 

164 'fake subnet id 2'], 

165 'segments': fake_nw_info['segments'], 

166 'mtu': fake_nw_info['mtu'], 

167} 

168 

169fake_share_network_multi = { 

170 'id': 'fake nw info id', 

171 'neutron_subnet_id': fake_neutron_network_multi['subnets'][0], 

172 'neutron_net_id': fake_neutron_network_multi['id'], 

173 'project_id': 'fake project id', 

174 'status': 'test_subnet_status', 

175 'name': 'fake name', 

176 'description': 'fake description', 

177 'security_services': [], 

178 'ip_version': None, 

179 'cidr': 'fake_cidr', 

180 'gateway': 'fake_gateway', 

181 'mtu': fake_neutron_network_multi['mtu'] - 1, 

182} 

183 

184fake_network_allocation_multi = { 

185 'id': fake_neutron_port['id'], 

186 'share_server_id': fake_share_server['id'], 

187 'ip_address': fake_neutron_port['fixed_ips'][0]['ip_address'], 

188 'mac_address': fake_neutron_port['mac_address'], 

189 'status': constants.STATUS_ACTIVE, 

190 'label': 'user', 

191 'network_type': None, 

192 'segmentation_id': None, 

193 'ip_version': fake_neutron_subnet['ip_version'], 

194 'cidr': fake_neutron_subnet['cidr'], 

195 'gateway': fake_neutron_subnet['gateway_ip'], 

196 'mtu': fake_neutron_network_multi['mtu'], 

197 'share_network_subnet_id': fake_share_network['id'], 

198} 

199 

200fake_binding_profile = { 

201 'neutron_switch_id': 'fake switch id', 

202 'neutron_port_id': 'fake port id', 

203 'neutron_switch_info': 'fake switch info' 

204} 

205 

206fake_network_allocation_ext = { 

207 'id': 'fake port binding id', 

208 'share_server_id': fake_share_server['id'], 

209 'ip_address': fake_neutron_port['fixed_ips'][0]['ip_address'], 

210 'mac_address': fake_neutron_port['mac_address'], 

211 'status': constants.STATUS_ACTIVE, 

212 'label': fake_nw_info['segments'][1]['provider:physical_network'], 

213 'network_type': fake_share_network_subnet['network_type'], 

214 'segmentation_id': ( 

215 fake_nw_info['segments'][1]['provider:segmentation_id'] 

216 ), 

217 'ip_version': fake_share_network_subnet['ip_version'], 

218 'cidr': fake_share_network_subnet['cidr'], 

219 'gateway': fake_share_network_subnet['gateway'], 

220 'mtu': 1509, 

221} 

222 

223 

224@ddt.ddt 

225class NeutronNetworkPluginTest(test.TestCase): 

226 

227 def setUp(self): 

228 super(NeutronNetworkPluginTest, self).setUp() 

229 self.plugin = self._get_neutron_network_plugin_instance() 

230 self.plugin.db = db_api 

231 self.fake_context = context.RequestContext(user_id='fake user', 

232 project_id='fake project', 

233 is_admin=False) 

234 

235 def _get_neutron_network_plugin_instance(self, config_data=None): 

236 if config_data is None: 236 ↛ 238line 236 didn't jump to line 238 because the condition on line 236 was always true

237 return plugin.NeutronNetworkPlugin() 

238 with test_utils.create_temp_config_with_opts(config_data): 

239 return plugin.NeutronNetworkPlugin() 

240 

241 @mock.patch.object(db_api, 'network_allocation_create', 

242 mock.Mock(return_values=fake_network_allocation)) 

243 @mock.patch.object(db_api, 'share_network_get', 

244 mock.Mock(return_value=fake_share_network)) 

245 @mock.patch.object(db_api, 'share_server_get', 

246 mock.Mock(return_value=fake_share_server)) 

247 def test_allocate_network_external_neutron_network(self): 

248 has_provider_nw_ext = mock.patch.object( 

249 self.plugin, '_has_provider_network_extension').start() 

250 has_provider_nw_ext.return_value = True 

251 save_nw_data = mock.patch.object(self.plugin, 

252 '_save_neutron_network_data', 

253 mock.Mock(return_value=True)).start() 

254 save_subnet_data = mock.patch.object( 

255 self.plugin, 

256 '_save_neutron_subnet_data').start() 

257 

258 with mock.patch.object(self.plugin.neutron_api, 'create_port', 

259 mock.Mock(return_value=fake_neutron_port)): 

260 self.plugin.allocate_network( 

261 self.fake_context, 

262 fake_share_server, 

263 fake_share_network, 

264 fake_share_network_subnet, 

265 allocation_info={'count': 1}) 

266 

267 has_provider_nw_ext.assert_any_call() 

268 save_nw_data.assert_called_once_with(self.fake_context, 

269 fake_share_network_subnet, 

270 save_db=True) 

271 save_subnet_data.assert_called_once_with(self.fake_context, 

272 fake_share_network_subnet, 

273 save_db=True) 

274 self.plugin.neutron_api.create_port.assert_called_once_with( 

275 fake_share_network['project_id'], 

276 network_id=fake_share_network_subnet['neutron_net_id'], 

277 subnet_id=fake_share_network_subnet['neutron_subnet_id'], 

278 device_owner='manila:share', 

279 device_id=fake_share_network['id'], 

280 name=fake_share_network['id'] + '_0', 

281 admin_state_up=False, 

282 ) 

283 db_api.network_allocation_create.assert_called_once_with( 

284 self.fake_context, 

285 fake_network_allocation) 

286 

287 has_provider_nw_ext.stop() 

288 save_nw_data.stop() 

289 save_subnet_data.stop() 

290 

291 @mock.patch.object(db_api, 'network_allocation_create', 

292 mock.Mock(return_values=fake_network_allocation)) 

293 @mock.patch.object(db_api, 'share_network_get', 

294 mock.Mock(return_value=fake_share_network)) 

295 @mock.patch.object(db_api, 'share_server_get', 

296 mock.Mock(return_value=fake_share_server)) 

297 def test_allocate_network_one_allocation(self): 

298 has_provider_nw_ext = mock.patch.object( 

299 self.plugin, '_has_provider_network_extension').start() 

300 has_provider_nw_ext.return_value = True 

301 save_nw_data = mock.patch.object(self.plugin, 

302 '_save_neutron_network_data').start() 

303 save_subnet_data = mock.patch.object( 

304 self.plugin, 

305 '_save_neutron_subnet_data').start() 

306 

307 with mock.patch.object(self.plugin.neutron_api, 'create_port', 

308 mock.Mock(return_value=fake_neutron_port)): 

309 self.plugin.allocate_network( 

310 self.fake_context, 

311 fake_share_server, 

312 fake_share_network, 

313 fake_share_network_subnet, 

314 allocation_info={'count': 1}) 

315 

316 has_provider_nw_ext.assert_any_call() 

317 save_nw_data.assert_called_once_with(self.fake_context, 

318 fake_share_network_subnet, 

319 save_db=True) 

320 save_subnet_data.assert_called_once_with(self.fake_context, 

321 fake_share_network_subnet, 

322 save_db=True) 

323 self.plugin.neutron_api.create_port.assert_called_once_with( 

324 fake_share_network['project_id'], 

325 network_id=fake_share_network_subnet['neutron_net_id'], 

326 subnet_id=fake_share_network_subnet['neutron_subnet_id'], 

327 device_owner='manila:share', 

328 device_id=fake_share_network['id'], 

329 name=fake_share_network['id'] + '_0', 

330 admin_state_up=False, 

331 ) 

332 db_api.network_allocation_create.assert_called_once_with( 

333 self.fake_context, 

334 fake_network_allocation) 

335 

336 has_provider_nw_ext.stop() 

337 save_nw_data.stop() 

338 save_subnet_data.stop() 

339 

340 @mock.patch.object(db_api, 'network_allocation_create', 

341 mock.Mock(return_values=fake_network_allocation)) 

342 @mock.patch.object(db_api, 'share_network_get', 

343 mock.Mock(return_value=fake_share_network)) 

344 @mock.patch.object(db_api, 'share_server_get', 

345 mock.Mock(return_value=fake_share_server)) 

346 def test_allocate_network_two_allocation(self): 

347 has_provider_nw_ext = mock.patch.object( 

348 self.plugin, '_has_provider_network_extension').start() 

349 has_provider_nw_ext.return_value = True 

350 save_nw_data = mock.patch.object(self.plugin, 

351 '_save_neutron_network_data').start() 

352 save_subnet_data = mock.patch.object( 

353 self.plugin, 

354 '_save_neutron_subnet_data').start() 

355 

356 with mock.patch.object(self.plugin.neutron_api, 'create_port', 

357 mock.Mock(return_value=fake_neutron_port)): 

358 self.plugin.allocate_network( 

359 self.fake_context, fake_share_server, fake_share_network, 

360 fake_share_network_subnet, count=2) 

361 

362 neutron_api_calls = [ 

363 mock.call( 

364 fake_share_network['project_id'], 

365 network_id=fake_share_network_subnet['neutron_net_id'], 

366 subnet_id=fake_share_network_subnet['neutron_subnet_id'], 

367 device_owner='manila:share', 

368 device_id=fake_share_network['id'], 

369 name=fake_share_network['id'] + '_0', 

370 admin_state_up=False, 

371 ), 

372 mock.call( 

373 fake_share_network['project_id'], 

374 network_id=fake_share_network_subnet['neutron_net_id'], 

375 subnet_id=fake_share_network_subnet['neutron_subnet_id'], 

376 device_owner='manila:share', 

377 device_id=fake_share_network['id'], 

378 name=fake_share_network['id'] + '_1', 

379 admin_state_up=False, 

380 ) 

381 ] 

382 db_api_calls = [ 

383 mock.call(self.fake_context, fake_network_allocation), 

384 mock.call(self.fake_context, fake_network_allocation) 

385 ] 

386 self.plugin.neutron_api.create_port.assert_has_calls( 

387 neutron_api_calls) 

388 db_api.network_allocation_create.assert_has_calls(db_api_calls) 

389 

390 has_provider_nw_ext.stop() 

391 save_nw_data.stop() 

392 save_subnet_data.stop() 

393 

394 @mock.patch.object(db_api, 'share_network_update', mock.Mock()) 

395 def test_allocate_network_create_port_exception(self): 

396 has_provider_nw_ext = mock.patch.object( 

397 self.plugin, '_has_provider_network_extension').start() 

398 has_provider_nw_ext.return_value = True 

399 save_nw_data = mock.patch.object(self.plugin, 

400 '_save_neutron_network_data').start() 

401 save_subnet_data = mock.patch.object( 

402 self.plugin, 

403 '_save_neutron_subnet_data').start() 

404 create_port = mock.patch.object(self.plugin.neutron_api, 

405 'create_port').start() 

406 create_port.side_effect = exception.NetworkException 

407 

408 self.assertRaises(exception.NetworkException, 

409 self.plugin.allocate_network, 

410 self.fake_context, 

411 fake_share_server, 

412 fake_share_network) 

413 

414 has_provider_nw_ext.stop() 

415 save_nw_data.stop() 

416 save_subnet_data.stop() 

417 create_port.stop() 

418 

419 def _setup_manage_network_allocations(self): 

420 

421 allocations = ['192.168.0.11', '192.168.0.12', 'fd12::2000'] 

422 

423 neutron_ports = [ 

424 copy.deepcopy(fake_neutron_port), copy.deepcopy(fake_neutron_port), 

425 copy.deepcopy(fake_neutron_port), copy.deepcopy(fake_neutron_port), 

426 ] 

427 

428 neutron_ports[0]['fixed_ips'][0]['ip_address'] = '192.168.0.10' 

429 neutron_ports[0]['id'] = 'fake_port_id_0' 

430 neutron_ports[1]['fixed_ips'][0]['ip_address'] = '192.168.0.11' 

431 neutron_ports[1]['id'] = 'fake_port_id_1' 

432 neutron_ports[2]['fixed_ips'][0]['ip_address'] = '192.168.0.12' 

433 neutron_ports[2]['id'] = 'fake_port_id_2' 

434 neutron_ports[3]['fixed_ips'][0]['ip_address'] = '192.168.0.13' 

435 neutron_ports[3]['id'] = 'fake_port_id_3' 

436 

437 self.mock_object(self.plugin, '_verify_share_network_subnet') 

438 self.mock_object(self.plugin, '_store_and_get_neutron_net_info') 

439 

440 self.mock_object(self.plugin.neutron_api, 'list_ports', 

441 mock.Mock(return_value=neutron_ports)) 

442 

443 return neutron_ports, allocations 

444 

445 @ddt.data({}, exception.NotFound) 

446 def test_manage_network_allocations_create_update(self, side_effect): 

447 

448 neutron_ports, allocations = self._setup_manage_network_allocations() 

449 

450 self.mock_object(db_api, 'network_allocation_get', 

451 mock.Mock( 

452 side_effect=[exception.NotFound, side_effect, 

453 exception.NotFound, side_effect])) 

454 if side_effect: 

455 self.mock_object(db_api, 'network_allocation_create') 

456 else: 

457 self.mock_object(db_api, 'network_allocation_update') 

458 

459 result = self.plugin.manage_network_allocations( 

460 self.fake_context, allocations, fake_share_server, 

461 share_network_subnet=fake_share_network_subnet) 

462 

463 self.assertEqual(['fd12::2000'], result) 

464 

465 self.plugin.neutron_api.list_ports.assert_called_once_with( 

466 network_id=fake_share_network_subnet['neutron_net_id'], 

467 device_owner='manila:share', 

468 fixed_ips='subnet_id=' + 

469 fake_share_network_subnet['neutron_subnet_id']) 

470 

471 db_api.network_allocation_get.assert_has_calls([ 

472 mock.call(self.fake_context, 'fake_port_id_1', read_deleted=False), 

473 mock.call(self.fake_context, 'fake_port_id_1', read_deleted=True), 

474 mock.call(self.fake_context, 'fake_port_id_2', read_deleted=False), 

475 mock.call(self.fake_context, 'fake_port_id_2', read_deleted=True), 

476 ]) 

477 

478 port_dict_list = [{ 

479 'share_server_id': fake_share_server['id'], 

480 'ip_address': x, 

481 'gateway': fake_share_network_subnet['gateway'], 

482 'mac_address': fake_neutron_port['mac_address'], 

483 'status': constants.STATUS_ACTIVE, 

484 'label': 'user', 

485 'network_type': fake_share_network_subnet['network_type'], 

486 'segmentation_id': fake_share_network_subnet['segmentation_id'], 

487 'ip_version': fake_share_network_subnet['ip_version'], 

488 'cidr': fake_share_network_subnet['cidr'], 

489 'mtu': fake_share_network_subnet['mtu'], 

490 'share_network_subnet_id': fake_share_network_subnet['id'], 

491 } for x in ['192.168.0.11', '192.168.0.12']] 

492 

493 if side_effect: 

494 port_dict_list[0]['id'] = 'fake_port_id_1' 

495 port_dict_list[1]['id'] = 'fake_port_id_2' 

496 db_api.network_allocation_create.assert_has_calls([ 

497 mock.call(self.fake_context, port_dict_list[0]), 

498 mock.call(self.fake_context, port_dict_list[1]) 

499 ]) 

500 else: 

501 for x in port_dict_list: 

502 x['deleted_at'] = None 

503 x['deleted'] = 'False' 

504 

505 db_api.network_allocation_update.assert_has_calls([ 

506 mock.call(self.fake_context, 'fake_port_id_1', 

507 port_dict_list[0], read_deleted=True), 

508 mock.call(self.fake_context, 'fake_port_id_2', 

509 port_dict_list[1], read_deleted=True) 

510 ]) 

511 

512 self.plugin._verify_share_network_subnet.assert_called_once_with( 

513 fake_share_server['id'], fake_share_network_subnet) 

514 

515 self.plugin._store_and_get_neutron_net_info( 

516 self.fake_context, fake_share_network_subnet) 

517 

518 def test__get_ports_respective_to_ips_multiple_fixed_ips(self): 

519 self.mock_object(plugin.LOG, 'warning') 

520 

521 allocations = ['192.168.0.10', '192.168.0.11', '192.168.0.12'] 

522 

523 neutron_ports = [ 

524 copy.deepcopy(fake_neutron_port), copy.deepcopy(fake_neutron_port), 

525 ] 

526 

527 neutron_ports[0]['fixed_ips'][0]['ip_address'] = '192.168.0.10' 

528 neutron_ports[0]['id'] = 'fake_port_id_0' 

529 neutron_ports[0]['fixed_ips'].append({'ip_address': '192.168.0.11', 

530 'subnet_id': 'test_subnet_id'}) 

531 neutron_ports[1]['fixed_ips'][0]['ip_address'] = '192.168.0.12' 

532 neutron_ports[1]['id'] = 'fake_port_id_2' 

533 

534 expected = [{'port': neutron_ports[0], 'allocation': '192.168.0.10'}, 

535 {'port': neutron_ports[1], 'allocation': '192.168.0.12'}] 

536 

537 result = self.plugin._get_ports_respective_to_ips(allocations, 

538 neutron_ports) 

539 

540 self.assertEqual(expected, result) 

541 

542 self.assertIs(True, plugin.LOG.warning.called) 

543 

544 def test_manage_network_allocations_exception(self): 

545 

546 neutron_ports, allocations = self._setup_manage_network_allocations() 

547 

548 fake_allocation = { 

549 'id': 'fake_port_id', 

550 'share_server_id': 'fake_server_id' 

551 } 

552 

553 self.mock_object(db_api, 'network_allocation_get', 

554 mock.Mock(return_value=fake_allocation)) 

555 

556 self.assertRaises( 

557 exception.ManageShareServerError, 

558 self.plugin.manage_network_allocations, self.fake_context, 

559 allocations, fake_share_server, fake_share_network, 

560 fake_share_network_subnet) 

561 

562 db_api.network_allocation_get.assert_called_once_with( 

563 self.fake_context, 'fake_port_id_1', read_deleted=False) 

564 

565 def test_unmanage_network_allocations(self): 

566 

567 neutron_ports = [ 

568 copy.deepcopy(fake_neutron_port), copy.deepcopy(fake_neutron_port), 

569 ] 

570 

571 neutron_ports[0]['id'] = 'fake_port_id_0' 

572 neutron_ports[1]['id'] = 'fake_port_id_1' 

573 

574 get_mock = self.mock_object( 

575 db_api, 'network_allocations_get_for_share_server', 

576 mock.Mock(return_value=neutron_ports)) 

577 

578 self.mock_object(db_api, 'network_allocation_delete') 

579 

580 self.plugin.unmanage_network_allocations( 

581 self.fake_context, fake_share_server['id']) 

582 

583 get_mock.assert_called_once_with( 

584 self.fake_context, fake_share_server['id']) 

585 

586 db_api.network_allocation_delete.assert_has_calls([ 

587 mock.call(self.fake_context, 'fake_port_id_0'), 

588 mock.call(self.fake_context, 'fake_port_id_1') 

589 ]) 

590 

591 @mock.patch.object(db_api, 'network_allocation_delete', mock.Mock()) 

592 @mock.patch.object(db_api, 'share_network_update', mock.Mock()) 

593 @mock.patch.object(db_api, 'network_allocations_get_for_share_server', 

594 mock.Mock(return_value=[fake_network_allocation])) 

595 def test_deallocate_network_nominal(self): 

596 share_srv = {'id': fake_share_server['id']} 

597 share_srv['network_allocations'] = [fake_network_allocation] 

598 

599 with mock.patch.object(self.plugin.neutron_api, 'delete_port', 

600 mock.Mock()): 

601 self.plugin.deallocate_network(self.fake_context, share_srv) 

602 self.plugin.neutron_api.delete_port.assert_called_once_with( 

603 fake_network_allocation['id']) 

604 db_api.network_allocation_delete.assert_called_once_with( 

605 self.fake_context, 

606 fake_network_allocation['id']) 

607 

608 @mock.patch.object(db_api, 'share_network_update', 

609 mock.Mock(return_value=fake_share_network)) 

610 @mock.patch.object(db_api, 'network_allocation_update', mock.Mock()) 

611 @mock.patch.object(db_api, 'network_allocations_get_for_share_server', 

612 mock.Mock(return_value=[fake_network_allocation])) 

613 def test_deallocate_network_neutron_api_exception(self): 

614 share_srv = {'id': fake_share_server['id']} 

615 share_srv['network_allocations'] = [fake_network_allocation] 

616 

617 delete_port = mock.patch.object(self.plugin.neutron_api, 

618 'delete_port').start() 

619 delete_port.side_effect = exception.NetworkException 

620 

621 self.assertRaises(exception.NetworkException, 

622 self.plugin.deallocate_network, 

623 self.fake_context, 

624 share_srv) 

625 db_api.network_allocation_update.assert_called_once_with( 

626 self.fake_context, 

627 fake_network_allocation['id'], 

628 {'status': constants.STATUS_ERROR}) 

629 delete_port.stop() 

630 

631 @mock.patch.object(db_api, 'share_network_subnet_update', mock.Mock()) 

632 def test_save_neutron_network_data(self): 

633 neutron_nw_info = { 

634 'provider:network_type': 'vlan', 

635 'provider:segmentation_id': 1000, 

636 'mtu': 1509, 

637 'router:external': True, 

638 } 

639 share_nw_update_dict = { 

640 'network_type': 'vlan', 

641 'segmentation_id': 1000, 

642 'mtu': 1509, 

643 } 

644 

645 with mock.patch.object(self.plugin.neutron_api, 

646 'get_network', 

647 mock.Mock(return_value=neutron_nw_info)): 

648 is_external_network = self.plugin._save_neutron_network_data( 

649 self.fake_context, fake_share_network_subnet) 

650 

651 self.plugin.neutron_api.get_network.assert_called_once_with( 

652 fake_share_network_subnet['neutron_net_id']) 

653 self.plugin.db.share_network_subnet_update.assert_called_once_with( 

654 self.fake_context, 

655 fake_share_network_subnet['id'], 

656 share_nw_update_dict) 

657 self.assertTrue(is_external_network) 

658 

659 @mock.patch.object(db_api, 'share_network_subnet_update', mock.Mock()) 

660 def test_save_neutron_network_data_multi_segment(self): 

661 share_nw_update_dict = { 

662 'network_type': 'vlan', 

663 'segmentation_id': 3926, 

664 'mtu': 1509 

665 } 

666 config_data = { 

667 'DEFAULT': { 

668 'neutron_physical_net_name': 'net1', 

669 } 

670 } 

671 

672 self.mock_object(self.plugin.neutron_api, 'get_network') 

673 self.plugin.neutron_api.get_network.return_value = fake_nw_info 

674 

675 with test_utils.create_temp_config_with_opts(config_data): 

676 self.plugin._save_neutron_network_data(self.fake_context, 

677 fake_share_network_subnet) 

678 

679 self.plugin.neutron_api.get_network.assert_called_once_with( 

680 fake_share_network_subnet['neutron_net_id']) 

681 self.plugin.db.share_network_subnet_update.assert_called_once_with( 

682 self.fake_context, 

683 fake_share_network_subnet['id'], 

684 share_nw_update_dict) 

685 

686 @mock.patch.object(db_api, 'share_network_update', mock.Mock()) 

687 def test_save_neutron_network_data_multi_segment_without_ident(self): 

688 config_data = { 

689 'DEFAULT': { 

690 'neutron_physical_net_name': 'net100', 

691 } 

692 } 

693 

694 self.mock_object(self.plugin.neutron_api, 'get_network') 

695 self.plugin.neutron_api.get_network.return_value = fake_nw_info 

696 

697 with test_utils.create_temp_config_with_opts(config_data): 

698 self.assertRaises(exception.NetworkBadConfigurationException, 

699 self.plugin._save_neutron_network_data, 

700 self.fake_context, fake_share_network_subnet) 

701 

702 @mock.patch.object(db_api, 'share_network_update', mock.Mock()) 

703 def test_save_neutron_network_data_multi_segment_without_cfg(self): 

704 self.mock_object(self.plugin.neutron_api, 'get_network') 

705 self.plugin.neutron_api.get_network.return_value = fake_nw_info 

706 

707 self.assertRaises(exception.NetworkBadConfigurationException, 

708 self.plugin._save_neutron_network_data, 

709 self.fake_context, fake_share_network_subnet) 

710 

711 @mock.patch.object(db_api, 'share_network_subnet_update', mock.Mock()) 

712 def test_save_neutron_subnet_data(self): 

713 neutron_subnet_info = fake_neutron_subnet 

714 subnet_value = { 

715 'cidr': '10.0.0.0/24', 

716 'ip_version': 4, 

717 'gateway': '10.0.0.1', 

718 } 

719 

720 with mock.patch.object(self.plugin.neutron_api, 

721 'get_subnet', 

722 mock.Mock(return_value=neutron_subnet_info)): 

723 self.plugin._save_neutron_subnet_data(self.fake_context, 

724 fake_share_network_subnet) 

725 

726 self.plugin.neutron_api.get_subnet.assert_called_once_with( 

727 fake_share_network_subnet['neutron_subnet_id']) 

728 self.plugin.db.share_network_subnet_update.assert_called_once_with( 

729 self.fake_context, 

730 fake_share_network_subnet['id'], 

731 subnet_value) 

732 

733 def test_has_network_provider_extension_true(self): 

734 extensions = {neutron_constants.PROVIDER_NW_EXT: {}} 

735 with mock.patch.object(self.plugin.neutron_api, 

736 'list_extensions', 

737 mock.Mock(return_value=extensions)): 

738 result = self.plugin._has_provider_network_extension() 

739 

740 self.plugin.neutron_api.list_extensions.assert_any_call() 

741 self.assertTrue(result) 

742 

743 def test_has_network_provider_extension_false(self): 

744 with mock.patch.object(self.plugin.neutron_api, 

745 'list_extensions', 

746 mock.Mock(return_value={})): 

747 result = self.plugin._has_provider_network_extension() 

748 

749 self.plugin.neutron_api.list_extensions.assert_any_call() 

750 self.assertFalse(result) 

751 

752 

753@ddt.ddt 

754class NeutronSingleNetworkPluginTest(test.TestCase): 

755 

756 def setUp(self): 

757 super(NeutronSingleNetworkPluginTest, self).setUp() 

758 self.context = 'fake_context' 

759 

760 def test_init_valid(self): 

761 fake_net_id = 'fake_net_id' 

762 fake_subnet_id = 'fake_subnet_id' 

763 config_data = { 

764 'DEFAULT': { 

765 'neutron_net_id': fake_net_id, 

766 'neutron_subnet_id': fake_subnet_id, 

767 } 

768 } 

769 fake_net = {'subnets': ['fake1', 'fake2', fake_subnet_id]} 

770 self.mock_object( 

771 neutron_api.API, 'get_network', mock.Mock(return_value=fake_net)) 

772 

773 with test_utils.create_temp_config_with_opts(config_data): 

774 instance = plugin.NeutronSingleNetworkPlugin() 

775 

776 self.assertEqual(fake_net_id, instance.net) 

777 self.assertEqual(fake_subnet_id, instance.subnet) 

778 neutron_api.API.get_network.assert_called_once_with(fake_net_id) 

779 

780 @ddt.data( 

781 {'net': None, 'subnet': None}, 

782 {'net': 'fake_net_id', 'subnet': None}, 

783 {'net': None, 'subnet': 'fake_subnet_id'}) 

784 @ddt.unpack 

785 def test_init_invalid(self, net, subnet): 

786 config_data = dict() 

787 # Simulate absence of set values 

788 if net: 

789 config_data['neutron_net_id'] = net 

790 if subnet: 

791 config_data['neutron_subnet_id'] = subnet 

792 config_data = dict(DEFAULT=config_data) 

793 

794 with test_utils.create_temp_config_with_opts(config_data): 

795 self.assertRaises( 

796 exception.NetworkBadConfigurationException, 

797 plugin.NeutronSingleNetworkPlugin) 

798 

799 @ddt.data({}, {'subnets': []}, {'subnets': ['different_foo_subnet']}) 

800 def test_init_subnet_does_not_belong_to_net(self, fake_net): 

801 fake_net_id = 'fake_net_id' 

802 config_data = { 

803 'DEFAULT': { 

804 'neutron_net_id': fake_net_id, 

805 'neutron_subnet_id': 'fake_subnet_id', 

806 } 

807 } 

808 self.mock_object( 

809 neutron_api.API, 'get_network', mock.Mock(return_value=fake_net)) 

810 

811 with test_utils.create_temp_config_with_opts(config_data): 

812 self.assertRaises( 

813 exception.NetworkBadConfigurationException, 

814 plugin.NeutronSingleNetworkPlugin) 

815 neutron_api.API.get_network.assert_called_once_with(fake_net_id) 

816 

817 def _get_neutron_network_plugin_instance( 

818 self, config_data=None, label=None): 

819 if not config_data: 819 ↛ 831line 819 didn't jump to line 831 because the condition on line 819 was always true

820 fake_subnet_id = 'fake_subnet_id' 

821 config_data = { 

822 'DEFAULT': { 

823 'neutron_net_id': 'fake_net_id', 

824 'neutron_subnet_id': fake_subnet_id, 

825 } 

826 } 

827 fake_net = {'subnets': [fake_subnet_id]} 

828 self.mock_object( 

829 neutron_api.API, 'get_network', 

830 mock.Mock(return_value=fake_net)) 

831 with test_utils.create_temp_config_with_opts(config_data): 

832 instance = plugin.NeutronSingleNetworkPlugin(label=label) 

833 return instance 

834 

835 def test___update_share_network_net_data_same_values(self): 

836 instance = self._get_neutron_network_plugin_instance() 

837 share_network = { 

838 'neutron_net_id': instance.net, 

839 'neutron_subnet_id': instance.subnet, 

840 } 

841 

842 result = instance._update_share_network_net_data( 

843 self.context, share_network) 

844 

845 self.assertEqual(share_network, result) 

846 

847 def test___update_share_network_net_data_different_values_empty(self): 

848 instance = self._get_neutron_network_plugin_instance() 

849 share_network_input = { 

850 'id': 'fake_share_network_id', 

851 } 

852 share_network_result = { 

853 'neutron_net_id': instance.net, 

854 'neutron_subnet_id': instance.subnet, 

855 } 

856 self.mock_object( 

857 instance.db, 'share_network_subnet_update', 

858 mock.Mock(return_value='foo')) 

859 

860 instance._update_share_network_net_data( 

861 self.context, share_network_input) 

862 

863 instance.db.share_network_subnet_update.assert_called_once_with( 

864 self.context, share_network_input['id'], share_network_result) 

865 

866 @ddt.data( 

867 {'n': 'fake_net_id', 's': 'bar'}, 

868 {'n': 'foo', 's': 'fake_subnet_id'}) 

869 @ddt.unpack 

870 def test___update_share_network_net_data_different_values(self, n, s): 

871 instance = self._get_neutron_network_plugin_instance() 

872 share_network = { 

873 'id': 'fake_share_network_id', 

874 'neutron_net_id': n, 

875 'neutron_subnet_id': s, 

876 } 

877 self.mock_object( 

878 instance.db, 'share_network_update', 

879 mock.Mock(return_value=share_network)) 

880 

881 self.assertRaises( 

882 exception.NetworkBadConfigurationException, 

883 instance._update_share_network_net_data, 

884 self.context, share_network) 

885 self.assertFalse(instance.db.share_network_update.called) 

886 

887 def test_allocate_network(self): 

888 self.mock_object(plugin.NeutronNetworkPlugin, 'allocate_network') 

889 plugin.NeutronNetworkPlugin.allocate_network.return_value = [ 

890 fake_neutron_port, fake_neutron_port] 

891 instance = self._get_neutron_network_plugin_instance() 

892 share_server = 'fake_share_server' 

893 share_network = {'id': 'fake_share_network'} 

894 share_network_subnet = {'id': 'fake_share_network_subnet'} 

895 share_network_subnet_upd = {'id': 'updated_fake_share_network_subnet'} 

896 count = 2 

897 device_owner = 'fake_device_owner' 

898 self.mock_object( 

899 instance, '_update_share_network_net_data', 

900 mock.Mock(return_value=share_network_subnet_upd)) 

901 

902 instance.allocate_network( 

903 self.context, share_server, share_network, share_network_subnet, 

904 count=count, device_owner=device_owner) 

905 

906 instance._update_share_network_net_data.assert_called_once_with( 

907 self.context, share_network_subnet) 

908 plugin.NeutronNetworkPlugin.allocate_network.assert_called_once_with( 

909 self.context, share_server, share_network, 

910 share_network_subnet_upd, count=count, device_owner=device_owner) 

911 

912 def test_manage_network_allocations(self): 

913 allocations = ['192.168.10.10', 'fd12::2000'] 

914 instance = self._get_neutron_network_plugin_instance() 

915 parent = self.mock_object( 

916 plugin.NeutronNetworkPlugin, 'manage_network_allocations', 

917 mock.Mock(return_value=['fd12::2000'])) 

918 self.mock_object( 

919 instance, '_update_share_network_net_data', 

920 mock.Mock(return_value=fake_share_network_subnet)) 

921 

922 result = instance.manage_network_allocations( 

923 self.context, allocations, fake_share_server, fake_share_network, 

924 fake_share_network_subnet) 

925 

926 self.assertEqual(['fd12::2000'], result) 

927 

928 instance._update_share_network_net_data.assert_called_once_with( 

929 self.context, fake_share_network_subnet) 

930 

931 parent.assert_called_once_with( 

932 self.context, allocations, fake_share_server, fake_share_network, 

933 fake_share_network_subnet) 

934 

935 def test_manage_network_allocations_admin(self): 

936 allocations = ['192.168.10.10', 'fd12::2000'] 

937 instance = self._get_neutron_network_plugin_instance(label='admin') 

938 parent = self.mock_object( 

939 plugin.NeutronNetworkPlugin, 'manage_network_allocations', 

940 mock.Mock(return_value=['fd12::2000'])) 

941 

942 share_network_dict = { 

943 'project_id': instance.neutron_api.admin_project_id, 

944 'neutron_net_id': 'fake_net_id', 

945 'neutron_subnet_id': 'fake_subnet_id', 

946 } 

947 

948 result = instance.manage_network_allocations( 

949 self.context, allocations, fake_share_server, 

950 share_network_subnet=share_network_dict) 

951 

952 self.assertEqual(['fd12::2000'], result) 

953 

954 parent.assert_called_once_with( 

955 self.context, allocations, fake_share_server, None, 

956 share_network_dict) 

957 

958 

959@ddt.ddt 

960class NeutronBindNetworkPluginTest(test.TestCase): 

961 def setUp(self): 

962 super(NeutronBindNetworkPluginTest, self).setUp() 

963 self.fake_context = context.RequestContext(user_id='fake user', 

964 project_id='fake project', 

965 is_admin=False) 

966 self.has_binding_ext_mock = self.mock_object( 

967 neutron_api.API, '_has_port_binding_extension') 

968 self.has_binding_ext_mock.return_value = True 

969 self.bind_plugin = self._get_neutron_network_plugin_instance() 

970 self.bind_plugin.db = db_api 

971 self.sleep_mock = self.mock_object(time, 'sleep') 

972 self.fake_share_network_multi = dict(fake_share_network_multi) 

973 

974 def _get_neutron_network_plugin_instance(self, config_data=None): 

975 if config_data is None: 

976 return plugin.NeutronBindNetworkPlugin() 

977 with test_utils.create_temp_config_with_opts(config_data): 

978 return plugin.NeutronBindNetworkPlugin() 

979 

980 def test_wait_for_bind(self): 

981 self.mock_object(self.bind_plugin.neutron_api, 'show_port') 

982 self.bind_plugin.neutron_api.show_port.return_value = fake_neutron_port 

983 

984 self.bind_plugin._wait_for_ports_bind([fake_neutron_port], 

985 fake_share_server) 

986 

987 self.bind_plugin.neutron_api.show_port.assert_called_once_with( 

988 fake_neutron_port['id']) 

989 self.sleep_mock.assert_not_called() 

990 

991 def test_wait_for_bind_error(self): 

992 fake_neut_port = copy.copy(fake_neutron_port) 

993 fake_neut_port['status'] = 'ERROR' 

994 self.mock_object(self.bind_plugin.neutron_api, 'show_port') 

995 self.bind_plugin.neutron_api.show_port.return_value = fake_neut_port 

996 

997 self.assertRaises(exception.NetworkException, 

998 self.bind_plugin._wait_for_ports_bind, 

999 [fake_neut_port, fake_neut_port], 

1000 fake_share_server) 

1001 

1002 self.bind_plugin.neutron_api.show_port.assert_called_once_with( 

1003 fake_neutron_port['id']) 

1004 self.sleep_mock.assert_not_called() 

1005 

1006 @ddt.data(('DOWN', 'ACTIVE'), ('DOWN', 'DOWN'), ('ACTIVE', 'DOWN')) 

1007 def test_wait_for_bind_two_ports_no_bind(self, state): 

1008 fake_neut_port1 = copy.copy(fake_neutron_port) 

1009 fake_neut_port1['status'] = state[0] 

1010 fake_neut_port2 = copy.copy(fake_neutron_port) 

1011 fake_neut_port2['status'] = state[1] 

1012 self.mock_object(self.bind_plugin.neutron_api, 'show_port') 

1013 self.bind_plugin.neutron_api.show_port.side_effect = ( 

1014 [fake_neut_port1, fake_neut_port2] * 20) 

1015 

1016 self.assertRaises(exception.NetworkBindException, 

1017 self.bind_plugin._wait_for_ports_bind, 

1018 [fake_neut_port1, fake_neut_port2], 

1019 fake_share_server) 

1020 

1021 @mock.patch.object(db_api, 'share_network_get', 

1022 mock.Mock(return_value=fake_share_network)) 

1023 @mock.patch.object(db_api, 'share_server_get', 

1024 mock.Mock(return_value=fake_share_server)) 

1025 def test_allocate_network_one_allocation(self): 

1026 self.mock_object(self.bind_plugin, '_has_provider_network_extension') 

1027 self.bind_plugin._has_provider_network_extension.return_value = True 

1028 save_nw_data = self.mock_object(self.bind_plugin, 

1029 '_save_neutron_network_data', 

1030 mock.Mock(return_value=False)) 

1031 save_subnet_data = self.mock_object(self.bind_plugin, 

1032 '_save_neutron_subnet_data') 

1033 self.mock_object(self.bind_plugin, '_wait_for_ports_bind') 

1034 neutron_host_id_opts = plugin.neutron_bind_network_plugin_opts[1] 

1035 self.mock_object(neutron_host_id_opts, 'default') 

1036 neutron_host_id_opts.default = 'foohost1' 

1037 self.mock_object(db_api, 'network_allocation_create') 

1038 db_api.network_allocation_create.return_value = fake_network_allocation 

1039 self.mock_object(self.bind_plugin.neutron_api, 'get_network') 

1040 self.bind_plugin.neutron_api.get_network.return_value = ( 

1041 fake_neutron_network) 

1042 

1043 with mock.patch.object(self.bind_plugin.neutron_api, 'create_port', 

1044 mock.Mock(return_value=fake_neutron_port)): 

1045 self.bind_plugin.allocate_network( 

1046 self.fake_context, 

1047 fake_share_server, 

1048 fake_share_network, 

1049 fake_share_network_subnet, 

1050 allocation_info={'count': 1}) 

1051 

1052 self.bind_plugin._has_provider_network_extension.assert_any_call() 

1053 save_nw_data.assert_called_once_with(self.fake_context, 

1054 fake_share_network_subnet, 

1055 save_db=True) 

1056 save_subnet_data.assert_called_once_with(self.fake_context, 

1057 fake_share_network_subnet, 

1058 save_db=True) 

1059 expected_kwargs = { 

1060 'binding:vnic_type': 'baremetal', 

1061 'host_id': 'foohost1', 

1062 'network_id': fake_share_network_subnet['neutron_net_id'], 

1063 'subnet_id': fake_share_network_subnet['neutron_subnet_id'], 

1064 'device_owner': 'manila:share', 

1065 'device_id': fake_share_network['id'], 

1066 'name': fake_share_network['id'] + '_0', 

1067 'admin_state_up': True, 

1068 } 

1069 self.bind_plugin.neutron_api.create_port.assert_called_once_with( 

1070 fake_share_network['project_id'], **expected_kwargs) 

1071 db_api.network_allocation_create.assert_called_once_with( 

1072 self.fake_context, 

1073 fake_network_allocation) 

1074 self.bind_plugin._wait_for_ports_bind.assert_called_once_with( 

1075 [db_api.network_allocation_create( 

1076 self.fake_context, fake_network_allocation)], 

1077 fake_share_server) 

1078 

1079 @mock.patch.object(db_api, 'network_allocation_create', 

1080 mock.Mock(return_values=fake_network_allocation_multi)) 

1081 @mock.patch.object(db_api, 'share_network_get', 

1082 mock.Mock(return_value=fake_share_network_multi)) 

1083 @mock.patch.object(db_api, 'share_server_get', 

1084 mock.Mock(return_value=fake_share_server)) 

1085 def test_allocate_network_multi_segment(self): 

1086 network_allocation_update_data = { 

1087 'network_type': 

1088 fake_nw_info['segments'][0]['provider:network_type'], 

1089 'segmentation_id': 

1090 fake_nw_info['segments'][0]['provider:segmentation_id'], 

1091 } 

1092 network_update_data = dict(network_allocation_update_data) 

1093 network_update_data['mtu'] = fake_nw_info['mtu'] 

1094 fake_network_allocation_multi_updated = dict( 

1095 fake_network_allocation_multi) 

1096 fake_network_allocation_multi_updated.update( 

1097 network_allocation_update_data) 

1098 fake_share_network_multi_updated = dict(fake_share_network_multi) 

1099 fake_share_network_multi_updated.update(network_update_data) 

1100 fake_share_network_multi_updated.update(fake_neutron_subnet) 

1101 config_data = { 

1102 'DEFAULT': { 

1103 'neutron_net_id': 'fake net id', 

1104 'neutron_subnet_id': 'fake subnet id', 

1105 'neutron_physical_net_name': 'net1', 

1106 } 

1107 } 

1108 self.bind_plugin = self._get_neutron_network_plugin_instance( 

1109 config_data) 

1110 self.bind_plugin.db = db_api 

1111 

1112 self.mock_object(self.bind_plugin, '_has_provider_network_extension') 

1113 self.bind_plugin._has_provider_network_extension.return_value = True 

1114 self.mock_object(self.bind_plugin, '_wait_for_ports_bind') 

1115 neutron_host_id_opts = plugin.neutron_bind_network_plugin_opts[1] 

1116 self.mock_object(neutron_host_id_opts, 'default') 

1117 neutron_host_id_opts.default = 'foohost1' 

1118 

1119 self.mock_object(db_api, 'network_allocation_create') 

1120 db_api.network_allocation_create.return_value = ( 

1121 fake_network_allocation_multi) 

1122 self.mock_object(db_api, 'network_allocation_update') 

1123 db_api.network_allocation_update.return_value = ( 

1124 fake_network_allocation_multi_updated) 

1125 self.mock_object(self.bind_plugin.neutron_api, 'get_network') 

1126 self.bind_plugin.neutron_api.get_network.return_value = ( 

1127 fake_neutron_network_multi) 

1128 self.mock_object(self.bind_plugin.neutron_api, 'get_subnet') 

1129 self.bind_plugin.neutron_api.get_subnet.return_value = ( 

1130 fake_neutron_subnet) 

1131 self.mock_object(db_api, 'share_network_subnet_update') 

1132 

1133 with mock.patch.object(self.bind_plugin.neutron_api, 'create_port', 

1134 mock.Mock(return_value=fake_neutron_port)): 

1135 self.bind_plugin.allocate_network( 

1136 self.fake_context, 

1137 fake_share_server, 

1138 fake_share_network, 

1139 self.fake_share_network_multi, 

1140 allocation_info={'count': 1}) 

1141 

1142 self.bind_plugin._has_provider_network_extension.assert_any_call() 

1143 expected_kwargs = { 

1144 'binding:vnic_type': 'baremetal', 

1145 'host_id': 'foohost1', 

1146 'network_id': fake_share_network_multi['neutron_net_id'], 

1147 'subnet_id': fake_share_network_multi['neutron_subnet_id'], 

1148 'device_owner': 'manila:share', 

1149 'device_id': fake_share_network_multi['id'], 

1150 'name': fake_share_network['id'] + '_0', 

1151 'admin_state_up': True, 

1152 } 

1153 self.bind_plugin.neutron_api.create_port.assert_called_once_with( 

1154 fake_share_network_multi['project_id'], **expected_kwargs) 

1155 db_api.network_allocation_create.assert_called_once_with( 

1156 self.fake_context, 

1157 fake_network_allocation_multi) 

1158 db_api.share_network_subnet_update.assert_called_with( 

1159 self.fake_context, 

1160 fake_share_network_multi['id'], 

1161 network_update_data) 

1162 network_allocation_update_data['cidr'] = ( 

1163 fake_neutron_subnet['cidr']) 

1164 network_allocation_update_data['ip_version'] = ( 

1165 fake_neutron_subnet['ip_version']) 

1166 db_api.network_allocation_update.assert_called_once_with( 

1167 self.fake_context, 

1168 fake_neutron_port['id'], 

1169 network_allocation_update_data) 

1170 

1171 def test_extend_network_allocations(self): 

1172 old_network_allocation = copy.deepcopy(fake_network_allocation) 

1173 fake_network = copy.deepcopy(fake_neutron_network_multi) 

1174 fake_ss = copy.deepcopy(fake_share_server) 

1175 fake_ss["share_network_subnet"] = fake_share_network_subnet 

1176 

1177 fake_host_id = "fake_host_id" 

1178 fake_physical_net = "net2" 

1179 fake_port_id = old_network_allocation["id"] 

1180 fake_vnic_type = "baremetal" 

1181 config_data = { 

1182 'DEFAULT': { 

1183 "neutron_host_id": fake_host_id, 

1184 "neutron_vnic_type": fake_vnic_type, 

1185 'neutron_physical_net_name': fake_physical_net, 

1186 } 

1187 } 

1188 self.bind_plugin = self._get_neutron_network_plugin_instance( 

1189 config_data 

1190 ) 

1191 self.mock_object( 

1192 self.bind_plugin.neutron_api, 

1193 "get_network", 

1194 mock.Mock(return_value=fake_network), 

1195 ) 

1196 self.mock_object(self.bind_plugin.neutron_api, "bind_port_to_host") 

1197 self.mock_object( 

1198 self.bind_plugin.db, 

1199 "network_allocations_get_for_share_server", 

1200 mock.Mock(return_value=[old_network_allocation])) 

1201 

1202 # calling the extend_network_allocations method 

1203 self.bind_plugin.extend_network_allocations(self.fake_context, fake_ss) 

1204 

1205 # testing the calls, we expect the port to be bound to the current host 

1206 # and the new network allocation to be created 

1207 self.bind_plugin.neutron_api.bind_port_to_host.assert_called_once_with( 

1208 fake_port_id, fake_host_id, fake_vnic_type 

1209 ) 

1210 

1211 def test_delete_extended_allocations(self): 

1212 old_network_allocation = copy.deepcopy(fake_network_allocation) 

1213 fake_ss = copy.deepcopy(fake_share_server) 

1214 fake_host_id = "fake_host_id" 

1215 fake_physical_net = "net2" 

1216 fake_port_id = old_network_allocation["id"] 

1217 fake_vnic_type = "baremetal" 

1218 config_data = { 

1219 "DEFAULT": { 

1220 "neutron_host_id": fake_host_id, 

1221 "neutron_vnic_type": fake_vnic_type, 

1222 "neutron_physical_net_name": fake_physical_net, 

1223 } 

1224 } 

1225 

1226 self.bind_plugin = self._get_neutron_network_plugin_instance( 

1227 config_data) 

1228 self.mock_object(self.bind_plugin.neutron_api, "delete_port_binding") 

1229 self.mock_object(self.bind_plugin.db, "network_allocation_delete") 

1230 self.mock_object(self.bind_plugin.db, 

1231 "network_allocations_get_for_share_server", 

1232 mock.Mock(return_value=[old_network_allocation])) 

1233 

1234 self.bind_plugin.delete_extended_allocations(self.fake_context, 

1235 fake_ss) 

1236 neutron_api = self.bind_plugin.neutron_api 

1237 neutron_api.delete_port_binding.assert_called_once_with( 

1238 fake_port_id, fake_host_id) 

1239 

1240 @ddt.unpack 

1241 def test_cutover_network_allocation(self): 

1242 fake_alloc = copy.deepcopy(fake_network_allocation) 

1243 fake_network = copy.deepcopy(fake_neutron_network_multi) 

1244 fake_old_ss = copy.deepcopy(fake_share_server) 

1245 fake_old_ss["share_network_subnet"] = fake_share_network_subnet 

1246 fake_dest_ss = copy.deepcopy(fake_share_server) 

1247 fake_dest_ss["host"] = "fake_host2@backend2#pool2" 

1248 fake_old_host = share_utils.extract_host(fake_old_ss["host"], "host") 

1249 

1250 fake_host_id = "fake_host_id" 

1251 fake_physical_net = "net2" 

1252 fake_port_id = fake_alloc["id"] 

1253 fake_vnic_type = "baremetal" 

1254 config_data = { 

1255 "DEFAULT": { 

1256 "neutron_host_id": fake_host_id, 

1257 "neutron_vnic_type": fake_vnic_type, 

1258 "neutron_physical_net_name": fake_physical_net, 

1259 } 

1260 } 

1261 self.bind_plugin = self._get_neutron_network_plugin_instance( 

1262 config_data) 

1263 self.mock_object(self.bind_plugin.neutron_api, "get_network", 

1264 mock.Mock(return_value=fake_network)) 

1265 self.mock_object(self.bind_plugin.neutron_api, "bind_port_to_host") 

1266 self.mock_object(self.bind_plugin.db, "network_allocation_create") 

1267 

1268 self.mock_object(self.bind_plugin.db, 

1269 "network_allocations_get_for_share_server", 

1270 mock.Mock(return_value=[fake_alloc])) 

1271 

1272 neutron_api = self.bind_plugin.neutron_api 

1273 db_api = self.bind_plugin.db 

1274 self.mock_object(neutron_api, "activate_port_binding") 

1275 self.mock_object(neutron_api, "delete_port_binding") 

1276 self.mock_object(db_api, "network_allocation_update") 

1277 self.mock_object(db_api, "network_allocation_delete") 

1278 self.mock_object(db_api, "share_network_subnet_update") 

1279 

1280 self.bind_plugin.cutover_network_allocations( 

1281 self.fake_context, fake_old_ss) 

1282 neutron_api.activate_port_binding.assert_called_once_with( 

1283 fake_port_id, fake_host_id) 

1284 neutron_api.delete_port_binding.assert_called_once_with( 

1285 fake_port_id, fake_old_host) 

1286 

1287 @ddt.data({ 

1288 'neutron_binding_profiles': None, 

1289 'binding_profiles': {} 

1290 }, { 

1291 'neutron_binding_profiles': 'fake_profile', 

1292 'binding_profiles': {} 

1293 }, { 

1294 'neutron_binding_profiles': 'fake_profile', 

1295 'binding_profiles': None 

1296 }, { 

1297 'neutron_binding_profiles': 'fake_profile', 

1298 'binding_profiles': { 

1299 'fake_profile': { 

1300 'neutron_switch_id': 'fake switch id', 

1301 'neutron_port_id': 'fake port id', 

1302 'neutron_switch_info': 'switch_ip: 127.0.0.1' 

1303 } 

1304 } 

1305 }, { 

1306 'neutron_binding_profiles': None, 

1307 'binding_profiles': { 

1308 'fake_profile': { 

1309 'neutron_switch_id': 'fake switch id', 

1310 'neutron_port_id': 'fake port id', 

1311 'neutron_switch_info': 'switch_ip: 127.0.0.1' 

1312 } 

1313 } 

1314 }, { 

1315 'neutron_binding_profiles': 'fake_profile_one,fake_profile_two', 

1316 'binding_profiles': { 

1317 'fake_profile_one': { 

1318 'neutron_switch_id': 'fake switch id 1', 

1319 'neutron_port_id': 'fake port id 1', 

1320 'neutron_switch_info': 'switch_ip: 127.0.0.1' 

1321 }, 

1322 'fake_profile_two': { 

1323 'neutron_switch_id': 'fake switch id 2', 

1324 'neutron_port_id': 'fake port id 2', 

1325 'neutron_switch_info': 'switch_ip: 127.0.0.2' 

1326 } 

1327 } 

1328 }, { 

1329 'neutron_binding_profiles': 'fake_profile_two', 

1330 'binding_profiles': { 

1331 'fake_profile_one': { 

1332 'neutron_switch_id': 'fake switch id 1', 

1333 'neutron_port_id': 'fake port id 1', 

1334 'neutron_switch_info': 'switch_ip: 127.0.0.1' 

1335 }, 

1336 'fake_profile_two': { 

1337 'neutron_switch_id': 'fake switch id 2', 

1338 'neutron_port_id': 'fake port id 2', 

1339 'neutron_switch_info': 'switch_ip: 127.0.0.2' 

1340 } 

1341 } 

1342 }) 

1343 @ddt.unpack 

1344 @mock.patch.object(db_api, 'share_network_get', 

1345 mock.Mock(return_value=fake_share_network)) 

1346 @mock.patch.object(db_api, 'share_server_get', 

1347 mock.Mock(return_value=fake_share_server)) 

1348 def test__get_port_create_args(self, neutron_binding_profiles, 

1349 binding_profiles): 

1350 fake_device_owner = 'share' 

1351 fake_host_id = 'fake host' 

1352 neutron_host_id_opts = plugin.neutron_bind_network_plugin_opts[1] 

1353 self.mock_object(neutron_host_id_opts, 'default') 

1354 neutron_host_id_opts.default = fake_host_id 

1355 

1356 config_data = { 

1357 'DEFAULT': { 

1358 'neutron_net_id': fake_neutron_network['id'], 

1359 'neutron_subnet_id': fake_neutron_network['subnets'][0] 

1360 } 

1361 } 

1362 # Simulate absence of set values 

1363 if neutron_binding_profiles: 

1364 config_data['DEFAULT'][ 

1365 'neutron_binding_profiles'] = neutron_binding_profiles 

1366 if binding_profiles: 

1367 for name, binding_profile in binding_profiles.items(): 

1368 config_data[name] = binding_profile 

1369 

1370 instance = self._get_neutron_network_plugin_instance(config_data) 

1371 

1372 create_args = instance._get_port_create_args(fake_share_server, 

1373 fake_share_network_subnet, 

1374 fake_device_owner) 

1375 

1376 expected_create_args = { 

1377 'binding:vnic_type': 'baremetal', 

1378 'host_id': fake_host_id, 

1379 'network_id': fake_share_network_subnet['neutron_net_id'], 

1380 'subnet_id': fake_share_network_subnet['neutron_subnet_id'], 

1381 'device_owner': 'manila:' + fake_device_owner, 

1382 'device_id': fake_share_server['id'], 

1383 'name': fake_share_server['id'] + '_0', 

1384 'admin_state_up': True, 

1385 } 

1386 if neutron_binding_profiles: 

1387 expected_create_args['binding:profile'] = { 

1388 'local_link_information': [] 

1389 } 

1390 local_links = expected_create_args[ 

1391 'binding:profile']['local_link_information'] 

1392 for profile in neutron_binding_profiles.split(','): 

1393 if binding_profiles is None: 

1394 binding_profile = {} 

1395 else: 

1396 binding_profile = binding_profiles.get(profile, {}) 

1397 local_links.append({ 

1398 'port_id': binding_profile.get('neutron_port_id', None), 

1399 'switch_id': binding_profile.get('neutron_switch_id', None) 

1400 }) 

1401 switch_info = binding_profile.get('neutron_switch_info', None) 

1402 if switch_info is None: 

1403 local_links[-1]['switch_info'] = None 

1404 else: 

1405 local_links[-1]['switch_info'] = cfg.types.Dict()( 

1406 switch_info) 

1407 

1408 self.assertEqual(expected_create_args, create_args) 

1409 

1410 @mock.patch.object(db_api, 'share_network_get', 

1411 mock.Mock(return_value=fake_share_network)) 

1412 @mock.patch.object(db_api, 'share_server_get', 

1413 mock.Mock(return_value=fake_share_server)) 

1414 def test__get_port_create_args_host_id(self): 

1415 fake_device_owner = 'share' 

1416 fake_host_id = 'fake host' 

1417 

1418 config_data = { 

1419 'DEFAULT': { 

1420 'neutron_net_id': fake_neutron_network['id'], 

1421 'neutron_subnet_id': fake_neutron_network['subnets'][0], 

1422 'neutron_host_id': fake_host_id 

1423 } 

1424 } 

1425 

1426 instance = self._get_neutron_network_plugin_instance(config_data) 

1427 

1428 create_args = instance._get_port_create_args(fake_share_server, 

1429 fake_share_network_subnet, 

1430 fake_device_owner) 

1431 

1432 expected_create_args = { 

1433 'binding:vnic_type': 'baremetal', 

1434 'host_id': fake_host_id, 

1435 'network_id': fake_share_network_subnet['neutron_net_id'], 

1436 'subnet_id': fake_share_network_subnet['neutron_subnet_id'], 

1437 'device_owner': 'manila:' + fake_device_owner, 

1438 'device_id': fake_share_server['id'], 

1439 'name': fake_share_server['id'] + '_0', 

1440 'admin_state_up': True, 

1441 } 

1442 

1443 self.assertEqual(expected_create_args, create_args) 

1444 

1445 

1446@ddt.ddt 

1447class NeutronBindSingleNetworkPluginTest(test.TestCase): 

1448 def setUp(self): 

1449 super(NeutronBindSingleNetworkPluginTest, self).setUp() 

1450 self.context = 'fake_context' 

1451 self.fake_context = context.RequestContext(user_id='fake user', 

1452 project_id='fake project', 

1453 is_admin=False) 

1454 self.has_binding_ext_mock = self.mock_object( 

1455 neutron_api.API, '_has_port_binding_extension') 

1456 self.has_binding_ext_mock.return_value = True 

1457 self.bind_plugin = plugin.NeutronBindNetworkPlugin() 

1458 self.bind_plugin.db = db_api 

1459 self.sleep_mock = self.mock_object(time, 'sleep') 

1460 self.bind_plugin = self._get_neutron_network_plugin_instance() 

1461 self.bind_plugin.db = db_api 

1462 

1463 def _get_neutron_network_plugin_instance(self, config_data=None): 

1464 if not config_data: 

1465 fake_net_id = 'fake net id' 

1466 fake_subnet_id = 'fake subnet id' 

1467 config_data = { 

1468 'DEFAULT': { 

1469 'neutron_net_id': fake_net_id, 

1470 'neutron_subnet_id': fake_subnet_id, 

1471 'neutron_physical_net_name': 'net1', 

1472 } 

1473 } 

1474 fake_net = {'subnets': ['fake1', 'fake2', fake_subnet_id]} 

1475 self.mock_object( 

1476 neutron_api.API, 'get_network', 

1477 mock.Mock(return_value=fake_net)) 

1478 with test_utils.create_temp_config_with_opts(config_data): 

1479 return plugin.NeutronBindSingleNetworkPlugin() 

1480 

1481 def test_allocate_network(self): 

1482 self.mock_object(plugin.NeutronNetworkPlugin, 'allocate_network') 

1483 plugin.NeutronNetworkPlugin.allocate_network.return_value = [ 

1484 'port1', 'port2'] 

1485 instance = self._get_neutron_network_plugin_instance() 

1486 share_server = 'fake_share_server' 

1487 share_network = {} 

1488 share_network_subnet = {'neutron_net_id': {}} 

1489 share_network_upd = {'neutron_net_id': {'upd': True}} 

1490 count = 2 

1491 device_owner = 'fake_device_owner' 

1492 self.mock_object( 

1493 instance, '_update_share_network_net_data', 

1494 mock.Mock(return_value=share_network_upd)) 

1495 self.mock_object(instance, '_wait_for_ports_bind', mock.Mock()) 

1496 

1497 instance.allocate_network( 

1498 self.context, share_server, share_network, share_network_subnet, 

1499 count=count, device_owner=device_owner) 

1500 

1501 instance._update_share_network_net_data.assert_called_once_with( 

1502 self.context, share_network_subnet) 

1503 plugin.NeutronNetworkPlugin.allocate_network.assert_called_once_with( 

1504 self.context, share_server, share_network, share_network_upd, 

1505 count=count, device_owner=device_owner) 

1506 instance._wait_for_ports_bind.assert_called_once_with( 

1507 ['port1', 'port2'], share_server) 

1508 

1509 def test_init_valid(self): 

1510 fake_net_id = 'fake_net_id' 

1511 fake_subnet_id = 'fake_subnet_id' 

1512 config_data = { 

1513 'DEFAULT': { 

1514 'neutron_net_id': fake_net_id, 

1515 'neutron_subnet_id': fake_subnet_id, 

1516 } 

1517 } 

1518 fake_net = {'subnets': ['fake1', 'fake2', fake_subnet_id]} 

1519 self.mock_object( 

1520 neutron_api.API, 'get_network', mock.Mock(return_value=fake_net)) 

1521 

1522 with test_utils.create_temp_config_with_opts(config_data): 

1523 instance = plugin.NeutronSingleNetworkPlugin() 

1524 

1525 self.assertEqual(fake_net_id, instance.net) 

1526 self.assertEqual(fake_subnet_id, instance.subnet) 

1527 neutron_api.API.get_network.assert_called_once_with(fake_net_id) 

1528 

1529 @ddt.data( 

1530 {'net': None, 'subnet': None}, 

1531 {'net': 'fake_net_id', 'subnet': None}, 

1532 {'net': None, 'subnet': 'fake_subnet_id'}) 

1533 @ddt.unpack 

1534 def test_init_invalid(self, net, subnet): 

1535 config_data = dict() 

1536 # Simulate absence of set values 

1537 if net: 

1538 config_data['neutron_net_id'] = net 

1539 if subnet: 

1540 config_data['neutron_subnet_id'] = subnet 

1541 config_data = dict(DEFAULT=config_data) 

1542 

1543 with test_utils.create_temp_config_with_opts(config_data): 

1544 self.assertRaises( 

1545 exception.NetworkBadConfigurationException, 

1546 plugin.NeutronSingleNetworkPlugin) 

1547 

1548 @ddt.data({}, {'subnets': []}, {'subnets': ['different_foo_subnet']}) 

1549 def test_init_subnet_does_not_belong_to_net(self, fake_net): 

1550 fake_net_id = 'fake_net_id' 

1551 config_data = { 

1552 'DEFAULT': { 

1553 'neutron_net_id': fake_net_id, 

1554 'neutron_subnet_id': 'fake_subnet_id', 

1555 } 

1556 } 

1557 self.mock_object( 

1558 neutron_api.API, 'get_network', mock.Mock(return_value=fake_net)) 

1559 

1560 with test_utils.create_temp_config_with_opts(config_data): 

1561 self.assertRaises( 

1562 exception.NetworkBadConfigurationException, 

1563 plugin.NeutronSingleNetworkPlugin) 

1564 neutron_api.API.get_network.assert_called_once_with(fake_net_id) 

1565 

1566 def _get_neutron_single_network_plugin_instance(self): 

1567 fake_subnet_id = 'fake_subnet_id' 

1568 config_data = { 

1569 'DEFAULT': { 

1570 'neutron_net_id': 'fake_net_id', 

1571 'neutron_subnet_id': fake_subnet_id, 

1572 } 

1573 } 

1574 fake_net = {'subnets': [fake_subnet_id]} 

1575 self.mock_object( 

1576 neutron_api.API, 'get_network', mock.Mock(return_value=fake_net)) 

1577 with test_utils.create_temp_config_with_opts(config_data): 

1578 instance = plugin.NeutronSingleNetworkPlugin() 

1579 return instance 

1580 

1581 def test___update_share_network_net_data_same_values(self): 

1582 instance = self._get_neutron_single_network_plugin_instance() 

1583 share_network = { 

1584 'neutron_net_id': instance.net, 

1585 'neutron_subnet_id': instance.subnet, 

1586 } 

1587 

1588 result = instance._update_share_network_net_data( 

1589 self.context, share_network) 

1590 

1591 self.assertEqual(share_network, result) 

1592 

1593 def test___update_share_network_net_data_different_values_empty(self): 

1594 instance = self._get_neutron_single_network_plugin_instance() 

1595 share_network_subnet_input = { 

1596 'id': 'fake_share_network_id', 

1597 } 

1598 share_network_result = { 

1599 'neutron_net_id': instance.net, 

1600 'neutron_subnet_id': instance.subnet, 

1601 } 

1602 self.mock_object( 

1603 instance.db, 'share_network_subnet_update', 

1604 mock.Mock(return_value='foo')) 

1605 

1606 instance._update_share_network_net_data( 

1607 self.context, share_network_subnet_input) 

1608 

1609 instance.db.share_network_subnet_update.assert_called_once_with( 

1610 self.context, share_network_subnet_input['id'], 

1611 share_network_result) 

1612 

1613 @ddt.data( 

1614 {'n': 'fake_net_id', 's': 'bar'}, 

1615 {'n': 'foo', 's': 'fake_subnet_id'}) 

1616 @ddt.unpack 

1617 def test___update_share_network_net_data_different_values(self, n, s): 

1618 instance = self._get_neutron_single_network_plugin_instance() 

1619 share_network = { 

1620 'id': 'fake_share_network_id', 

1621 'neutron_net_id': n, 

1622 'neutron_subnet_id': s, 

1623 } 

1624 self.mock_object( 

1625 instance.db, 'share_network_update', 

1626 mock.Mock(return_value=share_network)) 

1627 

1628 self.assertRaises( 

1629 exception.NetworkBadConfigurationException, 

1630 instance._update_share_network_net_data, 

1631 self.context, share_network) 

1632 self.assertFalse(instance.db.share_network_update.called) 

1633 

1634 def test_wait_for_bind(self): 

1635 self.mock_object(self.bind_plugin.neutron_api, 'show_port') 

1636 self.bind_plugin.neutron_api.show_port.return_value = fake_neutron_port 

1637 

1638 self.bind_plugin._wait_for_ports_bind([fake_neutron_port], 

1639 fake_share_server) 

1640 

1641 self.bind_plugin.neutron_api.show_port.assert_called_once_with( 

1642 fake_neutron_port['id']) 

1643 self.sleep_mock.assert_not_called() 

1644 

1645 def test_wait_for_bind_error(self): 

1646 fake_neut_port = copy.copy(fake_neutron_port) 

1647 fake_neut_port['status'] = 'ERROR' 

1648 self.mock_object(self.bind_plugin.neutron_api, 'show_port') 

1649 self.bind_plugin.neutron_api.show_port.return_value = fake_neut_port 

1650 

1651 self.assertRaises(exception.NetworkException, 

1652 self.bind_plugin._wait_for_ports_bind, 

1653 [fake_neut_port, fake_neut_port], 

1654 fake_share_server) 

1655 

1656 self.bind_plugin.neutron_api.show_port.assert_called_once_with( 

1657 fake_neutron_port['id']) 

1658 self.sleep_mock.assert_not_called() 

1659 

1660 @ddt.data(('DOWN', 'ACTIVE'), ('DOWN', 'DOWN'), ('ACTIVE', 'DOWN')) 

1661 def test_wait_for_bind_two_ports_no_bind(self, state): 

1662 fake_neut_port1 = copy.copy(fake_neutron_port) 

1663 fake_neut_port1['status'] = state[0] 

1664 fake_neut_port2 = copy.copy(fake_neutron_port) 

1665 fake_neut_port2['status'] = state[1] 

1666 self.mock_object(self.bind_plugin.neutron_api, 'show_port') 

1667 self.bind_plugin.neutron_api.show_port.side_effect = ( 

1668 [fake_neut_port1, fake_neut_port2] * 20) 

1669 

1670 self.assertRaises(exception.NetworkBindException, 

1671 self.bind_plugin._wait_for_ports_bind, 

1672 [fake_neut_port1, fake_neut_port2], 

1673 fake_share_server) 

1674 

1675 @mock.patch.object(db_api, 'network_allocation_create', 

1676 mock.Mock(return_values=fake_network_allocation)) 

1677 @mock.patch.object(db_api, 'share_network_get', 

1678 mock.Mock(return_value=fake_share_network)) 

1679 @mock.patch.object(db_api, 'share_server_get', 

1680 mock.Mock(return_value=fake_share_server)) 

1681 def test_allocate_network_one_allocation(self): 

1682 self.mock_object(self.bind_plugin, '_has_provider_network_extension') 

1683 self.bind_plugin._has_provider_network_extension.return_value = True 

1684 save_nw_data = self.mock_object(self.bind_plugin, 

1685 '_save_neutron_network_data', 

1686 mock.Mock(return_value=False)) 

1687 save_subnet_data = self.mock_object(self.bind_plugin, 

1688 '_save_neutron_subnet_data') 

1689 self.mock_object(self.bind_plugin, '_wait_for_ports_bind') 

1690 neutron_host_id_opts = plugin.neutron_bind_network_plugin_opts[1] 

1691 self.mock_object(neutron_host_id_opts, 'default') 

1692 neutron_host_id_opts.default = 'foohost1' 

1693 self.mock_object(db_api, 'network_allocation_create') 

1694 

1695 with mock.patch.object(self.bind_plugin.neutron_api, 'create_port', 

1696 mock.Mock(return_value=fake_neutron_port)): 

1697 self.bind_plugin.allocate_network( 

1698 self.fake_context, 

1699 fake_share_server, 

1700 fake_share_network, 

1701 fake_share_network_subnet, 

1702 allocation_info={'count': 1}) 

1703 

1704 self.bind_plugin._has_provider_network_extension.assert_any_call() 

1705 save_nw_data.assert_called_once_with(self.fake_context, 

1706 fake_share_network_subnet, 

1707 save_db=True) 

1708 save_subnet_data.assert_called_once_with(self.fake_context, 

1709 fake_share_network_subnet, 

1710 save_db=True) 

1711 expected_kwargs = { 

1712 'binding:vnic_type': 'baremetal', 

1713 'host_id': 'foohost1', 

1714 'network_id': fake_share_network_subnet['neutron_net_id'], 

1715 'subnet_id': fake_share_network_subnet['neutron_subnet_id'], 

1716 'device_owner': 'manila:share', 

1717 'device_id': fake_share_network['id'], 

1718 'name': fake_share_network['id'] + '_0', 

1719 'admin_state_up': True, 

1720 } 

1721 self.bind_plugin.neutron_api.create_port.assert_called_once_with( 

1722 fake_share_network['project_id'], **expected_kwargs) 

1723 db_api.network_allocation_create.assert_called_once_with( 

1724 self.fake_context, 

1725 fake_network_allocation) 

1726 self.bind_plugin._wait_for_ports_bind.assert_called_once_with( 

1727 [db_api.network_allocation_create( 

1728 self.fake_context, fake_network_allocation)], 

1729 fake_share_server) 

1730 

1731 @ddt.data({ 

1732 'neutron_binding_profiles': None, 

1733 'binding_profiles': {} 

1734 }, { 

1735 'neutron_binding_profiles': 'fake_profile', 

1736 'binding_profiles': {} 

1737 }, { 

1738 'neutron_binding_profiles': 'fake_profile', 

1739 'binding_profiles': None 

1740 }, { 

1741 'neutron_binding_profiles': 'fake_profile', 

1742 'binding_profiles': { 

1743 'fake_profile': { 

1744 'neutron_switch_id': 'fake switch id', 

1745 'neutron_port_id': 'fake port id', 

1746 'neutron_switch_info': 'switch_ip: 127.0.0.1' 

1747 } 

1748 } 

1749 }, { 

1750 'neutron_binding_profiles': None, 

1751 'binding_profiles': { 

1752 'fake_profile': { 

1753 'neutron_switch_id': 'fake switch id', 

1754 'neutron_port_id': 'fake port id', 

1755 'neutron_switch_info': 'switch_ip: 127.0.0.1' 

1756 } 

1757 } 

1758 }, { 

1759 'neutron_binding_profiles': 'fake_profile_one,fake_profile_two', 

1760 'binding_profiles': { 

1761 'fake_profile_one': { 

1762 'neutron_switch_id': 'fake switch id 1', 

1763 'neutron_port_id': 'fake port id 1', 

1764 'neutron_switch_info': 'switch_ip: 127.0.0.1' 

1765 }, 

1766 'fake_profile_two': { 

1767 'neutron_switch_id': 'fake switch id 2', 

1768 'neutron_port_id': 'fake port id 2', 

1769 'neutron_switch_info': 'switch_ip: 127.0.0.2' 

1770 } 

1771 } 

1772 }, { 

1773 'neutron_binding_profiles': 'fake_profile_two', 

1774 'binding_profiles': { 

1775 'fake_profile_one': { 

1776 'neutron_switch_id': 'fake switch id 1', 

1777 'neutron_port_id': 'fake port id 1', 

1778 'neutron_switch_info': 'switch_ip: 127.0.0.1' 

1779 }, 

1780 'fake_profile_two': { 

1781 'neutron_switch_id': 'fake switch id 2', 

1782 'neutron_port_id': 'fake port id 2', 

1783 'neutron_switch_info': 'switch_ip: 127.0.0.2' 

1784 } 

1785 } 

1786 }) 

1787 @ddt.unpack 

1788 @mock.patch.object(db_api, 'share_network_get', 

1789 mock.Mock(return_value=fake_share_network)) 

1790 @mock.patch.object(db_api, 'share_server_get', 

1791 mock.Mock(return_value=fake_share_server)) 

1792 def test__get_port_create_args(self, neutron_binding_profiles, 

1793 binding_profiles): 

1794 fake_device_owner = 'share' 

1795 fake_host_id = 'fake host' 

1796 neutron_host_id_opts = plugin.neutron_bind_network_plugin_opts[1] 

1797 self.mock_object(neutron_host_id_opts, 'default') 

1798 neutron_host_id_opts.default = fake_host_id 

1799 

1800 config_data = { 

1801 'DEFAULT': { 

1802 'neutron_net_id': fake_neutron_network['id'], 

1803 'neutron_subnet_id': fake_neutron_network['subnets'][0] 

1804 } 

1805 } 

1806 # Simulate absence of set values 

1807 if neutron_binding_profiles: 

1808 config_data['DEFAULT'][ 

1809 'neutron_binding_profiles'] = neutron_binding_profiles 

1810 if binding_profiles: 

1811 for name, binding_profile in binding_profiles.items(): 

1812 config_data[name] = binding_profile 

1813 

1814 instance = self._get_neutron_network_plugin_instance(config_data) 

1815 

1816 create_args = instance._get_port_create_args(fake_share_server, 

1817 fake_share_network_subnet, 

1818 fake_device_owner) 

1819 

1820 expected_create_args = { 

1821 'binding:vnic_type': 'baremetal', 

1822 'host_id': fake_host_id, 

1823 'network_id': fake_share_network_subnet['neutron_net_id'], 

1824 'subnet_id': fake_share_network_subnet['neutron_subnet_id'], 

1825 'device_owner': 'manila:' + fake_device_owner, 

1826 'device_id': fake_share_server['id'], 

1827 'name': fake_share_server['id'] + '_0', 

1828 'admin_state_up': True, 

1829 } 

1830 if neutron_binding_profiles: 

1831 expected_create_args['binding:profile'] = { 

1832 'local_link_information': [] 

1833 } 

1834 local_links = expected_create_args[ 

1835 'binding:profile']['local_link_information'] 

1836 for profile in neutron_binding_profiles.split(','): 

1837 if binding_profiles is None: 

1838 binding_profile = {} 

1839 else: 

1840 binding_profile = binding_profiles.get(profile, {}) 

1841 local_links.append({ 

1842 'port_id': binding_profile.get('neutron_port_id', None), 

1843 'switch_id': binding_profile.get('neutron_switch_id', None) 

1844 }) 

1845 switch_info = binding_profile.get('neutron_switch_info', None) 

1846 if switch_info is None: 

1847 local_links[-1]['switch_info'] = None 

1848 else: 

1849 local_links[-1]['switch_info'] = cfg.types.Dict()( 

1850 switch_info) 

1851 

1852 self.assertEqual(expected_create_args, create_args) 

1853 

1854 @mock.patch.object(db_api, 'share_network_get', 

1855 mock.Mock(return_value=fake_share_network)) 

1856 @mock.patch.object(db_api, 'share_server_get', 

1857 mock.Mock(return_value=fake_share_server)) 

1858 def test__get_port_create_args_host_id(self): 

1859 fake_device_owner = 'share' 

1860 fake_host_id = 'fake host' 

1861 

1862 config_data = { 

1863 'DEFAULT': { 

1864 'neutron_net_id': fake_neutron_network['id'], 

1865 'neutron_subnet_id': fake_neutron_network['subnets'][0], 

1866 'neutron_host_id': fake_host_id 

1867 } 

1868 } 

1869 

1870 instance = self._get_neutron_network_plugin_instance(config_data) 

1871 

1872 create_args = instance._get_port_create_args(fake_share_server, 

1873 fake_share_network_subnet, 

1874 fake_device_owner) 

1875 

1876 expected_create_args = { 

1877 'binding:vnic_type': 'baremetal', 

1878 'host_id': fake_host_id, 

1879 'network_id': fake_share_network_subnet['neutron_net_id'], 

1880 'subnet_id': fake_share_network_subnet['neutron_subnet_id'], 

1881 'device_owner': 'manila:' + fake_device_owner, 

1882 'device_id': fake_share_server['id'], 

1883 'name': fake_share_server['id'] + '_0', 

1884 'admin_state_up': True, 

1885 } 

1886 

1887 self.assertEqual(expected_create_args, create_args) 

1888 

1889 

1890class NeutronBindNetworkPluginWithNormalTypeTest(test.TestCase): 

1891 def setUp(self): 

1892 super(NeutronBindNetworkPluginWithNormalTypeTest, self).setUp() 

1893 config_data = { 

1894 'DEFAULT': { 

1895 'neutron_vnic_type': 'normal', 

1896 } 

1897 } 

1898 self.plugin = plugin.NeutronNetworkPlugin() 

1899 self.plugin.db = db_api 

1900 self.fake_context = context.RequestContext(user_id='fake user', 

1901 project_id='fake project', 

1902 is_admin=False) 

1903 

1904 with test_utils.create_temp_config_with_opts(config_data): 

1905 self.bind_plugin = plugin.NeutronBindNetworkPlugin() 

1906 self.bind_plugin.db = db_api 

1907 

1908 @mock.patch.object(db_api, 'network_allocation_create', 

1909 mock.Mock(return_values=fake_network_allocation)) 

1910 @mock.patch.object(db_api, 'share_network_get', 

1911 mock.Mock(return_value=fake_share_network)) 

1912 @mock.patch.object(db_api, 'share_server_get', 

1913 mock.Mock(return_value=fake_share_server)) 

1914 def test_allocate_network_one_allocation(self): 

1915 self.mock_object(self.bind_plugin, '_has_provider_network_extension') 

1916 self.bind_plugin._has_provider_network_extension.return_value = True 

1917 save_nw_data = self.mock_object(self.bind_plugin, 

1918 '_save_neutron_network_data', 

1919 mock.Mock(return_value=False)) 

1920 save_subnet_data = self.mock_object(self.bind_plugin, 

1921 '_save_neutron_subnet_data') 

1922 self.mock_object(self.bind_plugin, '_wait_for_ports_bind') 

1923 neutron_host_id_opts = plugin.neutron_bind_network_plugin_opts[1] 

1924 self.mock_object(neutron_host_id_opts, 'default') 

1925 neutron_host_id_opts.default = 'foohost1' 

1926 self.mock_object(db_api, 'network_allocation_create') 

1927 multi_seg = self.mock_object( 

1928 self.bind_plugin, '_is_neutron_multi_segment') 

1929 multi_seg.return_value = False 

1930 

1931 with mock.patch.object(self.bind_plugin.neutron_api, 'create_port', 

1932 mock.Mock(return_value=fake_neutron_port)): 

1933 self.bind_plugin.allocate_network( 

1934 self.fake_context, 

1935 fake_share_server, 

1936 fake_share_network, 

1937 fake_share_network_subnet, 

1938 allocation_info={'count': 1}) 

1939 

1940 self.bind_plugin._has_provider_network_extension.assert_any_call() 

1941 save_nw_data.assert_called_once_with(self.fake_context, 

1942 fake_share_network_subnet, 

1943 save_db=True) 

1944 save_subnet_data.assert_called_once_with(self.fake_context, 

1945 fake_share_network_subnet, 

1946 save_db=True) 

1947 expected_kwargs = { 

1948 'binding:vnic_type': 'normal', 

1949 'host_id': 'foohost1', 

1950 'network_id': fake_share_network_subnet['neutron_net_id'], 

1951 'subnet_id': fake_share_network_subnet['neutron_subnet_id'], 

1952 'device_owner': 'manila:share', 

1953 'device_id': fake_share_server['id'], 

1954 'name': fake_share_server['id'] + '_0', 

1955 'admin_state_up': True, 

1956 } 

1957 self.bind_plugin.neutron_api.create_port.assert_called_once_with( 

1958 fake_share_network['project_id'], **expected_kwargs) 

1959 db_api.network_allocation_create.assert_called_once_with( 

1960 self.fake_context, 

1961 fake_network_allocation) 

1962 self.bind_plugin._wait_for_ports_bind.assert_not_called() 

1963 

1964 def test_update_network_allocation(self): 

1965 self.mock_object(self.bind_plugin, '_wait_for_ports_bind') 

1966 self.mock_object(db_api, 'network_allocations_get_for_share_server') 

1967 db_api.network_allocations_get_for_share_server.return_value = [ 

1968 fake_neutron_port] 

1969 

1970 self.bind_plugin.update_network_allocation(self.fake_context, 

1971 fake_share_server) 

1972 

1973 self.bind_plugin._wait_for_ports_bind.assert_called_once_with( 

1974 [fake_neutron_port], fake_share_server) 

1975 

1976 

1977@ddt.ddt 

1978class NeutronBindSingleNetworkPluginWithNormalTypeTest(test.TestCase): 

1979 def setUp(self): 

1980 super(NeutronBindSingleNetworkPluginWithNormalTypeTest, self).setUp() 

1981 fake_net_id = 'fake net id' 

1982 fake_subnet_id = 'fake subnet id' 

1983 config_data = { 

1984 'DEFAULT': { 

1985 'neutron_net_id': fake_net_id, 

1986 'neutron_subnet_id': fake_subnet_id, 

1987 'neutron_vnic_type': 'normal', 

1988 } 

1989 } 

1990 fake_net = {'subnets': ['fake1', 'fake2', fake_subnet_id]} 

1991 self.mock_object( 

1992 neutron_api.API, 'get_network', mock.Mock(return_value=fake_net)) 

1993 self.plugin = plugin.NeutronNetworkPlugin() 

1994 self.plugin.db = db_api 

1995 self.fake_context = context.RequestContext(user_id='fake user', 

1996 project_id='fake project', 

1997 is_admin=False) 

1998 

1999 with test_utils.create_temp_config_with_opts(config_data): 

2000 self.bind_plugin = plugin.NeutronBindSingleNetworkPlugin() 

2001 self.bind_plugin.db = db_api 

2002 

2003 @mock.patch.object(db_api, 'network_allocation_create', 

2004 mock.Mock(return_values=fake_network_allocation)) 

2005 @mock.patch.object(db_api, 'share_network_get', 

2006 mock.Mock(return_value=fake_share_network)) 

2007 @mock.patch.object(db_api, 'share_server_get', 

2008 mock.Mock(return_value=fake_share_server)) 

2009 def test_allocate_network_one_allocation(self): 

2010 self.mock_object(self.bind_plugin, '_has_provider_network_extension') 

2011 self.bind_plugin._has_provider_network_extension.return_value = True 

2012 save_nw_data = self.mock_object(self.bind_plugin, 

2013 '_save_neutron_network_data', 

2014 mock.Mock(return_value=False)) 

2015 save_subnet_data = self.mock_object(self.bind_plugin, 

2016 '_save_neutron_subnet_data') 

2017 self.mock_object(self.bind_plugin, '_wait_for_ports_bind') 

2018 neutron_host_id_opts = plugin.neutron_bind_network_plugin_opts[1] 

2019 self.mock_object(neutron_host_id_opts, 'default') 

2020 neutron_host_id_opts.default = 'foohost1' 

2021 self.mock_object(db_api, 'network_allocation_create') 

2022 

2023 with mock.patch.object(self.bind_plugin.neutron_api, 'create_port', 

2024 mock.Mock(return_value=fake_neutron_port)): 

2025 self.bind_plugin.allocate_network( 

2026 self.fake_context, 

2027 fake_share_server, 

2028 fake_share_network, 

2029 fake_share_network_subnet, 

2030 allocation_info={'count': 1}) 

2031 

2032 self.bind_plugin._has_provider_network_extension.assert_any_call() 

2033 save_nw_data.assert_called_once_with(self.fake_context, 

2034 fake_share_network_subnet, 

2035 save_db=True) 

2036 save_subnet_data.assert_called_once_with(self.fake_context, 

2037 fake_share_network_subnet, 

2038 save_db=True) 

2039 expected_kwargs = { 

2040 'binding:vnic_type': 'normal', 

2041 'host_id': 'foohost1', 

2042 'network_id': fake_share_network_subnet['neutron_net_id'], 

2043 'subnet_id': fake_share_network_subnet['neutron_subnet_id'], 

2044 'device_owner': 'manila:share', 

2045 'device_id': fake_share_network['id'], 

2046 'name': fake_share_network['id'] + '_0', 

2047 'admin_state_up': True, 

2048 } 

2049 self.bind_plugin.neutron_api.create_port.assert_called_once_with( 

2050 fake_share_network['project_id'], **expected_kwargs) 

2051 db_api.network_allocation_create.assert_called_once_with( 

2052 self.fake_context, 

2053 fake_network_allocation) 

2054 self.bind_plugin._wait_for_ports_bind.assert_not_called() 

2055 

2056 def test_update_network_allocation(self): 

2057 self.mock_object(self.bind_plugin, '_wait_for_ports_bind') 

2058 self.mock_object(db_api, 'network_allocations_get_for_share_server') 

2059 db_api.network_allocations_get_for_share_server.return_value = [ 

2060 fake_neutron_port] 

2061 

2062 self.bind_plugin.update_network_allocation(self.fake_context, 

2063 fake_share_server) 

2064 

2065 self.bind_plugin._wait_for_ports_bind.assert_called_once_with( 

2066 [fake_neutron_port], fake_share_server) 

2067 

2068 @ddt.data({'fix_ips': [{'ip_address': 'test_ip'}, 

2069 {'ip_address': '10.78.223.129'}], 

2070 'ip_version': 4}, 

2071 {'fix_ips': [{'ip_address': 'test_ip'}, 

2072 {'ip_address': 'ad80::abaa:0:c2:2'}], 

2073 'ip_version': 6}, 

2074 {'fix_ips': [{'ip_address': '10.78.223.129'}, 

2075 {'ip_address': 'ad80::abaa:0:c2:2'}], 

2076 'ip_version': 6}, 

2077 ) 

2078 @ddt.unpack 

2079 def test__get_matched_ip_address(self, fix_ips, ip_version): 

2080 result = self.bind_plugin._get_matched_ip_address(fix_ips, ip_version) 

2081 self.assertEqual(fix_ips[1]['ip_address'], result) 

2082 

2083 @ddt.data({'fix_ips': [{'ip_address': 'test_ip_1'}, 

2084 {'ip_address': 'test_ip_2'}], 

2085 'ip_version': (4, 6)}, 

2086 {'fix_ips': [{'ip_address': 'ad80::abaa:0:c2:1'}, 

2087 {'ip_address': 'ad80::abaa:0:c2:2'}], 

2088 'ip_version': (4, )}, 

2089 {'fix_ips': [{'ip_address': '192.0.0.2'}, 

2090 {'ip_address': '192.0.0.3'}], 

2091 'ip_version': (6, )}, 

2092 {'fix_ips': [{'ip_address': '192.0.0.2/12'}, 

2093 {'ip_address': '192.0.0.330'}, 

2094 {'ip_address': 'ad80::001::ad80'}, 

2095 {'ip_address': 'ad80::abaa:0:c2:2/64'}], 

2096 'ip_version': (4, 6)}, 

2097 ) 

2098 @ddt.unpack 

2099 def test__get_matched_ip_address_illegal(self, fix_ips, ip_version): 

2100 for version in ip_version: 

2101 self.assertRaises(exception.NetworkBadConfigurationException, 

2102 self.bind_plugin._get_matched_ip_address, 

2103 fix_ips, version) 

2104 

2105 def _setup_include_network_info(self): 

2106 data = { 

2107 'DEFAULT': { 

2108 'neutron_net_id': 'fake net id', 

2109 'neutron_subnet_id': 'fake subnet id', 

2110 'neutron_physical_net_name': 'net1', 

2111 } 

2112 } 

2113 with test_utils.create_temp_config_with_opts(data): 

2114 instance = plugin.NeutronNetworkPlugin() 

2115 

2116 return instance 

2117 

2118 def test_include_network_info(self): 

2119 instance = self._setup_include_network_info() 

2120 self.mock_object(instance, '_store_and_get_neutron_net_info') 

2121 instance.include_network_info(fake_share_network) 

2122 instance._store_and_get_neutron_net_info.assert_called_once_with( 

2123 None, fake_share_network, save_db=False)