forked from fsouza/fake-gcs-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
215 lines (209 loc) · 5.93 KB
/
config_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2019 Francisco Souza. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package config
import (
"testing"
"github.com/fsouza/fake-gcs-server/fakestorage"
"github.com/fsouza/fake-gcs-server/internal/notification"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/sirupsen/logrus"
)
func TestLoadConfig(t *testing.T) {
t.Parallel()
tests := []struct {
name string
args []string
expectedConfig Config
expectErr bool
}{
{
name: "all parameters",
args: []string{
"-backend", "memory",
"-filesystem-root", "/tmp/something",
"-public-host", "127.0.0.1.nip.io:8443",
"-external-url", "https://myhost.example.com:8443",
"-cors-headers", "X-Goog-Meta-Uploader",
"-host", "127.0.0.1",
"-port", "443",
"-data", "/var/gcs",
"-scheme", "http",
"-event.pubsub-project-id", "test-project",
"-event.pubsub-topic", "gcs-events",
"-event.object-prefix", "uploads/",
"-event.list", "finalize,delete,metadataUpdate,archive",
"-location", "US-EAST1",
"-log-level", "warn",
},
expectedConfig: Config{
Seed: "/var/gcs",
backend: "memory",
fsRoot: "/tmp/something",
publicHost: "127.0.0.1.nip.io:8443",
externalURL: "https://myhost.example.com:8443",
allowedCORSHeaders: []string{"X-Goog-Meta-Uploader"},
host: "127.0.0.1",
port: 443,
scheme: "http",
event: EventConfig{
pubsubProjectID: "test-project",
pubsubTopic: "gcs-events",
prefix: "uploads/",
list: []string{"finalize", "delete", "metadataUpdate", "archive"},
},
bucketLocation: "US-EAST1",
LogLevel: logrus.WarnLevel,
},
},
{
name: "default parameters",
expectedConfig: Config{
Seed: "",
backend: "filesystem",
fsRoot: "/storage",
publicHost: "storage.googleapis.com",
externalURL: "",
allowedCORSHeaders: nil,
host: "0.0.0.0",
port: 4443,
scheme: "https",
event: EventConfig{
list: []string{"finalize"},
},
bucketLocation: "US-CENTRAL1",
LogLevel: logrus.InfoLevel,
},
},
{
name: "invalid port value type",
args: []string{"-port", "not-a-number"},
expectErr: true,
},
{
name: "invalid port value",
args: []string{"-port", "65536"},
expectErr: true,
},
{
name: "invalid backend",
args: []string{"-backend", "in-memory"},
expectErr: true,
},
{
name: "filesystem backend with no root",
args: []string{"-backend", "filesystem", "-filesystem-root", ""},
expectErr: true,
},
{
name: "missing event pubsub project ID",
args: []string{"-event.pubsub-topic", "gcs-events"},
expectErr: true,
},
{
name: "missing event pubsub topic",
args: []string{"-event.pubsub-project-id", "test-project"},
expectErr: true,
},
{
name: "invalid events",
args: []string{"-event.list", "invalid,stuff", "-event.pubsub-topic", "gcs-events", "-event.pubsub-project-id", "test-project"},
expectErr: true,
},
{
name: "invalid log level",
args: []string{"-log-level", "non-existent-level"},
expectErr: true,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cfg, err := Load(test.args)
if err != nil && !test.expectErr {
t.Fatalf("unexpected non-nil error: %v", err)
} else if err == nil && test.expectErr {
t.Fatal("unexpected <nil> error")
}
if diff := cmp.Diff(cfg, test.expectedConfig, cmp.AllowUnexported(Config{}, EventConfig{})); !test.expectErr && diff != "" {
t.Errorf("wrong config returned\nwant %#v\ngot %#v\ndiff: %v", test.expectedConfig, cfg, diff)
}
})
}
}
func TestToFakeGcsOptions(t *testing.T) {
t.Parallel()
tests := []struct {
name string
config Config
expected fakestorage.Options
}{
{
"filesystem",
Config{
backend: "filesystem",
fsRoot: "/tmp/something",
publicHost: "127.0.0.1.nip.io:8443",
externalURL: "https://myhost.example.com:8443",
host: "0.0.0.0",
port: 443,
event: EventConfig{
pubsubProjectID: "test-project",
pubsubTopic: "gcs-events",
prefix: "uploads/",
list: []string{"finalize", "delete"},
},
bucketLocation: "US-EAST1",
},
fakestorage.Options{
StorageRoot: "/tmp/something",
PublicHost: "127.0.0.1.nip.io:8443",
ExternalURL: "https://myhost.example.com:8443",
Host: "0.0.0.0",
Port: 443,
EventOptions: notification.EventManagerOptions{
ProjectID: "test-project",
TopicName: "gcs-events",
ObjectPrefix: "uploads/",
NotifyOn: notification.EventNotificationOptions{
Finalize: true,
Delete: true,
MetadataUpdate: false,
},
},
BucketsLocation: "US-EAST1",
},
},
{
"memory",
Config{
backend: "memory",
fsRoot: "/tmp/something",
publicHost: "127.0.0.1.nip.io:8443",
externalURL: "https://myhost.example.com:8443",
host: "0.0.0.0",
port: 443,
},
fakestorage.Options{
StorageRoot: "",
PublicHost: "127.0.0.1.nip.io:8443",
ExternalURL: "https://myhost.example.com:8443",
Host: "0.0.0.0",
Port: 443,
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
opts := test.config.ToFakeGcsOptions()
ignWriter := cmpopts.IgnoreFields(fakestorage.Options{}, "Writer")
if diff := cmp.Diff(opts, test.expected, ignWriter); diff != "" {
t.Errorf("wrong set of options returned\nwant %#v\ngot %#v\ndiff: %v", test.expected, opts, diff)
}
})
}
}