]>
Commit | Line | Data |
---|---|---|
d2e6a577 FG |
1 | import os |
2 | import pytest | |
3 | from ceph_volume import exceptions, decorators, terminal | |
4 | ||
5 | ||
6 | class TestNeedsRoot(object): | |
7 | ||
8 | def test_is_root(self, monkeypatch): | |
9 | def func(): | |
10 | return True | |
11 | monkeypatch.setattr(decorators.os, 'getuid', lambda: 0) | |
12 | assert decorators.needs_root(func)() is True | |
13 | ||
20effc67 TL |
14 | def test_is_not_root_env_var_skip_needs_root(self, monkeypatch): |
15 | def func(): | |
16 | return True | |
17 | monkeypatch.setattr(decorators.os, 'getuid', lambda: 123) | |
18 | monkeypatch.setattr(decorators.os, 'environ', {'CEPH_VOLUME_SKIP_NEEDS_ROOT': '1'}) | |
19 | assert decorators.needs_root(func)() is True | |
20 | ||
d2e6a577 FG |
21 | def test_is_not_root(self, monkeypatch): |
22 | def func(): | |
23 | return True # pragma: no cover | |
24 | monkeypatch.setattr(decorators.os, 'getuid', lambda: 20) | |
25 | with pytest.raises(exceptions.SuperUserError) as error: | |
26 | decorators.needs_root(func)() | |
27 | ||
28 | msg = 'This command needs to be executed with sudo or as root' | |
29 | assert str(error.value) == msg | |
30 | ||
31 | ||
32 | class TestExceptionMessage(object): | |
33 | ||
34 | def test_has_str_method(self): | |
35 | result = decorators.make_exception_message(RuntimeError('an error')) | |
36 | expected = "%s %s\n" % (terminal.red_arrow, 'RuntimeError: an error') | |
37 | assert result == expected | |
38 | ||
39 | def test_has_no_str_method(self): | |
40 | class Error(Exception): | |
41 | pass | |
42 | result = decorators.make_exception_message(Error()) | |
43 | expected = "%s %s\n" % (terminal.red_arrow, 'Error') | |
44 | assert result == expected | |
45 | ||
46 | ||
47 | class TestCatches(object): | |
48 | ||
49 | def teardown(self): | |
50 | try: | |
51 | del(os.environ['CEPH_VOLUME_DEBUG']) | |
52 | except KeyError: | |
53 | pass | |
54 | ||
55 | def test_ceph_volume_debug_enabled(self): | |
56 | os.environ['CEPH_VOLUME_DEBUG'] = '1' | |
57 | @decorators.catches() # noqa | |
58 | def func(): | |
59 | raise RuntimeError() | |
60 | with pytest.raises(RuntimeError): | |
61 | func() | |
62 | ||
63 | def test_ceph_volume_debug_disabled_no_exit(self, capsys): | |
64 | @decorators.catches(exit=False) | |
65 | def func(): | |
66 | raise RuntimeError() | |
67 | func() | |
68 | stdout, stderr = capsys.readouterr() | |
69 | assert 'RuntimeError\n' in stderr | |
70 | ||
71 | def test_ceph_volume_debug_exits(self, capsys): | |
72 | @decorators.catches() | |
73 | def func(): | |
74 | raise RuntimeError() | |
75 | with pytest.raises(SystemExit): | |
76 | func() | |
77 | stdout, stderr = capsys.readouterr() | |
78 | assert 'RuntimeError\n' in stderr |