2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-09-14 15:42:42 +00:00
|
|
|
|
|
|
|
package webhookcachecleaner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
2020-10-30 16:03:25 +00:00
|
|
|
authv1alpha "go.pinniped.dev/generated/1.19/apis/concierge/authentication/v1alpha1"
|
2020-09-18 19:56:24 +00:00
|
|
|
pinnipedfake "go.pinniped.dev/generated/1.19/client/clientset/versioned/fake"
|
|
|
|
pinnipedinformers "go.pinniped.dev/generated/1.19/client/informers/externalversions"
|
2020-10-30 19:02:21 +00:00
|
|
|
"go.pinniped.dev/internal/controller/authenticator/authncache"
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
|
|
|
"go.pinniped.dev/internal/testutil/testlogger"
|
2020-09-14 15:42:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestController(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
testKey1 := authncache.Key{
|
2020-10-30 16:03:25 +00:00
|
|
|
APIGroup: "authentication.concierge.pinniped.dev",
|
2020-10-30 16:39:26 +00:00
|
|
|
Kind: "WebhookAuthenticator",
|
2020-09-21 16:37:54 +00:00
|
|
|
Namespace: "test-namespace",
|
|
|
|
Name: "test-name-one",
|
|
|
|
}
|
2020-10-30 19:02:21 +00:00
|
|
|
testKey2 := authncache.Key{
|
2020-10-30 16:03:25 +00:00
|
|
|
APIGroup: "authentication.concierge.pinniped.dev",
|
2020-10-30 16:39:26 +00:00
|
|
|
Kind: "WebhookAuthenticator",
|
2020-09-21 16:37:54 +00:00
|
|
|
Namespace: "test-namespace",
|
|
|
|
Name: "test-name-two",
|
|
|
|
}
|
2020-10-30 19:02:21 +00:00
|
|
|
testKeyNonwebhook := authncache.Key{
|
2020-10-30 16:03:25 +00:00
|
|
|
APIGroup: "authentication.concierge.pinniped.dev",
|
2020-10-30 19:02:21 +00:00
|
|
|
Kind: "SomeOtherAuthenticator",
|
2020-09-21 16:37:54 +00:00
|
|
|
Namespace: "test-namespace",
|
|
|
|
Name: "test-name-one",
|
|
|
|
}
|
2020-09-14 15:42:42 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2020-10-30 19:02:21 +00:00
|
|
|
webhooks []runtime.Object
|
|
|
|
initialCache map[authncache.Key]authncache.Value
|
2020-09-14 15:42:42 +00:00
|
|
|
wantErr string
|
|
|
|
wantLogs []string
|
2020-10-30 19:02:21 +00:00
|
|
|
wantCacheKeys []authncache.Key
|
2020-09-14 15:42:42 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no change",
|
2020-10-30 19:02:21 +00:00
|
|
|
initialCache: map[authncache.Key]authncache.Value{testKey1: nil},
|
|
|
|
webhooks: []runtime.Object{
|
2020-10-30 16:39:26 +00:00
|
|
|
&authv1alpha.WebhookAuthenticator{
|
2020-09-14 15:42:42 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: testKey1.Namespace,
|
|
|
|
Name: testKey1.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-10-30 19:02:21 +00:00
|
|
|
wantCacheKeys: []authncache.Key{testKey1},
|
2020-09-14 15:42:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-10-30 19:02:21 +00:00
|
|
|
name: "authenticators not yet added",
|
2020-09-14 15:42:42 +00:00
|
|
|
initialCache: nil,
|
2020-10-30 19:02:21 +00:00
|
|
|
webhooks: []runtime.Object{
|
2020-10-30 16:39:26 +00:00
|
|
|
&authv1alpha.WebhookAuthenticator{
|
2020-09-14 15:42:42 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: testKey1.Namespace,
|
|
|
|
Name: testKey1.Name,
|
|
|
|
},
|
|
|
|
},
|
2020-10-30 16:39:26 +00:00
|
|
|
&authv1alpha.WebhookAuthenticator{
|
2020-09-14 15:42:42 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: testKey2.Namespace,
|
|
|
|
Name: testKey2.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-10-30 19:02:21 +00:00
|
|
|
wantCacheKeys: []authncache.Key{},
|
2020-09-14 15:42:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-21 16:37:54 +00:00
|
|
|
name: "successful cleanup",
|
2020-10-30 19:02:21 +00:00
|
|
|
initialCache: map[authncache.Key]authncache.Value{
|
2020-09-21 16:37:54 +00:00
|
|
|
testKey1: nil,
|
|
|
|
testKey2: nil,
|
|
|
|
testKeyNonwebhook: nil,
|
2020-09-14 15:42:42 +00:00
|
|
|
},
|
2020-10-30 19:02:21 +00:00
|
|
|
webhooks: []runtime.Object{
|
2020-10-30 16:39:26 +00:00
|
|
|
&authv1alpha.WebhookAuthenticator{
|
2020-09-14 15:42:42 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: testKey1.Namespace,
|
|
|
|
Name: testKey1.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantLogs: []string{
|
2020-10-30 19:02:21 +00:00
|
|
|
`webhookcachecleaner-controller "level"=0 "msg"="deleting webhook authenticator from cache" "webhook"={"name":"test-name-two","namespace":"test-namespace"}`,
|
2020-09-14 15:42:42 +00:00
|
|
|
},
|
2020-10-30 19:02:21 +00:00
|
|
|
wantCacheKeys: []authncache.Key{testKey1, testKeyNonwebhook},
|
2020-09-14 15:42:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
fakeClient := pinnipedfake.NewSimpleClientset(tt.webhooks...)
|
2020-09-14 15:42:42 +00:00
|
|
|
informers := pinnipedinformers.NewSharedInformerFactory(fakeClient, 0)
|
2020-10-30 19:02:21 +00:00
|
|
|
cache := authncache.New()
|
2020-09-14 15:42:42 +00:00
|
|
|
for k, v := range tt.initialCache {
|
|
|
|
cache.Store(k, v)
|
|
|
|
}
|
|
|
|
testLog := testlogger.New(t)
|
|
|
|
|
2020-10-30 16:39:26 +00:00
|
|
|
controller := New(cache, informers.Authentication().V1alpha1().WebhookAuthenticators(), testLog)
|
2020-09-14 15:42:42 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
informers.Start(ctx.Done())
|
|
|
|
controllerlib.TestRunSynchronously(t, controller)
|
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
syncCtx := controllerlib.Context{
|
|
|
|
Context: ctx,
|
|
|
|
Key: controllerlib.Key{
|
|
|
|
Namespace: "test-namespace",
|
|
|
|
Name: "test-name-one",
|
|
|
|
},
|
|
|
|
}
|
2020-09-14 15:42:42 +00:00
|
|
|
|
|
|
|
if err := controllerlib.TestSync(t, controller, syncCtx); tt.wantErr != "" {
|
|
|
|
require.EqualError(t, err, tt.wantErr)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
require.Equal(t, tt.wantLogs, testLog.Lines())
|
|
|
|
require.ElementsMatch(t, tt.wantCacheKeys, cache.Keys())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|