forked from deis/k8s-claimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
128 lines (120 loc) · 3.27 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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/arschles/assert"
)
type configureRoutesTestCase struct {
postHandler http.Handler
deleteHandler http.Handler
method string
path string
respCode int
respBody string
}
func TestConfigureRoutes(t *testing.T) {
handler1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("handler1"))
})
handler2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusFound)
w.Write([]byte("handler2"))
})
testCases := []configureRoutesTestCase{
configureRoutesTestCase{
postHandler: handler1,
deleteHandler: handler2,
method: "GET",
path: "/lease",
respCode: http.StatusNotFound,
respBody: "GET /lease not found",
},
configureRoutesTestCase{
postHandler: handler1,
deleteHandler: handler2,
method: "POST",
path: "/lease",
respCode: http.StatusOK,
respBody: "handler1",
},
configureRoutesTestCase{
postHandler: handler1,
deleteHandler: handler2,
method: "POST",
path: "/lease/abc",
respCode: http.StatusNotFound,
respBody: "POST /lease/abc not found",
},
configureRoutesTestCase{
postHandler: handler1,
deleteHandler: handler2,
method: "DELETE",
path: "/lease",
respCode: http.StatusNotFound,
respBody: "DELETE /lease not found",
},
configureRoutesTestCase{
postHandler: handler1,
deleteHandler: handler2,
method: "DELETE",
path: "/lease/abc",
respCode: http.StatusFound,
respBody: "handler2",
},
}
for _, testCase := range testCases {
mux := http.NewServeMux()
configureRoutesWithAuth(mux, testCase.postHandler, testCase.deleteHandler, "auth token")
req, err := http.NewRequest(testCase.method, testCase.path, nil)
req.Header.Set("Authorization", "auth token")
assert.NoErr(t, err)
res := httptest.NewRecorder()
mux.ServeHTTP(res, req)
assert.Equal(t,
res.Code,
testCase.respCode,
fmt.Sprintf("response code for %s %s", testCase.method, testCase.path),
)
assert.Equal(t,
strings.TrimSpace(string(res.Body.Bytes())),
testCase.respBody,
fmt.Sprintf("response body for %s %s", testCase.method, testCase.path),
)
}
}
func TestKubeNamespacesFromConfig(t *testing.T) {
fn := kubeNamespacesFromConfig()
ld, err := fn(nil)
assert.Nil(t, ld, "namespace lister/deleter")
assert.Err(t, err, errNilConfig)
cfg := k8s.KubeConfig{
Kind: "config",
APIVersion: "v1",
Preferences: k8s.Preferences{},
Clusters: []k8s.NamedCluster{
k8s.NamedCluster{Name: "testCluster1"},
},
AuthInfos: []k8s.NamedAuthInfo{
k8s.NamedAuthInfo{Name: "testAuthInfo1"},
},
Contexts: []k8s.NamedContext{
k8s.NamedContext{Name: "testContext1"},
},
CurrentContext: "testctx",
}
ld, err = fn(&cfg)
assert.NoErr(t, err)
assert.NotNil(t, ld, "namespace lister/deleter")
}
func TestHealthz(t *testing.T) {
hdl := CreateHealthzHandler()
req, err := http.NewRequest("GET", "/healthz", nil)
assert.NoErr(t, err)
res := httptest.NewRecorder()
hdl.ServeHTTP(res, req)
assert.Equal(t, res.Code, http.StatusOK, "response code")
}