1
1
package local_test
2
2
3
3
import (
4
- "github.com/aws/aws-sdk-go/aws"
5
- "github.com/aws/aws-sdk-go/service/s3"
6
4
"io/ioutil"
7
5
"os"
6
+ "path/filepath"
7
+ "sort"
8
8
"strings"
9
9
"testing"
10
10
11
+ "github.com/aws/aws-sdk-go/aws"
12
+ "github.com/aws/aws-sdk-go/service/s3"
13
+ "github.com/go-test/deep"
11
14
"github.com/treeverse/lakefs/block"
12
15
"github.com/treeverse/lakefs/block/local"
13
16
"github.com/treeverse/lakefs/testutil"
14
17
)
15
18
16
- func makeAdapter (t * testing.T ) (* local.Adapter , func ()) {
19
+ const testStorageNamespace = "local://test"
20
+
21
+ func makeAdapter (t * testing.T ) * local.Adapter {
17
22
t .Helper ()
18
23
dir , err := ioutil .TempDir ("" , "testing-local-adapter-*" )
19
24
testutil .MustDo (t , "TempDir" , err )
20
25
testutil .MustDo (t , "NewAdapter" , os .MkdirAll (dir , 0700 ))
21
26
a , err := local .NewAdapter (dir )
22
27
testutil .MustDo (t , "NewAdapter" , err )
23
28
24
- return a , func () {
25
- if _ , ok := os .LookupEnv ("GOTEST_KEEP_LOCAL" ); ! ok {
26
- testutil . MustDo ( t , "RemoveAll (cleanup)" , os . RemoveAll ( dir ))
29
+ t . Cleanup ( func () {
30
+ if _ , ok := os .LookupEnv ("GOTEST_KEEP_LOCAL" ); ok {
31
+ return
27
32
}
28
- }
33
+ _ = os .RemoveAll (dir )
34
+ })
35
+ return a
29
36
}
30
37
31
38
func makePointer (path string ) block.ObjectPointer {
32
- return block.ObjectPointer {Identifier : path , StorageNamespace : "local://test/" }
39
+ return block.ObjectPointer {Identifier : path , StorageNamespace : testStorageNamespace }
33
40
}
34
41
35
42
func TestLocalPutExistsGet (t * testing.T ) {
36
- a , cleanup := makeAdapter (t )
37
- defer cleanup ()
43
+ a := makeAdapter (t )
38
44
39
45
cases := []struct {
40
46
name string
@@ -66,8 +72,7 @@ func TestLocalPutExistsGet(t *testing.T) {
66
72
}
67
73
68
74
func TestLocalNotExists (t * testing.T ) {
69
- a , cleanup := makeAdapter (t )
70
- defer cleanup ()
75
+ a := makeAdapter (t )
71
76
72
77
cases := []string {"missing" , "nested/down" , "nested/quite/deeply/and/missing" }
73
78
for _ , c := range cases {
@@ -82,8 +87,7 @@ func TestLocalNotExists(t *testing.T) {
82
87
}
83
88
84
89
func TestLocalMultipartUpload (t * testing.T ) {
85
- a , cleanup := makeAdapter (t )
86
- defer cleanup ()
90
+ a := makeAdapter (t )
87
91
88
92
cases := []struct {
89
93
name string
@@ -125,8 +129,7 @@ func TestLocalMultipartUpload(t *testing.T) {
125
129
}
126
130
127
131
func TestLocalCopy (t * testing.T ) {
128
- a , cleanup := makeAdapter (t )
129
- defer cleanup ()
132
+ a := makeAdapter (t )
130
133
131
134
contents := "foo bar baz quux"
132
135
@@ -141,3 +144,80 @@ func TestLocalCopy(t *testing.T) {
141
144
t .Errorf ("expected to read \" %s\" as written, got \" %s\" " , contents , string (got ))
142
145
}
143
146
}
147
+
148
+ func dumpPathTree (t testing.TB , root string ) []string {
149
+ t .Helper ()
150
+ tree := make ([]string , 0 )
151
+ err := filepath .Walk (root , func (path string , _ os.FileInfo , err error ) error {
152
+ if err != nil {
153
+ return err
154
+ }
155
+ p := strings .TrimPrefix (path , root )
156
+ tree = append (tree , p )
157
+ return nil
158
+ })
159
+ if err != nil {
160
+ t .Fatalf ("walking on '%s': %s" , root , err )
161
+ }
162
+ sort .Strings (tree )
163
+ return tree
164
+ }
165
+
166
+ func TestAdapter_Remove (t * testing.T ) {
167
+ const content = "Content used for testing"
168
+ tests := []struct {
169
+ name string
170
+ additionalObjects []string
171
+ path string
172
+ wantErr bool
173
+ wantTree []string
174
+ }{
175
+ {
176
+ name : "single" ,
177
+ path : "README" ,
178
+ wantErr : false ,
179
+ wantTree : []string {"" },
180
+ },
181
+
182
+ {
183
+ name : "under folder" ,
184
+ path : "src/tools.go" ,
185
+ wantErr : false ,
186
+ wantTree : []string {"" },
187
+ },
188
+ {
189
+ name : "under multiple folders" ,
190
+ path : "a/b/c/d.txt" ,
191
+ wantErr : false ,
192
+ wantTree : []string {"" },
193
+ },
194
+ {
195
+ name : "file in the way" ,
196
+ path : "a/b/c/d.txt" ,
197
+ additionalObjects : []string {"a/b/blocker.txt" },
198
+ wantErr : false ,
199
+ wantTree : []string {"" , "/test" , "/test/a" , "/test/a/b" , "/test/a/b/blocker.txt" },
200
+ },
201
+ }
202
+
203
+ for _ , tt := range tests {
204
+ t .Run (tt .name , func (t * testing.T ) {
205
+ // setup env
206
+ adp := makeAdapter (t )
207
+ envObjects := append (tt .additionalObjects , tt .path )
208
+ for _ , o := range envObjects {
209
+ obj := makePointer (o )
210
+ testutil .MustDo (t , "Put" , adp .Put (obj , 0 , strings .NewReader (content ), block.PutOpts {}))
211
+ }
212
+ // test Remove with remove empty folders
213
+ obj := makePointer (tt .path )
214
+ if err := adp .Remove (obj ); (err != nil ) != tt .wantErr {
215
+ t .Errorf ("Remove() error = %v, wantErr %v" , err , tt .wantErr )
216
+ }
217
+ tree := dumpPathTree (t , adp .Path ())
218
+ if diff := deep .Equal (tt .wantTree , tree ); diff != nil {
219
+ t .Errorf ("Remove() tree diff = %s" , diff )
220
+ }
221
+ })
222
+ }
223
+ }
0 commit comments