-
Notifications
You must be signed in to change notification settings - Fork 18
/
web_test.go
153 lines (122 loc) · 3.37 KB
/
web_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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/CiscoCloud/marathon-consul/apps"
"github.com/CiscoCloud/marathon-consul/consul"
"github.com/CiscoCloud/marathon-consul/events"
"github.com/CiscoCloud/marathon-consul/mocks"
"github.com/CiscoCloud/marathon-consul/tasks"
"github.com/stretchr/testify/assert"
)
var (
testTask = &tasks.Task{
Timestamp: "2014-03-01T23:29:30.158Z",
SlaveID: "20140909-054127-177048842-5050-1494-0",
ID: "my-app_0-1396592784349",
TaskStatus: "TASK_RUNNING",
AppID: "/my-app",
Host: "slave-1234.acme.org",
Ports: []int{31372},
Version: "2014-04-04T06:26:23.051Z",
}
testApp = &apps.App{ID: "test"}
testTaskKV = testTask.KV()
testAppKV = testApp.KV()
)
func TestHealthHandler(t *testing.T) {
t.Parallel()
req, err := http.NewRequest("GET", "http://example.com/health", nil)
assert.Nil(t, err)
recorder := httptest.NewRecorder()
HealthHandler(recorder, req)
assert.Equal(t, 200, recorder.Code)
assert.Equal(t, "OK\n", recorder.Body.String())
}
func TestForwardHandlerHandleAppEvent(t *testing.T) {
t.Parallel()
// create a handler
kv := mocks.NewKVer()
consul := consul.NewConsul(kv, "")
handler := ForwardHandler{consul}
body, err := json.Marshal(events.APIPostEvent{"api_post_event", testApp})
assert.Nil(t, err)
// test!
err = handler.HandleAppEvent(body)
assert.Nil(t, err)
result, _, err := kv.Get(testApp.Key())
assert.Nil(t, err)
assert.Equal(t, result, testAppKV)
}
func TestForwardHandlerHandleTerminationEvent(t *testing.T) {
t.Parallel()
// create a handler
kv := mocks.NewKVer()
consul := consul.NewConsul(kv, "")
handler := ForwardHandler{consul}
err := consul.UpdateApp(testApp)
assert.Nil(t, err)
body, err := json.Marshal(events.AppTerminatedEvent{
Type: "app_terminated_event",
AppID: testApp.ID,
})
assert.Nil(t, err)
// test!
err = handler.HandleTerminationEvent(body)
assert.Nil(t, err)
result, _, err := kv.Get(testApp.Key())
assert.Nil(t, err)
assert.Nil(t, result)
}
func tempTaskBody(status string) []byte {
body, _ := json.Marshal(testTask)
return []byte(strings.Replace(
string(body),
testTask.TaskStatus,
status,
1,
))
}
func TestForwardHandlerHandleStatusEvent(t *testing.T) {
t.Parallel()
// create a handler
kv := mocks.NewKVer()
consul := consul.NewConsul(kv, "")
handler := ForwardHandler{consul}
// deletes
for _, status := range []string{"TASK_FINISHED", "TASK_FAILED", "TASK_KILLED", "TASK_LOST"} {
tempBody := tempTaskBody(status)
err := consul.UpdateTask(testTask)
assert.Nil(t, err)
// test
err = handler.HandleStatusEvent(tempBody)
assert.Nil(t, err)
// assert
result, _, err := kv.Get(testTask.Key())
assert.Nil(t, err)
assert.Nil(t, result)
}
// puts
for _, status := range []string{"TASK_STAGING", "TASK_STARTING", "TASK_RUNNING"} {
tempBody := tempTaskBody(status)
tempTask, _ := tasks.ParseTask(tempBody)
// test
err := handler.HandleStatusEvent(tempBody)
assert.Nil(t, err)
// assert
result, _, err := kv.Get(tempTask.Key())
assert.Nil(t, err)
assert.Equal(t, result, tempTask.KV())
// cleanup
_, err = kv.Delete(testTask.Key())
assert.Nil(t, err)
}
// bad status
tempBody := tempTaskBody("TASK_BATMAN")
err := handler.HandleStatusEvent(tempBody)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "unknown task status")
}