]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: create functional mock for fchown
authorJohn Mulligan <jmulligan@redhat.com>
Tue, 6 Jun 2023 00:08:49 +0000 (20:08 -0400)
committerAdam King <adking@redhat.com>
Thu, 31 Aug 2023 17:35:13 +0000 (13:35 -0400)
The pyfakefs library apparently doesn't have its own mock for os.fchown.
This means that code using fchown currently calls into a mock with
no affect on the fake fs. For some reason I don't fully understand,
existing test cases work because they don't always follow the pattern
of open-write-rename. Switching to `write_new`, which always does a
rename, breaks some of the assertions performed in the tests on the fake
fs. Add a mock fchown that updates the state of the fake fs so
that converting call sites to use `write_new` will continue to work.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
(cherry picked from commit 02b6ce8a44f234aa3fe30fe21d6f28d4e36d7af5)

src/cephadm/tests/fixtures.py

index b3926f9a9de840d901a99764abb3dd4ab3b04711..879053f4860bd20b647aa7e659523a201d5139eb 100644 (file)
@@ -71,8 +71,17 @@ def cephadm_fs(
     uid = os.getuid()
     gid = os.getgid()
 
+    def fchown(fd, _uid, _gid):
+        """pyfakefs doesn't provide a working fchown or fchmod.
+        In order to get permissions working generally across renames
+        we need to provide our own implemenation.
+        """
+        file_obj = fs.get_open_file(fd).get_object()
+        file_obj.st_uid = _uid
+        file_obj.st_gid = _gid
+
     _cephadm = import_cephadm()
-    with mock.patch('os.fchown'), \
+    with mock.patch('os.fchown', side_effect=fchown), \
          mock.patch('os.fchmod'), \
          mock.patch('platform.processor', return_value='x86_64'), \
          mock.patch('cephadm.extract_uid_gid', return_value=(uid, gid)):