@@ -21,13 +21,15 @@ import (
21
21
"testing"
22
22
23
23
"github.com/argoproj-labs/argocd-agent/internal/backend"
24
+ "github.com/argoproj-labs/argocd-agent/internal/informer"
24
25
"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
25
26
fakeappclient "github.com/argoproj/argo-cd/v3/pkg/client/clientset/versioned/fake"
26
27
"github.com/stretchr/testify/assert"
27
28
"github.com/stretchr/testify/require"
28
29
"github.com/wI2L/jsondiff"
29
30
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
31
"k8s.io/apimachinery/pkg/runtime"
32
+ "k8s.io/apimachinery/pkg/watch"
31
33
)
32
34
33
35
func Test_NewKubernetes (t * testing.T ) {
@@ -110,19 +112,57 @@ func Test_Create(t *testing.T) {
110
112
111
113
func Test_Get (t * testing.T ) {
112
114
apps := mkApps ()
115
+ ctx := context .TODO ()
113
116
t .Run ("Get existing app" , func (t * testing.T ) {
114
117
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" )
117
137
assert .NoError (t , err )
118
138
assert .NotNil (t , app )
139
+ assert .Equal (t , "app" , app .Name )
140
+ assert .Equal (t , "ns1" , app .Namespace )
141
+
119
142
})
120
143
t .Run ("Get non-existing app" , func (t * testing.T ) {
121
144
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
+
126
166
})
127
167
}
128
168
0 commit comments