-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcsfs_test.go
153 lines (124 loc) · 3.09 KB
/
gcsfs_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
package gcsfs
import (
"context"
"errors"
"io/fs"
"testing"
"testing/fstest"
"time"
gcs "cloud.google.com/go/storage"
"google.golang.org/api/option"
)
var (
gcsFSCached *FS = nil
)
const (
testBucketName = "gcsfs-io-fs-test-files"
)
func newTestingStorageClient(t *testing.T) *gcs.Client {
gcsClient, err := gcs.NewClient(context.TODO(), option.WithoutAuthentication())
if err != nil {
t.Error("Failed to create new google cloud storage client")
}
return gcsClient
}
func newTestingFS(t *testing.T) *FS {
if gcsFSCached == nil {
gcsFSCached = NewWithClient(newTestingStorageClient(t), testBucketName)
}
return gcsFSCached
}
func TestWithContext(t *testing.T) {
gfs := newTestingFS(t)
doneChan := make(chan struct{}, 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond*10)
defer cancel()
go func(ctx context.Context) {
fs.ReadFile(gfs.WithContext(ctx), "test.txt")
doneChan <- struct{}{}
}(ctx)
select {
case <-ctx.Done():
// all went right
case <-doneChan:
t.Error("context timeout in FS should be reached")
}
}
func TestNewWithBucketHandle(t *testing.T) {
gcsClient := newTestingStorageClient(t)
_ = NewWithBucketHandle(gcsClient.Bucket(testBucketName))
}
func TestNewWithClient(t *testing.T) {
gcsClient := newTestingStorageClient(t)
_ = NewWithClient(gcsClient, testBucketName)
}
func TestOpen(t *testing.T) {
gfs := newTestingFS(t)
tests := []struct {
name string
err error
}{
{"test.txt", nil},
{"subdir/a.txt", nil},
{"404.txt", fs.ErrNotExist},
{"subdir/404.txt", fs.ErrNotExist},
}
for _, test := range tests {
f, err := gfs.Open(test.name)
if test.err != nil && !errors.Is(err, test.err) {
t.Fatalf("Opened %#v, got error %#v, want %#v", test.name, err, test.err)
}
if test.err == nil && f == nil {
t.Fatalf("Opened %#v but got no file handle, just nil", test.name)
}
}
}
func TestReadFile(t *testing.T) {
gfs := newTestingFS(t)
tests := []struct {
name string
contents string
err error
}{
{"test.txt", "This file is in the root directory.\n", nil},
{"404.txt", "", fs.ErrNotExist},
}
for _, test := range tests {
contents, err := fs.ReadFile(gfs, test.name)
if test.err != nil && !errors.Is(err, test.err) {
t.Fatalf("Opened %#v, got error %#v, want %#v", test.name, err, test.err)
}
if test.err == nil && string(contents) != test.contents {
t.Fatalf("Read %#v but the contents does not match, want %#v got %#v", test.name, test.contents, string(contents))
}
}
}
func TestDirExists(t *testing.T) {
gfs := newTestingFS(t)
tests := []struct {
name string
exists bool
}{
{".", true},
{"subdir", true},
{"subdir/", true},
{"not-found", false},
}
for _, test := range tests {
exists := gfs.dirExists(test.name)
if test.exists != exists {
t.Fatalf("dirExists %#v: expected %v but got %v", test.name, test.exists, exists)
}
}
}
func TestFS(t *testing.T) {
gfs := newTestingFS(t)
expectedFiles := []string{
"test.txt",
"subdir/a.txt",
"a/really/long/dir/hello.txt",
}
if err := fstest.TestFS(gfs, expectedFiles...); err != nil {
t.Fatal(err)
}
}