-
Notifications
You must be signed in to change notification settings - Fork 18
/
main_test.go
275 lines (223 loc) · 8.44 KB
/
main_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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"aliyun-oss-website-action/config"
"aliyun-oss-website-action/operation"
"aliyun-oss-website-action/utils"
"fmt"
"os"
"testing"
"github.com/fangbinwei/aliyun-oss-go-sdk/oss"
"github.com/stretchr/testify/assert"
)
func testSetStaticWebsiteConfig(t *testing.T) {
assert := assert.New(t)
err := operation.SetStaticWebsiteConfig()
assert.NoError(err)
}
func testUpload(t *testing.T) {
assert := assert.New(t)
fmt.Println("---- [delete] ---->")
errs := operation.DeleteObjects(config.Bucket)
fmt.Println("<---- [delete end] ----")
assert.Equal(len(errs), 0)
lor, err := config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(0, len(lor.Objects))
records := utils.WalkDir(config.Folder)
// overwrite, since dotenv doesn't support multiline
config.Exclude = []string{"exclude.txt", "exclude/"}
fmt.Println("---- [upload] ---->")
uploaded, uploadErrs := operation.UploadObjects(config.Folder, config.Bucket, records, nil)
fmt.Println("<---- [upload end] ----")
assert.Equal(0, len(uploadErrs), uploadErrs)
lor, err = config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(len(uploaded), len(lor.Objects))
// test exclude
lor, err = config.Bucket.ListObjects(oss.Prefix("exclude"))
assert.NoError(err)
assert.Empty(lor.Objects)
// test cache-control
for _, u := range uploaded {
// 如果自定义域名解析到了cdn, 这个接口会报错, 但是上面的测试流程正常
// 避开方法: env中endpoint使用bucket的endpoint或者bucket域名, 而不是自定义域名
props, err := config.Bucket.GetObjectDetailedMeta(u.ObjectKey)
assert.NoError(err)
cacheControl := props.Get("Cache-Control")
if utils.IsImage(u.ObjectKey) {
assert.Equal(config.ImageCacheControl, cacheControl)
} else if utils.IsHTML(u.ObjectKey) {
assert.Equal(config.HTMLCacheControl, cacheControl)
} else {
assert.Equal(config.OtherCacheControl, cacheControl)
}
}
}
func testUploadIncrementalFirst(t *testing.T) {
assert := assert.New(t)
fmt.Println("---- [incremental] ---->")
incremental, err := operation.GetRemoteIncrementalConfig(config.Bucket)
assert.Error(err)
assert.Empty(incremental)
fmt.Println("<---- [incremental end] ----")
if incremental == nil {
fmt.Println("---- [delete] ---->")
errs := operation.DeleteObjects(config.Bucket)
fmt.Println("<---- [delete end] ----")
assert.Equal(len(errs), 0)
}
lor, err := config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(0, len(lor.Objects))
records := utils.WalkDir(config.Folder)
// overwrite, since dotenv doesn't support multiline
config.Exclude = []string{"exclude.txt", "exclude/"}
fmt.Println("---- [upload] ---->")
uploaded, uploadErrs := operation.UploadObjects(config.Folder, config.Bucket, records, incremental)
fmt.Println("<---- [upload end] ----")
assert.Equal(0, len(uploadErrs), uploadErrs)
lor, err = config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(len(uploaded), len(lor.Objects))
fmt.Println("---- [incremental] ---->")
err = operation.UploadIncrementalConfig(config.Bucket, uploaded)
fmt.Println("<---- [incremental end] ----")
assert.NoError(err)
incremental, err = operation.GetRemoteIncrementalConfig(config.Bucket)
assert.NoError(err)
assert.Equal(len(uploaded), len(incremental.M))
for _, v := range uploaded {
assert.False(v.Incremental)
assert.Equal(v.ContentMD5, incremental.M[v.ObjectKey].ContentMD5)
assert.Equal(v.CacheControl, incremental.M[v.ObjectKey].CacheControl)
}
// test exclude
lor, err = config.Bucket.ListObjects(oss.Prefix("exclude"))
assert.NoError(err)
assert.Empty(lor.Objects)
// test cache-control
for _, u := range uploaded {
// 如果自定义域名解析到了cdn, 这个接口会报错, 但是上面的测试流程正常
// 避开方法: env中endpoint使用bucket的endpoint或者bucket域名, 而不是自定义域名
props, err := config.Bucket.GetObjectDetailedMeta(u.ObjectKey)
assert.NoError(err)
cacheControl := props.Get("Cache-Control")
if utils.IsImage(u.ObjectKey) {
assert.Equal(config.ImageCacheControl, cacheControl)
} else if utils.IsPDF(u.ObjectKey) {
assert.Equal(config.PDFCacheControl, cacheControl)
} else if utils.IsHTML(u.ObjectKey) {
assert.Equal(config.HTMLCacheControl, cacheControl)
} else {
assert.Equal(config.OtherCacheControl, cacheControl)
}
}
}
func testUploadIncrementalSecond(t *testing.T) {
assert := assert.New(t)
fmt.Println("---- [incremental] ---->")
incremental, err := operation.GetRemoteIncrementalConfig(config.Bucket)
assert.NoError(err)
fmt.Println("<---- [incremental end] ----")
lor, err := config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(len(lor.Objects)-1, len(incremental.M))
records := utils.WalkDir(config.Folder)
fmt.Println("---- [upload] ---->")
uploaded, uploadErrs := operation.UploadObjects(config.Folder, config.Bucket, records, incremental)
fmt.Println("<---- [upload end] ----")
assert.Equal(0, len(uploadErrs), uploadErrs)
lor, err = config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(len(uploaded), len(lor.Objects)-1)
// incremental.M中剩余的项是待删除的, 数量为0, 因为此次上传和上次上传的文件一模一样
assert.Equal(0, len(incremental.M))
fmt.Println("---- [delete] ---->")
// 只删除.actioninfo
errs := operation.DeleteObjectsIncremental(config.Bucket, incremental)
fmt.Println("<---- [delete end] ----")
assert.Equal(0, len(errs))
lor, err = config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(len(uploaded), len(lor.Objects))
fmt.Println("---- [incremental] ---->")
err = operation.UploadIncrementalConfig(config.Bucket, uploaded)
fmt.Println("<---- [incremental end] ----")
assert.NoError(err)
incremental, err = operation.GetRemoteIncrementalConfig(config.Bucket)
assert.NoError(err)
assert.Equal(len(uploaded), len(incremental.M))
for _, v := range uploaded {
// 全都不需要上传, 命中incremental
assert.True(v.Incremental)
assert.Equal(v.ContentMD5, incremental.M[v.ObjectKey].ContentMD5)
assert.Equal(v.CacheControl, incremental.M[v.ObjectKey].CacheControl)
}
}
func testUploadIncrementalThird(t *testing.T) {
assert := assert.New(t)
folder := "testdata/group2"
// 改变cache-control会让对应文件重新上传, 即使hash没变
config.ImageCacheControl = "no-cache"
fmt.Println("---- [incremental] ---->")
incremental, err := operation.GetRemoteIncrementalConfig(config.Bucket)
assert.NoError(err)
fmt.Println("<---- [incremental end] ----")
lor, err := config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(len(lor.Objects)-1, len(incremental.M))
records := utils.WalkDir(folder)
fmt.Println("---- [upload] ---->")
uploaded, uploadErrs := operation.UploadObjects(folder, config.Bucket, records, incremental)
fmt.Println("<---- [upload end] ----")
assert.Equal(0, len(uploadErrs), uploadErrs)
// incremental.M中剩余的项是待删除的, 大于0
assert.Greater(len(incremental.M), 0)
for _, v := range uploaded {
if v.ObjectKey == "empty.js" {
assert.True(v.Incremental)
continue
}
if v.ObjectKey == "example.js" {
assert.False(v.Incremental)
continue
}
if v.ObjectKey == "favicon.ico" {
assert.False(v.Incremental)
continue
}
t.Fail()
}
fmt.Println("---- [delete] ---->")
errs := operation.DeleteObjectsIncremental(config.Bucket, incremental)
fmt.Println("<---- [delete end] ----")
assert.Equal(0, len(errs))
lor, err = config.Bucket.ListObjects()
assert.NoError(err)
assert.Equal(len(uploaded), len(lor.Objects))
fmt.Println("---- [incremental] ---->")
err = operation.UploadIncrementalConfig(config.Bucket, uploaded)
fmt.Println("<---- [incremental end] ----")
assert.NoError(err)
incremental, err = operation.GetRemoteIncrementalConfig(config.Bucket)
assert.NoError(err)
assert.Equal(len(uploaded), len(incremental.M))
for _, v := range uploaded {
assert.Equal(v.ContentMD5, incremental.M[v.ObjectKey].ContentMD5)
assert.Equal(v.CacheControl, incremental.M[v.ObjectKey].CacheControl)
}
}
func TestAction(t *testing.T) {
t.Run("SetStaticWebsiteConfig", testSetStaticWebsiteConfig)
t.Run("First upload", testUpload)
t.Run("Second upload", testUpload)
t.Run("First incremental upload without .actioninfo", testUploadIncrementalFirst)
t.Run("Second incremental upload", testUploadIncrementalSecond)
t.Run("Third incremental upload, change cache-control", testUploadIncrementalThird)
}
func TestMain(m *testing.M) {
code := m.Run()
fmt.Println("Empty bucket after test")
operation.DeleteObjects(config.Bucket)
os.Exit(code)
}