-
Notifications
You must be signed in to change notification settings - Fork 3
/
runner_test.go
171 lines (136 loc) · 4.1 KB
/
runner_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
package http_wasm_tck
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"testing"
"github.com/http-wasm/http-wasm-host-go/tck"
)
// TestMain is the entrypoint for the test binary. Commands like backend and extract-guest
// don't actually run tests, but we can still have a single binary for all commands by
// implementing them as part of TestMain.
func TestMain(m *testing.M) {
var help bool
flag.BoolVar(&help, "h", false, "print usage")
flag.Parse()
if help || flag.NArg() == 0 {
printUsage(os.Stderr)
os.Exit(0)
}
subCmd := flag.Arg(0)
switch subCmd {
case "backend":
backend(flag.Args()[1:])
case "extract-guest":
extractGuest(flag.Args()[1:])
case "run":
// Make sure test runs are outputted.
_ = flag.Lookup("test.v").Value.Set("true")
flags := flag.NewFlagSet("run", flag.ExitOnError)
flags.SetOutput(os.Stderr)
var help bool
flags.BoolVar(&help, "h", false, "print usage")
var addr string
flags.StringVar(&addr, "url", "http://localhost:8080", "URL to send test requests too")
_ = flags.Parse(flag.Args()[1:])
if help {
printRunUsage(os.Stderr, flags)
os.Exit(0)
}
os.Exit(m.Run())
default:
fmt.Fprintf(os.Stderr, "unknown command: %s\n", subCmd)
printUsage(os.Stderr)
os.Exit(1)
}
}
func backend(args []string) {
flags := flag.NewFlagSet("backend", flag.ExitOnError)
var help bool
flags.BoolVar(&help, "h", false, "print usage")
var addr string
flags.StringVar(&addr, "addr", "0.0.0.0:9080", "address to listen on")
_ = flags.Parse(args)
if help {
printBackendUsage(os.Stderr, flags)
os.Exit(0)
}
exitCh := make(chan os.Signal, 1)
signal.Notify(exitCh, os.Interrupt, syscall.SIGTERM)
s := tck.StartBackend(addr)
// Don't change this string since we allow it to be parsed to find the address.
fmt.Printf("Started backend server on %s\n", s.Listener.Addr())
for {
select {
case <-exitCh:
return
}
}
}
func extractGuest(args []string) {
flags := flag.NewFlagSet("extract-guest", flag.ExitOnError)
flags.SetOutput(os.Stderr)
var help bool
flags.BoolVar(&help, "h", false, "print usage")
_ = flags.Parse(args)
if help {
printExtractGuestUsage(os.Stderr)
os.Exit(0)
}
if flags.NArg() < 1 {
fmt.Fprintln(os.Stderr, "missing path to extract wasm file to")
printExtractGuestUsage(os.Stderr)
os.Exit(1)
}
wasmPath := flags.Arg(0)
if err := os.WriteFile(wasmPath, tck.GuestWASM, 0o644); err != nil {
fmt.Printf("error opening path: %v\n", err)
os.Exit(1)
}
}
func printUsage(stdErr io.Writer) {
fmt.Fprintln(stdErr, "http-wasm TCK")
fmt.Fprintln(stdErr)
fmt.Fprintln(stdErr, "Usage:\n http-wasm-tck <command>")
fmt.Fprintln(stdErr)
fmt.Fprintln(stdErr, "Commands:")
fmt.Fprintln(stdErr, " extract-guest\t\tExtracts the guest wasm file")
fmt.Fprintln(stdErr, " backend\t\tStarts the backend server")
fmt.Fprintln(stdErr, " run\t\t\tRuns the tests")
}
func printBackendUsage(stdErr io.Writer, flags *flag.FlagSet) {
fmt.Fprintln(stdErr, "http-wasm TCK - backend server")
fmt.Fprintln(stdErr)
fmt.Fprintln(stdErr, "Usage:\n http-wasm-tck backend <options>")
fmt.Fprintln(stdErr)
fmt.Fprintln(stdErr, "Options:")
flags.PrintDefaults()
}
func printExtractGuestUsage(stdErr io.Writer) {
fmt.Fprintln(stdErr, "http-wasm TCK - extract guest")
fmt.Fprintln(stdErr)
fmt.Fprintln(stdErr, "Usage:\n http-wasm-tck extract-guest <path to wasm file>")
fmt.Fprintln(stdErr)
}
func printRunUsage(stdErr io.Writer, flags *flag.FlagSet) {
fmt.Fprintln(stdErr, "http-wasm TCK - run the tests")
fmt.Fprintln(stdErr)
fmt.Fprintln(stdErr, "Usage:\n http-wasm-tck run <options>")
fmt.Fprintln(stdErr)
fmt.Fprintln(stdErr, "Options:")
flags.PrintDefaults()
}
// TestTCK is a standard unit test function. It is not invoked by us directly, instead being
// run by Go's test framework reflectively when testing.M.Run is invoked in TestMain.
func TestTCK(t *testing.T) {
flags := flag.NewFlagSet("run", flag.ExitOnError)
flags.SetOutput(os.Stderr)
var addr string
flags.StringVar(&addr, "url", "http://localhost:8080", "URL to send test requests too")
_ = flags.Parse(flag.Args()[1:])
tck.Run(t, http.DefaultClient, addr)
}