3 from ceph_volume import exceptions, decorators, terminal
6 class TestNeedsRoot(object):
8 def test_is_root(self, monkeypatch):
11 monkeypatch.setattr(decorators.os, 'getuid', lambda: 0)
12 assert decorators.needs_root(func)() is True
14 def test_is_not_root(self, monkeypatch):
16 return True # pragma: no cover
17 monkeypatch.setattr(decorators.os, 'getuid', lambda: 20)
18 with pytest.raises(exceptions.SuperUserError) as error:
19 decorators.needs_root(func)()
21 msg = 'This command needs to be executed with sudo or as root'
22 assert str(error.value) == msg
25 class TestExceptionMessage(object):
27 def test_has_str_method(self):
28 result = decorators.make_exception_message(RuntimeError('an error'))
29 expected = "%s %s\n" % (terminal.red_arrow, 'RuntimeError: an error')
30 assert result == expected
32 def test_has_no_str_method(self):
33 class Error(Exception):
35 result = decorators.make_exception_message(Error())
36 expected = "%s %s\n" % (terminal.red_arrow, 'Error')
37 assert result == expected
40 class TestCatches(object):
44 del(os.environ['CEPH_VOLUME_DEBUG'])
48 def test_ceph_volume_debug_enabled(self):
49 os.environ['CEPH_VOLUME_DEBUG'] = '1'
50 @decorators.catches() # noqa
53 with pytest.raises(RuntimeError):
56 def test_ceph_volume_debug_disabled_no_exit(self, capsys):
57 @decorators.catches(exit=False)
61 stdout, stderr = capsys.readouterr()
62 assert 'RuntimeError\n' in stderr
64 def test_ceph_volume_debug_exits(self, capsys):
68 with pytest.raises(SystemExit):
70 stdout, stderr = capsys.readouterr()
71 assert 'RuntimeError\n' in stderr