Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / ceph-volume / ceph_volume / tests / conftest.py
1 import os
2 import pytest
3 from ceph_volume.api import lvm as lvm_api
4
5
6 class Capture(object):
7
8     def __init__(self, *a, **kw):
9         self.a = a
10         self.kw = kw
11         self.calls = []
12
13     def __call__(self, *a, **kw):
14         self.calls.append({'args': a, 'kwargs': kw})
15
16
17 class Factory(object):
18
19     def __init__(self, **kw):
20         for k, v in kw.items():
21             setattr(self, k, v)
22
23
24 @pytest.fixture
25 def factory():
26     return Factory
27
28
29 @pytest.fixture
30 def capture():
31     return Capture()
32
33
34 @pytest.fixture
35 def volumes(monkeypatch):
36     monkeypatch.setattr('ceph_volume.process.call', lambda x: ('', '', 0))
37     volumes = lvm_api.Volumes()
38     volumes._purge()
39     return volumes
40
41
42 @pytest.fixture
43 def volume_groups(monkeypatch):
44     monkeypatch.setattr('ceph_volume.process.call', lambda x: ('', '', 0))
45     vgs = lvm_api.VolumeGroups()
46     vgs._purge()
47     return vgs
48
49
50 @pytest.fixture
51 def is_root(monkeypatch):
52     """
53     Patch ``os.getuid()`` so that ceph-volume's decorators that ensure a user
54     is root (or is sudoing to superuser) can continue as-is
55     """
56     monkeypatch.setattr('os.getuid', lambda: 0)
57
58
59 @pytest.fixture
60 def tmpfile(tmpdir):
61     """
62     Create a temporary file, optionally filling it with contents, returns an
63     absolute path to the file when called
64     """
65     def generate_file(name='file', contents=''):
66         path = os.path.join(str(tmpdir), name)
67         with open(path, 'w') as fp:
68             fp.write(contents)
69         return path
70     return generate_file