]> git.proxmox.com Git - ceph.git/blame - ceph/src/ceph-volume/ceph_volume/tests/util/test_prepare.py
update sources to 12.2.7
[ceph.git] / ceph / src / ceph-volume / ceph_volume / tests / util / test_prepare.py
CommitLineData
b32b8144
FG
1import pytest
2from textwrap import dedent
3import json
4from ceph_volume.util import prepare
5from ceph_volume.util.prepare import system
6from ceph_volume import conf
7from ceph_volume.tests.conftest import Factory
8
9
10class TestCheckID(object):
11
12 def test_false_if_id_is_none(self):
13 assert not prepare.check_id(None)
14
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):
18 prepare.check_id(1)
19
20 def test_id_does_exist(self, monkeypatch):
21 stdout = dict(nodes=[
22 dict(id=0),
23 ])
24 stdout = ['', json.dumps(stdout)]
25 monkeypatch.setattr('ceph_volume.process.call', lambda *a, **kw: (stdout, '', 0))
26 result = prepare.check_id(0)
27 assert result
28
29 def test_id_does_not_exist(self, monkeypatch):
30 stdout = dict(nodes=[
31 dict(id=0),
32 ])
33 stdout = ['', json.dumps(stdout)]
34 monkeypatch.setattr('ceph_volume.process.call', lambda *a, **kw: (stdout, '', 0))
35 result = prepare.check_id(1)
36 assert not result
37
38 def test_invalid_osd_id(self, monkeypatch):
39 stdout = dict(nodes=[
40 dict(id=0),
41 ])
42 stdout = ['', json.dumps(stdout)]
43 monkeypatch.setattr('ceph_volume.process.call', lambda *a, **kw: (stdout, '', 0))
44 result = prepare.check_id("foo")
45 assert not result
46
47
48class TestFormatDevice(object):
49
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]
54 assert '-f' in flags
55
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'
61
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
67
68 def test_default_options(self, conf_ceph_stub, fake_run):
69 conf_ceph_stub(dedent("""[global]
70 fsid = 1234lkjh1234"""))
71 conf.cluster = 'ceph'
72 prepare.format_device('/dev/sda1')
73 expected = [
74 'mkfs', '-t', 'xfs',
75 '-f', '-i', 'size=2048', # default flags
76 '/dev/sda1']
77 assert expected == fake_run.calls[0]['args'][0]
78
79 def test_multiple_options_are_used(self, conf_ceph_stub, fake_run):
80 conf_ceph_stub(dedent("""[global]
81 fsid = 1234lkjh1234
82 [osd]
83 osd mkfs options xfs = -f -i size=1024"""))
84 conf.cluster = 'ceph'
85 prepare.format_device('/dev/sda1')
86 expected = [
87 'mkfs', '-t', 'xfs',
88 '-f', '-i', 'size=1024',
89 '/dev/sda1']
90 assert expected == fake_run.calls[0]['args'][0]
91
92 def test_multiple_options_will_get_the_force_flag(self, conf_ceph_stub, fake_run):
93 conf_ceph_stub(dedent("""[global]
94 fsid = 1234lkjh1234
95 [osd]
96 osd mkfs options xfs = -i size=1024"""))
97 conf.cluster = 'ceph'
98 prepare.format_device('/dev/sda1')
99 expected = [
100 'mkfs', '-t', 'xfs',
101 '-f', '-i', 'size=1024',
102 '/dev/sda1']
103 assert expected == fake_run.calls[0]['args'][0]
104
105 def test_underscore_options_are_used(self, conf_ceph_stub, fake_run):
106 conf_ceph_stub(dedent("""[global]
107 fsid = 1234lkjh1234
108 [osd]
109 osd_mkfs_options_xfs = -i size=128"""))
110 conf.cluster = 'ceph'
111 prepare.format_device('/dev/sda1')
112 expected = [
113 'mkfs', '-t', 'xfs',
114 '-f', '-i', 'size=128',
115 '/dev/sda1']
116 assert expected == fake_run.calls[0]['args'][0]
117
118
119class TestOsdMkfsBluestore(object):
120
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]
125
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]
130
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]
136
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]
142
143
144class TestMountOSD(object):
145
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)
151 expected = [
152 'mount', '-t', 'xfs', '-o',
3a9019d9 153 'rw,noatime,inode64', # default flags
b32b8144
FG
154 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
155 assert expected == fake_run.calls[0]['args'][0]
156
157 def test_mount_options_are_used(self, conf_ceph_stub, fake_run):
158 conf_ceph_stub(dedent("""[global]
159 fsid = 1234lkjh1234
160 [osd]
161 osd mount options xfs = rw"""))
162 conf.cluster = 'ceph'
163 prepare.mount_osd('/dev/sda1', 1)
164 expected = [
165 'mount', '-t', 'xfs', '-o',
166 'rw',
167 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
168 assert expected == fake_run.calls[0]['args'][0]
169
3a9019d9 170 def test_multiple_whitespace_options_are_used(self, conf_ceph_stub, fake_run):
b32b8144
FG
171 conf_ceph_stub(dedent("""[global]
172 fsid = 1234lkjh1234
173 [osd]
174 osd mount options xfs = rw auto exec"""))
175 conf.cluster = 'ceph'
176 prepare.mount_osd('/dev/sda1', 1)
177 expected = [
178 'mount', '-t', 'xfs', '-o',
3a9019d9
FG
179 'rw,auto,exec',
180 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
181 assert expected == fake_run.calls[0]['args'][0]
182
183 def test_multiple_comma_whitespace_options_are_used(self, conf_ceph_stub, fake_run):
184 conf_ceph_stub(dedent("""[global]
185 fsid = 1234lkjh1234
186 [osd]
187 osd mount options xfs = rw, auto, exec"""))
188 conf.cluster = 'ceph'
189 prepare.mount_osd('/dev/sda1', 1)
190 expected = [
191 'mount', '-t', 'xfs', '-o',
192 'rw,auto,exec',
b32b8144
FG
193 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
194 assert expected == fake_run.calls[0]['args'][0]
195
196 def test_underscore_mount_options_are_used(self, conf_ceph_stub, fake_run):
197 conf_ceph_stub(dedent("""[global]
198 fsid = 1234lkjh1234
199 [osd]
200 osd mount options xfs = rw"""))
201 conf.cluster = 'ceph'
202 prepare.mount_osd('/dev/sda1', 1)
203 expected = [
204 'mount', '-t', 'xfs', '-o',
205 'rw',
206 '/dev/sda1', '/var/lib/ceph/osd/ceph-1']
207 assert expected == fake_run.calls[0]['args'][0]
3a9019d9
FG
208
209
210ceph_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,'],
217]
218
219string_mount_values = [
220 'rw, auto exec ',
221 'rw auto exec',
222 ',rw, auto, exec,',
223 ' rw auto exec ',
224 ' rw,auto,exec ',
225 'rw,auto,exec',
226 ',rw,auto,exec,',
227 'rw,auto,exec ',
228 'rw, auto, exec ',
229]
230
231
232class TestNormalizeFlags(object):
233 # a bit overkill since most of this is already tested in prepare.mount_osd
234 # tests
235
236 @pytest.mark.parametrize("flags", ceph_conf_mount_values)
237 def test_normalize_lists(self, flags):
94b18763
FG
238 result = sorted(prepare._normalize_mount_flags(flags).split(','))
239 assert ','.join(result) == 'auto,exec,rw'
3a9019d9
FG
240
241 @pytest.mark.parametrize("flags", string_mount_values)
242 def test_normalize_strings(self, flags):
94b18763
FG
243 result = sorted(prepare._normalize_mount_flags(flags).split(','))
244 assert ','.join(result) == 'auto,exec,rw'
245
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']
250
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']
255
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'
260
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'
28e407b8
AA
265
266
267class TestMkfsBluestore(object):
268
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)
276
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([
284 'ceph-osd',
285 '--cluster',
286 'ceph',
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)