Skip to content

Commit c743f66

Browse files
mikalstillstephenfin
authored andcommitted
Remove no longer required "inner" methods.
Previously we were testing by using methods without decorators, which were labelled as "inner" methods. We don't need to do that now that we can fixture away the decorator. Change-Id: I9b21e8a0f9a0ed9e70d1fc07e60a6beff110c19c
1 parent 707deb1 commit c743f66

File tree

5 files changed

+77
-72
lines changed

5 files changed

+77
-72
lines changed

nova/privsep/linux_net.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,12 @@ def delete_net_dev(dev):
6666

6767
@nova.privsep.sys_admin_pctxt.entrypoint
6868
def delete_net_dev_escalated(dev):
69-
_delete_net_dev_inner(dev)
70-
71-
72-
def _delete_net_dev_inner(dev):
7369
processutils.execute('ip', 'link', 'delete', dev,
7470
check_exit_code=[0, 2, 254])
7571

7672

7773
@nova.privsep.sys_admin_pctxt.entrypoint
7874
def set_device_mtu(dev, mtu):
79-
_set_device_mtu_inner(dev, mtu)
80-
81-
82-
def _set_device_mtu_inner(dev, mtu):
8375
if mtu:
8476
processutils.execute('ip', 'link', 'set', dev, 'mtu',
8577
mtu, check_exit_code=[0, 2, 254])
@@ -200,11 +192,6 @@ def route_delete_deprecated(dev, routes):
200192

201193
@nova.privsep.sys_admin_pctxt.entrypoint
202194
def create_tap_dev(dev, mac_address=None, multiqueue=False):
203-
_create_tap_dev_inner(dev, mac_address=mac_address,
204-
multiqueue=multiqueue)
205-
206-
207-
def _create_tap_dev_inner(dev, mac_address=None, multiqueue=False):
208195
if not device_exists(dev):
209196
try:
210197
# First, try with 'ip'

nova/privsep/path.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,24 @@ def exists(path):
9494

9595
@nova.privsep.sys_admin_pctxt.entrypoint
9696
def last_bytes(path, num):
97-
# NOTE(mikal): this is implemented in this contrived manner because you
98-
# can't mock a decorator in python (they're loaded at file parse time,
99-
# and the mock happens later).
100-
with open(path, 'rb') as f:
101-
return _last_bytes_inner(f, num)
102-
103-
104-
def _last_bytes_inner(file_like_object, num):
10597
"""Return num bytes from the end of the file, and remaining byte count.
10698
107-
:param file_like_object: The file to read
99+
:param path: The file to read
108100
:param num: The number of bytes to return
109101
110102
:returns: (data, remaining)
111103
"""
112-
113-
try:
114-
file_like_object.seek(-num, os.SEEK_END)
115-
except IOError as e:
116-
# seek() fails with EINVAL when trying to go before the start of
117-
# the file. It means that num is larger than the file size, so
118-
# just go to the start.
119-
if e.errno == errno.EINVAL:
120-
file_like_object.seek(0, os.SEEK_SET)
121-
else:
122-
raise
123-
124-
remaining = file_like_object.tell()
125-
return (file_like_object.read(), remaining)
104+
with open(path, 'rb') as f:
105+
try:
106+
f.seek(-num, os.SEEK_END)
107+
except IOError as e:
108+
# seek() fails with EINVAL when trying to go before the start of
109+
# the file. It means that num is larger than the file size, so
110+
# just go to the start.
111+
if e.errno == errno.EINVAL:
112+
f.seek(0, os.SEEK_SET)
113+
else:
114+
raise
115+
116+
remaining = f.tell()
117+
return (f.read(), remaining)

nova/tests/unit/privsep/test_linux_net.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,34 @@
1919

2020
import nova.privsep.linux_net
2121
from nova import test
22+
from nova.tests import fixtures
2223

2324

2425
class LinuxNetTestCase(test.NoDBTestCase):
2526
"""Test networking helpers."""
2627

