| 1 |
# Copyright (c) 2015 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.tests import base |
| 19 |
from neutron_vpnaas.services.vpn.common import netns_wrapper as nswrap |
| 20 |
|
| 21 |
|
| 22 |
class TestNetnsWrapper(base.BaseTestCase): |
| 23 |
|
| 24 |
def setUp(self): |
| 25 |
super().setUp() |
| 26 |
patch_methods = ['filter_command', |
| 27 |
'execute', |
| 28 |
'setup_conf'] |
| 29 |
for method in patch_methods: |
| 30 |
self.patch_obj(nswrap, method) |
| 31 |
patch_classes = ['neutron.common.config.setup_logging', |
| 32 |
'os.path.isdir', |
| 33 |
'os.path.samefile', |
| 34 |
'sys.exit'] |
| 35 |
for cls in patch_classes: |
| 36 |
self.patch_cls(cls) |
| 37 |
|
| 38 |
self.filter_command.return_value = False |
| 39 |
self.execute.return_value = 0 |
| 40 |
self.conf = mock.Mock() |
| 41 |
self.conf.cmd = 'ls,-al' |
| 42 |
self.conf.mount_paths = {'/foo': '/dir/foo', |
| 43 |
'/var': '/dir/var'} |
| 44 |
self.setup_conf.return_value = self.conf |
| 45 |
self.conf.rootwrap_config = 'conf' |
| 46 |
self.isdir.return_value = True |
| 47 |
self.samefile.return_value = False |
| 48 |
|
| 49 |
def patch_obj(self, obj, method): |
| 50 |
_m = mock.patch.object(obj, method) |
| 51 |
_mock = _m.start() |
| 52 |
setattr(self, method, _mock) |
| 53 |
|
| 54 |
def patch_cls(self, patch_class): |
| 55 |
_m = mock.patch(patch_class) |
| 56 |
mock_name = patch_class.split('.')[-1] |
| 57 |
_mock = _m.start() |
| 58 |
setattr(self, mock_name, _mock) |
| 59 |
|
| 60 |
def test_netns_wrap_fail_without_netns(self): |
| 61 |
self.samefile.return_value = True |
| 62 |
return_val = nswrap.execute_with_mount() |
| 63 |
self.assertTrue(return_val) |
| 64 |
|
| 65 |
def test_netns_wrap(self): |
| 66 |
self.conf.cmd = 'ls,-al' |
| 67 |
return_val = nswrap.execute_with_mount() |
| 68 |
exp_calls = [mock.call(['mount', '--bind', '/dir/foo', '/foo']), |
| 69 |
mock.call(['mount', '--bind', '/dir/var', '/var']), |
| 70 |
mock.call('ls,-al')] |
| 71 |
self.execute.assert_has_calls(exp_calls, any_order=True) |
| 72 |
self.assertFalse(return_val) |
| 73 |
|
| 74 |
def test_netns_wrap_fail_without_cmd(self): |
| 75 |
self.conf.cmd = None |
| 76 |
return_val = nswrap.execute_with_mount() |
| 77 |
self.assertFalse(self.execute.called) |
| 78 |
self.assertTrue(return_val) |
| 79 |
|
| 80 |
def test_netns_wrap_fail_without_mount_paths(self): |
| 81 |
self.conf.mount_paths = None |
| 82 |
return_val = nswrap.execute_with_mount() |
| 83 |
self.assertFalse(self.execute.called) |
| 84 |
self.assertTrue(return_val) |