From 6ed1c41fede0a767adcf1ba53f2a57d94ec2fda9 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 13 Jul 2016 00:47:49 +0800 Subject: [PATCH] ceph-detect-init: fix the py3 test the mock_open() does not support "for i in open()", and readline() and readlines() support was added in py3.3 and py3.4 so for better backwards compatibility, we should change the code being tested to use the plain read() call. and also use open(path) instead of open(path, 'rb') for simplicity, otherwise we need to use bytestring for comparison and pass the same parameters to mock. Signed-off-by: Kefu Chai --- .../ceph_detect_init/gentoo/__init__.py | 7 ++----- src/ceph-detect-init/tests/test_all.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/ceph-detect-init/ceph_detect_init/gentoo/__init__.py b/src/ceph-detect-init/ceph_detect_init/gentoo/__init__.py index e5e0fcbde3f0..c6f4e1e2429f 100644 --- a/src/ceph-detect-init/ceph_detect_init/gentoo/__init__.py +++ b/src/ceph-detect-init/ceph_detect_init/gentoo/__init__.py @@ -10,11 +10,8 @@ def is_systemd(): Detect whether systemd is running; WARNING: not mutually exclusive with openrc """ - with open('/proc/1/comm', 'rb') as i: - for line in i: - if 'systemd' in line: - return True - return False + with open('/proc/1/comm') as i: + return 'systemd' in i.read() def is_openrc(): diff --git a/src/ceph-detect-init/tests/test_all.py b/src/ceph-detect-init/tests/test_all.py index e8f91e5b99a9..7d2c4628ed8f 100644 --- a/src/ceph-detect-init/tests/test_all.py +++ b/src/ceph-detect-init/tests/test_all.py @@ -103,16 +103,22 @@ class TestCephDetectInit(testtools.TestCase): self.assertEqual(gentoo.is_openrc(), False) def test_gentoo_is_systemd(self): + import sys + if sys.version_info >= (3, 0): + mocked_fn = 'builtins.open' + else: + mocked_fn = '__builtin__.open' + f = mock.mock_open(read_data='systemd') - with mock.patch('__main__.file', f, create=True) as m: + with mock.patch(mocked_fn, f, create=True) as m: self.assertEqual(gentoo.is_systemd(), True) m.assert_called_once_with('/proc/1/comm') f = mock.mock_open(read_data='init') - with mock.patch('__main__.file', f, create=True) as m: + with mock.patch(mocked_fn, f, create=True) as m: self.assertEqual(gentoo.is_systemd(), False) m.assert_called_once_with('/proc/1/comm') f = mock.mock_open(read_data='upstart') - with mock.patch('__main__.file', f, create=True) as m: + with mock.patch(mocked_fn, f, create=True) as m: self.assertEqual(gentoo.is_systemd(), False) m.assert_called_once_with('/proc/1/comm') -- 2.47.3