28+
def setUp(self):
29+
super(LinuxNetTestCase, self).setUp()
30+
self.useFixture(fixtures.PrivsepFixture())
31+
2732
@mock.patch('oslo_concurrency.processutils.execute',
2833
return_value=('', ''))
2934
def test_set_device_mtu_default(self, mock_exec):
30-
calls = []
31-
nova.privsep.linux_net._set_device_mtu_inner('fake-dev', None)
32-
mock_exec.assert_has_calls(calls)
35+
nova.privsep.linux_net.set_device_mtu('fake-dev', None)
36+
mock_exec.assert_has_calls([])
37+
38+
@mock.patch('oslo_concurrency.processutils.execute',
39+
return_value=('', ''))
40+
def test_set_device_mtu_actual(self, mock_exec):
41+
nova.privsep.linux_net.set_device_mtu('fake-dev', 1500)
42+
mock_exec.assert_has_calls([
43+
mock.call('ip', 'link', 'set', 'fake-dev', 'mtu',
44+
1500, check_exit_code=[0, 2, 254])])
3345

3446
@mock.patch('oslo_concurrency.processutils.execute')
3547
@mock.patch('nova.privsep.linux_net._set_device_enabled_inner')
3648
def test_create_tap_dev(self, mock_enabled, mock_execute):
37-
nova.privsep.linux_net._create_tap_dev_inner('tap42')
49+
nova.privsep.linux_net.create_tap_dev('tap42')
3850

