forked from bitnami-labs/sealed-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
94 lines (79 loc) · 1.8 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
package main
import (
"context"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
"testing"
"time"
certUtil "k8s.io/client-go/util/cert"
)
type testCertStore struct {
sync.Mutex
cert *x509.Certificate
}
func (c *testCertStore) getCert() ([]*x509.Certificate, error) {
c.Lock()
defer c.Unlock()
return []*x509.Certificate{c.cert}, nil
}
func (c *testCertStore) setCert(cert *x509.Certificate) {
c.Lock()
defer c.Unlock()
c.cert = cert
}
func shutdownServer(server *http.Server, t *testing.T) (){
err := server.Shutdown(context.Background())
if err != nil {
t.Fatal(err)
}
}
func TestHttpCert(t *testing.T) {
_, certBefore, err := generatePrivateKeyAndCert(2048)
if err != nil {
t.Fatal(err)
}
_, certAfter, err := generatePrivateKeyAndCert(2048)
if err != nil {
t.Fatal(err)
}
cs := &testCertStore{}
server := httpserver(cs.getCert, nil, nil)
defer shutdownServer(server, t)
hp := *listenAddr
if strings.HasPrefix(hp, ":") {
hp = fmt.Sprintf("localhost%s", hp)
}
time.Sleep(1 * time.Second) // TODO(mkm) find a better way, e.g. retries
check := func(cert *x509.Certificate) {
resp, err := http.Get(fmt.Sprintf("http://%s/v1/cert.pem", hp))
if err != nil {
t.Fatal(err)
}
if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Fatalf("got: %v, want: %v", got, want)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
certs, err := certUtil.ParseCertsPEM(b)
if err != nil {
t.Fatal(err)
}
if got, want := len(certs), 1; got != want {
t.Fatalf("got: %v, want: %v", got, want)
}
if got, want := certs[0], cert; !got.Equal(want) {
t.Fatalf("got: %v, want: %v", got, want)
}
}
cs.setCert(certBefore)
check(certBefore)
cs.setCert(certAfter)
check(certAfter)
}