Migrate the webhook-certgen program to inside ingress repo (#7475)
This commit is contained in:
parent
9a9ad47857
commit
492c7b0d94
18 changed files with 1910 additions and 0 deletions
61
images/kube-webhook-certgen/rootfs/pkg/certs/certs_test.go
Normal file
61
images/kube-webhook-certgen/rootfs/pkg/certs/certs_test.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package certs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = fmt.Fprintf(w, "Hello World")
|
||||
}
|
||||
|
||||
func TestCertificateCreation(t *testing.T) {
|
||||
|
||||
ca, cert, key := GenerateCerts("localhost")
|
||||
|
||||
c, err := tls.X509KeyPair(cert, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
caCertPool := x509.NewCertPool()
|
||||
caCertPool.AppendCertsFromPEM(ca)
|
||||
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: caCertPool,
|
||||
ServerName: "localhost"}}
|
||||
|
||||
ts := httptest.NewUnstartedServer(http.HandlerFunc(handler))
|
||||
ts.TLS = &tls.Config{Certificates: []tls.Certificate{c}}
|
||||
ts.StartTLS()
|
||||
defer ts.Close()
|
||||
|
||||
client := &http.Client{Transport: tr}
|
||||
res, err := client.Get(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Errorf("Response code was %v; want 200", res.StatusCode)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := []byte("Hello World")
|
||||
|
||||
if bytes.Compare(expected, body) != 0 {
|
||||
t.Errorf("Response body was '%v'; want '%v'", expected, body)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue