Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Xuhui zhang <[email protected]>
  • Loading branch information
zxh326 committed Oct 11, 2024
1 parent 5a1805f commit 715223b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 21 deletions.
19 changes: 18 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ MountPodPatch:
- name: block-devices
persistentVolumeClaim:
claimName: block-pvc
- cacheDirs:
- type: PVC
name: cache-pvc
- type: HostPath
Path: /tmp
`)
err := os.WriteFile(configPath, testData, 0644)
if err != nil {
Expand All @@ -100,7 +105,7 @@ MountPodPatch:
}
defer GlobalConfig.Reset()
// Check the loaded config
assert.Equal(t, len(GlobalConfig.MountPodPatch), 9)
assert.Equal(t, len(GlobalConfig.MountPodPatch), 10)
assert.Equal(t, GlobalConfig.MountPodPatch[0], MountPodPatch{
CEMountImage: "juicedata/mount:ce-test",
EEMountImage: "juicedata/mount:ee-test",
Expand Down Expand Up @@ -200,6 +205,18 @@ MountPodPatch:
},
},
})
assert.Equal(t, GlobalConfig.MountPodPatch[9], MountPodPatch{
CacheDirs: []MountPatchCacheDir{
{
Type: "PVC",
Name: "cache-pvc",
},
{
Type: "HostPath",
Path: "/tmp",
},
},
})
}

func TestGenMountPodPatch(t *testing.T) {
Expand Down
32 changes: 16 additions & 16 deletions pkg/config/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,23 @@ func genCacheDirs(jfsSetting *JfsSetting, volCtx map[string]string) error {
if volCtx != nil && volCtx[common.CachePVC] != "" {
cachePVCs = strings.Split(strings.TrimSpace(volCtx[common.CachePVC]), ",")
}
if len(jfsSetting.Attr.CacheDirs) > 0 {
if jfsSetting.Attr != nil {
for _, cacheDir := range jfsSetting.Attr.CacheDirs {
if cacheDir.Type == MountPatchCacheDirTypePVC {
cachePVCs = append(cachePVCs, cacheDir.Name)
}
}
}
if len(cachePVCs) > 0 {
for i, pvc := range cachePVCs {
if pvc == "" {
continue
}
volPath := fmt.Sprintf("/var/jfsCache-%d", i)
jfsSetting.CachePVCs = append(jfsSetting.CachePVCs, CachePVC{
PVCName: pvc,
Path: volPath,
})
cacheDirsInContainer = append(cacheDirsInContainer, volPath)
for i, pvc := range cachePVCs {
if pvc == "" {
continue
}
volPath := fmt.Sprintf("/var/jfsCache-%d", i)
jfsSetting.CachePVCs = append(jfsSetting.CachePVCs, CachePVC{
PVCName: pvc,
Path: volPath,
})
cacheDirsInContainer = append(cacheDirsInContainer, volPath)
}

// parse emptydir of cache
Expand Down Expand Up @@ -348,10 +346,12 @@ func genCacheDirs(jfsSetting *JfsSetting, volCtx map[string]string) error {
}
}
// parse hostPath dirs in setting attr
for _, cacheDir := range jfsSetting.Attr.CacheDirs {
if cacheDir.Type == MountPatchCacheDirTypeHostPath {
cacheDirsInContainer = append(cacheDirsInContainer, cacheDir.Path)
jfsSetting.CacheDirs = append(jfsSetting.CacheDirs, cacheDir.Path)
if jfsSetting.Attr != nil {
for _, cacheDir := range jfsSetting.Attr.CacheDirs {
if cacheDir.Type == MountPatchCacheDirTypeHostPath {
cacheDirsInContainer = append(cacheDirsInContainer, cacheDir.Path)
jfsSetting.CacheDirs = append(jfsSetting.CacheDirs, cacheDir.Path)
}
}
}
if len(cacheDirsInContainer) == 0 {
Expand Down
55 changes: 51 additions & 4 deletions pkg/config/setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,29 @@ func Test_genCacheDirs(t *testing.T) {
{
name: "test-cache-pvcs",
args: args{
JfsSetting: JfsSetting{},
volCtx: map[string]string{"juicefs/mount-cache-pvc": "abc,def"},
JfsSetting: JfsSetting{
Attr: &PodAttr{
CacheDirs: []MountPatchCacheDir{
{
Type: "PVC",
Name: "sss",
},
},
},
},
volCtx: map[string]string{"juicefs/mount-cache-pvc": "abc,def"},
},
want: JfsSetting{
CachePVCs: []CachePVC{{PVCName: "abc", Path: "/var/jfsCache-0"}, {PVCName: "def", Path: "/var/jfsCache-1"}},
Options: []string{"cache-dir=/var/jfsCache-0:/var/jfsCache-1"},
CachePVCs: []CachePVC{{PVCName: "abc", Path: "/var/jfsCache-0"}, {PVCName: "def", Path: "/var/jfsCache-1"}, {PVCName: "sss", Path: "/var/jfsCache-2"}},
Options: []string{"cache-dir=/var/jfsCache-0:/var/jfsCache-1:/var/jfsCache-2"},
Attr: &PodAttr{
CacheDirs: []MountPatchCacheDir{
{
Type: "PVC",
Name: "sss",
},
},
},
},
wantErr: false,
},
Expand Down Expand Up @@ -686,6 +703,36 @@ func Test_genCacheDirs(t *testing.T) {
},
wantErr: false,
},
{
name: "test-hostPath-with-pod-attr",
args: args{
JfsSetting: JfsSetting{
Attr: &PodAttr{
CacheDirs: []MountPatchCacheDir{
{
Type: "HostPath",
Path: "/abc",
},
},
},
},
},
want: JfsSetting{
Attr: &PodAttr{
CacheDirs: []MountPatchCacheDir{
{
Type: "HostPath",
Path: "/abc",
},
},
},
CacheDirs: []string{
"/abc",
},
Options: []string{"cache-dir=/abc"},
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 715223b

Please sign in to comment.