Debian Codegraph @

vif_plug_linux_bridge/tests/unit/test_linux_net.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 import fixtures
16 import testtools
17
18 from oslo_concurrency import lockutils
19 from oslo_concurrency import processutils
20 from oslo_config import cfg
21 from oslo_config import fixture as config_fixture
22 from oslo_log.fixture import logging_error as log_fixture
23
24 from os_vif.internal.ip.api import ip as ip_lib
25
26 from vif_plug_linux_bridge import linux_net
27 from vif_plug_linux_bridge import privsep
28
29 CONF = cfg.CONF
30
31
32 class LinuxNetTest(testtools.TestCase):
33
34 def setUp(self):
35 super(LinuxNetTest, self).setUp()
36
37 privsep.vif_plug.set_client_mode(False)
38 lock_path = self.useFixture(fixtures.TempDir()).path
39 self.fixture = self.useFixture(
40 config_fixture.Config(lockutils.CONF))
41 self.fixture.config(lock_path=lock_path,
42 group='oslo_concurrency')
43 self.useFixture(log_fixture.get_logging_handle_error_fixture())
44
45 @mock.patch.object(ip_lib, "set")
46 def test_set_device_mtu(self, mock_ip_set):
47 linux_net._set_device_mtu(dev='fakedev', mtu=1500)
48 mock_ip_set.assert_called_once_with('fakedev', mtu=1500,
49 check_exit_code=[0, 2, 254])
50
51 @mock.patch.object(processutils, "execute")
52 def test_set_device_invalid_mtu(self, mock_exec):
53 linux_net._set_device_mtu(dev='fakedev', mtu=None)
54 mock_exec.assert_not_called()
55
56 @mock.patch.object(ip_lib, "add")
57 @mock.patch.object(ip_lib, "set")
58 @mock.patch.object(ip_lib, "exists", return_value=False)
59 @mock.patch.object(linux_net, "_set_device_mtu")
60 def test_ensure_vlan(self, mock_set_mtu, mock_dev_exists, mock_ip_set,
61 mock_ip_add):
62 linux_net._ensure_vlan_privileged(123, 'fake-bridge',
63 mac_address='fake-mac',
64 mtu=1500)
65 self.assertTrue(mock_dev_exists.called)
66 set_calls = [mock.call('vlan123', address='fake-mac',
67 check_exit_code=[0, 2, 254]),
68 mock.call('vlan123', state='up',
69 check_exit_code=[0, 2, 254])]
70 mock_ip_add.assert_called_once_with(
71 'vlan123', 'vlan', link='fake-bridge', vlan_id=123,
72 check_exit_code=[0, 2, 254])
73 mock_ip_set.assert_has_calls(set_calls)
74 mock_set_mtu.assert_called_once_with('vlan123', 1500)
75
76 @mock.patch.object(linux_net, "_ensure_bridge_privileged")
77 @mock.patch.object(linux_net, "_ensure_bridge_filtering")
78 def test_ensure_bridge(self, mock_filtering, mock_priv):
79 linux_net.ensure_bridge("br0", None, filtering=False)
80
81 mock_priv.assert_called_once_with("br0", None, None, True,
82 filtering=False, mtu=None)
83 mock_filtering.assert_not_called()
84
85 linux_net.ensure_bridge("br0", None, filtering=True)
86 mock_filtering.assert_called_once_with("br0", True)
87
88 @mock.patch.object(ip_lib, "exists", return_value=False)
89 @mock.patch.object(ip_lib, "add")
90 def test_ensure_bridge_addbr_exception(self, mock_add, mock_dev_exists):
91 mock_add.side_effect = ValueError()
92 with testtools.ExpectedException(ValueError):
93 linux_net.ensure_bridge("br0", None, filtering=False)
94
95 @mock.patch.object(ip_lib, "add")
96 @mock.patch.object(ip_lib, "set")
97 @mock.patch.object(linux_net, "_arp_filtering")
98 @mock.patch.object(linux_net, "_set_device_mtu")
99 @mock.patch.object(linux_net, "_disable_ipv6")
100 @mock.patch.object(linux_net, "_update_bridge_routes")
101 @mock.patch.object(ip_lib, "exists")
102 def test_ensure_bridge_priv_mtu_not_called(self, mock_dev_exists,
103 mock_routes, mock_disable_ipv6, mock_set_mtu, mock_arp_filtering,
104 mock_ip_set, mock_add):
105 """This test validates that mtus are updated only if an interface
106 is added to the bridge
107 """
108 mock_dev_exists.return_value = False
109 linux_net._ensure_bridge_privileged("fake-bridge", None,
110 None, False, mtu=1500)
111 mock_set_mtu.assert_not_called()
112 mock_ip_set.assert_called_once_with('fake-bridge', state='up')
113
114 @mock.patch.object(ip_lib, "add")
115 @mock.patch.object(ip_lib, "set")
116 @mock.patch.object(linux_net, "_arp_filtering")
117 @mock.patch.object(linux_net, "_set_device_mtu")
118 @mock.patch.object(linux_net, "_disable_ipv6")
119 @mock.patch.object(linux_net, "_update_bridge_routes")
120 @mock.patch.object(ip_lib, "exists")
121 def test_ensure_bridge_priv_mtu_order(self, mock_dev_exists, mock_routes,
122 mock_disable_ipv6, mock_set_mtu, mock_arp_filtering, mock_ip_set,
123 mock_add):
124 """This test validates that when adding an interface
125 to a bridge, the interface mtu is updated first
126 followed by the bridge. This is required to work around
127 https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1399064
128 """
129 mock_dev_exists.side_effect = [False, True]
130 linux_net._ensure_bridge_privileged("fake-bridge", "fake-interface",
131 None, False, mtu=1500)
132 calls = [mock.call('fake-interface', 1500),
133 mock.call('fake-bridge', 1500)]
134 mock_set_mtu.assert_has_calls(calls)
135 calls = [mock.call('fake-bridge', state='up'),
136 mock.call('fake-interface', master='fake-bridge', state='up',
137 check_exit_code=[0, 2, 254])]
138 mock_ip_set.assert_has_calls(calls)
139
140 @mock.patch('builtins.open')
141 @mock.patch("os.path.exists")
142 def test__disable_ipv6(self, mock_exists, mock_open):
143
144 exists_path = "/proc/sys/net/ipv6/conf/br0/disable_ipv6"
145 mock_exists.return_value = False
146
147 linux_net._disable_ipv6("br0")
148 mock_exists.assert_called_once_with(exists_path)
149 mock_open.assert_not_called()
150
151 mock_exists.reset_mock()
152 mock_exists.return_value = True
153 linux_net._disable_ipv6("br0")
154 mock_exists.assert_called_once_with(exists_path)
155 mock_open.assert_called_once_with(exists_path, 'w')