-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.go
225 lines (190 loc) · 6.24 KB
/
test_helper.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
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"net/http"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
)
const (
sampleJPEG = "samplefile/image.jpg"
sampleJPEGWebP = "samplefile/image.jpg.webp"
samplePNG = "samplefile/image.png"
samplePNGWebP = "samplefile/image.png.webp"
sampleCSS = "samplefile/style.css"
sampleMinCSS = "samplefile/style.min.css"
sampleNominifyCSS = "samplefile/nominify.css"
sampleJPEGSize = int64(23838)
sampleJPEGWebPSize = int64(5294)
samplePNGSize = int64(28877)
samplePNGWebPSize = int64(5138)
sampleCSSSize = int64(91)
sampleMinCSSSize = int64(58)
sampleNominifyCSSSize = int64(91)
chromeAcceptHeader = "image/avif,image/webp,image/apng,image/*,*/*;q=0.8"
oldSafariAcceptHeader = "image/png,image/svg+xml,image/*;q=0.8,video/*;q=0.8,*/*;q=0.5"
publicContentPathPattern = "wp-content/uploads/*"
)
func sampleETag(size int64) string {
return fmt.Sprintf("\"%x-%x\"", sampleModTime.UnixNano(), size)
}
var (
oldModTime = time.Date(2019, time.January, 1, 0, 0, 0, 0, time.UTC)
sampleModTime = time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC)
sampleLastModified = sampleModTime.Format(http.TimeFormat)
sampleJPEGETag = sampleETag(sampleJPEGSize)
sampleCSSETag = sampleETag(sampleCSSSize)
sampleMinCSSETag = sampleETag(sampleMinCSSSize)
sampleNominifyCSSETag = sampleETag(sampleNominifyCSSSize)
)
// InitTest moves working directory to project root directory.
// https://brandur.org/fragments/testing-go-project-root
func InitTest() {
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), ".")
err := os.Chdir(dir)
if err != nil {
panic(err)
}
}
func readTestConfig(name string) string {
cwd, err := os.Getwd()
if err != nil {
panic("failed to get current working directory")
}
path := cwd + "/config/test/" + name
val, err := os.ReadFile(path)
if err != nil {
panic("failed to load config file: " + path + ", error: " + err.Error())
}
return strings.TrimSpace(string(val))
}
func generateSafeRandomString() string {
v := make([]byte, 256/8)
if _, err := rand.Read(v); err != nil {
panic(err.Error())
}
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(v)
}
func getTestConfig(name string) *configure {
region := "ap-northeast-1"
sqsName := "test-" + name + "-" + generateSafeRandomString()
sqsURL := fmt.Sprintf("https://sqs.%s.amazonaws.com/%s/%s",
region,
readTestConfig("aws-account-id"),
sqsName)
efsPath := fmt.Sprintf("work/test/%s/%s", name, generateSafeRandomString())
publicContentPathPatterns := "/" + generateSafeRandomString() + "/" + publicContentPathPattern + ",foobarbaz/*"
bypassMinifierPathPatterns := "*/nominify.css"
cfg := &configure{
region: region,
accessKeyID: readTestConfig("access-key-id"),
secretAccessKey: readTestConfig("secret-access-key"),
s3Bucket: readTestConfig("s3-bucket"),
s3SrcPrefix: generateSafeRandomString() + "/" + name + "/",
s3DestPrefix: generateSafeRandomString() + "/" + name + "/",
sqsQueueURL: sqsURL,
sqsBatchWaitTime: 2,
efsMountPath: efsPath,
gracefulShutdownTimeout: 5,
port: 0, // Not used
logPath: efsPath + "/imgserver.log",
errorLogPath: efsPath + "/imgserver-error.log",
publicContentS3Bucket: readTestConfig("public-content-s3-bucket"),
publicContentPathPatterns: publicContentPathPatterns,
publicContentPathGlob: createPathGlob(publicContentPathPatterns),
bypassMinifierPathPatterns: bypassMinifierPathPatterns,
bypassMinifierPathIgnore: createPathIgnoreGlob(bypassMinifierPathPatterns),
temporaryCache: &cacheControl{
name: "temporary",
value: fmt.Sprintf("public, max-age=%d", 20*60),
},
permanentCache: &cacheControl{
name: "permanent",
value: fmt.Sprintf("public, max-age=%d", 365*24*60*60),
},
}
return cfg
}
func getTestSQSQueueNameFromURL(url string) string {
parts := strings.Split(url, "/")
return parts[len(parts)-1]
}
func newTestEnvironment(name string, s *TestSuite) *environment {
cfg := getTestConfig(name)
require.NoError(
s.T(),
os.RemoveAll(cfg.efsMountPath),
"failed to remove directory")
require.NoError(
s.T(),
os.MkdirAll(cfg.efsMountPath, 0755),
"failed to create directory")
log := createLogger(s.ctx, cfg.logPath, cfg.errorLogPath)
e := newEnvironment(s.ctx, cfg, log)
sqsName := getTestSQSQueueNameFromURL(e.sqsQueueURL)
_, err := e.sqsClient.CreateQueue(s.ctx, &sqs.CreateQueueInput{
QueueName: &sqsName,
})
require.NoError(s.T(), err, "failed to create SQS queue")
return e
}
func initTestSuite(name string, t require.TestingT) *TestSuite {
InitTest()
require.NoError(t, os.RemoveAll("work/test/"+name), "failed to remove directory")
ctx := context.Background()
return &TestSuite{ctx: ctx}
}
func cleanTestEnvironment(ctx context.Context, s *TestSuite) {
if _, err := s.env.sqsClient.DeleteQueue(ctx, &sqs.DeleteQueueInput{
QueueUrl: &s.env.sqsQueueURL,
}); err != nil {
s.env.log.Error("failed to clean up SQS queue", zap.Error(err))
}
}
// TestSuite holds configs and sessions required to execute program.
type TestSuite struct {
suite.Suite
env *environment
ctx context.Context
}
func copy(src, dst string, s *suite.Suite) {
in, err := os.Open(src)
s.Require().NoError(err)
defer func() {
s.Require().NoError(in.Close())
}()
out, err := os.Create(dst)
s.Require().NoError(err)
defer func() {
s.Require().NoError(out.Close())
s.Require().NoError(os.Chtimes(dst, sampleModTime, sampleModTime))
}()
{
_, err := io.Copy(out, in)
s.Require().NoError(err)
}
}
type httpHeader http.Response
func (h *httpHeader) cacheControl() string {
return h.Header.Get(cacheControlHeader)
}
func (h *httpHeader) contentType() string {
return h.Header.Get(contentTypeHeader)
}
func (h *httpHeader) eTag() string {
return h.Header.Get(eTagHeader)
}
func (h *httpHeader) lastModified() string {
return h.Header.Get(lastModifiedHeader)
}