]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-detect-init: fix the py3 test 10266/head
authorKefu Chai <kchai@redhat.com>
Tue, 12 Jul 2016 16:47:49 +0000 (00:47 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 12 Jul 2016 18:25:07 +0000 (02:25 +0800)
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 <kchai@redhat.com>
src/ceph-detect-init/ceph_detect_init/gentoo/__init__.py
src/ceph-detect-init/tests/test_all.py

index e5e0fcbde3f0df0da0afca6e47f17fa01cfeb075..c6f4e1e2429f871948aca5a654082d4c72f8dae1 100644 (file)
@@ -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():
index e8f91e5b99a9ab554571f66204644cb8a0c884da..7d2c4628ed8f6134fa27830a018f93b7776e0ba5 100644 (file)
@@ -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')