forked from knative/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_test.go
138 lines (116 loc) · 3.39 KB
/
webhook_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
/*
Copyright 2019 The Knative 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
import (
"context"
"crypto/tls"
"net"
"testing"
"time"
"golang.org/x/sync/errgroup"
// Make system.Namespace() work in tests.
_ "knative.dev/pkg/system/testing"
. "knative.dev/pkg/reconciler/testing"
)
func newDefaultOptions() Options {
return Options{
ServiceName: "webhook",
Port: 8443,
SecretName: "webhook-certs",
}
}
func newCustomOptions() Options {
return Options{
ServiceName: "webhook",
Port: 8443,
SecretName: "webhook-certs",
ServerPrivateKeyName: "tls.key",
ServerCertificateName: "tls.crt",
}
}
const (
testResourceName = "test-resource"
user1 = "brutto@knative.dev"
)
func newNonRunningTestWebhook(t *testing.T, options Options, acs ...interface{}) (
ctx context.Context, ac *Webhook, cancel context.CancelFunc) {
t.Helper()
// override the grace period so it drains quickly
options.GracePeriod = 100 * time.Millisecond
// Create fake clients
ctx, ctxCancel, informers := SetupFakeContextWithCancel(t)
ctx = WithOptions(ctx, options)
stopCb, err := RunAndSyncInformers(ctx, informers...)
if err != nil {
t.Fatal("StartInformers() =", err)
}
cancel = func() {
ctxCancel()
stopCb()
}
ac, err = New(ctx, acs)
if err != nil {
t.Fatal("Failed to create new admission controller:", err)
}
return
}
func TestRegistrationStopChanFire(t *testing.T) {
wh, serverURL, _, cancel, err := testSetupNoTLS(t)
if err != nil {
t.Fatal("testSetup() =", err)
}
defer cancel()
stopCh := make(chan struct{})
var g errgroup.Group
g.Go(func() error {
return wh.Run(stopCh)
})
close(stopCh)
if err := g.Wait(); err != nil {
t.Fatal("Error during run: ", err)
}
conn, err := net.Dial("tcp", serverURL)
if err == nil {
conn.Close()
t.Error("Unexpected success to dial to ", serverURL)
}
}
func newAdmissionControllerWebhook(t *testing.T, options Options, acs ...interface{}) (*Webhook, error) {
ctx, cancel, _ := SetupFakeContextWithCancel(t)
defer cancel()
ctx = WithOptions(ctx, options)
return New(ctx, acs)
}
func TestTLSMinVersionWebhookOption(t *testing.T) {
opts := newDefaultOptions()
t.Run("when TLSMinVersion is not configured, and the default is used", func(t *testing.T) {
_, err := newAdmissionControllerWebhook(t, opts)
if err != nil {
t.Fatal("Unexpected error", err)
}
})
t.Run("when the TLS minimum version configured is supported", func(t *testing.T) {
opts.TLSMinVersion = tls.VersionTLS12
_, err := newAdmissionControllerWebhook(t, opts)
if err != nil {
t.Fatal("Unexpected error", err)
}
})
t.Run("when the TLS minimum version configured is not supported", func(t *testing.T) {
opts.TLSMinVersion = tls.VersionTLS11
_, err := newAdmissionControllerWebhook(t, opts)
if err == nil {
t.Fatal("Admission Controller Webhook creation expected to fail due to unsupported TLS version")
}
})
}