-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
182 lines (152 loc) · 3.97 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
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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"testing"
"time"
cp "github.com/otiai10/copy"
)
// This package contains the integration tests which spin up a real docker
// container with the local project.
var integration = flag.Bool("integration", false, "run integration tests")
// TestMain ensures that the SAM build output directory exists
func TestMain(m *testing.M) {
flag.Parse()
if *integration {
if err := cp.Copy("testdata/integration/v1", "testdata/integration/src"); err != nil {
fmt.Fprintf(os.Stderr, "failed to set up initial data directory: %v\n", err)
os.Exit(1)
}
}
os.Exit(m.Run())
}
type FirstResponse struct {
Message string `json:"message"`
}
func (r *FirstResponse) CheckMatches(t *testing.T, body []byte) {
var got FirstResponse
err := json.Unmarshal(body, &got)
if err != nil {
t.Fatalf("invalid json response")
}
if r.Message != got.Message {
t.Fatalf("invalid message (response: %s, expected %s)", got.Message, r.Message)
}
}
type SecondResponse struct {
Message string `json:"message"`
Foo int `json:"foo"`
}
func (r *SecondResponse) CheckMatches(t *testing.T, body []byte) {
var got SecondResponse
err := json.Unmarshal(body, &got)
if err != nil {
t.Fatalf("invalid json response")
}
if r.Message != got.Message {
t.Fatalf("invalid message (response: %s, expected %s)", got.Message, r.Message)
}
if r.Foo != got.Foo {
t.Fatalf("invalid foo (response: %d, expected %d)", got.Foo, r.Foo)
}
}
type expectation interface {
CheckMatches(t *testing.T, body []byte)
}
func TestIntegration(t *testing.T) {
if !*integration {
t.Skip("not running integration tests")
}
host := "localhost"
port := 8080
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opts := Opts{
RootDir: "testdata/integration/src",
Args: Args{
Template: "testdata/integration/template.yaml",
},
Host: host,
Port: port,
Verbose: []bool{true, true, true},
}
firstResponse := FirstResponse{
Message: "hello world",
}
errCh := make(chan error)
go func() {
t.Logf("starting web server")
errCh <- run(ctx, opts)
t.Logf("shutting down web server")
}()
assertHTTPResponse(t, host, port, &firstResponse)
// trigger reload
secondResponse := SecondResponse{
Message: "hello other",
Foo: 42,
}
// write the new file contents
newContents, err := ioutil.ReadFile("testdata/integration/v2/HelloWorldFunction/app.py")
if err != nil {
t.Fatalf("reading fixture for file change: %v", err)
}
if err := ioutil.WriteFile("testdata/integration/src/HelloWorldFunction/app.py", newContents, 0644); err != nil {
t.Fatalf("writing new source: %v", err)
}
t.Logf("waiting for reload")
time.Sleep(3 * time.Second)
assertHTTPResponse(t, host, port, &secondResponse)
cancel()
if err := <-errCh; err != nil {
t.Fatalf("error from run function: %v", err)
}
}
func assertHTTPResponse(t *testing.T, host string, port int, expected expectation) {
t.Helper()
http5XXCount := 0
for {
// try to make an HTTP request
t.Logf("making HTTP request")
resp, err := http.Get(fmt.Sprintf("http://%s:%d/hello", host, port))
if err != nil {
if _, ok := err.(net.Error); ok {
if strings.Contains(err.Error(), "connection refused") {
t.Logf("server not up yet")
time.Sleep(time.Second)
continue
}
} else {
t.Fatalf("error making HTTP request: %v", err)
}
}
t.Logf("request completed")
defer resp.Body.Close()
// assert on the response
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == 500 {
t.Logf("got 5XX, current count: %d", http5XXCount)
http5XXCount++
if http5XXCount >= 5 {
t.Fatalf("too many 5XX failures")
}
time.Sleep(time.Second)
continue
} else {
t.Fatalf("bad status code %d", resp.StatusCode)
}
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("could not read response body: %v", err)
}
expected.CheckMatches(t, b)
return
}
}