- Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathservice_test.go
172 lines (157 loc) · 4.48 KB
/
service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package blobs
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/cockroachdb/cockroach/pkg/blobs/blobspb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/errors/oserror"
)
funcTestBlobServiceList(t*testing.T) {
tmpDir, cleanupFn:=testutils.TempDir(t)
defercleanupFn()
fileContent:= []byte("a")
files:= []string{"/file/dir/a.csv", "/file/dir/b.csv", "/file/dir/c.csv"}
for_, file:=rangefiles {
writeTestFile(t, filepath.Join(tmpDir, file), fileContent)
}
service, err:=NewBlobService(tmpDir)
iferr!=nil {
t.Fatal(err)
}
ctx:=context.Background()
t.Run("list-correct-files", func(t*testing.T) {
resp, err:=service.List(ctx, &blobspb.GlobRequest{
Pattern: "file/dir/*.csv",
})
iferr!=nil {
t.Fatal(err)
}
resultList:=resp.Files
iflen(resultList) !=len(files) {
t.Fatal("result list does not have the correct number of files")
}
fori, f:=rangeresultList {
iff!=files[i] {
t.Fatalf("result list is incorrect %s", resultList)
}
}
})
t.Run("not-in-external-io-dir", func(t*testing.T) {
_, err:=service.List(ctx, &blobspb.GlobRequest{
Pattern: "file/../../*.csv",
})
iferr==nil {
t.Fatal("expected error but was not caught")
}
if!testutils.IsError(err, "outside of external-io-dir is not allowed") {
t.Fatal("incorrect error message: "+err.Error())
}
})
}
funcTestBlobServiceDelete(t*testing.T) {
tmpDir, cleanupFn:=testutils.TempDir(t)
defercleanupFn()
fileContent:= []byte("file_content")
filename:="path/to/file/content.txt"
writeTestFile(t, filepath.Join(tmpDir, filename), fileContent)
service, err:=NewBlobService(tmpDir)
iferr!=nil {
t.Fatal(err)
}
ctx:=context.Background()
t.Run("delete-correct-file", func(t*testing.T) {
_, err:=service.Delete(ctx, &blobspb.DeleteRequest{
Filename: filename,
})
iferr!=nil {
t.Fatal(err)
}
if_, err:=os.Stat(filepath.Join(tmpDir, filename)); !oserror.IsNotExist(err) {
t.Fatalf("expected not exists err, got: %s", err)
}
})
t.Run("file-not-exist", func(t*testing.T) {
_, err:=service.Delete(ctx, &blobspb.DeleteRequest{
Filename: "file/does/not/exist",
})
iferr==nil {
t.Fatal("expected error but was not caught")
}
if!testutils.IsError(err, "no such file") {
t.Fatal("incorrect error message: "+err.Error())
}
})
t.Run("not-in-external-io-dir", func(t*testing.T) {
_, err:=service.Delete(ctx, &blobspb.DeleteRequest{
Filename: "file/../../content.txt",
})
iferr==nil {
t.Fatal("expected error but was not caught")
}
if!testutils.IsError(err, "outside of external-io-dir is not allowed") {
t.Fatal("incorrect error message: "+err.Error())
}
})
}
funcTestBlobServiceStat(t*testing.T) {
tmpDir, cleanupFn:=testutils.TempDir(t)
defercleanupFn()
fileContent:= []byte("file_content")
filename:="path/to/file/content.txt"
writeTestFile(t, filepath.Join(tmpDir, filename), fileContent)
service, err:=NewBlobService(tmpDir)
iferr!=nil {
t.Fatal(err)
}
ctx:=context.Background()
t.Run("get-correct-file-size", func(t*testing.T) {
resp, err:=service.Stat(ctx, &blobspb.StatRequest{
Filename: filename,
})
iferr!=nil {
t.Fatal(err)
}
ifresp.Filesize!=int64(len(fileContent)) {
t.Fatalf("expected filesize: %d, got %d", len(fileContent), resp.Filesize)
}
})
t.Run("file-not-exist", func(t*testing.T) {
_, err:=service.Stat(ctx, &blobspb.StatRequest{
Filename: "file/does/not/exist",
})
iferr==nil {
t.Fatal("expected error but was not caught")
}
if!testutils.IsError(err, "no such file") {
t.Fatal("incorrect error message: "+err.Error())
}
})
t.Run("not-in-external-io-dir", func(t*testing.T) {
_, err:=service.Stat(ctx, &blobspb.StatRequest{
Filename: "file/../../content.txt",
})
iferr==nil {
t.Fatal("expected error but was not caught")
}
if!testutils.IsError(err, "outside of external-io-dir is not allowed") {
t.Fatal("incorrect error message: "+err.Error())
}
})
t.Run("stat-directory", func(t*testing.T) {
_, err:=service.Stat(ctx, &blobspb.StatRequest{
Filename: filepath.Dir(filename),
})
iferr==nil {
t.Fatalf("expected error but was not caught")
}
if!testutils.IsError(err, "expected a file") {
t.Fatal("incorrect error message: "+err.Error())
}
})
}