Skip to content

Commit

Permalink
fix parse mount point and fix unit test
Browse files Browse the repository at this point in the history
Signed-off-by: zwwhdls <[email protected]>
  • Loading branch information
zwwhdls committed Oct 12, 2023
1 parent e7b8f16 commit 148743e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
5 changes: 4 additions & 1 deletion pkg/juicefs/mount/builder/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ func (r *PodBuilder) NewMountPod(podName string) *corev1.Pod {

pod.Name = podName
mountCmd := r.genMountCommand()
cmd := mountCmd
initCmd := r.genInitCommand()
cmd := strings.Join([]string{initCmd, mountCmd}, "\n")
if initCmd != "" {
cmd = strings.Join([]string{initCmd, mountCmd}, "\n")
}
pod.Spec.Containers[0].Command = []string{"sh", "-c", cmd}
pod.Spec.Containers[0].Env = []corev1.EnvVar{{
Name: "JFS_FOREGROUND",
Expand Down
8 changes: 4 additions & 4 deletions pkg/juicefs/mount/builder/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func Test_getCacheDirVolumes(t *testing.T) {
cacheVolumes, cacheVolumeMounts := r.genCacheDirVolumes()
volumes = append(volumes, cacheVolumes...)
volumeMounts = append(volumeMounts, cacheVolumeMounts...)
if len(volumes) != 3 || len(volumeMounts) != 3 {
if len(volumes) != 2 || len(volumeMounts) != 2 {
t.Error("getCacheDirVolumes can't work properly")
}

Expand All @@ -197,7 +197,7 @@ func Test_getCacheDirVolumes(t *testing.T) {
cacheVolumes, cacheVolumeMounts = r.genCacheDirVolumes()
volumes = append(volumes, cacheVolumes...)
volumeMounts = append(volumeMounts, cacheVolumeMounts...)
if len(volumes) != 4 || len(volumeMounts) != 4 {
if len(volumes) != 3 || len(volumeMounts) != 3 {
t.Error("getCacheDirVolumes can't work properly")
}

Expand All @@ -206,7 +206,7 @@ func Test_getCacheDirVolumes(t *testing.T) {
cacheVolumes, cacheVolumeMounts = r.genCacheDirVolumes()
volumes = append(volumes, cacheVolumes...)
volumeMounts = append(volumeMounts, cacheVolumeMounts...)
if len(volumes) != 6 || len(volumeMounts) != 6 {
if len(volumes) != 5 || len(volumeMounts) != 5 {
t.Error("getCacheDirVolumes can't work properly")
}

Expand All @@ -215,7 +215,7 @@ func Test_getCacheDirVolumes(t *testing.T) {
cacheVolumes, cacheVolumeMounts = r.genCacheDirVolumes()
volumes = append(volumes, cacheVolumes...)
volumeMounts = append(volumeMounts, cacheVolumeMounts...)
if len(volumes) != 7 || len(volumeMounts) != 7 {
if len(volumes) != 6 || len(volumeMounts) != 6 {
t.Error("getCacheDirVolumes can't work properly")
}
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,15 @@ func GetReferenceKey(target string) string {

// ParseMntPath return mntPath, volumeId (/jfs/volumeId, volumeId err)
func ParseMntPath(cmd string) (string, string, error) {
args := strings.Fields(cmd)
cmds := strings.Split(cmd, "\n")
if len(cmds) > 2 {
return "", "", fmt.Errorf("err cmd:%s", cmd)
}
mountCmd := cmds[0]
if len(cmds) == 2 {
mountCmd = cmds[1]
}
args := strings.Fields(mountCmd)
if len(args) < 3 || !strings.HasPrefix(args[2], config.PodMountBase) {
return "", "", fmt.Errorf("err cmd:%s", cmd)
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,35 @@ func TestParseMntPath(t *testing.T) {
}{
{
name: "get sourcePath from pod cmd success",
args: args{cmd: "/usr/local/bin/juicefs format --storage=s3 --bucket=http://juicefs-bucket.minio.default.svc.cluster.local:9000 --access-key=minioadmin --secret-key=${secretkey} ${metaurl} ce-secret\n/bin/mount.juicefs redis://127.0.0.1/6379 /jfs/pvc-xxx"},
want: "/jfs/pvc-xxx",
want1: "pvc-xxx",
wantErr: false,
},
{
name: "without init cmd",
args: args{cmd: "/bin/mount.juicefs redis://127.0.0.1/6379 /jfs/pvc-xxx"},
want: "/jfs/pvc-xxx",
want1: "pvc-xxx",
wantErr: false,
},
{
name: "err-pod cmd args <3",
args: args{cmd: "/bin/mount.juicefs redis://127.0.0.1/6379"},
args: args{cmd: "/usr/local/bin/juicefs format --storage=s3 --bucket=http://juicefs-bucket.minio.default.svc.cluster.local:9000 --access-key=minioadmin --secret-key=${secretkey} ${metaurl} ce-secret\n/bin/mount.juicefs redis://127.0.0.1/6379"},
want: "",
want1: "",
wantErr: true,
},
{
name: "err-cmd sourcePath no MountBase prefix",
args: args{cmd: "/bin/mount.juicefs redis://127.0.0.1/6379 /err-jfs/pvc-xxx"},
args: args{cmd: "/usr/local/bin/juicefs format --storage=s3 --bucket=http://juicefs-bucket.minio.default.svc.cluster.local:9000 --access-key=minioadmin --secret-key=${secretkey} ${metaurl} ce-secret\n/bin/mount.juicefs redis://127.0.0.1/6379 /err-jfs/pvc-xxx"},
want: "",
want1: "",
wantErr: true,
},
{
name: "err-cmd sourcePath length err",
args: args{cmd: "/bin/mount.juicefs redis://127.0.0.1/6379 /jfs"},
args: args{cmd: "/usr/local/bin/juicefs format --storage=s3 --bucket=http://juicefs-bucket.minio.default.svc.cluster.local:9000 --access-key=minioadmin --secret-key=${secretkey} ${metaurl} ce-secret\n/bin/mount.juicefs redis://127.0.0.1/6379 /jfs"},
want: "",
want1: "",
wantErr: true,
Expand Down

0 comments on commit 148743e

Please sign in to comment.