-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeployer_test.go
More file actions
205 lines (164 loc) · 4.19 KB
/
deployer_test.go
File metadata and controls
205 lines (164 loc) · 4.19 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// +build go1.7
package main
import (
"context"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
type testDeployer struct {
C chan struct{}
P chan *Payload
}
func (t testDeployer) Deploy(p *Payload) error {
t.P <- p
t.C <- struct{}{}
return nil
}
type testNotifier struct {
C chan struct{}
}
func (t testNotifier) Notify(p *Payload) {
t.C <- struct{}{}
}
func StartRequest(payload_data string) (*http.Request, Deploy) {
//
token = "token"
if payload_data == "" {
payload_data = "{\"image\": \"company/app:latest\"}"
}
deployer := Deploy{
Deployer: testDeployer{
C: make(chan struct{}, 1),
P: make(chan *Payload, 1),
},
Notifier: testNotifier{
C: make(chan struct{}, 1),
},
}
ts := httptest.NewServer(deployer.getRoutes())
defer ts.Close()
url := ts.URL + "/service/test"
req, _ := http.NewRequest("PUT", url, strings.NewReader(payload_data))
req.Header.Set("Authorization", token)
client := http.Client{}
_, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
return req, deployer
}
func TestAuthorizer(t *testing.T) {
token = "TEST_TOKEN"
td := []struct {
name string
value string
expected interface{}
}{
{"Missing token", "", http.StatusUnauthorized},
{"Wrong token", "whatever", http.StatusForbidden},
{"Correct token", token, http.StatusOK},
}
handler := Authorize(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("HI"))
}))
for _, tc := range td {
tc := tc
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest("PUT", "/somewhere", nil)
req = req.WithContext(context.Background())
req.Header.Set("Authorization", tc.value)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code != tc.expected {
t.Errorf("Expecting http code %v got %v", tc.expected, recorder.Code)
}
})
}
}
func TestDeployEndpointMethods(t *testing.T) {
t.Skip()
}
func TestDeployEndpointParsesPayload(t *testing.T) {
_, deployer := StartRequest("")
image_name := "company/app:latest"
p := deployer.Deployer.(testDeployer).P
for {
select {
case payload := <-p:
if payload.Artifact != image_name {
t.Errorf("Expecting %s, got %v\n", image_name, payload.Artifact)
}
if payload.ServiceName != "test" {
t.Errorf("Expecting ServiceName of test, got %v", payload.ServiceName)
}
return
case <-time.After(time.Second):
t.Errorf("Expecting /service endpoint to call Deploy(). Timed out.\n")
return
}
}
}
func TestDeployServeHTTPCodes(t *testing.T) {
td := []struct {
name string
deploy Deploy
req *http.Request
expected int
}{
{"TestDeployRouteChecksAuthorization", Deploy{}, httptest.NewRequest("PUT", "/s", nil), http.StatusForbidden},
{"TestDeployEndPointMalformedJSON", Deploy{}, func() *http.Request {
r := httptest.NewRequest("PUT", "/s", strings.NewReader("{image"))
return r.WithContext(context.WithValue(context.Background(), "Authorized", true))
}(), http.StatusBadRequest},
{"TestInvalidServiceName", Deploy{}, func() *http.Request {
r := httptest.NewRequest("PUT", "/--abc1234..", nil)
return r.WithContext(context.WithValue(context.Background(), "Authorized", true))
}(), http.StatusBadRequest},
}
for _, tc := range td {
tc := tc
t.Run(tc.name, func(t *testing.T) {
rec := httptest.NewRecorder()
tc.deploy.ServeHTTP(rec, tc.req)
if rec.Code != tc.expected {
t.Errorf("Expecting HTTP code %d, got %d", tc.expected, rec.Code)
}
})
}
}
func TestValidService(t *testing.T) {
deployer := Deploy{}
if !deployer.Service("test") {
t.Errorf("Expecting true, got false")
}
}
func TestEndpointCallsDeploy(t *testing.T) {
_, deployer := StartRequest("")
called := deployer.Deployer.(testDeployer).C
for {
select {
case <-called:
return
case <-time.After(time.Second):
t.Errorf("Expecting /service endpoint to call Deploy(). Timed out.\n")
return
}
}
}
func TestEndpintNotifys(t *testing.T) {
_, deployer := StartRequest("")
called := deployer.Deployer.(testDeployer).C
for {
select {
case <-called:
return
case <-time.After(time.Second):
t.Errorf("Expecting /service endpoint to call Notify(). Timed out.\n")
return
}
}
}