-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaos_test.go
184 lines (145 loc) Β· 4.73 KB
/
chaos_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
183
184
package chaos
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path"
"testing"
"time"
"github.com/falzm/chaos"
"github.com/gorilla/mux"
)
type testClient struct {
chaos *Client
api *Client
}
func (c *testClient) sendRequest(method, path string) (int, string, float64, error) {
req, err := http.NewRequest(method, fmt.Sprintf("http://test%s", path), nil)
if err != nil {
return 0, "", 0.0, fmt.Errorf("error creating HTTP request: %s\n", err)
}
startTime := time.Now()
res, err := c.api.http.Do(req)
if err != nil {
return 0, "", 0.0, fmt.Errorf("error sending HTTP request: %s\n", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return 0, "", 0.0, fmt.Errorf("unable to read response body: %s", err)
}
body = bytes.TrimSpace(body)
return res.StatusCode, string(body), time.Now().Sub(startTime).Seconds() * 1000, nil
}
func (c *testClient) testRouteChaos(method, path string, spec *Spec, testfunc func() error) error {
if err := c.chaos.AddRouteChaos(method, path, spec); err != nil {
return fmt.Errorf("unable to add route chaos spec: %s", err)
}
if err := testfunc(); err != nil {
return err
}
if err := c.chaos.DeleteRouteChaos(method, path); err != nil {
return fmt.Errorf("unable to delete chaos spec from route: %s", err)
}
return nil
}
func Test_Chaos(t *testing.T) {
var testcli testClient
tmpDir, err := ioutil.TempDir(os.TempDir(), "chaos")
if err != nil {
t.Errorf("unable to create temporary test directory: %s", err)
}
defer os.RemoveAll(tmpDir)
apiSock := path.Join(tmpDir, "api.sock")
chaosSock := path.Join(tmpDir, "chaos.sock")
chaos, err := chaos.NewChaos(fmt.Sprintf("unix:%s", chaosSock))
if err != nil {
t.Errorf("unable to bind chaos management controller UNIX socket: %s", err)
}
listener, err := net.Listen("unix", apiSock)
if err != nil {
t.Errorf("unable to bind UNIX socket: %s", err)
}
router := mux.NewRouter()
router.HandleFunc("/api/{action}",
func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintln(rw, "ohai!")
})
server := http.Server{
Handler: chaos.Handler(router.ServeHTTP),
}
t.Logf("startup up test server (listening on %s)\n", apiSock)
go server.Serve(listener)
testcli.api = NewClient("unix:" + apiSock)
testcli.chaos = NewClient("unix:" + chaosSock)
// Test adding route chaos with invalid error spec
if err := testcli.testRouteChaos("POST", "/api/a", NewSpec().Error(0, "", 1.0), nil); err == nil {
t.Errorf("route chaos test failed: %s", err)
t.FailNow()
}
// Test adding route chaos with invalid delay spec
if err := testcli.testRouteChaos("POST", "/api/a", NewSpec().Delay(0, 1.0), nil); err == nil {
t.Errorf("route chaos test failed: %s", err)
t.FailNow()
}
// Test chaos delay/error injection
if err := testcli.testRouteChaos("POST", "/api/a", NewSpec().
Delay(3000, 1.0).
Error(http.StatusGatewayTimeout, "Whoopsie...", 1.0),
func() error {
var (
expectedStatusCode = http.StatusGatewayTimeout
expectedErrMessage = "Whoopsie..."
expectedMinDelay float64 = 3000
)
status, errMessage, latency, err := testcli.sendRequest("POST", "/api/a")
if err != nil {
return fmt.Errorf("error sending HTTP request: %s\n", err)
}
if latency <= expectedMinDelay {
return fmt.Errorf("expected minimum request delay > %.2fms but took %.2fms", expectedMinDelay, latency)
}
if status != expectedStatusCode {
return fmt.Errorf("expected status code %d but got %d", expectedStatusCode, status)
}
if errMessage != expectedErrMessage {
return fmt.Errorf("expected error message %q but got %q", expectedErrMessage, errMessage)
}
return nil
}); err != nil {
t.Errorf("route chaos test failed: %s", err)
t.FailNow()
}
// Test chaos effect temporary duration
if err := testcli.testRouteChaos("PUT", "/api/b", NewSpec().
Error(http.StatusTooManyRequests, "", 1.0).
During("3s"),
func() error {
var expectedStatusCode = http.StatusTooManyRequests
status, _, _, err := testcli.sendRequest("PUT", "/api/b")
if err != nil {
return fmt.Errorf("error sending HTTP request: %s\n", err)
}
if status != expectedStatusCode {
return fmt.Errorf("expected status code %d but got %d", expectedStatusCode, status)
}
time.Sleep(5 * time.Second)
expectedStatusCode = http.StatusOK
status, _, _, err = testcli.sendRequest("PUT", "/api/b")
if err != nil {
return fmt.Errorf("error sending HTTP request: %s\n", err)
}
if status != expectedStatusCode {
return fmt.Errorf("expected status code %d but got %d", expectedStatusCode, status)
}
return nil
}); err != nil {
t.Errorf("route chaos test failed: %s", err)
t.FailNow()
}
t.Log("shutting down test server")
server.Shutdown(nil)
}