Skip to content

Commit d30b197

Browse files
authored
Merge pull request #520 from tam7t/tam7t/atomic
feat: use the atomic file writer
2 parents deb60f4 + ddc8feb commit d30b197

File tree

8 files changed

+1930
-230
lines changed

8 files changed

+1930
-230
lines changed

pkg/secrets-store/nodeserver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func TestNodeUnpublishVolume(t *testing.T) {
363363
name: "Success for a mounted volume with a retry",
364364
nodeUnpublishVolReq: csi.NodeUnpublishVolumeRequest{
365365
VolumeId: "testvolid1",
366-
TargetPath: tmpdir.New(t, "", `*\\pods\\fakePod\\volumes\\kubernetes.io~csi\\myvol\\mount`),
366+
TargetPath: tmpdir.New(t, "", `*mount`),
367367
},
368368
mountPoints: []mount.MountPoint{},
369369
shouldRetryUnmount: true,

pkg/secrets-store/provider_client_test.go

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"errors"
2222
"fmt"
2323
"os"
24-
"path/filepath"
2524
"reflect"
25+
"runtime"
2626
"sync"
2727
"testing"
2828
"time"
@@ -33,6 +33,7 @@ import (
3333
"google.golang.org/grpc/status"
3434

3535
"sigs.k8s.io/secrets-store-csi-driver/pkg/test_utils/tmpdir"
36+
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/fileutil"
3637
"sigs.k8s.io/secrets-store-csi-driver/provider/fake"
3738
"sigs.k8s.io/secrets-store-csi-driver/provider/v1alpha1"
3839
)
@@ -64,6 +65,7 @@ func TestMountContent(t *testing.T) {
6465
files []*v1alpha1.File
6566
// expectations
6667
expectedFiles map[string]os.FileMode
68+
skipon string
6769
}{
6870
{
6971
name: "provider successful response (no files)",
@@ -77,13 +79,13 @@ func TestMountContent(t *testing.T) {
7779
files: []*v1alpha1.File{
7880
{
7981
Path: "foo",
80-
Mode: 0644,
82+
Mode: 0666,
8183
Contents: []byte("foo"),
8284
},
8385
},
8486
objectVersions: map[string]string{"foo": "v1"},
8587
expectedFiles: map[string]os.FileMode{
86-
"foo": 0644,
88+
"foo": 0666,
8789
},
8890
},
8991
{
@@ -92,25 +94,87 @@ func TestMountContent(t *testing.T) {
9294
files: []*v1alpha1.File{
9395
{
9496
Path: "foo",
95-
Mode: 0644,
97+
Mode: 0666,
9698
Contents: []byte("foo"),
9799
},
98100
{
99101
Path: "bar",
102+
Mode: 0444,
103+
Contents: []byte("bar"),
104+
},
105+
},
106+
objectVersions: map[string]string{"foo": "v1"},
107+
expectedFiles: map[string]os.FileMode{
108+
"foo": 0666,
109+
"bar": 0444,
110+
},
111+
},
112+
{
113+
name: "provider response with nested files (linux)",
114+
permission: "777",
115+
files: []*v1alpha1.File{
116+
{
117+
Path: "foo",
118+
Mode: 0644,
119+
Contents: []byte("foo"),
120+
},
121+
{
122+
Path: "baz/bar",
100123
Mode: 0777,
101124
Contents: []byte("bar"),
102125
},
126+
{
127+
Path: "baz/qux",
128+
Mode: 0777,
129+
Contents: []byte("qux"),
130+
},
103131
},
104132
objectVersions: map[string]string{"foo": "v1"},
105133
expectedFiles: map[string]os.FileMode{
106-
"foo": 0644,
107-
"bar": 0777,
134+
"foo": 0644,
135+
"baz/bar": 0777,
136+
"baz/qux": 0777,
137+
},
138+
skipon: "windows",
139+
},
140+
{
141+
// note: this is a bit weird because the path `baz\bar` on windows
142+
// should be a file `bar` nested in a folder `baz`. it _actually_
143+
// works on linux though because the `\` character is just treated
144+
// as part of the filename.
145+
name: "provider response with nested files (windows)",
146+
permission: "777",
147+
files: []*v1alpha1.File{
148+
{
149+
Path: "foo",
150+
Mode: 0444,
151+
Contents: []byte("foo"),
152+
},
153+
{
154+
Path: "baz\\bar",
155+
Mode: 0444,
156+
Contents: []byte("bar"),
157+
},
158+
{
159+
Path: "baz\\qux",
160+
Mode: 0666,
161+
Contents: []byte("qux"),
162+
},
163+
},
164+
objectVersions: map[string]string{"foo": "v1"},
165+
expectedFiles: map[string]os.FileMode{
166+
"foo": 0444,
167+
"baz\\bar": 0444,
168+
"baz\\qux": 0666,
108169
},
109170
},
110171
}
111172

112173
for _, test := range cases {
113174
t.Run(test.name, func(t *testing.T) {
175+
if test.skipon == runtime.GOOS {
176+
t.SkipNow()
177+
}
114178
socketPath := tmpdir.New(t, "", "ut")
115179
targetPath := tmpdir.New(t, "", "ut")
116180

@@ -140,18 +204,17 @@ func TestMountContent(t *testing.T) {
140204

141205
// check that file was written
142206
gotFiles := make(map[string]os.FileMode)
143-
filepath.Walk(targetPath, func(path string, info os.FileInfo, err error) error {
144-
// skip mount folder
145-
if path == targetPath {
146-
return nil
147-
}
148-
rel, err := filepath.Rel(targetPath, path)
207+
paths, err := fileutil.GetMountedFiles(targetPath)
208+
if err != nil {
209+
t.Fatalf("unable to read mounted files: %s", err)
210+
}
211+
for rel, abs := range paths {
212+
info, err := os.Lstat(abs)
149213
if err != nil {
150-
return err
214+
t.Fatalf("unable to read mounted files: %s", err)
151215
}
152216
gotFiles[rel] = info.Mode()
153-
return nil
154-
})
217+
}
155218

156219
if diff := cmp.Diff(test.expectedFiles, gotFiles); diff != "" {
157220
t.Errorf("MountContent() file mismatch (-want +got):\n%s", diff)

0 commit comments

Comments
 (0)