@@ -21,13 +21,18 @@ 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"
30+ corev1 "k8s.io/api/core/v1"
2931 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+ "k8s.io/apimachinery/pkg/labels"
3033 "k8s.io/apimachinery/pkg/runtime"
34+ "k8s.io/apimachinery/pkg/watch"
35+ "k8s.io/client-go/tools/cache"
3136)
3237
3338func Test_NewKubernetes (t * testing.T ) {
@@ -110,22 +115,123 @@ func Test_Create(t *testing.T) {
110115
111116func Test_Get (t * testing.T ) {
112117 apps := mkApps ()
118+ ctx := context .TODO ()
113119 t .Run ("Get existing app" , func (t * testing.T ) {
114120 fakeAppC := fakeappclient .NewSimpleClientset (apps ... )
115- k := NewKubernetesBackend (fakeAppC , "" , nil , true )
116- app , err := k .Get (context .TODO (), "app" , "ns1" )
121+
122+ inf , err := informer .NewInformer [* v1alpha1.Application ](
123+ ctx ,
124+ informer.WithListHandler [* v1alpha1.Application ](func (ctx context.Context , options v1.ListOptions ) (runtime.Object , error ) {
125+ return fakeAppC .ArgoprojV1alpha1 ().Applications ("" ).List (ctx , options )
126+ }),
127+ informer.WithWatchHandler [* v1alpha1.Application ](func (ctx context.Context , options v1.ListOptions ) (watch.Interface , error ) {
128+ return fakeAppC .ArgoprojV1alpha1 ().Applications ("" ).Watch (ctx , options )
129+ }),
130+ informer .WithGroupResource [* v1alpha1.Application ]("argoproj.io" , "applications" ),
131+ )
132+ require .NoError (t , err )
133+
134+ go inf .Start (ctx )
135+ require .NoError (t , inf .WaitForSync (ctx ))
136+
137+ // Create the backend with the informer
138+ backend := NewKubernetesBackend (fakeAppC , "" , inf , true )
139+
140+ app , err := backend .Get (ctx , "app" , "ns1" )
117141 assert .NoError (t , err )
118142 assert .NotNil (t , app )
143+ assert .Equal (t , "app" , app .Name )
144+ assert .Equal (t , "ns1" , app .Namespace )
145+
119146 })
120147 t .Run ("Get non-existing app" , func (t * testing.T ) {
121148 fakeAppC := fakeappclient .NewSimpleClientset (apps ... )
122- k := NewKubernetesBackend (fakeAppC , "" , nil , true )
123- app , err := k .Get (context .TODO (), "foo" , "ns1" )
149+ inf , err := informer .NewInformer [* v1alpha1.Application ](
150+ ctx ,
151+ informer.WithListHandler [* v1alpha1.Application ](func (ctx context.Context , options v1.ListOptions ) (runtime.Object , error ) {
152+ return fakeAppC .ArgoprojV1alpha1 ().Applications ("" ).List (ctx , options )
153+ }),
154+ informer.WithWatchHandler [* v1alpha1.Application ](func (ctx context.Context , options v1.ListOptions ) (watch.Interface , error ) {
155+ return fakeAppC .ArgoprojV1alpha1 ().Applications ("" ).Watch (ctx , options )
156+ }),
157+ informer .WithGroupResource [* v1alpha1.Application ]("argoproj.io" , "applications" ),
158+ )
159+ require .NoError (t , err )
160+ go inf .Start (ctx )
161+ require .NoError (t , inf .WaitForSync (ctx ))
162+
163+ backend := NewKubernetesBackend (fakeAppC , "" , inf , true )
164+
165+ app , err := backend .Get (ctx , "nonexistent" , "ns1" )
124166 assert .ErrorContains (t , err , "not found" )
125167 assert .Equal (t , & v1alpha1.Application {}, app )
168+
169+ })
170+
171+ t .Run ("Get returns type assertion error for invalid object" , func (t * testing.T ) {
172+ fakeAppC := fakeappclient .NewSimpleClientset ()
173+
174+ mockInf := & mockInformerWithInvalidType {}
175+
176+ backend := & KubernetesBackend {
177+ appClient : fakeAppC ,
178+ appInformer : mockInf ,
179+ appLister : mockInf .Lister (),
180+ }
181+
182+ app , err := backend .Get (ctx , "test" , "ns1" )
183+ require .Error (t , err )
184+ require .Nil (t , app )
185+ assert .Contains (t , err .Error (), "object is not an Application" )
126186 })
127187}
128188
189+ type mockInformerWithInvalidType struct {}
190+
191+ func (m * mockInformerWithInvalidType ) Start (ctx context.Context ) error {
192+ return nil
193+ }
194+
195+ func (m * mockInformerWithInvalidType ) WaitForSync (ctx context.Context ) error {
196+ return nil
197+ }
198+
199+ func (m * mockInformerWithInvalidType ) HasSynced () bool {
200+ return true
201+ }
202+
203+ func (m * mockInformerWithInvalidType ) Stop () error {
204+ return nil
205+ }
206+
207+ func (m * mockInformerWithInvalidType ) Lister () cache.GenericLister {
208+ return & mockListerWithInvalidType {}
209+ }
210+
211+ type mockListerWithInvalidType struct {}
212+
213+ func (m * mockListerWithInvalidType ) List (selector labels.Selector ) ([]runtime.Object , error ) {
214+ return nil , nil
215+ }
216+
217+ func (m * mockListerWithInvalidType ) Get (name string ) (runtime.Object , error ) {
218+ return & corev1.ConfigMap {}, nil
219+ }
220+
221+ func (m * mockListerWithInvalidType ) ByNamespace (namespace string ) cache.GenericNamespaceLister {
222+ return & mockNamespaceListerWithInvalidType {}
223+ }
224+
225+ type mockNamespaceListerWithInvalidType struct {}
226+
227+ func (m * mockNamespaceListerWithInvalidType ) List (selector labels.Selector ) ([]runtime.Object , error ) {
228+ return nil , nil
229+ }
230+
231+ func (m * mockNamespaceListerWithInvalidType ) Get (name string ) (runtime.Object , error ) {
232+ return & corev1.ConfigMap {}, nil
233+ }
234+
129235func Test_Delete (t * testing.T ) {
130236 apps := mkApps ()
131237 t .Run ("Delete existing app" , func (t * testing.T ) {
0 commit comments