2023-11-07 10:03:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2023 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cephfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
|
|
|
|
2023-12-18 11:04:31 +00:00
|
|
|
cephcsi "github.com/ceph/ceph-csi/api/deploy/kubernetes"
|
2023-11-07 10:03:30 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/cephfs/mounter"
|
|
|
|
"github.com/ceph/ceph-csi/internal/cephfs/store"
|
2023-11-17 06:29:00 +00:00
|
|
|
csicommon "github.com/ceph/ceph-csi/internal/csi-common"
|
2023-11-07 10:03:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_setMountOptions(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cliKernelMountOptions := "noexec,nodev"
|
|
|
|
cliFuseMountOptions := "default_permissions,auto_cache"
|
|
|
|
|
|
|
|
configKernelMountOptions := "crc"
|
|
|
|
configFuseMountOptions := "allow_other"
|
|
|
|
|
2023-12-18 11:04:31 +00:00
|
|
|
csiConfig := []cephcsi.ClusterInfo{
|
2023-11-07 10:03:30 +00:00
|
|
|
{
|
|
|
|
ClusterID: "cluster-1",
|
2023-12-18 11:04:31 +00:00
|
|
|
CephFS: cephcsi.CephFS{
|
2023-11-07 10:03:30 +00:00
|
|
|
KernelMountOptions: configKernelMountOptions,
|
|
|
|
FuseMountOptions: configFuseMountOptions,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ClusterID: "cluster-2",
|
2023-12-18 11:04:31 +00:00
|
|
|
CephFS: cephcsi.CephFS{
|
2023-11-07 10:03:30 +00:00
|
|
|
KernelMountOptions: "",
|
|
|
|
FuseMountOptions: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
csiConfigFileContent, err := json.Marshal(csiConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to marshal csi config info %v", err)
|
|
|
|
}
|
|
|
|
tmpConfPath := t.TempDir() + "/ceph-csi.json"
|
|
|
|
t.Logf("path = %s", tmpConfPath)
|
|
|
|
err = os.WriteFile(tmpConfPath, csiConfigFileContent, 0o600)
|
|
|
|
if err != nil {
|
2023-11-17 06:29:00 +00:00
|
|
|
t.Errorf("failed to write %s file content: %v", tmpConfPath, err)
|
2023-11-07 10:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-11-17 06:29:00 +00:00
|
|
|
ns *NodeServer
|
2023-11-07 10:03:30 +00:00
|
|
|
mnt mounter.VolumeMounter
|
|
|
|
volOptions *store.VolumeOptions
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "KernelMountOptions set in cluster-1 config and not set in CLI",
|
2023-11-17 06:29:00 +00:00
|
|
|
ns: &NodeServer{},
|
2024-01-16 14:58:22 +00:00
|
|
|
mnt: mounter.VolumeMounter(mounter.NewKernelMounter()),
|
2023-11-07 10:03:30 +00:00
|
|
|
volOptions: &store.VolumeOptions{
|
|
|
|
ClusterID: "cluster-1",
|
|
|
|
},
|
|
|
|
want: configKernelMountOptions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FuseMountOptions set in cluster-1 config and not set in CLI",
|
2023-11-17 06:29:00 +00:00
|
|
|
ns: &NodeServer{},
|
2023-11-07 10:03:30 +00:00
|
|
|
mnt: mounter.VolumeMounter(&mounter.FuseMounter{}),
|
|
|
|
volOptions: &store.VolumeOptions{
|
|
|
|
ClusterID: "cluster-1",
|
|
|
|
},
|
|
|
|
want: configFuseMountOptions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "KernelMountOptions set in cluster-1 config and set in CLI",
|
2023-11-17 06:29:00 +00:00
|
|
|
ns: &NodeServer{
|
2023-11-07 10:03:30 +00:00
|
|
|
kernelMountOptions: cliKernelMountOptions,
|
|
|
|
},
|
2024-01-16 14:58:22 +00:00
|
|
|
mnt: mounter.VolumeMounter(mounter.NewKernelMounter()),
|
2023-11-07 10:03:30 +00:00
|
|
|
volOptions: &store.VolumeOptions{
|
|
|
|
ClusterID: "cluster-1",
|
|
|
|
},
|
|
|
|
want: configKernelMountOptions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FuseMountOptions not set in cluster-2 config and set in CLI",
|
2023-11-17 06:29:00 +00:00
|
|
|
ns: &NodeServer{
|
2023-11-07 10:03:30 +00:00
|
|
|
fuseMountOptions: cliFuseMountOptions,
|
|
|
|
},
|
|
|
|
mnt: mounter.VolumeMounter(&mounter.FuseMounter{}),
|
|
|
|
volOptions: &store.VolumeOptions{
|
|
|
|
ClusterID: "cluster-1",
|
|
|
|
},
|
|
|
|
want: configFuseMountOptions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "KernelMountOptions not set in cluster-2 config and set in CLI",
|
2023-11-17 06:29:00 +00:00
|
|
|
ns: &NodeServer{
|
2023-11-07 10:03:30 +00:00
|
|
|
kernelMountOptions: cliKernelMountOptions,
|
|
|
|
},
|
2024-01-16 14:58:22 +00:00
|
|
|
mnt: mounter.VolumeMounter(mounter.NewKernelMounter()),
|
2023-11-07 10:03:30 +00:00
|
|
|
volOptions: &store.VolumeOptions{
|
|
|
|
ClusterID: "cluster-2",
|
|
|
|
},
|
|
|
|
want: cliKernelMountOptions,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FuseMountOptions not set in cluster-1 config and set in CLI",
|
2023-11-17 06:29:00 +00:00
|
|
|
ns: &NodeServer{
|
2023-11-07 10:03:30 +00:00
|
|
|
fuseMountOptions: cliFuseMountOptions,
|
|
|
|
},
|
|
|
|
mnt: mounter.VolumeMounter(&mounter.FuseMounter{}),
|
|
|
|
volOptions: &store.VolumeOptions{
|
|
|
|
ClusterID: "cluster-2",
|
|
|
|
},
|
|
|
|
want: cliFuseMountOptions,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
volCap := &csi.VolumeCapability{
|
|
|
|
AccessMode: &csi.VolumeCapability_AccessMode{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2024-04-26 08:35:01 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-11-07 10:03:30 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-11-17 06:29:00 +00:00
|
|
|
driver := &csicommon.CSIDriver{}
|
2024-04-26 08:35:01 +00:00
|
|
|
tt.ns.DefaultNodeServer = csicommon.NewDefaultNodeServer(
|
2023-11-17 06:29:00 +00:00
|
|
|
driver, "cephfs", "", map[string]string{}, map[string]string{},
|
|
|
|
)
|
|
|
|
|
2024-04-26 08:35:01 +00:00
|
|
|
err := tt.ns.setMountOptions(tt.mnt, tt.volOptions, volCap, tmpConfPath)
|
2023-11-07 10:03:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("setMountOptions() = %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-26 08:35:01 +00:00
|
|
|
switch tt.mnt.(type) {
|
2023-11-07 10:03:30 +00:00
|
|
|
case *mounter.FuseMounter:
|
2024-04-26 08:35:01 +00:00
|
|
|
if !strings.Contains(tt.volOptions.FuseMountOptions, tt.want) {
|
|
|
|
t.Errorf("Set FuseMountOptions = %v Required FuseMountOptions = %v", tt.volOptions.FuseMountOptions, tt.want)
|
2023-11-07 10:03:30 +00:00
|
|
|
}
|
2024-01-16 14:58:22 +00:00
|
|
|
case mounter.KernelMounter:
|
2024-04-26 08:35:01 +00:00
|
|
|
if !strings.Contains(tt.volOptions.KernelMountOptions, tt.want) {
|
|
|
|
t.Errorf("Set KernelMountOptions = %v Required KernelMountOptions = %v", tt.volOptions.KernelMountOptions, tt.want)
|
2023-11-07 10:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|