|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package webhook_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "net" |
| 23 | + "net/http" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + "k8s.io/client-go/rest" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 30 | +) |
| 31 | + |
| 32 | +var _ = Describe("Webhook Server", func() { |
| 33 | + var ( |
| 34 | + stop chan struct{} |
| 35 | + testHostPort string |
| 36 | + client *http.Client |
| 37 | + server *webhook.Server |
| 38 | + ) |
| 39 | + |
| 40 | + BeforeEach(func() { |
| 41 | + stop = make(chan struct{}) |
| 42 | + // closed in indivual tests differently |
| 43 | + |
| 44 | + servingOpts := envtest.WebhookInstallOptions{} |
| 45 | + Expect(servingOpts.PrepWithoutInstalling()).To(Succeed()) |
| 46 | + |
| 47 | + testHostPort = net.JoinHostPort(servingOpts.LocalServingHost, fmt.Sprintf("%d", servingOpts.LocalServingPort)) |
| 48 | + |
| 49 | + // bypass needing to set up the x509 cert pool, etc ourselves |
| 50 | + clientTransport, err := rest.TransportFor(&rest.Config{ |
| 51 | + TLSClientConfig: rest.TLSClientConfig{CAData: servingOpts.LocalServingCAData}, |
| 52 | + }) |
| 53 | + Expect(err).NotTo(HaveOccurred()) |
| 54 | + client = &http.Client{ |
| 55 | + Transport: clientTransport, |
| 56 | + } |
| 57 | + |
| 58 | + server = &webhook.Server{ |
| 59 | + Host: servingOpts.LocalServingHost, |
| 60 | + Port: servingOpts.LocalServingPort, |
| 61 | + CertDir: servingOpts.LocalServingCertDir, |
| 62 | + } |
| 63 | + |
| 64 | + // TODO(directxman12): cleanup generated certificate dir, etc |
| 65 | + }) |
| 66 | + |
| 67 | + startServer := func() (done <-chan struct{}) { |
| 68 | + doneCh := make(chan struct{}) |
| 69 | + go func() { |
| 70 | + defer GinkgoRecover() |
| 71 | + defer close(doneCh) |
| 72 | + Expect(server.Start(stop)).To(Succeed()) |
| 73 | + }() |
| 74 | + // wait till we can ping the server to start the test |
| 75 | + Eventually(func() error { |
| 76 | + _, err := client.Get(fmt.Sprintf("https://%s/unservedpath", testHostPort)) |
| 77 | + return err |
| 78 | + }).Should(Succeed()) |
| 79 | + |
| 80 | + // this is normally called before Start by the manager |
| 81 | + Expect(server.InjectFunc(func(i interface{}) error { |
| 82 | + boolInj, canInj := i.(interface{ InjectBool(bool) error }) |
| 83 | + if !canInj { |
| 84 | + return nil |
| 85 | + } |
| 86 | + return boolInj.InjectBool(true) |
| 87 | + })).To(Succeed()) |
| 88 | + |
| 89 | + return doneCh |
| 90 | + } |
| 91 | + |
| 92 | + // TODO(directxman12): figure out a good way to test all the serving setup |
| 93 | + // with httptest.Server to get all the niceness from that. |
| 94 | + |
| 95 | + Context("when serving", func() { |
| 96 | + PIt("should verify the client CA name when asked to", func() { |
| 97 | + |
| 98 | + }) |
| 99 | + PIt("should support HTTP/2", func() { |
| 100 | + |
| 101 | + }) |
| 102 | + |
| 103 | + // TODO(directxman12): figure out a good way to test the port default, etc |
| 104 | + }) |
| 105 | + |
| 106 | + It("should panic if a duplicate path is registered", func() { |
| 107 | + server.Register("/somepath", &testHandler{}) |
| 108 | + doneCh := startServer() |
| 109 | + |
| 110 | + Expect(func() { server.Register("/somepath", &testHandler{}) }).To(Panic()) |
| 111 | + |
| 112 | + close(stop) |
| 113 | + Eventually(doneCh, "4s").Should(BeClosed()) |
| 114 | + }) |
| 115 | + |
| 116 | + Context("when registering new webhooks before starting", func() { |
| 117 | + It("should serve a webhook on the requested path", func() { |
| 118 | + server.Register("/somepath", &testHandler{}) |
| 119 | + |
| 120 | + doneCh := startServer() |
| 121 | + |
| 122 | + Eventually(func() ([]byte, error) { |
| 123 | + resp, err := client.Get(fmt.Sprintf("https://%s/somepath", testHostPort)) |
| 124 | + Expect(err).NotTo(HaveOccurred()) |
| 125 | + defer resp.Body.Close() |
| 126 | + return ioutil.ReadAll(resp.Body) |
| 127 | + }).Should(Equal([]byte("gadzooks!"))) |
| 128 | + |
| 129 | + close(stop) |
| 130 | + Eventually(doneCh, "4s").Should(BeClosed()) |
| 131 | + }) |
| 132 | + |
| 133 | + It("should inject dependencies eventually, given an inject func is eventually provided", func() { |
| 134 | + handler := &testHandler{} |
| 135 | + server.Register("/somepath", handler) |
| 136 | + doneCh := startServer() |
| 137 | + |
| 138 | + Eventually(func() bool { return handler.injectedField }).Should(BeTrue()) |
| 139 | + |
| 140 | + close(stop) |
| 141 | + Eventually(doneCh, "4s").Should(BeClosed()) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + Context("when registering webhooks after starting", func() { |
| 146 | + var ( |
| 147 | + doneCh <-chan struct{} |
| 148 | + ) |
| 149 | + BeforeEach(func() { |
| 150 | + doneCh = startServer() |
| 151 | + }) |
| 152 | + AfterEach(func() { |
| 153 | + // wait for cleanup to happen |
| 154 | + close(stop) |
| 155 | + Eventually(doneCh, "4s").Should(BeClosed()) |
| 156 | + }) |
| 157 | + |
| 158 | + It("should serve a webhook on the requested path", func() { |
| 159 | + server.Register("/somepath", &testHandler{}) |
| 160 | + resp, err := client.Get(fmt.Sprintf("https://%s/somepath", testHostPort)) |
| 161 | + Expect(err).NotTo(HaveOccurred()) |
| 162 | + defer resp.Body.Close() |
| 163 | + |
| 164 | + Expect(ioutil.ReadAll(resp.Body)).To(Equal([]byte("gadzooks!"))) |
| 165 | + }) |
| 166 | + |
| 167 | + It("should inject dependencies, if an inject func has been provided already", func() { |
| 168 | + handler := &testHandler{} |
| 169 | + server.Register("/somepath", handler) |
| 170 | + Expect(handler.injectedField).To(BeTrue()) |
| 171 | + }) |
| 172 | + }) |
| 173 | +}) |
| 174 | + |
| 175 | +type testHandler struct { |
| 176 | + injectedField bool |
| 177 | +} |
| 178 | + |
| 179 | +func (t *testHandler) InjectBool(val bool) error { |
| 180 | + t.injectedField = val |
| 181 | + return nil |
| 182 | +} |
| 183 | +func (t *testHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 184 | + if _, err := resp.Write([]byte("gadzooks!")); err != nil { |
| 185 | + panic("unable to write http response!") |
| 186 | + } |
| 187 | +} |
0 commit comments