From: Guillaume Abrioux Date: Tue, 7 Dec 2021 14:18:10 +0000 (+0100) Subject: ceph-volume: make it possible to skip needs_root() X-Git-Tag: v16.2.8~290^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=bf325a83e985906664f298f19d890e9bbd79ed87;p=ceph.git ceph-volume: make it possible to skip needs_root() Add the possibility to skip the `needs_root()` decorator. See linked tracker for details. Fixes: https://tracker.ceph.com/issues/53511 Signed-off-by: Guillaume Abrioux (cherry picked from commit 068a1d2a300bc21e9aa08142c4195970ef480e41) --- diff --git a/src/ceph-volume/ceph_volume/decorators.py b/src/ceph-volume/ceph_volume/decorators.py index 95acce064fe18..3c003ad77c417 100644 --- a/src/ceph-volume/ceph_volume/decorators.py +++ b/src/ceph-volume/ceph_volume/decorators.py @@ -11,7 +11,7 @@ def needs_root(func): """ @wraps(func) def is_root(*a, **kw): - if not os.getuid() == 0: + if not os.getuid() == 0 and not os.environ.get('CEPH_VOLUME_SKIP_NEEDS_ROOT', False): raise exceptions.SuperUserError() return func(*a, **kw) return is_root diff --git a/src/ceph-volume/ceph_volume/tests/test_decorators.py b/src/ceph-volume/ceph_volume/tests/test_decorators.py index 8df891456e607..5bdf6b3d27bf9 100644 --- a/src/ceph-volume/ceph_volume/tests/test_decorators.py +++ b/src/ceph-volume/ceph_volume/tests/test_decorators.py @@ -11,6 +11,13 @@ class TestNeedsRoot(object): monkeypatch.setattr(decorators.os, 'getuid', lambda: 0) assert decorators.needs_root(func)() is True + def test_is_not_root_env_var_skip_needs_root(self, monkeypatch): + def func(): + return True + monkeypatch.setattr(decorators.os, 'getuid', lambda: 123) + monkeypatch.setattr(decorators.os, 'environ', {'CEPH_VOLUME_SKIP_NEEDS_ROOT': '1'}) + assert decorators.needs_root(func)() is True + def test_is_not_root(self, monkeypatch): def func(): return True # pragma: no cover