Skip to content

Commit 29659aa

Browse files
committed
test: add unit tests for Get method using informer cache
Signed-off-by: yeonsoo <[email protected]>
1 parent d809156 commit 29659aa

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

internal/backend/kubernetes/application/kubernetes_test.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import (
2121
"testing"
2222

2323
"github.com/argoproj-labs/argocd-agent/internal/backend"
24+
"github.com/argoproj-labs/argocd-agent/internal/informer"
2425
"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
2526
fakeappclient "github.com/argoproj/argo-cd/v3/pkg/client/clientset/versioned/fake"
2627
"github.com/stretchr/testify/assert"
2728
"github.com/stretchr/testify/require"
2829
"github.com/wI2L/jsondiff"
2930
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031
"k8s.io/apimachinery/pkg/runtime"
32+
"k8s.io/apimachinery/pkg/watch"
3133
)
3234

3335
func Test_NewKubernetes(t *testing.T) {
@@ -110,19 +112,57 @@ func Test_Create(t *testing.T) {
110112

111113
func Test_Get(t *testing.T) {
112114
apps := mkApps()
115+
ctx := context.TODO()
113116
t.Run("Get existing app", func(t *testing.T) {
114117
fakeAppC := fakeappclient.NewSimpleClientset(apps...)
115-
k := NewKubernetesBackend(fakeAppC, "", nil, true)
116-
app, err := k.Get(context.TODO(), "app", "ns1")
118+
119+
inf, err := informer.NewInformer[*v1alpha1.Application](
120+
ctx,
121+
informer.WithListHandler[*v1alpha1.Application](func(ctx context.Context, options v1.ListOptions) (runtime.Object, error) {
122+
return fakeAppC.ArgoprojV1alpha1().Applications("").List(ctx, options)
123+
}),
124+
informer.WithWatchHandler[*v1alpha1.Application](func(ctx context.Context, options v1.ListOptions) (watch.Interface, error) {
125+
return fakeAppC.ArgoprojV1alpha1().Applications("").Watch(ctx, options)
126+
}),
127+
)
128+
require.NoError(t, err)
129+
130+
go inf.Start(ctx)
131+
require.NoError(t, inf.WaitForSync(ctx))
132+
133+
// Create the backend with the informer
134+
backend := NewKubernetesBackend(fakeAppC, "", inf, true)
135+
136+
app, err := backend.Get(ctx, "app", "ns1")
117137
assert.NoError(t, err)
118138
assert.NotNil(t, app)
139+
assert.Equal(t, "app", app.Name)
140+
assert.Equal(t, "ns1", app.Namespace)
141+
119142
})
120143
t.Run("Get non-existing app", func(t *testing.T) {
121144
fakeAppC := fakeappclient.NewSimpleClientset(apps...)
122-
k := NewKubernetesBackend(fakeAppC, "", nil, true)
123-
app, err := k.Get(context.TODO(), "foo", "ns1")
124-
assert.ErrorContains(t, err, "not found")
125-
assert.Equal(t, &v1alpha1.Application{}, app)
145+
inf, err := informer.NewInformer[*v1alpha1.Application](
146+
ctx,
147+
informer.WithListHandler[*v1alpha1.Application](func(ctx context.Context, options v1.ListOptions) (runtime.Object, error) {
148+
return fakeAppC.ArgoprojV1alpha1().Applications("").List(ctx, options)
149+
}),
150+
informer.WithWatchHandler[*v1alpha1.Application](func(ctx context.Context, options v1.ListOptions) (watch.Interface, error) {
151+
return fakeAppC.ArgoprojV1alpha1().Applications("").Watch(ctx, options)
152+
}),
153+
)
154+
require.NoError(t, err)
155+
// Start the informer
156+
go inf.Start(ctx)
157+
require.NoError(t, inf.WaitForSync(ctx))
158+
159+
// Create the backend with the informer
160+
backend := NewKubernetesBackend(fakeAppC, "", inf, true)
161+
162+
app, err := backend.Get(ctx, "nonexistent", "ns1")
163+
require.Error(t, err)
164+
require.Nil(t, app)
165+
126166
})
127167
}
128168

0 commit comments

Comments
 (0)