]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/tests/util/test_arg_validators.py
update sources to 12.2.8
[ceph.git] / ceph / src / ceph-volume / ceph_volume / tests / util / test_arg_validators.py
1 import argparse
2 import pytest
3 from ceph_volume import exceptions
4 from ceph_volume.util import arg_validators
5
6
7 invalid_lv_paths = [
8 '', 'lv_name', '/lv_name', 'lv_name/',
9 '/dev/lv_group/lv_name'
10 ]
11
12
13 class TestLVPath(object):
14
15 def setup(self):
16 self.validator = arg_validators.LVPath()
17
18 @pytest.mark.parametrize('path', invalid_lv_paths)
19 def test_no_slash_is_an_error(self, path):
20 with pytest.raises(argparse.ArgumentError):
21 self.validator(path)
22
23 def test_is_valid(self):
24 path = 'vg/lv'
25 assert self.validator(path) == path
26
27 def test_abspath_is_valid(self):
28 path = '/'
29 assert self.validator(path) == path
30
31
32 class TestOSDPath(object):
33
34 def setup(self):
35 self.validator = arg_validators.OSDPath()
36
37 def test_is_not_root(self):
38 with pytest.raises(exceptions.SuperUserError):
39 self.validator('')
40
41 def test_path_is_not_a_directory(self, is_root, tmpfile, monkeypatch):
42 monkeypatch.setattr(arg_validators.disk, 'is_partition', lambda x: False)
43 validator = arg_validators.OSDPath()
44 with pytest.raises(argparse.ArgumentError):
45 validator(tmpfile())
46
47 def test_files_are_missing(self, is_root, tmpdir, monkeypatch):
48 tmppath = str(tmpdir)
49 monkeypatch.setattr(arg_validators.disk, 'is_partition', lambda x: False)
50 validator = arg_validators.OSDPath()
51 with pytest.raises(argparse.ArgumentError) as error:
52 validator(tmppath)
53 assert 'Required file (ceph_fsid) was not found in OSD' in str(error)
54
55
56 class TestExcludeGroupOptions(object):
57
58 def setup(self):
59 self.parser = argparse.ArgumentParser()
60
61 def test_flags_in_one_group(self):
62 argv = ['<prog>', '--filestore', '--bar']
63 filestore_group = self.parser.add_argument_group('filestore')
64 bluestore_group = self.parser.add_argument_group('bluestore')
65 filestore_group.add_argument('--filestore')
66 bluestore_group.add_argument('--bluestore')
67 result = arg_validators.exclude_group_options(
68 self.parser,
69 ['filestore', 'bluestore'],
70 argv=argv
71 )
72 assert result is None
73
74 def test_flags_in_no_group(self):
75 argv = ['<prog>', '--foo', '--bar']
76 filestore_group = self.parser.add_argument_group('filestore')
77 bluestore_group = self.parser.add_argument_group('bluestore')
78 filestore_group.add_argument('--filestore')
79 bluestore_group.add_argument('--bluestore')
80 result = arg_validators.exclude_group_options(
81 self.parser,
82 ['filestore', 'bluestore'],
83 argv=argv
84 )
85 assert result is None
86
87 def test_flags_conflict(self, capsys):
88 argv = ['<prog>', '--filestore', '--bluestore']
89 filestore_group = self.parser.add_argument_group('filestore')
90 bluestore_group = self.parser.add_argument_group('bluestore')
91 filestore_group.add_argument('--filestore')
92 bluestore_group.add_argument('--bluestore')
93
94 arg_validators.exclude_group_options(
95 self.parser, ['filestore', 'bluestore'], argv=argv
96 )
97 stdout, stderr = capsys.readouterr()
98 assert 'Cannot use --filestore (filestore) with --bluestore (bluestore)' in stdout
99
100
101 class TestValidDevice(object):
102
103 def setup(self):
104 self.validator = arg_validators.ValidDevice()
105
106 def test_path_is_valid(self, fake_call):
107 result = self.validator('/')
108 assert result.abspath == '/'
109
110 def test_path_is_invalid(self, fake_call):
111 with pytest.raises(argparse.ArgumentError):
112 self.validator('/device/does/not/exist')