-
Notifications
You must be signed in to change notification settings - Fork 62
/
storage_test.go
240 lines (214 loc) · 8.26 KB
/
storage_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package storage
import (
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
type StorageTestSuite struct {
suite.Suite
StorageBackends map[string]Backend
TempDirectory string
}
func (suite *StorageTestSuite) setupStorageBackends() {
timestamp := time.Now().Format("20060102150405")
suite.TempDirectory = fmt.Sprintf("../../.test/storage-storage/%s", timestamp)
suite.StorageBackends = make(map[string]Backend)
suite.StorageBackends["LocalFilesystem"] = Backend(NewLocalFilesystemBackend(suite.TempDirectory))
// create empty dir in local storage to make sure it doesnt end up in ListObjects
err := os.MkdirAll(fmt.Sprintf("%s/%s", suite.TempDirectory, "ignoreme"), 0o777)
suite.Nil(err, "No error creating ignored dir in local storage")
if os.Getenv("TEST_CLOUD_STORAGE") == "1" {
prefix := fmt.Sprintf("unittest/%s", timestamp)
s3Bucket := os.Getenv("TEST_STORAGE_AMAZON_BUCKET")
s3Region := os.Getenv("TEST_STORAGE_AMAZON_REGION")
gcsBucket := os.Getenv("TEST_STORAGE_GOOGLE_BUCKET")
blobContainer := os.Getenv("TEST_STORAGE_MICROSOFT_CONTAINER")
ossBucket := os.Getenv("TEST_STORAGE_ALIBABA_BUCKET")
ossEndpoint := os.Getenv("TEST_STORAGE_ALIBABA_ENDPOINT")
osContainer := os.Getenv("TEST_STORAGE_OPENSTACK_CONTAINER")
osRegion := os.Getenv("TEST_STORAGE_OPENSTACK_REGION")
ocsBucket := os.Getenv("TEST_STORAGE_ORACLE_BUCKET")
ocsRegion := os.Getenv("TEST_STORAGE_ORACLE_REGION")
ocsCompartmentId := os.Getenv("TEST_STORAGE_ORACLE_COMPARTMENTID")
bosBucket := os.Getenv("TEST_STORAGE_BAIDU_BUCKET")
bosEndpoint := os.Getenv("TEST_STORAGE_BAIDU_ENDPOINT")
cosBucket := os.Getenv("TEST_STORAGE_TENCENT_BUCKET")
cosEndpoint := os.Getenv("TEST_STORAGE_TENCENT_ENDPOINT")
if s3Bucket != "" && s3Region != "" {
suite.StorageBackends["AmazonS3"] = Backend(NewAmazonS3Backend(s3Bucket, prefix, s3Region, "", ""))
}
if gcsBucket != "" {
suite.StorageBackends["GoogleCS"] = Backend(NewGoogleCSBackend(gcsBucket, prefix))
}
if blobContainer != "" {
suite.StorageBackends["MicrosoftBlob"] = Backend(NewMicrosoftBlobBackend(blobContainer, prefix))
}
if ossBucket != "" {
suite.StorageBackends["AlibabaCloudOSS"] = Backend(NewAlibabaCloudOSSBackend(ossBucket, prefix, ossEndpoint, ""))
}
if osContainer != "" {
suite.StorageBackends["OpenStackOS"] = Backend(NewOpenstackOSBackend(osContainer, prefix, osRegion, ""))
}
if ocsBucket != "" {
suite.StorageBackends["OracleCS"] = Backend(NewOracleCSBackend(ocsBucket, prefix, ocsRegion, ocsCompartmentId))
}
if bosBucket != "" {
suite.StorageBackends["BaiduCloudBOS"] = Backend(NewBaiDuBOSBackend(bosBucket, prefix, bosEndpoint))
}
if cosBucket != "" {
suite.StorageBackends["TencentCloudCOS"] = Backend(NewTencentCloudCOSBackend(cosBucket, prefix, cosEndpoint))
}
}
}
func (suite *StorageTestSuite) SetupSuite() {
suite.setupStorageBackends()
for i := 1; i <= 9; i++ {
data := []byte(fmt.Sprintf("test content %d", i))
path := fmt.Sprintf("test%d.txt", i)
for key, backend := range suite.StorageBackends {
err := backend.PutObject(path, data)
message := fmt.Sprintf("no error putting object %s using %s backend", path, key)
suite.Nil(err, message)
}
}
for key, backend := range suite.StorageBackends {
if key == "LocalFilesystem" {
continue
}
data := []byte("skipped object")
path := "this/is/a/skipped/object.txt"
err := backend.PutObject(path, data)
message := fmt.Sprintf("no error putting skipped object %s using %s backend", path, key)
suite.Nil(err, message)
}
}
func (suite *StorageTestSuite) TearDownSuite() {
defer os.RemoveAll(suite.TempDirectory)
for i := 1; i <= 9; i++ {
path := fmt.Sprintf("test%d.txt", i)
for key, backend := range suite.StorageBackends {
err := backend.DeleteObject(path)
message := fmt.Sprintf("no error deleting object %s using %s backend", path, key)
suite.Nil(err, message)
}
}
for key, backend := range suite.StorageBackends {
if key == "LocalFilesystem" {
continue
}
path := "this/is/a/skipped/object.txt"
err := backend.DeleteObject(path)
message := fmt.Sprintf("no error deleting skipped object %s using %s backend", path, key)
suite.Nil(err, message)
}
}
func (suite *StorageTestSuite) TestListObjects() {
for key, backend := range suite.StorageBackends {
objects, err := backend.ListObjects("")
message := fmt.Sprintf("no error listing objects using %s backend", key)
suite.Nil(err, message)
expectedNumObjects := 9
message = fmt.Sprintf("%d objects listed using %s backend", expectedNumObjects, key)
suite.Equal(expectedNumObjects, len(objects), message)
for i, object := range objects {
path := fmt.Sprintf("test%d.txt", (i + 1))
message = fmt.Sprintf("object %s found in list objects using %s backend", path, key)
suite.Equal(path, object.Path, message)
}
}
}
func (suite *StorageTestSuite) TestGetObject() {
for key, backend := range suite.StorageBackends {
for i := 1; i <= 9; i++ {
path := fmt.Sprintf("test%d.txt", i)
object, err := backend.GetObject(path)
message := fmt.Sprintf("no error getting object %s using %s backend", path, key)
suite.Nil(err, message)
message = fmt.Sprintf("object %s content as expected using %s backend", path, key)
suite.Equal(object.Content, []byte(fmt.Sprintf("test content %d", i)), message)
}
}
}
func (suite *StorageTestSuite) TestHasSuffix() {
now := time.Now()
o1 := Object{
Path: "mychart-0.1.0.tgz",
Content: []byte{},
LastModified: now,
}
suite.True(o1.HasExtension("tgz"), "object has tgz suffix")
o2 := Object{
Path: "mychart-0.1.0.txt",
Content: []byte{},
LastModified: now,
}
suite.False(o2.HasExtension("tgz"), "object does not have tgz suffix")
}
func (suite *StorageTestSuite) TestGetObjectSliceDiff() {
now := time.Now()
os1 := []Object{
{
Path: "test1.txt",
Content: []byte{},
LastModified: now,
},
}
var os2 []Object
diff := GetObjectSliceDiff(os1, os2, time.Duration(0))
suite.True(diff.Change, "change detected")
suite.Equal(diff.Removed, os1, "removed slice populated")
suite.Empty(diff.Added, "added slice empty")
suite.Empty(diff.Updated, "updated slice empty")
os2 = append(os2, os1[0])
diff = GetObjectSliceDiff(os1, os2, time.Duration(0))
suite.False(diff.Change, "no change detected")
suite.Empty(diff.Removed, "removed slice empty")
suite.Empty(diff.Added, "added slice empty")
suite.Empty(diff.Updated, "updated slice empty")
os2[0].LastModified = now.Add(1)
diff = GetObjectSliceDiff(os1, os2, time.Duration(0))
suite.True(diff.Change, "change detected")
suite.Empty(diff.Removed, "removed slice empty")
suite.Empty(diff.Added, "added slice empty")
suite.Equal(diff.Updated, os2, "updated slice populated")
os2[0].LastModified = now.Add(time.Second)
diff = GetObjectSliceDiff(os1, os2, time.Second)
suite.False(diff.Change, "no change detected")
suite.Empty(diff.Removed, "removed slice empty")
suite.Empty(diff.Added, "added slice empty")
suite.Empty(diff.Updated, "updated slice empty")
os2[0].LastModified = now.Add(time.Second + time.Nanosecond)
diff = GetObjectSliceDiff(os1, os2, time.Second)
suite.True(diff.Change, "change detected")
suite.Empty(diff.Removed, "removed slice empty")
suite.Empty(diff.Added, "added slice empty")
suite.Equal(diff.Updated, os2, "updated slice populated")
os2[0].LastModified = now
os2 = append(os2, Object{
Path: "test2.txt",
Content: []byte{},
LastModified: now,
})
diff = GetObjectSliceDiff(os1, os2, time.Duration(0))
suite.True(diff.Change, "change detected")
suite.Empty(diff.Removed, "removed slice empty")
suite.Equal(diff.Added, []Object{os2[1]}, "added slice empty")
suite.Empty(diff.Updated, "updated slice empty")
}
func TestStorageTestSuite(t *testing.T) {
suite.Run(t, new(StorageTestSuite))
}