forked from thanos-io/objstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreach.go
201 lines (162 loc) · 5.12 KB
/
foreach.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package objtesting
import (
"os"
"strings"
"testing"
"github.com/thanos-io/objstore"
"github.com/thanos-io/objstore/client"
"github.com/thanos-io/objstore/providers/azure"
"github.com/thanos-io/objstore/providers/bos"
"github.com/thanos-io/objstore/providers/cos"
"github.com/thanos-io/objstore/providers/filesystem"
"github.com/thanos-io/objstore/providers/gcs"
"github.com/thanos-io/objstore/providers/obs"
"github.com/thanos-io/objstore/providers/oci"
"github.com/thanos-io/objstore/providers/oss"
"github.com/thanos-io/objstore/providers/s3"
"github.com/thanos-io/objstore/providers/swift"
"github.com/efficientgo/core/testutil"
)
// IsObjStoreSkipped returns true if given provider ID is found in THANOS_TEST_OBJSTORE_SKIP array delimited by comma e.g:
// THANOS_TEST_OBJSTORE_SKIP=GCS,S3,AZURE,SWIFT,COS,ALIYUNOSS,BOS,OCI.
func IsObjStoreSkipped(t *testing.T, provider client.ObjProvider) bool {
if e, ok := os.LookupEnv("THANOS_TEST_OBJSTORE_SKIP"); ok {
obstores := strings.Split(e, ",")
for _, objstore := range obstores {
if objstore == string(provider) {
t.Logf("%s found in THANOS_TEST_OBJSTORE_SKIP array. Skipping.", provider)
return true
}
}
}
return false
}
// ForeachStore runs given test using all available objstore implementations.
// For each it creates a new bucket with a random name and a cleanup function
// that deletes it after test was run.
// Use THANOS_TEST_OBJSTORE_SKIP to skip explicitly certain object storages.
func ForeachStore(t *testing.T, testFn func(t *testing.T, bkt objstore.Bucket)) {
t.Parallel()
// Mandatory Inmem. Not parallel, to detect problem early.
if ok := t.Run("inmem", func(t *testing.T) {
testFn(t, objstore.NewInMemBucket())
}); !ok {
return
}
// Mandatory Filesystem.
t.Run("filesystem", func(t *testing.T) {
t.Parallel()
dir, err := os.MkdirTemp("", "filesystem-foreach-store-test")
testutil.Ok(t, err)
defer testutil.Ok(t, os.RemoveAll(dir))
b, err := filesystem.NewBucket(dir)
testutil.Ok(t, err)
testFn(t, b)
testFn(t, objstore.NewPrefixedBucket(b, "some_prefix"))
})
// Optional GCS.
if !IsObjStoreSkipped(t, client.GCS) {
t.Run("gcs", func(t *testing.T) {
bkt, closeFn, err := gcs.NewTestBucket(t, os.Getenv("GCP_PROJECT"))
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
// TODO(bwplotka): Add goleak when https://github.com/GoogleCloudPlatform/google-cloud-go/issues/1025 is resolved.
testFn(t, bkt)
testFn(t, objstore.NewPrefixedBucket(bkt, "some_prefix"))
})
}
// Optional S3.
if !IsObjStoreSkipped(t, client.S3) {
t.Run("aws s3", func(t *testing.T) {
// TODO(bwplotka): Allow taking location from envvar.
bkt, closeFn, err := s3.NewTestBucket(t, "us-west-2")
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
// TODO(bwplotka): Add goleak when we fix potential leak in minio library.
// We cannot use goleak for detecting our own potential leaks, when goleak detects leaks in minio itself.
// This needs to be investigated more.
testFn(t, bkt)
testFn(t, objstore.NewPrefixedBucket(bkt, "some_prefix"))
})
}
// Optional Azure.
if !IsObjStoreSkipped(t, client.AZURE) {
t.Run("azure", func(t *testing.T) {
bkt, closeFn, err := azure.NewTestBucket(t, "e2e-tests")
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
testFn(t, bkt)
testFn(t, objstore.NewPrefixedBucket(bkt, "some_prefix"))
})
}
// Optional SWIFT.
if !IsObjStoreSkipped(t, client.SWIFT) {
t.Run("swift", func(t *testing.T) {
container, closeFn, err := swift.NewTestContainer(t)
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
testFn(t, container)
testFn(t, objstore.NewPrefixedBucket(container, "some_prefix"))
})
}
// Optional COS.
if !IsObjStoreSkipped(t, client.COS) {
t.Run("Tencent cos", func(t *testing.T) {
bkt, closeFn, err := cos.NewTestBucket(t)
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
testFn(t, bkt)
testFn(t, objstore.NewPrefixedBucket(bkt, "some_prefix"))
})
}
// Optional OSS.
if !IsObjStoreSkipped(t, client.ALIYUNOSS) {
t.Run("AliYun oss", func(t *testing.T) {
bkt, closeFn, err := oss.NewTestBucket(t)
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
testFn(t, bkt)
testFn(t, objstore.NewPrefixedBucket(bkt, "some_prefix"))
})
}
// Optional BOS.
if !IsObjStoreSkipped(t, client.BOS) {
t.Run("Baidu BOS", func(t *testing.T) {
bkt, closeFn, err := bos.NewTestBucket(t)
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
testFn(t, bkt)
testFn(t, objstore.NewPrefixedBucket(bkt, "some_prefix"))
})
}
// Optional OCI.
if !IsObjStoreSkipped(t, client.OCI) {
t.Run("oci", func(t *testing.T) {
bkt, closeFn, err := oci.NewTestBucket(t)
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
testFn(t, bkt)
})
}
// Optional OBS.
if !IsObjStoreSkipped(t, client.OBS) {
t.Run("obs", func(t *testing.T) {
bkt, closeFn, err := obs.NewTestBucket(t, "cn-south-1")
testutil.Ok(t, err)
t.Parallel()
defer closeFn()
testFn(t, bkt)
testFn(t, objstore.NewPrefixedBucket(bkt, "some_prefix"))
})
}
}