Debian Codegraph @

neutron_vpnaas/tests/unit/services/vpn/test_vpn_service.py

raw
1 # Copyright 2014 OpenStack Foundation.
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.
15
16 from unittest import mock
17
18 from neutron_lib.callbacks import registry
19 from neutron_lib.exceptions import vpn as vpn_exception
20 from oslo_config import cfg
21 from oslo_utils import uuidutils
22
23 from neutron_vpnaas.services.vpn import agent as vpn_agent
24 from neutron_vpnaas.services.vpn import device_drivers
25 from neutron_vpnaas.services.vpn import vpn_service
26 from neutron_vpnaas.tests import base
27
28 _uuid = uuidutils.generate_uuid
29
30 VPNAAS_NOP_DEVICE = ('neutron_vpnaas.tests.unit.services.'
31 'vpn.test_vpn_service.NoopDeviceDriver')
32 VPNAAS_DEFAULT_DEVICE = ('neutron_vpnaas.services.vpn.'
33 'device_drivers.ipsec.OpenSwanDriver')
34 FAKE_ROUTER_ID = _uuid()
35
36
37 class NoopDeviceDriver(device_drivers.DeviceDriver):
38
39 def sync(self, context, processes):
40 pass
41
42 def create_router(self, router_info):
43 pass
44
45 def destroy_router(self, process_id):
46 pass
47
48
49 class VPNBaseTestCase(base.BaseTestCase):
50
51 def setUp(self):
52 super().setUp()
53 self.conf = cfg.CONF
54 self.ri_kwargs = {'router': {'id': FAKE_ROUTER_ID, 'ha': False},
55 'agent_conf': self.conf,
56 'interface_driver': mock.sentinel.interface_driver}
57
58
59 class TestVirtualPrivateNetworkDeviceDriverLoading(VPNBaseTestCase):
60
61 def setUp(self):
62 self.skipTest('This test is skipped after the eventlet removal and '
63 'needs to be refactored')
64 super().setUp()
65 cfg.CONF.register_opts(vpn_agent.vpn_agent_opts, 'vpnagent')
66 self.agent = mock.Mock()
67 self.agent.conf = cfg.CONF
68 mock.patch.object(registry, 'subscribe').start()
69 self.service = vpn_service.VPNService(self.agent)
70
71 def test_loading_vpn_device_drivers(self):
72 """Get two device drivers (in a list) for VPNaaS."""
73 cfg.CONF.set_override('vpn_device_driver',
74 [VPNAAS_NOP_DEVICE, VPNAAS_NOP_DEVICE],
75 'vpnagent')
76
77 drivers = self.service.load_device_drivers('host')
78 self.assertEqual(2, len(drivers))
79 self.assertIn(drivers[0].__class__.__name__, VPNAAS_NOP_DEVICE)
80 self.assertIn(drivers[1].__class__.__name__, VPNAAS_NOP_DEVICE)
81
82 def test_use_default_for_vpn_device_driver(self):
83 """When no VPNaaS device drivers specified, we get the default."""
84 drivers = self.service.load_device_drivers('host')
85 self.assertEqual(1, len(drivers))
86 self.assertIn(drivers[0].__class__.__name__, VPNAAS_DEFAULT_DEVICE)
87
88 def test_fail_no_such_vpn_device_driver(self):
89 """Failure test of import error for VPNaaS device driver."""
90 cfg.CONF.set_override('vpn_device_driver',
91 ['no.such.class'],
92 'vpnagent')
93 self.assertRaises(vpn_exception.DeviceDriverImportError,
94 self.service.load_device_drivers, 'host')