summaryrefslogtreecommitdiffstats
path: root/src/cephadm/tests/test_nfs.py
blob: 0649ef934c1699cb8e55c9ed823bcd393aba3c4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from unittest import mock
import json

import pytest

from tests.fixtures import with_cephadm_ctx, cephadm_fs, import_cephadm

_cephadm = import_cephadm()


SAMPLE_UUID = "2d018a3f-8a8f-4cb9-a7cf-48bebb2cbaae"


def good_nfs_json():
    return nfs_json(
        pool=True,
        files=True,
    )


def nfs_json(**kwargs):
    result = {}
    if kwargs.get("pool"):
        result["pool"] = "party"
    if kwargs.get("files"):
        result["files"] = {
            "ganesha.conf": "",
        }
    if kwargs.get("rgw_content"):
        result["rgw"] = dict(kwargs["rgw_content"])
    elif kwargs.get("rgw"):
        result["rgw"] = {
            "keyring": "foobar",
            "user": "jsmith",
        }
    return result


@pytest.mark.parametrize(
    "args,kwargs",
    # args: <fsid>, <daemon_id>, <config_json>; kwargs: <image>
    [
        # fail due to: invalid fsid
        (["foobar", "fred", good_nfs_json()], {}),
        # fail due to: invalid daemon_id
        ([SAMPLE_UUID, "", good_nfs_json()], {}),
        # fail due to: invalid image
        (
            [SAMPLE_UUID, "fred", good_nfs_json()],
            {"image": ""},
        ),
        # fail due to: no files in config_json
        (
            [
                SAMPLE_UUID,
                "fred",
                nfs_json(pool=True),
            ],
            {},
        ),
        # fail due to: no pool in config_json
        (
            [
                SAMPLE_UUID,
                "fred",
                nfs_json(files=True),
            ],
            {},
        ),
        # fail due to: bad rgw content
        (
            [
                SAMPLE_UUID,
                "fred",
                nfs_json(pool=True, files=True, rgw_content={"foo": True}),
            ],
            {},
        ),
        # fail due to: rgw keyring given but no user
        (
            [
                SAMPLE_UUID,
                "fred",
                nfs_json(
                    pool=True, files=True, rgw_content={"keyring": "foo"}
                ),
            ],
            {},
        ),
    ],
)
def test_nfsganesha_validation_errors(args, kwargs):
    with pytest.raises(_cephadm.Error):
        with with_cephadm_ctx([]) as ctx:
            _cephadm.NFSGanesha(ctx, *args, **kwargs)


def test_nfsganesha_init():
    with with_cephadm_ctx([]) as ctx:
        ctx.config_json = json.dumps(good_nfs_json())
        ctx.image = "test_image"
        nfsg = _cephadm.NFSGanesha.init(
            ctx,
            SAMPLE_UUID,
            "fred",
        )
    assert nfsg.fsid == SAMPLE_UUID
    assert nfsg.daemon_id == "fred"
    assert nfsg.pool == "party"


def test_nfsganesha_container_mounts():
    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            good_nfs_json(),
        )
        cmounts = nfsg.get_container_mounts("/var/tmp")
        assert len(cmounts) == 3
        assert cmounts["/var/tmp/config"] == "/etc/ceph/ceph.conf:z"
        assert cmounts["/var/tmp/keyring"] == "/etc/ceph/keyring:z"
        assert cmounts["/var/tmp/etc/ganesha"] == "/etc/ganesha:z"

    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            nfs_json(pool=True, files=True, rgw=True),
        )
        cmounts = nfsg.get_container_mounts("/var/tmp")
        assert len(cmounts) == 4
        assert cmounts["/var/tmp/config"] == "/etc/ceph/ceph.conf:z"
        assert cmounts["/var/tmp/keyring"] == "/etc/ceph/keyring:z"
        assert cmounts["/var/tmp/etc/ganesha"] == "/etc/ganesha:z"
        assert (
            cmounts["/var/tmp/keyring.rgw"]
            == "/var/lib/ceph/radosgw/ceph-jsmith/keyring:z"
        )


def test_nfsganesha_container_envs():
    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            good_nfs_json(),
        )
        envs = nfsg.get_container_envs()
        assert len(envs) == 1
        assert envs[0] == "CEPH_CONF=/etc/ceph/ceph.conf"


def test_nfsganesha_get_version():
    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            good_nfs_json(),
        )

        with mock.patch("cephadm.call") as _call:
            _call.return_value = ("NFS-Ganesha Release = V100", "", 0)
            ver = nfsg.get_version(ctx, "fake_version")
            _call.assert_called()
        assert ver == "100"


def test_nfsganesha_get_daemon_name():
    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            good_nfs_json(),
        )
        assert nfsg.get_daemon_name() == "nfs.fred"


def test_nfsganesha_get_container_name():
    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            good_nfs_json(),
        )
        name1 = nfsg.get_container_name()
        assert name1 == "ceph-2d018a3f-8a8f-4cb9-a7cf-48bebb2cbaae-nfs.fred"
        name2 = nfsg.get_container_name(desc="extra")
        assert (
            name2 == "ceph-2d018a3f-8a8f-4cb9-a7cf-48bebb2cbaae-nfs.fred-extra"
        )


def test_nfsganesha_get_daemon_args():
    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            good_nfs_json(),
        )
        args = nfsg.get_daemon_args()
        assert args == ["-F", "-L", "STDERR"]


@mock.patch("cephadm.logger")
def test_nfsganesha_create_daemon_dirs(_logger, cephadm_fs):
    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            good_nfs_json(),
        )
        with pytest.raises(OSError):
            nfsg.create_daemon_dirs("/var/tmp", 45, 54)
        cephadm_fs.create_dir("/var/tmp")
        nfsg.create_daemon_dirs("/var/tmp", 45, 54)
        # TODO: make assertions about the dirs created


@mock.patch("cephadm.logger")
def test_nfsganesha_create_daemon_dirs_rgw(_logger, cephadm_fs):
    with with_cephadm_ctx([]) as ctx:
        nfsg = _cephadm.NFSGanesha(
            ctx,
            SAMPLE_UUID,
            "fred",
            nfs_json(pool=True, files=True, rgw=True),
        )
        cephadm_fs.create_dir("/var/tmp")
        nfsg.create_daemon_dirs("/var/tmp", 45, 54)
        # TODO: make assertions about the dirs created