Integration library between network (Neutron) and compute (Nova) providers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
7.9KB

  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. import mock
  13. import os.path
  14. import testtools
  15. import fixtures
  16. from os_vif.internal.command import ip as ip_lib
  17. from oslo_concurrency import lockutils
  18. from oslo_concurrency import processutils
  19. from oslo_config import cfg
  20. from oslo_config import fixture as config_fixture
  21. from oslo_log.fixture import logging_error as log_fixture
  22. from vif_plug_linux_bridge import linux_net
  23. from vif_plug_linux_bridge import privsep
  24. CONF = cfg.CONF
  25. class LinuxNetTest(testtools.TestCase):
  26. def setUp(self):
  27. super(LinuxNetTest, self).setUp()
  28. privsep.vif_plug.set_client_mode(False)
  29. lock_path = self.useFixture(fixtures.TempDir()).path
  30. self.fixture = self.useFixture(
  31. config_fixture.Config(lockutils.CONF))
  32. self.fixture.config(lock_path=lock_path,
  33. group='oslo_concurrency')
  34. self.useFixture(log_fixture.get_logging_handle_error_fixture())
  35. @mock.patch.object(ip_lib, "set")
  36. def test_set_device_mtu(self, mock_ip_set):
  37. linux_net._set_device_mtu(dev='fakedev', mtu=1500)
  38. mock_ip_set.assert_called_once_with('fakedev', mtu=1500,
  39. check_exit_code=[0, 2, 254])
  40. @mock.patch.object(processutils, "execute")
  41. def test_set_device_invalid_mtu(self, mock_exec):
  42. linux_net._set_device_mtu(dev='fakedev', mtu=None)
  43. mock_exec.assert_not_called()
  44. @mock.patch.object(ip_lib, "add")
  45. @mock.patch.object(ip_lib, "set")
  46. @mock.patch.object(linux_net, "device_exists", return_value=False)
  47. @mock.patch.object(linux_net, "_set_device_mtu")
  48. def test_ensure_vlan(self, mock_set_mtu, mock_dev_exists, mock_ip_set,
  49. mock_ip_add):
  50. linux_net._ensure_vlan_privileged(123, 'fake-bridge',
  51. mac_address='fake-mac',
  52. mtu=1500)
  53. self.assertTrue(mock_dev_exists.called)
  54. set_calls = [mock.call('vlan123', address='fake-mac',
  55. check_exit_code=[0, 2, 254]),
  56. mock.call('vlan123', state='up',
  57. check_exit_code=[0, 2, 254])]
  58. mock_ip_add.assert_called_once_with(
  59. 'vlan123', 'vlan', link='fake-bridge', vlan_id=123,
  60. check_exit_code=[0, 2, 254])
  61. mock_ip_set.assert_has_calls(set_calls)
  62. mock_set_mtu.assert_called_once_with('vlan123', 1500)
  63. @mock.patch.object(processutils, "execute")
  64. @mock.patch.object(linux_net, "device_exists", return_value=True)
  65. def test_ensure_bridge_exists(self, mock_dev_exists, mock_exec):
  66. linux_net.ensure_bridge("br0", None, filtering=False)
  67. mock_exec.assert_not_called()
  68. mock_dev_exists.assert_called_once_with("br0")
  69. @mock.patch.object(processutils, "execute")
  70. @mock.patch.object(linux_net, "device_exists", return_value=False)
  71. def test_ensure_bridge_addbr_exception(self, mock_dev_exists, mock_exec):
  72. mock_exec.side_effect = ValueError()
  73. with testtools.ExpectedException(ValueError):
  74. linux_net.ensure_bridge("br0", None, filtering=False)
  75. @mock.patch.object(ip_lib, "set")
  76. @mock.patch.object(processutils, "execute")
  77. @mock.patch.object(linux_net, "device_exists", side_effect=[False, True])
  78. def test_ensure_bridge_concurrent_add(self, mock_dev_exists, mock_exec,
  79. mock_ip_set):
  80. mock_exec.side_effect = [ValueError(), 0, 0, 0]
  81. linux_net.ensure_bridge("br0", None, filtering=False)
  82. calls = [mock.call('brctl', 'addbr', 'br0'),
  83. mock.call('brctl', 'setfd', 'br0', 0),
  84. mock.call('brctl', 'stp', 'br0', "off")]
  85. mock_exec.assert_has_calls(calls)
  86. mock_dev_exists.assert_has_calls([mock.call("br0"), mock.call("br0")])
  87. mock_ip_set.assert_called_once_with('br0', state='up')
  88. @mock.patch.object(ip_lib, "set")
  89. @mock.patch.object(linux_net, "_set_device_mtu")
  90. @mock.patch.object(os.path, "exists", return_value=False)
  91. @mock.patch.object(processutils, "execute")
  92. @mock.patch.object(linux_net, "device_exists", return_value=False)
  93. def test_ensure_bridge_mtu_not_called(self, mock_dev_exists, mock_exec,
  94. mock_path_exists, mock_set_mtu, mock_ip_set):
  95. """This test validates that mtus are updated only if an interface
  96. is added to the bridge
  97. """
  98. linux_net._ensure_bridge_privileged("fake-bridge", None,
  99. None, False, mtu=1500)
  100. mock_set_mtu.assert_not_called()
  101. mock_ip_set.assert_called_once_with('fake-bridge', state='up')
  102. @mock.patch.object(ip_lib, "set")
  103. @mock.patch.object(linux_net, "_set_device_mtu")
  104. @mock.patch.object(os.path, "exists", return_value=False)
  105. @mock.patch.object(processutils, "execute", return_value=("", ""))
  106. @mock.patch.object(linux_net, "device_exists", return_value=False)
  107. def test_ensure_bridge_mtu_order(self, mock_dev_exists, mock_exec,
  108. mock_path_exists, mock_set_mtu, mock_ip_set):
  109. """This test validates that when adding an interface
  110. to a bridge, the interface mtu is updated first
  111. followed by the bridge. This is required to work around
  112. https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1399064
  113. """
  114. linux_net._ensure_bridge_privileged("fake-bridge", "fake-interface",
  115. None, False, mtu=1500)
  116. calls = [mock.call('fake-interface', 1500),
  117. mock.call('fake-bridge', 1500)]
  118. mock_set_mtu.assert_has_calls(calls)
  119. calls = [mock.call('fake-bridge', state = 'up'),
  120. mock.call('fake-interface', state='up')]
  121. mock_ip_set.assert_has_calls(calls)
  122. @mock.patch.object(linux_net, "_arp_filtering")
  123. @mock.patch.object(ip_lib, "set")
  124. @mock.patch.object(os.path, "exists", return_value=False)
  125. @mock.patch.object(processutils, "execute")
  126. @mock.patch.object(linux_net, "device_exists", return_value=False)
  127. def test_ensure_bridge_new_ipv4(self, mock_dev_exists, mock_exec,
  128. mock_path_exists, mock_ip_set, *args):
  129. linux_net.ensure_bridge("br0", None, filtering=False)
  130. calls = [mock.call('brctl', 'addbr', 'br0'),
  131. mock.call('brctl', 'setfd', 'br0', 0),
  132. mock.call('brctl', 'stp', 'br0', "off")]
  133. mock_exec.assert_has_calls(calls)
  134. mock_dev_exists.assert_called_once_with("br0")
  135. mock_ip_set.assert_called_once_with('br0', state='up')
  136. @mock.patch.object(linux_net, "_arp_filtering")
  137. @mock.patch.object(ip_lib, "set")
  138. @mock.patch.object(os.path, "exists", return_value=True)
  139. @mock.patch.object(processutils, "execute")
  140. @mock.patch.object(linux_net, "device_exists", return_value=False)
  141. def test_ensure_bridge_new_ipv6(self, mock_dev_exists, mock_exec,
  142. mock_path_exists, mock_ip_set, *args):
  143. linux_net.ensure_bridge("br0", None, filtering=False)
  144. calls = [mock.call('brctl', 'addbr', 'br0'),
  145. mock.call('brctl', 'setfd', 'br0', 0),
  146. mock.call('brctl', 'stp', 'br0', "off"),
  147. mock.call('tee', '/proc/sys/net/ipv6/conf/br0/disable_ipv6',
  148. check_exit_code=[0, 1], process_input='1')]
  149. mock_exec.assert_has_calls(calls)
  150. mock_dev_exists.assert_called_once_with("br0")
  151. mock_ip_set.assert_called_once_with('br0', state='up')