3951
mock_execute.assert_has_calls([
4052
mock.call('ip', 'tuntap', 'add', 'tap42', 'mode', 'tap',
@@ -45,7 +57,7 @@ def test_create_tap_dev(self, mock_enabled, mock_execute):
4557
@mock.patch('os.path.exists', return_value=True)
4658
@mock.patch('oslo_concurrency.processutils.execute')
4759
def test_create_tap_skipped_when_exists(self, mock_execute, mock_exists):
48-
nova.privsep.linux_net._create_tap_dev_inner('tap42')
60+
nova.privsep.linux_net.create_tap_dev('tap42')
4961

5062
mock_exists.assert_called_once_with('/sys/class/net/tap42')
5163
mock_execute.assert_not_called()
@@ -55,7 +67,7 @@ def test_create_tap_skipped_when_exists(self, mock_execute, mock_exists):
5567
@mock.patch('nova.privsep.linux_net._set_device_macaddr_inner')
5668
def test_create_tap_dev_mac(self, mock_set_macaddr, mock_enabled,
5769
mock_execute):
58-
nova.privsep.linux_net._create_tap_dev_inner(
70+
nova.privsep.linux_net.create_tap_dev(
5971
'tap42', '00:11:22:33:44:55')
6072

6173
mock_execute.assert_has_calls([
@@ -73,7 +85,7 @@ def test_create_tap_dev_fallback_to_tunctl(self, mock_enabled,
7385
# ip failed, fall back to tunctl
7486
mock_execute.side_effect = [processutils.ProcessExecutionError, 0, 0]
7587

76-
nova.privsep.linux_net._create_tap_dev_inner('tap42')
88+
nova.privsep.linux_net.create_tap_dev('tap42')
7789

7890
mock_execute.assert_has_calls([
7991
mock.call('ip', 'tuntap', 'add', 'tap42', 'mode', 'tap',
@@ -85,7 +97,7 @@ def test_create_tap_dev_fallback_to_tunctl(self, mock_enabled,
8597
@mock.patch('oslo_concurrency.processutils.execute')
8698
@mock.patch('nova.privsep.linux_net._set_device_enabled_inner')
8799
def test_create_tap_dev_multiqueue(self, mock_enabled, mock_execute):
88-
nova.privsep.linux_net._create_tap_dev_inner(
100+
nova.privsep.linux_net.create_tap_dev(
89101
'tap42', multiqueue=True)
90102

91103
mock_execute.assert_has_calls([
@@ -101,5 +113,5 @@ def test_create_tap_dev_multiqueue_tunctl_raises(self, mock_execute):
101113
mock_execute.side_effect = processutils.ProcessExecutionError
102114
# but tunctl can't create multiqueue taps, so the failure is expected
103115
self.assertRaises(processutils.ProcessExecutionError,
104-
nova.privsep.linux_net._create_tap_dev_inner,
116+
nova.privsep.linux_net.create_tap_dev,
105117
'tap42', multiqueue=True)

nova/tests/unit/privsep/test_path.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,25 +146,30 @@ class LastBytesTestCase(test.NoDBTestCase):
146146

147147
def setUp(self):
148148
super(LastBytesTestCase, self).setUp()
149-
self.f = six.BytesIO(b'1234567890')
149+
self.useFixture(fixtures.PrivsepFixture())
150150

151151
def test_truncated(self):
152-
self.f.seek(0, os.SEEK_SET)
153-
out, remaining = nova.privsep.path._last_bytes_inner(self.f, 5)
154-
self.assertEqual(out, b'67890')
155-
self.assertGreater(remaining, 0)
152+
try:
153+
fd, path = tempfile.mkstemp()
154+
os.write(fd, b'1234567890')
155+
os.close(fd)
156+
157+
out, remaining = nova.privsep.path.last_bytes(path, 5)
158+
self.assertEqual(out, b'67890')
159+
self.assertGreater(remaining, 0)
160+
161+
finally:
162+
os.unlink(path)
156163

157164
def test_read_all(self):
158-
self.f.seek(0, os.SEEK_SET)
159-
out, remaining = nova.privsep.path._last_bytes_inner(self.f, 1000)
160-
self.assertEqual(out, b'1234567890')
161-
self.assertFalse(remaining > 0)
162-
163-
def test_seek_too_far_real_file(self):
164-
# StringIO doesn't raise IOError if you see past the start of the file.
165-
with tempfile.TemporaryFile() as flo:
166-
content = b'1234567890'
167-
flo.write(content)
168-
self.assertEqual(
169-
(content, 0),
170-
nova.privsep.path._last_bytes_inner(flo, 1000))
165+
try:
166+
fd, path = tempfile.mkstemp()
167+
os.write(fd, b'1234567890')
168+
os.close(fd)
169+
170+
out, remaining = nova.privsep.path.last_bytes(path, 1000)
171+
self.assertEqual(out, b'1234567890')
172+
self.assertFalse(remaining > 0)
173+
174+
finally:
175+
os.unlink(path)

nova/tests/unit/virt/libvirt/test_driver.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import nova.privsep.fs
8080
import nova.privsep.libvirt
8181
from nova import test
82+
from nova.tests import fixtures as nova_fixtures
8283
from nova.tests.unit import fake_block_device
8384
from nova.tests.unit import fake_diagnostics
8485
from nova.tests.unit import fake_flavor
@@ -13881,6 +13882,8 @@ def test_get_console_output_not_available(self, mock_get_xml, get_domain):
1388113882
@mock.patch('nova.virt.libvirt.host.Host._get_domain')
1388213883
@mock.patch.object(libvirt_guest.Guest, 'get_xml_desc')
1388313884
def test_get_console_output_logrotate(self, mock_get_xml, get_domain):
13885+
self.useFixture(nova_fixtures.PrivsepFixture())
13886+
1388413887
fake_files = {}
1388513888
fake_files['console.log'] = b'uvwxyz'
1388613889
fake_files['console.log.0'] = b'klmnopqrst'
@@ -13889,9 +13892,14 @@ def test_get_console_output_logrotate(self, mock_get_xml, get_domain):
1388913892
def mock_path_exists(path):
1389013893
return os.path.basename(path) in fake_files
1389113894

13892-
def mock_last_bytes(path, count):
13893-
flo = io.BytesIO(fake_files[os.path.basename(path)])
13894-
return nova.privsep.path._last_bytes_inner(flo, count)
13895+
def fake_open(path, mode):
13896+
if path.endswith('console.log'):
13897+
return io.BytesIO(b'uvwxyz')
13898+
if path.endswith('console.log.0'):
13899+
return io.BytesIO(b'klmnopqrst')
13900+
if path.endswith('console.log.1'):
13901+
return io.BytesIO(b'abcdefghij')
13902+
raise Exception('No such file in testing')
1389513903

1389613904
xml = """
1389713905
<domain type='kvm'>
@@ -13919,12 +13927,13 @@ def _get_logd_output(bytes_to_read):
1391913927
try:
1392013928
prev_max = libvirt_driver.MAX_CONSOLE_BYTES
1392113929
libvirt_driver.MAX_CONSOLE_BYTES = bytes_to_read
13922-
with mock.patch('os.path.exists',
13923-
side_effect=mock_path_exists):
13924-
with mock.patch('nova.privsep.path.last_bytes',
13925-
side_effect=mock_last_bytes):
13926-
log_data = drvr.get_console_output(self.context,
13927-
instance)
13930+
with test.nested(
13931+
mock.patch('os.path.exists',
13932+
side_effect=mock_path_exists),
13933+
mock.patch.object(six.moves.builtins, 'open',
13934+
fake_open)):
13935+
log_data = drvr.get_console_output(self.context,
13936+
instance)
1392813937
finally:
1392913938
libvirt_driver.MAX_CONSOLE_BYTES = prev_max
1393013939
return log_data

0 commit comments

Comments
 (0)