-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
64 lines (49 loc) · 1.34 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
//go:build !wasip1 || nofastlyhostcalls
// ^ important build tags
package fastlytest
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"testing"
)
func TestMain(m *testing.M) {
// wrap os.Exit in a function call so that defer's fire before process exit.
os.Exit(func() int {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a test server for each handler to test, this one adds a custom
// "Server" header to the response
srvCustom := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Server", "httptest-server")
}))
defer srvCustom.Close()
// create fastly config, add a backend for each test server or test case
cfg := Config{
LocalServer: LocalServer{
Backends: map[string]Backend{
"test-via": {URL: srvCustom.URL},
},
},
}
// create viceroy runner, set the config
vic, err := NewViceroy(cfg)
if err != nil {
panic(err)
}
defer vic.Cleanup()
// execute the go test command for this package via viceroy
if err = vic.GoTestPkg(ctx, "fastlytest").Run(); err == nil {
return 0
}
// exit with the same code after the tests have run via viceroy to indicate pass/fail
var eerr *exec.ExitError
if errors.Is(err, eerr) {
return eerr.ProcessState.ExitCode()
}
return -1
}())
}