-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
117 lines (99 loc) · 2.57 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
package main
import (
"bytes"
"io"
"net/http"
"os"
"reflect"
"runtime"
"syscall"
"testing"
"time"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
// Port to start the web server on for testing
var testPortSuccess = "13337"
var testPortPprof = "6060"
// Port to start the web server on for testing failures
var testPortFail = "65536"
func TestMain(t *testing.T) {
t.Run("SetupRoutes", testSetupRoutes)
// The next two functions capture main()'s stdout, implementing
// a timeout in case something goes wrong
os.Setenv("AUTH_PROFILE", ":"+testPortPprof)
t.Run("Main", testMainSuccess)
os.Setenv("AUTH_PROFILE", "")
t.Run("Main_fail", testMainFail)
}
func testMainFail(t *testing.T) {
timer := time.After(time.Second * 10)
old, r, w := newStdout()
outC := make(chan string)
go myIOCopy(r, outC)
os.Setenv("AUTH_PORT", testPortFail)
go main()
time.Sleep(time.Second * 3)
select {
// The channel main() listens for signals on
case quit <- syscall.SIGINT:
case <-timer:
t.Log("timed out starting HTTP server")
t.Fail()
}
resetStdout(w, old)
out := <-outC
assert.Contains(t, out, "error starting server: listen tcp: address "+testPortFail)
}
func testMainSuccess(t *testing.T) {
timer := time.After(time.Second * 10)
old, r, w := newStdout()
outC := make(chan string)
go myIOCopy(r, outC)
os.Setenv("AUTH_PORT", testPortSuccess)
go main()
time.Sleep(time.Second * 3)
// Check the pprof page while we're here
pprofResp, pprofErr := http.Get("http://localhost:" + testPortPprof + "/debug/pprof/")
select {
case quit <- syscall.SIGINT:
case <-timer:
t.Log("timed out starting HTTP server")
t.Fail()
}
resetStdout(w, old)
out := <-outC
assert.Contains(t, out, "http server started on [::]:"+testPortSuccess)
assert.Equal(t, pprofResp.StatusCode, http.StatusOK)
assert.NoError(t, pprofErr)
}
func testSetupRoutes(t *testing.T) {
e := echo.New()
setupRoutes(e, routes)
r := e.Routes()
for _, route := range routes {
for name, method := range route.methods {
for _, i := range r {
if i.Method == name && i.Path == route.path {
// Validates that the entry in routes got properly turned into an echo route handler
assert.Equal(t, i.Name, runtime.FuncForPC(reflect.ValueOf(method).Pointer()).Name())
}
}
}
}
}
func myIOCopy(r *os.File, outC chan string) {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}
func newStdout() (old, newRead, newWrite *os.File) {
old = os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
return old, r, w
}
func resetStdout(w, old *os.File) {
w.Close()
os.Stdout = old
}