Debian Codegraph @

neutron_vpnaas/tests/functional/strongswan/test_netns_wrapper.py

raw
1 # Copyright (c) 2015 Canonical, 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.
15
16 import re
17
18 from neutron.agent.linux import utils
19 from neutron.conf.agent import common as config
20 from neutron.tests.common import net_helpers
21 from neutron.tests.functional import base
22
23 WRAPPER_SCRIPT = 'neutron-vpn-netns-wrapper'
24 STATUS_PATTERN = re.compile('Command:.*ip.*addr.*show.*Exit code: 0')
25
26
27 class TestNetnsWrapper(base.BaseSudoTestCase):
28
29 def setUp(self):
30 super().setUp()
31 config.setup_logging()
32 self.fake_ns = 'func-8f1b728c-6eca-4042-9b6b-6ef66ab9352a'
33 self.mount_paths = ('--mount_paths=/etc:/var/lib/neutron'
34 '/vpnaas/%(ns)s/etc,/var/run:/var/lib'
35 '/neutron/vpnaas/%(ns)s/var/run')
36 self.fake_pth = self.mount_paths % {'ns': self.fake_ns}
37
38 def test_netns_wrap_success(self):
39 client_ns = self.useFixture(net_helpers.NamespaceFixture()).ip_wrapper
40 ns = client_ns.namespace
41 pth = self.mount_paths % {'ns': ns}
42 cmd = WRAPPER_SCRIPT, pth, '--cmd=ip,addr,show'
43 output = client_ns.netns.execute(cmd)
44 self.assertTrue(STATUS_PATTERN.search(output))
45
46 def test_netns_wrap_fail_without_netns(self):
47 cmd = [WRAPPER_SCRIPT, self.fake_pth,
48 '--cmd=ip,addr,show']
49 self.assertRaises(RuntimeError, utils.execute, cmd=cmd,
50 run_as_root=True)
51
52 def test_netns_wrap_unauthorized_command(self):
53 cmd = [WRAPPER_SCRIPT, self.fake_pth,
54 '--cmd=nofiltercommand']
55 self.assertRaises(RuntimeError, utils.execute, cmd=cmd,
56 run_as_root=True)