Mock call to sysctl for test_get_free_namespace_port

The code under test shouldn't execute local commands for multiple
reasons (eg the command doesn't exist on the test system), so mock out
the execution of sysctl in test_get_free_namespace_port (copying the
code from test_get_unused_port).

Change-Id: I58aa578274310ae2ca015069f99edb84716fb0b5
Closes-bug: #1619028
This commit is contained in:
Corey Wright 2016-08-31 14:34:13 -05:00
parent 92bcaed7aa
commit 5e4204804e

View File

@ -57,13 +57,16 @@ class PortAllocationTestCase(base.DietTestCase):
ss_output2 += ss_output_template % p
with mock.patch('neutron.agent.linux.ip_lib.IPWrapper') \
as ipwrapper:
as ipwrapper, \
mock.patch('neutron.agent.linux.utils.execute') as ex:
m = mock.MagicMock()
m.netns.execute.return_value = ss_output2
ipwrapper.return_value = m
local_port_range_start = 32768
ex.return_value = "%s\t61000" % local_port_range_start
result = net_helpers.get_free_namespace_port(
n_const.PROTO_NAME_TCP)
self.assertEqual(32767, result)
self.assertEqual((local_port_range_start - 1), result)
def test_get_unused_port(self):
with mock.patch('neutron.agent.linux.utils.execute') as ex: