Skip to content

Commit 97b3966

Browse files
committed
fix: GetMountedFiles() returns map that key includes sub directory
1 parent f8af03c commit 97b3966

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

pkg/util/fileutil/filesystem.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package fileutil
2020
import (
2121
"fmt"
2222
"os"
23+
"path/filepath"
2324
"regexp"
2425
"strings"
2526
)
@@ -32,7 +33,21 @@ var (
3233
func GetMountedFiles(targetPath string) (map[string]string, error) {
3334
paths := make(map[string]string)
3435
// loop thru all the mounted files
35-
files, err := os.ReadDir(targetPath)
36+
var files []string
37+
err := filepath.Walk(targetPath, func(path string, fi os.FileInfo, err error) error {
38+
if err != nil {
39+
return err
40+
}
41+
if fi.IsDir() {
42+
return nil
43+
}
44+
relpath, err := filepath.Rel(targetPath, path)
45+
if err != nil {
46+
return err
47+
}
48+
files = append(files, relpath)
49+
return nil
50+
})
3651
if err != nil {
3752
return nil, fmt.Errorf("failed to list all files in target path %s, err: %v", targetPath, err)
3853
}
@@ -44,7 +59,7 @@ func GetMountedFiles(targetPath string) (map[string]string, error) {
4459
sep = `\`
4560
}
4661
for _, file := range files {
47-
paths[file.Name()] = targetPath + sep + file.Name()
62+
paths[file] = targetPath + sep + file
4863
}
4964
return paths, nil
5065
}

pkg/util/fileutil/filesystem_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package fileutil
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"testing"
2123

2224
"sigs.k8s.io/secrets-store-csi-driver/pkg/test_utils/tmpdir"
@@ -27,27 +29,56 @@ func TestGetMountedFiles(t *testing.T) {
2729
name string
2830
targetPath func(t *testing.T) string
2931
expectedErr bool
32+
expectedKey string
3033
}{
3134
{
3235
name: "target path not found",
3336
targetPath: func(t *testing.T) string { return "" },
3437
expectedErr: true,
38+
expectedKey: "",
3539
},
3640
{
3741
name: "target path dir found",
3842
targetPath: func(t *testing.T) string {
3943
return tmpdir.New(t, "", "ut")
4044
},
4145
expectedErr: false,
46+
expectedKey: "",
47+
},
48+
{
49+
name: "target path dir/file found",
50+
targetPath: func(t *testing.T) string {
51+
dir := tmpdir.New(t, "", "ut")
52+
os.Create(filepath.Join(dir, "secret.txt"))
53+
return dir
54+
},
55+
expectedErr: false,
56+
expectedKey: "secret.txt",
57+
},
58+
{
59+
name: "target path dir/dir/file found",
60+
targetPath: func(t *testing.T) string {
61+
dir := tmpdir.New(t, "", "ut")
62+
os.MkdirAll(filepath.Join(dir, "subdir"), 0700)
63+
os.Create(filepath.Join(dir, "subdir", "secret.txt"))
64+
return dir
65+
},
66+
expectedErr: false,
67+
expectedKey: "subdir/secret.txt",
4268
},
4369
}
4470

4571
for _, test := range tests {
4672
t.Run(test.name, func(t *testing.T) {
47-
_, err := GetMountedFiles(test.targetPath(t))
73+
got, err := GetMountedFiles(test.targetPath(t))
4874
if test.expectedErr != (err != nil) {
4975
t.Fatalf("expected err: %v, got: %+v", test.expectedErr, err)
5076
}
77+
if !test.expectedErr && test.expectedKey != "" {
78+
if _, ok := got[test.expectedKey]; !ok {
79+
t.Fatalf("expected key not found: %s", test.expectedKey)
80+
}
81+
}
5182
})
5283
}
5384
}

0 commit comments

Comments
 (0)