]>
Commit | Line | Data |
---|---|---|
3efd9988 | 1 | import os |
d2e6a577 | 2 | import pytest |
11fdf7f2 | 3 | from ceph_volume.util import disk |
494da23a | 4 | from ceph_volume.util.constants import ceph_disk_guids |
3efd9988 | 5 | from ceph_volume.api import lvm as lvm_api |
b32b8144 | 6 | from ceph_volume import conf, configuration |
3efd9988 | 7 | |
d2e6a577 FG |
8 | |
9 | class Capture(object): | |
10 | ||
11 | def __init__(self, *a, **kw): | |
12 | self.a = a | |
13 | self.kw = kw | |
14 | self.calls = [] | |
28e407b8 AA |
15 | self.return_values = kw.get('return_values', False) |
16 | self.always_returns = kw.get('always_returns', False) | |
d2e6a577 FG |
17 | |
18 | def __call__(self, *a, **kw): | |
19 | self.calls.append({'args': a, 'kwargs': kw}) | |
28e407b8 AA |
20 | if self.always_returns: |
21 | return self.always_returns | |
22 | if self.return_values: | |
23 | return self.return_values.pop() | |
d2e6a577 FG |
24 | |
25 | ||
3efd9988 FG |
26 | class Factory(object): |
27 | ||
28 | def __init__(self, **kw): | |
29 | for k, v in kw.items(): | |
30 | setattr(self, k, v) | |
31 | ||
32 | ||
33 | @pytest.fixture | |
34 | def factory(): | |
35 | return Factory | |
36 | ||
37 | ||
d2e6a577 FG |
38 | @pytest.fixture |
39 | def capture(): | |
40 | return Capture() | |
181888fb FG |
41 | |
42 | ||
b32b8144 FG |
43 | @pytest.fixture |
44 | def fake_run(monkeypatch): | |
45 | fake_run = Capture() | |
46 | monkeypatch.setattr('ceph_volume.process.run', fake_run) | |
47 | return fake_run | |
48 | ||
49 | ||
50 | @pytest.fixture | |
51 | def fake_call(monkeypatch): | |
28e407b8 | 52 | fake_call = Capture(always_returns=([], [], 0)) |
b32b8144 FG |
53 | monkeypatch.setattr('ceph_volume.process.call', fake_call) |
54 | return fake_call | |
55 | ||
56 | ||
91327a77 AA |
57 | @pytest.fixture |
58 | def fakedevice(factory): | |
59 | def apply(**kw): | |
60 | params = dict( | |
61 | path='/dev/sda', | |
62 | abspath='/dev/sda', | |
63 | lv_api=None, | |
64 | pvs_api=[], | |
65 | disk_api={}, | |
66 | sys_api={}, | |
67 | exists=True, | |
68 | is_lvm_member=True, | |
69 | ) | |
70 | params.update(dict(kw)) | |
11fdf7f2 | 71 | params['lvm_size'] = disk.Size(b=params['sys_api'].get("size", 0)) |
91327a77 AA |
72 | return factory(**params) |
73 | return apply | |
74 | ||
75 | ||
b32b8144 FG |
76 | @pytest.fixture |
77 | def stub_call(monkeypatch): | |
78 | """ | |
79 | Monkeypatches process.call, so that a caller can add behavior to the response | |
80 | """ | |
28e407b8 AA |
81 | def apply(return_values): |
82 | if isinstance(return_values, tuple): | |
83 | return_values = [return_values] | |
84 | stubbed_call = Capture(return_values=return_values) | |
85 | monkeypatch.setattr('ceph_volume.process.call', stubbed_call) | |
86 | return stubbed_call | |
b32b8144 FG |
87 | |
88 | return apply | |
89 | ||
90 | ||
91327a77 AA |
91 | @pytest.fixture(autouse=True) |
92 | def reset_cluster_name(request, monkeypatch): | |
93 | """ | |
94 | The globally available ``ceph_volume.conf.cluster`` might get mangled in | |
95 | tests, make sure that after evert test, it gets reset, preventing pollution | |
96 | going into other tests later. | |
97 | """ | |
98 | def fin(): | |
99 | conf.cluster = None | |
100 | try: | |
101 | os.environ.pop('CEPH_CONF') | |
102 | except KeyError: | |
103 | pass | |
104 | request.addfinalizer(fin) | |
105 | ||
106 | ||
b32b8144 FG |
107 | @pytest.fixture |
108 | def conf_ceph(monkeypatch): | |
109 | """ | |
110 | Monkeypatches ceph_volume.conf.ceph, which is meant to parse/read | |
111 | a ceph.conf. The patching is naive, it allows one to set return values for | |
112 | specific method calls. | |
113 | """ | |
114 | def apply(**kw): | |
115 | stub = Factory(**kw) | |
116 | monkeypatch.setattr(conf, 'ceph', stub) | |
117 | return stub | |
118 | return apply | |
119 | ||
120 | ||
121 | @pytest.fixture | |
122 | def conf_ceph_stub(monkeypatch, tmpfile): | |
123 | """ | |
124 | Monkeypatches ceph_volume.conf.ceph with contents from a string that are | |
125 | written to a temporary file and then is fed through the same ceph.conf | |
126 | loading mechanisms for testing. Unlike ``conf_ceph`` which is just a fake, | |
127 | we are actually loading values as seen on a ceph.conf file | |
128 | ||
129 | This is useful when more complex ceph.conf's are needed. In the case of | |
130 | just trying to validate a key/value behavior ``conf_ceph`` is better | |
131 | suited. | |
132 | """ | |
133 | def apply(contents): | |
134 | conf_path = tmpfile(contents=contents) | |
135 | parser = configuration.load(conf_path) | |
136 | monkeypatch.setattr(conf, 'ceph', parser) | |
137 | return parser | |
138 | return apply | |
139 | ||
140 | ||
181888fb FG |
141 | @pytest.fixture |
142 | def volumes(monkeypatch): | |
91327a77 | 143 | monkeypatch.setattr('ceph_volume.process.call', lambda x, **kw: ('', '', 0)) |
3efd9988 | 144 | volumes = lvm_api.Volumes() |
181888fb FG |
145 | volumes._purge() |
146 | return volumes | |
147 | ||
148 | ||
149 | @pytest.fixture | |
150 | def volume_groups(monkeypatch): | |
91327a77 | 151 | monkeypatch.setattr('ceph_volume.process.call', lambda x, **kw: ('', '', 0)) |
3efd9988 | 152 | vgs = lvm_api.VolumeGroups() |
181888fb FG |
153 | vgs._purge() |
154 | return vgs | |
155 | ||
eafe8130 TL |
156 | def volume_groups_empty(monkeypatch): |
157 | monkeypatch.setattr('ceph_volume.process.call', lambda x, **kw: ('', '', 0)) | |
158 | vgs = lvm_api.VolumeGroups(populate=False) | |
159 | return vgs | |
181888fb | 160 | |
91327a77 AA |
161 | @pytest.fixture |
162 | def stub_vgs(monkeypatch, volume_groups): | |
163 | def apply(vgs): | |
164 | monkeypatch.setattr(lvm_api, 'get_api_vgs', lambda: vgs) | |
165 | return apply | |
166 | ||
167 | ||
1adf2230 AA |
168 | @pytest.fixture |
169 | def pvolumes(monkeypatch): | |
91327a77 | 170 | monkeypatch.setattr('ceph_volume.process.call', lambda x, **kw: ('', '', 0)) |
1adf2230 AA |
171 | pvolumes = lvm_api.PVolumes() |
172 | pvolumes._purge() | |
173 | return pvolumes | |
eafe8130 TL |
174 | @pytest.fixture |
175 | def pvolumes_empty(monkeypatch): | |
176 | monkeypatch.setattr('ceph_volume.process.call', lambda x, **kw: ('', '', 0)) | |
177 | pvolumes = lvm_api.PVolumes(populate=False) | |
178 | return pvolumes | |
179 | ||
1adf2230 AA |
180 | |
181 | ||
181888fb FG |
182 | @pytest.fixture |
183 | def is_root(monkeypatch): | |
184 | """ | |
185 | Patch ``os.getuid()`` so that ceph-volume's decorators that ensure a user | |
186 | is root (or is sudoing to superuser) can continue as-is | |
187 | """ | |
188 | monkeypatch.setattr('os.getuid', lambda: 0) | |
3efd9988 FG |
189 | |
190 | ||
191 | @pytest.fixture | |
192 | def tmpfile(tmpdir): | |
193 | """ | |
194 | Create a temporary file, optionally filling it with contents, returns an | |
195 | absolute path to the file when called | |
196 | """ | |
1adf2230 AA |
197 | def generate_file(name='file', contents='', directory=None): |
198 | directory = directory or str(tmpdir) | |
199 | path = os.path.join(directory, name) | |
3efd9988 FG |
200 | with open(path, 'w') as fp: |
201 | fp.write(contents) | |
202 | return path | |
203 | return generate_file | |
1adf2230 AA |
204 | |
205 | ||
494da23a TL |
206 | @pytest.fixture |
207 | def disable_kernel_queries(monkeypatch): | |
208 | ''' | |
209 | This speeds up calls to Device and Disk | |
210 | ''' | |
211 | monkeypatch.setattr("ceph_volume.util.device.disk.get_devices", lambda: {}) | |
212 | monkeypatch.setattr("ceph_volume.util.disk.udevadm_property", lambda *a, **kw: {}) | |
213 | ||
214 | ||
215 | @pytest.fixture | |
216 | def disable_lvm_queries(monkeypatch): | |
217 | ''' | |
218 | This speeds up calls to Device and Disk | |
219 | ''' | |
220 | monkeypatch.setattr("ceph_volume.util.device.lvm.get_lv_from_argument", lambda path: None) | |
221 | monkeypatch.setattr("ceph_volume.util.device.lvm.get_lv", lambda vg_name, lv_uuid: None) | |
222 | ||
223 | ||
224 | @pytest.fixture(params=[ | |
225 | '', 'ceph data', 'ceph journal', 'ceph block', | |
226 | 'ceph block.wal', 'ceph block.db', 'ceph lockbox']) | |
227 | def ceph_partlabel(request): | |
228 | return request.param | |
229 | ||
230 | ||
231 | @pytest.fixture(params=list(ceph_disk_guids.keys())) | |
232 | def ceph_parttype(request): | |
233 | return request.param | |
234 | ||
235 | ||
236 | @pytest.fixture | |
237 | def lsblk_ceph_disk_member(monkeypatch, request, ceph_partlabel, ceph_parttype): | |
238 | monkeypatch.setattr("ceph_volume.util.device.disk.lsblk", | |
239 | lambda path: {'PARTLABEL': ceph_partlabel}) | |
240 | # setting blkid here too in order to be able to fall back to PARTTYPE based | |
241 | # membership | |
242 | monkeypatch.setattr("ceph_volume.util.device.disk.blkid", | |
243 | lambda path: {'PARTLABEL': '', | |
244 | 'PARTTYPE': ceph_parttype}) | |
245 | ||
246 | ||
247 | @pytest.fixture | |
248 | def blkid_ceph_disk_member(monkeypatch, request, ceph_partlabel, ceph_parttype): | |
249 | monkeypatch.setattr("ceph_volume.util.device.disk.blkid", | |
250 | lambda path: {'PARTLABEL': ceph_partlabel, | |
251 | 'PARTTYPE': ceph_parttype}) | |
252 | ||
253 | ||
254 | @pytest.fixture(params=[ | |
255 | ('gluster partition', 'gluster partition'), | |
256 | # falls back to blkid | |
257 | ('', 'gluster partition'), | |
258 | ('gluster partition', ''), | |
259 | ]) | |
260 | def device_info_not_ceph_disk_member(monkeypatch, request): | |
261 | monkeypatch.setattr("ceph_volume.util.device.disk.lsblk", | |
262 | lambda path: {'PARTLABEL': request.param[0]}) | |
263 | monkeypatch.setattr("ceph_volume.util.device.disk.blkid", | |
264 | lambda path: {'PARTLABEL': request.param[1]}) | |
265 | ||
266 | ||
1adf2230 AA |
267 | @pytest.fixture |
268 | def device_info(monkeypatch): | |
f64942e4 | 269 | def apply(devices=None, lsblk=None, lv=None, blkid=None, udevadm=None): |
1adf2230 AA |
270 | devices = devices if devices else {} |
271 | lsblk = lsblk if lsblk else {} | |
91327a77 | 272 | blkid = blkid if blkid else {} |
f64942e4 | 273 | udevadm = udevadm if udevadm else {} |
1adf2230 AA |
274 | lv = Factory(**lv) if lv else None |
275 | monkeypatch.setattr("ceph_volume.sys_info.devices", {}) | |
276 | monkeypatch.setattr("ceph_volume.util.device.disk.get_devices", lambda: devices) | |
91327a77 AA |
277 | if not devices: |
278 | monkeypatch.setattr("ceph_volume.util.device.lvm.get_lv_from_argument", lambda path: lv) | |
279 | else: | |
280 | monkeypatch.setattr("ceph_volume.util.device.lvm.get_lv_from_argument", lambda path: None) | |
281 | monkeypatch.setattr("ceph_volume.util.device.lvm.get_lv", lambda vg_name, lv_uuid: lv) | |
1adf2230 | 282 | monkeypatch.setattr("ceph_volume.util.device.disk.lsblk", lambda path: lsblk) |
91327a77 | 283 | monkeypatch.setattr("ceph_volume.util.device.disk.blkid", lambda path: blkid) |
f64942e4 | 284 | monkeypatch.setattr("ceph_volume.util.disk.udevadm_property", lambda *a, **kw: udevadm) |
1adf2230 | 285 | return apply |