This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
e2e.go
255 lines (214 loc) · 6.54 KB
/
e2e.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
// Copyright wasilibs authors
// SPDX-License-Identifier: MIT
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/magefile/mage/sh"
)
func E2eCoraza() error {
if _, err := os.Stat(filepath.Join("e2e", "coraza-proxy-wasm")); os.IsNotExist(err) {
// Try not pinning version, there should be no compatibility issues causing unexpected failures from a
// green coraza build so we get to keep forward coverage this way.
if err := sh.RunV("git", "clone", "https://github.com/corazawaf/coraza-proxy-wasm.git", filepath.Join("e2e", "coraza-proxy-wasm")); err != nil {
return err
}
}
if err := os.Chdir(filepath.Join("e2e", "coraza-proxy-wasm")); err != nil {
return err
}
defer func() {
for _, f := range []string{"ftw-envoy.log"} {
content, err := os.ReadFile(filepath.Join("build", f))
if err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join("..", "..", "build", "logs", f), content, 0o644); err != nil {
panic(err)
}
}
}()
if err := sh.RunV("go", "mod", "edit", "-replace=github.com/wasilibs/nottinygc=../.."); err != nil {
return err
}
defer func() {
if err := sh.RunV("go", "mod", "edit", "-dropreplace=github.com/wasilibs/nottinygc"); err != nil {
panic(err)
}
}()
if err := sh.RunV("go", "run", "mage.go", "build"); err != nil {
return err
}
if err := sh.RunV("go", "run", "mage.go", "ftw"); err != nil {
return err
}
return nil
}
func E2eEnvoyDispatchCall() error {
if err := os.MkdirAll(filepath.Join("e2e", "envoy-dispatch-call", "build"), 0o755); err != nil {
return err
}
if err := sh.RunV("tinygo", "build", "-target=wasi", "-gc=custom", "-tags='custommalloc nottinygc_envoy'", "-scheduler=none",
"-o", filepath.Join("e2e", "envoy-dispatch-call", "build", "plugin.wasm"), "./e2e/envoy-dispatch-call"); err != nil {
return err
}
if err := sh.RunV("docker-compose", "--file", filepath.Join("e2e", "envoy-dispatch-call", "docker-compose.yml"), "up", "-d"); err != nil {
return err
}
defer func() {
if err := sh.RunV("docker-compose", "--file", filepath.Join("e2e", "envoy-dispatch-call", "docker-compose.yml"), "down", "-v"); err != nil {
panic(err)
}
}()
stats, err := e2eLoad("http://localhost:8080/status/200", "http://localhost:8082/stats", 40, 5000)
if err != nil {
return err
}
requestCount := 0
authCallbackCount := 0
authSuccessCount := 0
for _, s := range stats.Stats {
switch s.Name {
case "wasmcustom.envoy_wasm_plugin_on_http_request_headers_count":
requestCount = s.Value
case "wasmcustom.envoy_wasm_plugin_authCallback_count":
authCallbackCount = s.Value
case "wasmcustom.envoy_wasm_plugin_authCallback_success_count":
authSuccessCount = s.Value
}
}
if requestCount == 0 || authCallbackCount == 0 || authSuccessCount == 0 {
return fmt.Errorf("invalid stats: %v", stats)
}
if authCallbackCount != requestCount {
return fmt.Errorf("expected authCallback_count to equal request count, got %d != %d", authCallbackCount, requestCount)
}
if authSuccessCount != requestCount {
return fmt.Errorf("expected authSuccess_count to equal request count, got %d != %d", authSuccessCount, requestCount)
}
return nil
}
func E2eHigressGCTest() error {
if err := os.MkdirAll(filepath.Join("e2e", "higress-gc-test", "build"), 0o755); err != nil {
return err
}
if err := sh.RunV("tinygo", "build", "-target=wasi", "-gc=custom", "-tags='custommalloc nottinygc_envoy'", "-scheduler=none",
"-o", filepath.Join("e2e", "higress-gc-test", "build", "plugin.wasm"), "./e2e/higress-gc-test"); err != nil {
return err
}
if err := sh.RunV("docker-compose", "--file", filepath.Join("e2e", "higress-gc-test", "docker-compose.yml"), "up", "-d"); err != nil {
return err
}
defer func() {
if err := sh.RunV("docker-compose", "--file", filepath.Join("e2e", "higress-gc-test", "docker-compose.yml"), "down", "-v"); err != nil {
panic(err)
}
}()
_, err := e2eLoad("http://localhost:8080/hello", "http://localhost:8082/stats", 2, 10000)
if err != nil {
return err
}
type memStats struct {
Sys int `json:"Sys"`
}
res, err := http.Get("http://localhost:8080/hello")
if err != nil {
return err
}
defer res.Body.Close()
var stats memStats
if err := json.NewDecoder(res.Body).Decode(&stats); err != nil {
return err
}
// We expect around 20MB per VM (this reports per VM stat), a conservative
// 100MB should be a fine check without flakiness
if mem := stats.Sys; mem > 100_000_000 {
return fmt.Errorf("expected <100MB memory used, actual: %d", mem)
}
return nil
}
func E2eGzip() error {
if err := os.MkdirAll(filepath.Join("e2e", "gzip", "build"), 0o755); err != nil {
return err
}
if err := sh.RunV("tinygo", "build", "-target=wasi", "-gc=custom", "-tags='custommalloc'", "-scheduler=none",
"-o", filepath.Join("e2e", "gzip", "build", "main.wasm"), "./e2e/gzip"); err != nil {
return err
}
if err := sh.RunV("wasmtime", filepath.Join("e2e", "gzip", "build", "main.wasm")); err != nil {
return err
}
return nil
}
type counterStat struct {
Name string `json:"name"`
Value int `json:"value"`
}
type counterStats struct {
Stats []counterStat `json:"stats"`
}
// If needed, we can try being more sophisticated later but run some simple load for now.
func e2eLoad(url string, statsURL string, p int, n int) (*counterStats, error) {
wg := sync.WaitGroup{}
var success atomic.Uint32
var fails atomic.Uint32
healthy := false
// Wait for healthy
for i := 0; i < 100; i++ {
time.Sleep(100 * time.Millisecond)
res, err := http.Get(url)
if err != nil {
continue
}
if res.StatusCode == http.StatusOK {
healthy = true
break
}
}
if !healthy {
return nil, errors.New("failed to get healthy in 100 attempts")
}
for i := 0; i < p; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < n; j++ {
res, err := http.Get(url)
switch {
case err != nil:
fallthrough
case res.StatusCode != http.StatusOK:
fails.Add(1)
default:
success.Add(1)
}
}
}()
}
wg.Wait()
res, err := http.Get(fmt.Sprintf("%s?filter=wasmcustom&format=json", statsURL))
if err != nil {
return nil, err
}
defer res.Body.Close()
var stats counterStats
if err := json.NewDecoder(res.Body).Decode(&stats); err != nil {
return nil, err
}
if s := success.Load(); s != uint32(p*n) {
return &stats, fmt.Errorf("expected all requests to succeed, got success=%d, fails=%d, stats=%v", s, fails.Load(), stats)
}
return &stats, nil
}
func init() {
if err := os.MkdirAll(filepath.Join("build", "logs"), 0o755); err != nil {
panic(err)
}
}