Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barkbay committed Sep 25, 2023
1 parent 8fba8d5 commit 3d24a78
Showing 1 changed file with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
)

const (
ns = "test_namespace"
ns1 = "test_namespace1"
ns2 = "test_namespace2"
pass = "testing_passpass"
)

Expand All @@ -42,22 +43,36 @@ func Test_K8sSecretsProvider_Fetch(t *testing.T) {
require.True(t, ok, "cannot cast ContextProvider into contextProviderK8sSecrets")

// Use a fake reader provider that will handle requests for the fake ns
fp.k8sCacheProvider = newFakeCacheProvider().withSecret(
fakeCacheProvider := newFakeCacheProvider().withSecrets(
t,
&v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "apps/v1beta1",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "testing_secret",
Namespace: ns,
Namespace: ns1,
},
Data: map[string][]byte{
"secret_value": []byte(pass),
},
},
&v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "testing_secret",
Namespace: ns2,
},
Data: map[string][]byte{
"secret_value": []byte(pass),
},
},
)
fp.k8sCacheProvider = fakeCacheProvider

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -67,9 +82,23 @@ func Test_K8sSecretsProvider_Fetch(t *testing.T) {
_ = fp.Run(ctx, comm)
}()

val, found := fp.Fetch("kubernetes_secrets.test_namespace.testing_secret.secret_value")
val, found := fp.Fetch("kubernetes_secrets.test_namespace1.testing_secret.secret_value")
assert.True(t, found)
assert.Equal(t, val, pass)
// new(cfg *Config, namespace string) should have been called
assert.Equal(t, 1, fakeCacheProvider.newCount)

val, found = fp.Fetch("kubernetes_secrets.test_namespace1.testing_secret.secret_value")
assert.True(t, found)
assert.Equal(t, val, pass)
// new(cfg *Config, namespace string) should NOT have been called
assert.Equal(t, 1, fakeCacheProvider.newCount)

val, found = fp.Fetch("kubernetes_secrets.test_namespace2.testing_secret.secret_value")
assert.True(t, found)
assert.Equal(t, val, pass)
// new(cfg *Config, namespace string) should have been called
assert.Equal(t, 2, fakeCacheProvider.newCount)
}

func Test_K8sSecretsProvider_FetchWrongSecret(t *testing.T) {
Expand All @@ -83,16 +112,16 @@ func Test_K8sSecretsProvider_FetchWrongSecret(t *testing.T) {
fp, _ := p.(*contextProviderK8sSecrets)

// Use a fake reader provider that will handle requests for the fake ns
fp.k8sCacheProvider = newFakeCacheProvider().withSecret(
fp.k8sCacheProvider = newFakeCacheProvider().withSecrets(
t,
&v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "apps/v1beta1",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "testing_secret",
Namespace: ns,
Namespace: ns1,
},
Data: map[string][]byte{
"secret_value": []byte(pass),
Expand All @@ -108,7 +137,7 @@ func Test_K8sSecretsProvider_FetchWrongSecret(t *testing.T) {
_ = fp.Run(ctx, comm)
}()

val, found := fp.Fetch("kubernetes_secrets.test_namespace.testing_secretHACK.secret_value")
val, found := fp.Fetch("kubernetes_secrets.test_namespace1.testing_secretHACK.secret_value")
assert.False(t, found)
assert.EqualValues(t, val, "")
}
Expand All @@ -117,6 +146,9 @@ func Test_K8sSecretsProvider_FetchWrongSecret(t *testing.T) {

type fakeCacheProvider struct {
readers map[string]fakeCache

// record how many times new(...) has been called
newCount int
}

var _ cacheProvider = &fakeCacheProvider{}
Expand All @@ -127,22 +159,25 @@ func newFakeCacheProvider() *fakeCacheProvider {
}
}

func (f *fakeCacheProvider) withSecret(t *testing.T, obj client.Object) *fakeCacheProvider {
func (f *fakeCacheProvider) withSecrets(t *testing.T, objs ...client.Object) *fakeCacheProvider {
t.Helper()
c, exists := f.readers[obj.GetNamespace()]
if !exists {
c = fakeCache{
client: fake.NewFakeClient(),
for _, obj := range objs {
c, exists := f.readers[obj.GetNamespace()]
if !exists {
c = fakeCache{
client: fake.NewFakeClient(),
}
f.readers[obj.GetNamespace()] = c
}
if err := c.client.Create(context.TODO(), obj); err != nil {
t.Fatalf("Error while adding secret: %v", err)
}
f.readers[obj.GetNamespace()] = c
}
if err := c.client.Create(context.TODO(), obj); err != nil {
t.Fatalf("Error while adding secret: %v", err)
}
return f
}

func (f *fakeCacheProvider) new(_ *Config, namespace string) (cache.Cache, error) {
f.newCount++
fakeCache, exists := f.readers[namespace]
if !exists {
return nil, fmt.Errorf("no cache for namespace %s", namespace)
Expand Down

0 comments on commit 3d24a78

Please sign in to comment.