-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathserver_test.go
219 lines (178 loc) · 6.03 KB
/
server_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package webhook_test
import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
var _ = Describe("Webhook Server", func() {
var (
ctx context.Context
ctxCancel context.CancelFunc
testHostPort string
client *http.Client
server *webhook.Server
servingOpts envtest.WebhookInstallOptions
)
BeforeEach(func() {
ctx, ctxCancel = context.WithCancel(context.Background())
// closed in indivual tests differently
servingOpts = envtest.WebhookInstallOptions{}
Expect(servingOpts.PrepWithoutInstalling()).To(Succeed())
testHostPort = net.JoinHostPort(servingOpts.LocalServingHost, fmt.Sprintf("%d", servingOpts.LocalServingPort))
// bypass needing to set up the x509 cert pool, etc ourselves
clientTransport, err := rest.TransportFor(&rest.Config{
TLSClientConfig: rest.TLSClientConfig{CAData: servingOpts.LocalServingCAData},
})
Expect(err).NotTo(HaveOccurred())
client = &http.Client{
Transport: clientTransport,
}
server = &webhook.Server{
Host: servingOpts.LocalServingHost,
Port: servingOpts.LocalServingPort,
CertDir: servingOpts.LocalServingCertDir,
}
})
AfterEach(func() {
Expect(servingOpts.Cleanup()).To(Succeed())
})
startServer := func() (done <-chan struct{}) {
doneCh := make(chan struct{})
go func() {
defer GinkgoRecover()
defer close(doneCh)
Expect(server.Start(ctx)).To(Succeed())
}()
// wait till we can ping the server to start the test
Eventually(func() error {
_, err := client.Get(fmt.Sprintf("https://%s/unservedpath", testHostPort))
return err
}).Should(Succeed())
// this is normally called before Start by the manager
Expect(server.InjectFunc(func(i interface{}) error {
boolInj, canInj := i.(interface{ InjectBool(bool) error })
if !canInj {
return nil
}
return boolInj.InjectBool(true)
})).To(Succeed())
return doneCh
}
// TODO(directxman12): figure out a good way to test all the serving setup
// with httptest.Server to get all the niceness from that.
Context("when serving", func() {
PIt("should verify the client CA name when asked to", func() {
})
PIt("should support HTTP/2", func() {
})
// TODO(directxman12): figure out a good way to test the port default, etc
})
It("should panic if a duplicate path is registered", func() {
server.Register("/somepath", &testHandler{})
doneCh := startServer()
Expect(func() { server.Register("/somepath", &testHandler{}) }).To(Panic())
ctxCancel()
Eventually(doneCh, "4s").Should(BeClosed())
})
Context("when registering new webhooks before starting", func() {
It("should serve a webhook on the requested path", func() {
server.Register("/somepath", &testHandler{})
doneCh := startServer()
Eventually(func() ([]byte, error) {
resp, err := client.Get(fmt.Sprintf("https://%s/somepath", testHostPort))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}).Should(Equal([]byte("gadzooks!")))
ctxCancel()
Eventually(doneCh, "4s").Should(BeClosed())
})
It("should inject dependencies eventually, given an inject func is eventually provided", func() {
handler := &testHandler{}
server.Register("/somepath", handler)
doneCh := startServer()
Eventually(func() bool { return handler.injectedField }).Should(BeTrue())
ctxCancel()
Eventually(doneCh, "4s").Should(BeClosed())
})
})
Context("when registering webhooks after starting", func() {
var (
doneCh <-chan struct{}
)
BeforeEach(func() {
doneCh = startServer()
})
AfterEach(func() {
// wait for cleanup to happen
ctxCancel()
Eventually(doneCh, "4s").Should(BeClosed())
})
It("should serve a webhook on the requested path", func() {
server.Register("/somepath", &testHandler{})
resp, err := client.Get(fmt.Sprintf("https://%s/somepath", testHostPort))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
Expect(ioutil.ReadAll(resp.Body)).To(Equal([]byte("gadzooks!")))
})
It("should inject dependencies, if an inject func has been provided already", func() {
handler := &testHandler{}
server.Register("/somepath", handler)
Expect(handler.injectedField).To(BeTrue())
})
})
Context("when using an unmanaged webhook server", func() {
It("should serve a webhook on the requested path", func() {
opts := webhook.Options{
Host: servingOpts.LocalServingHost,
Port: servingOpts.LocalServingPort,
CertDir: servingOpts.LocalServingCertDir,
}
var err error
// overwrite the server so that startServer() starts it
server, err = webhook.NewUnmanaged(opts)
Expect(err).NotTo(HaveOccurred())
server.Register("/somepath", &testHandler{})
doneCh := startServer()
Eventually(func() ([]byte, error) {
resp, err := client.Get(fmt.Sprintf("https://%s/somepath", testHostPort))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}).Should(Equal([]byte("gadzooks!")))
ctxCancel()
Eventually(doneCh, "4s").Should(BeClosed())
})
})
})
type testHandler struct {
injectedField bool
}
func (t *testHandler) InjectBool(val bool) error {
t.injectedField = val
return nil
}
func (t *testHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if _, err := resp.Write([]byte("gadzooks!")); err != nil {
panic("unable to write http response!")
}
}