-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathadmissionwebhook_test.go
More file actions
131 lines (115 loc) · 3.57 KB
/
admissionwebhook_test.go
File metadata and controls
131 lines (115 loc) · 3.57 KB
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
package cabundleinjector
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
admissionregv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/service-ca-operator/pkg/controller/api"
)
func TestWebhookCABundleInjectorSync(t *testing.T) {
testCABundle := []byte("something")
tests := []struct {
name string
webhooks []admissionregv1.ValidatingWebhook
expectedWebhooks []admissionregv1.ValidatingWebhook
wantErr bool
}{
{
name: "no webhooks",
},
{
name: "single webhook to fill",
webhooks: []admissionregv1.ValidatingWebhook{
{
ClientConfig: admissionregv1.WebhookClientConfig{},
},
},
expectedWebhooks: []admissionregv1.ValidatingWebhook{
{
ClientConfig: admissionregv1.WebhookClientConfig{
CABundle: testCABundle,
},
},
},
},
{
name: "multiple webhooks to fill",
webhooks: []admissionregv1.ValidatingWebhook{
{
ClientConfig: admissionregv1.WebhookClientConfig{},
},
{
ClientConfig: admissionregv1.WebhookClientConfig{
CABundle: []byte("random other string"),
},
},
},
expectedWebhooks: []admissionregv1.ValidatingWebhook{
{
ClientConfig: admissionregv1.WebhookClientConfig{
CABundle: testCABundle,
},
},
{
ClientConfig: admissionregv1.WebhookClientConfig{
CABundle: testCABundle,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testWebhook := &admissionregv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "test-webhook",
Annotations: map[string]string{
api.InjectCABundleAnnotationName: "true",
},
},
Webhooks: tt.webhooks,
}
testCtx, cancel := context.WithCancel(context.Background())
defer cancel()
webhookClient := fake.NewSimpleClientset(testWebhook)
webhookInformer := informers.NewSharedInformerFactory(webhookClient, 1*time.Hour)
go webhookInformer.Start(testCtx.Done())
waitSuccess := cache.WaitForCacheSync(testCtx.Done(), webhookInformer.Admissionregistration().V1().ValidatingWebhookConfigurations().Informer().HasSynced)
require.True(t, waitSuccess)
cache := &bundleCache{}
cache.Store(testCABundle)
injector := webhookCABundleInjector[admissionregv1.ValidatingWebhookConfiguration]{
webhookConfigType: "testwebhook",
newWebhookConfigAccessor: newValidatingWebhookAccessor,
client: webhookClient.AdmissionregistrationV1().ValidatingWebhookConfigurations(),
lister: webhookInformer.Admissionregistration().V1().ValidatingWebhookConfigurations().Lister(),
caBundle: cache,
}
if gotErr := injector.Sync(testCtx, testContext{"test-webhook"}); (gotErr != nil) != tt.wantErr {
t.Errorf("webhookCABundleInjector.Sync() = %v, want %v", gotErr, tt.wantErr)
}
gotWebhook, err := webhookClient.AdmissionregistrationV1().ValidatingWebhookConfigurations().Get(testCtx, "test-webhook", metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t, tt.expectedWebhooks, gotWebhook.Webhooks)
})
}
}
type testContext struct {
key string
}
func (c testContext) Queue() workqueue.RateLimitingInterface {
return nil
}
func (c testContext) QueueKey() string {
return c.key
}
func (c testContext) Recorder() events.Recorder {
return nil
}