Skip to content

Commit 1cdae92

Browse files
committed
Add some client_factory tests
1 parent 175df1c commit 1cdae92

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

controllers/vaultdynamicsecret_controller_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,17 @@ func TestVaultDynamicSecretReconciler_vaultClientCallback(t *testing.T) {
10391039
},
10401040
},
10411041
},
1042+
{
1043+
ObjectMeta: metav1.ObjectMeta{
1044+
Namespace: "other",
1045+
Name: "canary-other-ns",
1046+
},
1047+
Status: secretsv1beta1.VaultDynamicSecretStatus{
1048+
VaultClientMeta: secretsv1beta1.VaultClientMeta{
1049+
CacheKey: "kubernetes-12345",
1050+
},
1051+
},
1052+
},
10421053
}
10431054

10441055
tests := []struct {

internal/vault/client_factory.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ func (m *cachingClientFactory) Stop() {
120120
}
121121
}
122122

123+
// RegisterClientCallbackHandler registers a ClientCallbackHandler with the
124+
// cachingClientFactory. The ClientCallbackHandler will be called when the
125+
// specified event occurs. There is no duplication detection, so the same handler
126+
// can be registered multiple times. The caller is responsible for ensuring that
127+
// there are no duplicates.
123128
func (m *cachingClientFactory) RegisterClientCallbackHandler(cb ClientCallbackHandler) {
124129
m.mu.Lock()
125130
defer m.mu.Unlock()

internal/vault/client_factory_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package vault
5+
6+
import (
7+
"context"
8+
"sync"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func Test_cachingClientFactory_RegisterClientCallbackHandler(t *testing.T) {
16+
cb1 := func(_ context.Context, _ Client, _ error) {
17+
// do nothing
18+
}
19+
cb2 := func(_ context.Context, _ Client, _ error) {
20+
// do nothing
21+
}
22+
cb3 := func(_ context.Context, _ Client, _ error) {
23+
// do nothing
24+
}
25+
tests := []struct {
26+
name string
27+
cbs []ClientCallbackHandler
28+
wantErr assert.ErrorAssertionFunc
29+
}{
30+
{
31+
name: "single",
32+
cbs: []ClientCallbackHandler{
33+
{
34+
On: ClientCallbackOnLifetimeWatcherDone,
35+
Callback: cb1,
36+
},
37+
},
38+
wantErr: assert.NoError,
39+
},
40+
{
41+
name: "multiple",
42+
cbs: []ClientCallbackHandler{
43+
{
44+
On: ClientCallbackOnLifetimeWatcherDone,
45+
Callback: cb1,
46+
},
47+
{
48+
On: ClientCallbackOnLifetimeWatcherDone,
49+
Callback: cb2,
50+
},
51+
{
52+
On: ClientCallbackOnLifetimeWatcherDone,
53+
Callback: cb3,
54+
},
55+
},
56+
wantErr: assert.NoError,
57+
},
58+
}
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
m := &cachingClientFactory{}
62+
require.Greater(t, len(tt.cbs), 0, "no test ClientCallbackHandlers provided")
63+
64+
wg := sync.WaitGroup{}
65+
wg.Add(len(tt.cbs))
66+
for _, cb := range tt.cbs {
67+
go func(cb ClientCallbackHandler) {
68+
defer wg.Done()
69+
m.RegisterClientCallbackHandler(cb)
70+
}(cb)
71+
}
72+
wg.Wait()
73+
assert.Equal(t, len(tt.cbs), len(m.clientCallbacks))
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)