]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume tests validate restorecon skip calls 31555/head
authorAlfredo Deza <adeza@redhat.com>
Tue, 5 Nov 2019 16:51:35 +0000 (11:51 -0500)
committerJan Fajerski <jfajerski@suse.com>
Tue, 12 Nov 2019 08:44:21 +0000 (09:44 +0100)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
(cherry picked from commit 01e7a14fdc48d6c328502d1fea3069dbb3098b1b)

src/ceph-volume/ceph_volume/tests/util/test_system.py

index d0919c99854618f6e0888e5f458d707988af4faf..808048a6a9c056bb72c55389f6cdfd8c92c6594a 100644 (file)
@@ -216,3 +216,56 @@ class TestWhich(object):
         cap = capsys.readouterr()
         assert 'Absolute path not found for executable: exedir' in cap.err
         assert 'Ensure $PATH environment variable contains common executable locations' in cap.err
+
+
+@pytest.fixture
+def stub_which(monkeypatch):
+    def apply(value='/bin/restorecon'):
+        monkeypatch.setattr(system, 'which', lambda x: value)
+    return apply
+
+
+class TestSetContext(object):
+
+    def setup(self):
+        try:
+            os.environ.pop('CEPH_VOLUME_SKIP_RESTORECON')
+        except KeyError:
+            pass
+
+    @pytest.mark.parametrize('value', ['1', 'True', 'true', 'TRUE', 'yes'])
+    def test_set_context_skips(self, stub_call, fake_run, value):
+        stub_call(('', '', 0))
+        os.environ['CEPH_VOLUME_SKIP_RESTORECON'] = value
+        system.set_context('/tmp/foo')
+        assert fake_run.calls == []
+
+    @pytest.mark.parametrize('value', ['0', 'False', 'false', 'FALSE', 'no'])
+    def test_set_context_doesnt_skip_with_env(self, stub_call, stub_which, fake_run, value):
+        stub_call(('', '', 0))
+        stub_which()
+        os.environ['CEPH_VOLUME_SKIP_RESTORECON'] = value
+        system.set_context('/tmp/foo')
+        assert len(fake_run.calls)
+
+    def test_set_context_skips_on_executable(self, stub_call, stub_which, fake_run):
+        stub_call(('', '', 0))
+        stub_which('restorecon')
+        system.set_context('/tmp/foo')
+        assert fake_run.calls == []
+
+    def test_set_context_no_skip_on_executable(self, stub_call, stub_which, fake_run):
+        stub_call(('', '', 0))
+        stub_which('/bin/restorecon')
+        system.set_context('/tmp/foo')
+        assert len(fake_run.calls)
+
+    def test_selinuxenabled_doesnt_exist(self, stub_call, fake_run):
+        stub_call(('', 'command not found: selinuxenabled', 127))
+        system.set_context('/tmp/foo')
+        assert fake_run.calls == []
+
+    def test_selinuxenabled_is_not_enabled(self, stub_call, fake_run):
+        stub_call(('', '', 1))
+        system.set_context('/tmp/foo')
+        assert fake_run.calls == []