]> git.proxmox.com Git - ceph.git/blame - ceph/src/cephadm/tests/test_nfs.py
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / cephadm / tests / test_nfs.py
CommitLineData
1e59de90
TL
1from unittest import mock
2import json
3
4import pytest
5
6from tests.fixtures import with_cephadm_ctx, cephadm_fs, import_cephadm
7
8_cephadm = import_cephadm()
9
10
11SAMPLE_UUID = "2d018a3f-8a8f-4cb9-a7cf-48bebb2cbaae"
12
13
14def good_nfs_json():
15 return nfs_json(
16 pool=True,
17 files=True,
18 )
19
20
21def nfs_json(**kwargs):
22 result = {}
23 if kwargs.get("pool"):
24 result["pool"] = "party"
25 if kwargs.get("files"):
26 result["files"] = {
27 "ganesha.conf": "",
28 }
29 if kwargs.get("rgw_content"):
30 result["rgw"] = dict(kwargs["rgw_content"])
31 elif kwargs.get("rgw"):
32 result["rgw"] = {
33 "keyring": "foobar",
34 "user": "jsmith",
35 }
36 return result
37
38
39@pytest.mark.parametrize(
40 "args,kwargs",
41 # args: <fsid>, <daemon_id>, <config_json>; kwargs: <image>
42 [
43 # fail due to: invalid fsid
44 (["foobar", "fred", good_nfs_json()], {}),
45 # fail due to: invalid daemon_id
46 ([SAMPLE_UUID, "", good_nfs_json()], {}),
47 # fail due to: invalid image
48 (
49 [SAMPLE_UUID, "fred", good_nfs_json()],
50 {"image": ""},
51 ),
52 # fail due to: no files in config_json
53 (
54 [
55 SAMPLE_UUID,
56 "fred",
57 nfs_json(pool=True),
58 ],
59 {},
60 ),
61 # fail due to: no pool in config_json
62 (
63 [
64 SAMPLE_UUID,
65 "fred",
66 nfs_json(files=True),
67 ],
68 {},
69 ),
70 # fail due to: bad rgw content
71 (
72 [
73 SAMPLE_UUID,
74 "fred",
75 nfs_json(pool=True, files=True, rgw_content={"foo": True}),
76 ],
77 {},
78 ),
79 # fail due to: rgw keyring given but no user
80 (
81 [
82 SAMPLE_UUID,
83 "fred",
84 nfs_json(
85 pool=True, files=True, rgw_content={"keyring": "foo"}
86 ),
87 ],
88 {},
89 ),
90 ],
91)
92def test_nfsganesha_validation_errors(args, kwargs):
93 with pytest.raises(_cephadm.Error):
94 with with_cephadm_ctx([]) as ctx:
95 _cephadm.NFSGanesha(ctx, *args, **kwargs)
96
97
98def test_nfsganesha_init():
99 with with_cephadm_ctx([]) as ctx:
100 ctx.config_json = json.dumps(good_nfs_json())
101 ctx.image = "test_image"
102 nfsg = _cephadm.NFSGanesha.init(
103 ctx,
104 SAMPLE_UUID,
105 "fred",
106 )
107 assert nfsg.fsid == SAMPLE_UUID
108 assert nfsg.daemon_id == "fred"
109 assert nfsg.pool == "party"
110
111
112def test_nfsganesha_container_mounts():
113 with with_cephadm_ctx([]) as ctx:
114 nfsg = _cephadm.NFSGanesha(
115 ctx,
116 SAMPLE_UUID,
117 "fred",
118 good_nfs_json(),
119 )
120 cmounts = nfsg.get_container_mounts("/var/tmp")
121 assert len(cmounts) == 3
122 assert cmounts["/var/tmp/config"] == "/etc/ceph/ceph.conf:z"
123 assert cmounts["/var/tmp/keyring"] == "/etc/ceph/keyring:z"
124 assert cmounts["/var/tmp/etc/ganesha"] == "/etc/ganesha:z"
125
126 with with_cephadm_ctx([]) as ctx:
127 nfsg = _cephadm.NFSGanesha(
128 ctx,
129 SAMPLE_UUID,
130 "fred",
131 nfs_json(pool=True, files=True, rgw=True),
132 )
133 cmounts = nfsg.get_container_mounts("/var/tmp")
134 assert len(cmounts) == 4
135 assert cmounts["/var/tmp/config"] == "/etc/ceph/ceph.conf:z"
136 assert cmounts["/var/tmp/keyring"] == "/etc/ceph/keyring:z"
137 assert cmounts["/var/tmp/etc/ganesha"] == "/etc/ganesha:z"
138 assert (
139 cmounts["/var/tmp/keyring.rgw"]
140 == "/var/lib/ceph/radosgw/ceph-jsmith/keyring:z"
141 )
142
143
144def test_nfsganesha_container_envs():
145 with with_cephadm_ctx([]) as ctx:
146 nfsg = _cephadm.NFSGanesha(
147 ctx,
148 SAMPLE_UUID,
149 "fred",
150 good_nfs_json(),
151 )
152 envs = nfsg.get_container_envs()
153 assert len(envs) == 1
154 assert envs[0] == "CEPH_CONF=/etc/ceph/ceph.conf"
155
156
157def test_nfsganesha_get_version():
158 with with_cephadm_ctx([]) as ctx:
159 nfsg = _cephadm.NFSGanesha(
160 ctx,
161 SAMPLE_UUID,
162 "fred",
163 good_nfs_json(),
164 )
165
166 with mock.patch("cephadm.call") as _call:
167 _call.return_value = ("NFS-Ganesha Release = V100", "", 0)
168 ver = nfsg.get_version(ctx, "fake_version")
169 _call.assert_called()
170 assert ver == "100"
171
172
173def test_nfsganesha_get_daemon_name():
174 with with_cephadm_ctx([]) as ctx:
175 nfsg = _cephadm.NFSGanesha(
176 ctx,
177 SAMPLE_UUID,
178 "fred",
179 good_nfs_json(),
180 )
181 assert nfsg.get_daemon_name() == "nfs.fred"
182
183
184def test_nfsganesha_get_container_name():
185 with with_cephadm_ctx([]) as ctx:
186 nfsg = _cephadm.NFSGanesha(
187 ctx,
188 SAMPLE_UUID,
189 "fred",
190 good_nfs_json(),
191 )
192 name1 = nfsg.get_container_name()
193 assert name1 == "ceph-2d018a3f-8a8f-4cb9-a7cf-48bebb2cbaae-nfs.fred"
194 name2 = nfsg.get_container_name(desc="extra")
195 assert (
196 name2 == "ceph-2d018a3f-8a8f-4cb9-a7cf-48bebb2cbaae-nfs.fred-extra"
197 )
198
199
200def test_nfsganesha_get_daemon_args():
201 with with_cephadm_ctx([]) as ctx:
202 nfsg = _cephadm.NFSGanesha(
203 ctx,
204 SAMPLE_UUID,
205 "fred",
206 good_nfs_json(),
207 )
208 args = nfsg.get_daemon_args()
209 assert args == ["-F", "-L", "STDERR"]
210
211
212@mock.patch("cephadm.logger")
213def test_nfsganesha_create_daemon_dirs(_logger, cephadm_fs):
214 with with_cephadm_ctx([]) as ctx:
215 nfsg = _cephadm.NFSGanesha(
216 ctx,
217 SAMPLE_UUID,
218 "fred",
219 good_nfs_json(),
220 )
221 with pytest.raises(OSError):
222 nfsg.create_daemon_dirs("/var/tmp", 45, 54)
223 cephadm_fs.create_dir("/var/tmp")
224 nfsg.create_daemon_dirs("/var/tmp", 45, 54)
225 # TODO: make assertions about the dirs created
226
227
228@mock.patch("cephadm.logger")
229def test_nfsganesha_create_daemon_dirs_rgw(_logger, cephadm_fs):
230 with with_cephadm_ctx([]) as ctx:
231 nfsg = _cephadm.NFSGanesha(
232 ctx,
233 SAMPLE_UUID,
234 "fred",
235 nfs_json(pool=True, files=True, rgw=True),
236 )
237 cephadm_fs.create_dir("/var/tmp")
238 nfsg.create_daemon_dirs("/var/tmp", 45, 54)
239 # TODO: make assertions about the dirs created