]>
Commit | Line | Data |
---|---|---|
3efd9988 | 1 | import os |
d2e6a577 | 2 | import pytest |
3efd9988 | 3 | from ceph_volume.api import lvm as lvm_api |
b32b8144 | 4 | from ceph_volume import conf, configuration |
3efd9988 | 5 | |
d2e6a577 FG |
6 | |
7 | class Capture(object): | |
8 | ||
9 | def __init__(self, *a, **kw): | |
10 | self.a = a | |
11 | self.kw = kw | |
12 | self.calls = [] | |
28e407b8 AA |
13 | self.return_values = kw.get('return_values', False) |
14 | self.always_returns = kw.get('always_returns', False) | |
d2e6a577 FG |
15 | |
16 | def __call__(self, *a, **kw): | |
17 | self.calls.append({'args': a, 'kwargs': kw}) | |
28e407b8 AA |
18 | if self.always_returns: |
19 | return self.always_returns | |
20 | if self.return_values: | |
21 | return self.return_values.pop() | |
d2e6a577 FG |
22 | |
23 | ||
3efd9988 FG |
24 | class Factory(object): |
25 | ||
26 | def __init__(self, **kw): | |
27 | for k, v in kw.items(): | |
28 | setattr(self, k, v) | |
29 | ||
30 | ||
31 | @pytest.fixture | |
32 | def factory(): | |
33 | return Factory | |
34 | ||
35 | ||
d2e6a577 FG |
36 | @pytest.fixture |
37 | def capture(): | |
38 | return Capture() | |
181888fb FG |
39 | |
40 | ||
b32b8144 FG |
41 | @pytest.fixture |
42 | def fake_run(monkeypatch): | |
43 | fake_run = Capture() | |
44 | monkeypatch.setattr('ceph_volume.process.run', fake_run) | |
45 | return fake_run | |
46 | ||
47 | ||
48 | @pytest.fixture | |
49 | def fake_call(monkeypatch): | |
28e407b8 | 50 | fake_call = Capture(always_returns=([], [], 0)) |
b32b8144 FG |
51 | monkeypatch.setattr('ceph_volume.process.call', fake_call) |
52 | return fake_call | |
53 | ||
54 | ||
55 | @pytest.fixture | |
56 | def stub_call(monkeypatch): | |
57 | """ | |
58 | Monkeypatches process.call, so that a caller can add behavior to the response | |
59 | """ | |
28e407b8 AA |
60 | def apply(return_values): |
61 | if isinstance(return_values, tuple): | |
62 | return_values = [return_values] | |
63 | stubbed_call = Capture(return_values=return_values) | |
64 | monkeypatch.setattr('ceph_volume.process.call', stubbed_call) | |
65 | return stubbed_call | |
b32b8144 FG |
66 | |
67 | return apply | |
68 | ||
69 | ||
70 | @pytest.fixture | |
71 | def conf_ceph(monkeypatch): | |
72 | """ | |
73 | Monkeypatches ceph_volume.conf.ceph, which is meant to parse/read | |
74 | a ceph.conf. The patching is naive, it allows one to set return values for | |
75 | specific method calls. | |
76 | """ | |
77 | def apply(**kw): | |
78 | stub = Factory(**kw) | |
79 | monkeypatch.setattr(conf, 'ceph', stub) | |
80 | return stub | |
81 | return apply | |
82 | ||
83 | ||
84 | @pytest.fixture | |
85 | def conf_ceph_stub(monkeypatch, tmpfile): | |
86 | """ | |
87 | Monkeypatches ceph_volume.conf.ceph with contents from a string that are | |
88 | written to a temporary file and then is fed through the same ceph.conf | |
89 | loading mechanisms for testing. Unlike ``conf_ceph`` which is just a fake, | |
90 | we are actually loading values as seen on a ceph.conf file | |
91 | ||
92 | This is useful when more complex ceph.conf's are needed. In the case of | |
93 | just trying to validate a key/value behavior ``conf_ceph`` is better | |
94 | suited. | |
95 | """ | |
96 | def apply(contents): | |
97 | conf_path = tmpfile(contents=contents) | |
98 | parser = configuration.load(conf_path) | |
99 | monkeypatch.setattr(conf, 'ceph', parser) | |
100 | return parser | |
101 | return apply | |
102 | ||
103 | ||
181888fb FG |
104 | @pytest.fixture |
105 | def volumes(monkeypatch): | |
106 | monkeypatch.setattr('ceph_volume.process.call', lambda x: ('', '', 0)) | |
3efd9988 | 107 | volumes = lvm_api.Volumes() |
181888fb FG |
108 | volumes._purge() |
109 | return volumes | |
110 | ||
111 | ||
112 | @pytest.fixture | |
113 | def volume_groups(monkeypatch): | |
114 | monkeypatch.setattr('ceph_volume.process.call', lambda x: ('', '', 0)) | |
3efd9988 | 115 | vgs = lvm_api.VolumeGroups() |
181888fb FG |
116 | vgs._purge() |
117 | return vgs | |
118 | ||
119 | ||
120 | @pytest.fixture | |
121 | def is_root(monkeypatch): | |
122 | """ | |
123 | Patch ``os.getuid()`` so that ceph-volume's decorators that ensure a user | |
124 | is root (or is sudoing to superuser) can continue as-is | |
125 | """ | |
126 | monkeypatch.setattr('os.getuid', lambda: 0) | |
3efd9988 FG |
127 | |
128 | ||
129 | @pytest.fixture | |
130 | def tmpfile(tmpdir): | |
131 | """ | |
132 | Create a temporary file, optionally filling it with contents, returns an | |
133 | absolute path to the file when called | |
134 | """ | |
135 | def generate_file(name='file', contents=''): | |
136 | path = os.path.join(str(tmpdir), name) | |
137 | with open(path, 'w') as fp: | |
138 | fp.write(contents) | |
139 | return path | |
140 | return generate_file |