From: Michael Fritch Date: Mon, 24 May 2021 14:03:55 +0000 (-0600) Subject: cephadm: add pyfakefs fixture X-Git-Tag: v16.2.5~72^2~10 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2d7785033a25b4b1da3a4c8ed22ac84a29025e6a;p=ceph.git cephadm: add pyfakefs fixture to mock the cephadm filesystem Signed-off-by: Michael Fritch (cherry picked from commit d5eac27833ea830f35fb0d75bc205cf85e4256e4) --- diff --git a/src/cephadm/tests/fixtures.py b/src/cephadm/tests/fixtures.py index bd0ae193a14..ab5dcab652d 100644 --- a/src/cephadm/tests/fixtures.py +++ b/src/cephadm/tests/fixtures.py @@ -3,6 +3,8 @@ import os import pytest import time +from pyfakefs import fake_filesystem + with mock.patch('builtins.open', create=True): from importlib.machinery import SourceFileLoader @@ -38,3 +40,25 @@ def exporter(): exporter = cd.CephadmDaemon(ctx, fsid='foobar', daemon_id='test') assert exporter.token == 'MyAccessToken' yield exporter + + +@pytest.fixture() +def cephadm_fs( + fs: fake_filesystem.FakeFilesystem, +): + """ + use pyfakefs to stub filesystem calls + """ + uid = os.getuid() + gid = os.getgid() + + with mock.patch('os.fchown'), \ + mock.patch('cephadm.extract_uid_gid', return_value=(uid, gid)): + + fs.create_dir(cd.DATA_DIR) + fs.create_dir(cd.LOG_DIR) + fs.create_dir(cd.LOCK_DIR) + fs.create_dir(cd.LOGROTATE_DIR) + fs.create_dir(cd.UNIT_DIR) + + yield fs diff --git a/src/cephadm/tox.ini b/src/cephadm/tox.ini index 8e127123b03..6f25b4008f0 100644 --- a/src/cephadm/tox.ini +++ b/src/cephadm/tox.ini @@ -34,8 +34,9 @@ addopts = [testenv] skip_install=true deps = - pytest + pyfakefs mock + pytest commands=pytest {posargs} [testenv:mypy]