From: Kefu Chai Date: Tue, 12 Jul 2016 16:47:49 +0000 (+0800) Subject: ceph-detect-init: fix the py3 test X-Git-Tag: ses5-milestone5~428^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6ed1c41fede0a767adcf1ba53f2a57d94ec2fda9;p=ceph.git 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 --- 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 e5e0fcbde3f..c6f4e1e2429 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 e8f91e5b99a..7d2c4628ed8 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')