]>
git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/tests/util/test_prepare.py
2 from textwrap
import dedent
4 from ceph_volume
.util
import prepare
5 from ceph_volume
.util
.prepare
import system
6 from ceph_volume
import conf
7 from ceph_volume
.tests
.conftest
import Factory
10 class TestCheckID(object):
12 def test_false_if_id_is_none(self
):
13 assert not prepare
.check_id(None)
15 def test_returncode_is_not_zero(self
, monkeypatch
):
16 monkeypatch
.setattr('ceph_volume.process.call', lambda *a
, **kw
: ('', '', 1))
17 with pytest
.raises(RuntimeError):
20 def test_id_does_exist(self
, monkeypatch
):
24 stdout
= ['', json
.dumps(stdout
)]
25 monkeypatch
.setattr('ceph_volume.process.call', lambda *a
, **kw
: (stdout
, '', 0))
26 result
= prepare
.check_id(0)
29 def test_id_does_not_exist(self
, monkeypatch
):
33 stdout
= ['', json
.dumps(stdout
)]
34 monkeypatch
.setattr('ceph_volume.process.call', lambda *a
, **kw
: (stdout
, '', 0))
35 result
= prepare
.check_id(1)
38 def test_invalid_osd_id(self
, monkeypatch
):
42 stdout
= ['', json
.dumps(stdout
)]
43 monkeypatch
.setattr('ceph_volume.process.call', lambda *a
, **kw
: (stdout
, '', 0))
44 result
= prepare
.check_id("foo")
48 class TestFormatDevice(object):
50 def test_include_force(self
, fake_run
, monkeypatch
):
51 monkeypatch
.setattr(conf
, 'ceph', Factory(get_list
=lambda *a
, **kw
: []))
52 prepare
.format_device('/dev/sxx')
53 flags
= fake_run
.calls
[0]['args'][0]
56 def test_device_is_always_appended(self
, fake_run
, conf_ceph
):
57 conf_ceph(get_list
=lambda *a
, **kw
: [])
58 prepare
.format_device('/dev/sxx')
59 flags
= fake_run
.calls
[0]['args'][0]
60 assert flags
[-1] == '/dev/sxx'
62 def test_extra_flags_are_added(self
, fake_run
, conf_ceph
):
63 conf_ceph(get_list
=lambda *a
, **kw
: ['--why-yes'])
64 prepare
.format_device('/dev/sxx')
65 flags
= fake_run
.calls
[0]['args'][0]
66 assert '--why-yes' in flags
68 def test_default_options(self
, conf_ceph_stub
, fake_run
):
69 conf_ceph_stub(dedent("""[global]
70 fsid = 1234lkjh1234"""))
72 prepare
.format_device('/dev/sda1')
75 '-f', '-i', 'size=2048', # default flags
77 assert expected
== fake_run
.calls
[0]['args'][0]
79 def test_multiple_options_are_used(self
, conf_ceph_stub
, fake_run
):
80 conf_ceph_stub(dedent("""[global]
83 osd mkfs options xfs = -f -i size=1024"""))
85 prepare
.format_device('/dev/sda1')
88 '-f', '-i', 'size=1024',
90 assert expected
== fake_run
.calls
[0]['args'][0]
92 def test_multiple_options_will_get_the_force_flag(self
, conf_ceph_stub
, fake_run
):
93 conf_ceph_stub(dedent("""[global]
96 osd mkfs options xfs = -i size=1024"""))
98 prepare
.format_device('/dev/sda1')
101 '-f', '-i', 'size=1024',
103 assert expected
== fake_run
.calls
[0]['args'][0]
105 def test_underscore_options_are_used(self
, conf_ceph_stub
, fake_run
):
106 conf_ceph_stub(dedent("""[global]
109 osd_mkfs_options_xfs = -i size=128"""))
110 conf
.cluster
= 'ceph'
111 prepare
.format_device('/dev/sda1')
114 '-f', '-i', 'size=128',
116 assert expected
== fake_run
.calls
[0]['args'][0]
119 class TestOsdMkfsBluestore(object):
121 def test_keyring_is_added(self
, fake_call
, monkeypatch
):
122 monkeypatch
.setattr(system
, 'chown', lambda path
: True)
123 prepare
.osd_mkfs_bluestore(1, 'asdf', keyring
='secret')
124 assert '--keyfile' in fake_call
.calls
[0]['args'][0]
126 def test_keyring_is_not_added(self
, fake_call
, monkeypatch
):
127 monkeypatch
.setattr(system
, 'chown', lambda path
: True)
128 prepare
.osd_mkfs_bluestore(1, 'asdf')
129 assert '--keyfile' not in fake_call
.calls
[0]['args'][0]
131 def test_wal_is_added(self
, fake_call
, monkeypatch
):
132 monkeypatch
.setattr(system
, 'chown', lambda path
: True)
133 prepare
.osd_mkfs_bluestore(1, 'asdf', wal
='/dev/smm1')
134 assert '--bluestore-block-wal-path' in fake_call
.calls
[0]['args'][0]
135 assert '/dev/smm1' in fake_call
.calls
[0]['args'][0]
137 def test_db_is_added(self
, fake_call
, monkeypatch
):
138 monkeypatch
.setattr(system
, 'chown', lambda path
: True)
139 prepare
.osd_mkfs_bluestore(1, 'asdf', db
='/dev/smm2')
140 assert '--bluestore-block-db-path' in fake_call
.calls
[0]['args'][0]
141 assert '/dev/smm2' in fake_call
.calls
[0]['args'][0]
144 class TestMountOSD(object):
146 def test_default_options(self
, conf_ceph_stub
, fake_run
):
147 conf_ceph_stub(dedent("""[global]
148 fsid = 1234lkjh1234"""))
149 conf
.cluster
= 'ceph'
150 prepare
.mount_osd('/dev/sda1', 1)
152 'mount', '-t', 'xfs', '-o',
153 'rw,noatime,inode64', # default flags
154 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
155 assert expected
== fake_run
.calls
[0]['args'][0]
157 def test_mount_options_are_used(self
, conf_ceph_stub
, fake_run
):
158 conf_ceph_stub(dedent("""[global]
161 osd mount options xfs = rw"""))
162 conf
.cluster
= 'ceph'
163 prepare
.mount_osd('/dev/sda1', 1)
165 'mount', '-t', 'xfs', '-o',
167 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
168 assert expected
== fake_run
.calls
[0]['args'][0]
170 def test_multiple_whitespace_options_are_used(self
, conf_ceph_stub
, fake_run
):
171 conf_ceph_stub(dedent("""[global]
174 osd mount options xfs = rw auto exec"""))
175 conf
.cluster
= 'ceph'
176 prepare
.mount_osd('/dev/sda1', 1)
178 'mount', '-t', 'xfs', '-o',
180 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
181 assert expected
== fake_run
.calls
[0]['args'][0]
183 def test_multiple_comma_whitespace_options_are_used(self
, conf_ceph_stub
, fake_run
):
184 conf_ceph_stub(dedent("""[global]
187 osd mount options xfs = rw, auto, exec"""))
188 conf
.cluster
= 'ceph'
189 prepare
.mount_osd('/dev/sda1', 1)
191 'mount', '-t', 'xfs', '-o',
193 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
194 assert expected
== fake_run
.calls
[0]['args'][0]
196 def test_underscore_mount_options_are_used(self
, conf_ceph_stub
, fake_run
):
197 conf_ceph_stub(dedent("""[global]
200 osd mount options xfs = rw"""))
201 conf
.cluster
= 'ceph'
202 prepare
.mount_osd('/dev/sda1', 1)
204 'mount', '-t', 'xfs', '-o',
206 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
207 assert expected
== fake_run
.calls
[0]['args'][0]
210 ceph_conf_mount_values
= [
211 ['rw,', 'auto,' 'exec'],
212 ['rw', 'auto', 'exec'],
213 [' rw ', ' auto ', ' exec '],
214 ['rw,', 'auto,', 'exec,'],
215 [',rw ', ',auto ', ',exec,'],
216 [',rw,', ',auto,', ',exec,'],
219 string_mount_values
= [
232 class TestNormalizeFlags(object):
233 # a bit overkill since most of this is already tested in prepare.mount_osd
236 @pytest.mark
.parametrize("flags", ceph_conf_mount_values
)
237 def test_normalize_lists(self
, flags
):
238 result
= sorted(prepare
._normalize
_mount
_flags
(flags
).split(','))
239 assert ','.join(result
) == 'auto,exec,rw'
241 @pytest.mark
.parametrize("flags", string_mount_values
)
242 def test_normalize_strings(self
, flags
):
243 result
= sorted(prepare
._normalize
_mount
_flags
(flags
).split(','))
244 assert ','.join(result
) == 'auto,exec,rw'
246 @pytest.mark
.parametrize("flags", ceph_conf_mount_values
)
247 def test_normalize_extra_flags(self
, flags
):
248 result
= prepare
._normalize
_mount
_flags
(flags
, extras
=['discard'])
249 assert sorted(result
.split(',')) == ['auto', 'discard', 'exec', 'rw']
251 @pytest.mark
.parametrize("flags", ceph_conf_mount_values
)
252 def test_normalize_duplicate_extra_flags(self
, flags
):
253 result
= prepare
._normalize
_mount
_flags
(flags
, extras
=['rw', 'discard'])
254 assert sorted(result
.split(',')) == ['auto', 'discard', 'exec', 'rw']
256 @pytest.mark
.parametrize("flags", string_mount_values
)
257 def test_normalize_strings_flags(self
, flags
):
258 result
= sorted(prepare
._normalize
_mount
_flags
(flags
, extras
=['discard']).split(','))
259 assert ','.join(result
) == 'auto,discard,exec,rw'
261 @pytest.mark
.parametrize("flags", string_mount_values
)
262 def test_normalize_strings_duplicate_flags(self
, flags
):
263 result
= sorted(prepare
._normalize
_mount
_flags
(flags
, extras
=['discard','rw']).split(','))
264 assert ','.join(result
) == 'auto,discard,exec,rw'
267 class TestMkfsBluestore(object):
269 def test_non_zero_exit_status(self
, stub_call
, monkeypatch
):
270 conf
.cluster
= 'ceph'
271 monkeypatch
.setattr('ceph_volume.util.prepare.system.chown', lambda x
: True)
272 stub_call(([], [], 1))
273 with pytest
.raises(RuntimeError) as error
:
274 prepare
.osd_mkfs_bluestore('1', 'asdf-1234', keyring
='keyring')
275 assert "Command failed with exit code 1" in str(error
)
277 def test_non_zero_exit_formats_command_correctly(self
, stub_call
, monkeypatch
):
278 conf
.cluster
= 'ceph'
279 monkeypatch
.setattr('ceph_volume.util.prepare.system.chown', lambda x
: True)
280 stub_call(([], [], 1))
281 with pytest
.raises(RuntimeError) as error
:
282 prepare
.osd_mkfs_bluestore('1', 'asdf-1234', keyring
='keyring')
283 expected
= ' '.join([
287 '--osd-objectstore', 'bluestore', '--mkfs',
288 '-i', '1', '--monmap', '/var/lib/ceph/osd/ceph-1/activate.monmap',
289 '--keyfile', '-', '--osd-data', '/var/lib/ceph/osd/ceph-1/',
290 '--osd-uuid', 'asdf-1234',
291 '--setuser', 'ceph', '--setgroup', 'ceph'])
292 assert expected
in str(error
)