Skip to content

Commit abe321a

Browse files
committed
Added more unit test cases
Signed-off-by: Raghavi Shirur <[email protected]>
1 parent 4b433d8 commit abe321a

File tree

1 file changed

+109
-17
lines changed

1 file changed

+109
-17
lines changed

controllers/argocd/argocd_controller_test.go

+109-17
Original file line numberDiff line numberDiff line change
@@ -104,61 +104,153 @@ func TestReconcileArgoCD_Reconcile(t *testing.T) {
104104

105105
func TestReconcileArgoCD_LabelSelector(t *testing.T) {
106106
logf.SetLogger(ZapLogger(true))
107+
//ctx := context.Background()
107108
a := makeTestArgoCD()
108-
r := makeTestReconciler(t, a)
109-
assert.NoError(t, createNamespace(r, a.Namespace, ""))
110-
111-
// ArgoCD instance should be reconciled if no label-selctor is applied to the operator
112-
req := reconcile.Request{
109+
a.Name = "argo-test-1"
110+
b := makeTestArgoCD()
111+
b.Name = "argo-test-2"
112+
c := makeTestArgoCD()
113+
c.Name = "argo-test-3"
114+
rt1 := makeTestReconciler(t, a)
115+
rt2 := makeTestReconciler(t, b)
116+
rt3 := makeTestReconciler(t, c)
117+
assert.NoError(t, createNamespace(rt1, a.Namespace, ""))
118+
assert.NoError(t, createNamespace(rt2, b.Namespace, ""))
119+
assert.NoError(t, createNamespace(rt3, c.Namespace, ""))
120+
121+
// All ArgoCD instance should be reconciled if no label-selctor is applied to the operator.
122+
// No label selector provided and all argocd instances are reconciled
123+
124+
// Instance 'a'
125+
req1 := reconcile.Request{
113126
NamespacedName: types.NamespacedName{
114127
Name: a.Name,
115128
Namespace: a.Namespace,
116129
},
117130
}
118-
res, err := r.Reconcile(context.TODO(), req)
131+
res1, err := rt1.Reconcile(context.TODO(), req1)
119132
assert.NoError(t, err)
120-
if res.Requeue {
133+
if res1.Requeue {
134+
t.Fatal("reconcile requeued request")
135+
}
136+
137+
//Instance 'b'
138+
req2 := reconcile.Request{
139+
NamespacedName: types.NamespacedName{
140+
Name: b.Name,
141+
Namespace: b.Namespace,
142+
},
143+
}
144+
res2, err := rt2.Reconcile(context.TODO(), req2)
145+
assert.NoError(t, err)
146+
if res2.Requeue {
121147
t.Fatal("reconcile requeued request")
122148
}
123149

124-
// Apply label-selector foo=bar to the operator but not to the argocd instance. No reconciliation will happen and an Error is expected.
125-
r.LabelSelector = "foo=bar"
126-
reqTest1 := reconcile.Request{
150+
//Instance 'c'
151+
req3 := reconcile.Request{
152+
NamespacedName: types.NamespacedName{
153+
Name: c.Name,
154+
Namespace: c.Namespace,
155+
},
156+
}
157+
res3, err := rt3.Reconcile(context.TODO(), req3)
158+
assert.NoError(t, err)
159+
if res3.Requeue {
160+
t.Fatal("reconcile requeued request")
161+
}
162+
163+
// Apply label-selector foo=bar to the operator but not to the argocd instances. No reconciliation will happen and an error is expected.
164+
rt1.LabelSelector = "foo=bar"
165+
reqTest := reconcile.Request{
127166
NamespacedName: types.NamespacedName{
128167
Name: a.Name,
129168
Namespace: a.Namespace,
130169
},
131170
}
132-
resTest1, err := r.Reconcile(context.TODO(), reqTest1)
171+
resTest, err := rt1.Reconcile(context.TODO(), reqTest)
133172
assert.Error(t, err)
134-
if resTest1.Requeue {
173+
if resTest.Requeue {
174+
t.Fatal("reconcile requeued request")
175+
}
176+
177+
//not reconciled should return error
178+
rt2.LabelSelector = "foo=bar"
179+
reqTest2 := reconcile.Request{
180+
NamespacedName: types.NamespacedName{
181+
Name: b.Name,
182+
Namespace: b.Namespace,
183+
},
184+
}
185+
resTest2, err := rt2.Reconcile(context.TODO(), reqTest2)
186+
assert.Error(t, err)
187+
if resTest2.Requeue {
188+
t.Fatal("reconcile requeued request")
189+
}
190+
191+
//not reconciled should return error
192+
rt3.LabelSelector = "foo=bar"
193+
reqTest3 := reconcile.Request{
194+
NamespacedName: types.NamespacedName{
195+
Name: c.Name,
196+
Namespace: c.Namespace,
197+
},
198+
}
199+
resTest3, err := rt3.Reconcile(context.TODO(), reqTest3)
200+
assert.Error(t, err)
201+
if resTest3.Requeue {
135202
t.Fatal("reconcile requeued request")
136203
}
137204
}
138205
func TestReconcileArgoCD_ReconcileLabel(t *testing.T) {
206+
207+
// Multiple ArgoCD instances present with matching label present on some and absent on some.
208+
// Only instances matching the label are reconciled.
209+
139210
logf.SetLogger(ZapLogger(true))
140211
ctx := context.Background()
141212
a := makeTestArgoCD()
142-
r := makeTestReconciler(t, a)
143-
assert.NoError(t, createNamespace(r, a.Namespace, ""))
213+
r1 := makeTestReconciler(t, a)
214+
assert.NoError(t, createNamespace(r1, a.Namespace, ""))
215+
b := makeTestArgoCD()
216+
r2 := makeTestReconciler(t, b)
217+
assert.NoError(t, createNamespace(r2, b.Namespace, ""))
144218

145219
// Apply label foo=bar to the argocd instance and to the operator for reconciliation without any error.
146-
r.LabelSelector = "foo=bar"
220+
r1.LabelSelector = "foo=bar"
221+
r2.LabelSelector = "foo=bar"
222+
223+
//Apply label to Instance 'a' but not to Instance 'b'
147224
a.SetLabels(map[string]string{"foo": "bar"})
148-
err := r.Client.Update(ctx, a)
225+
err := r1.Client.Update(ctx, a)
149226
fatalIfError(t, err, "failed to update the ArgoCD: %s", err)
150227
reqTest2 := reconcile.Request{
151228
NamespacedName: types.NamespacedName{
152229
Name: a.Name,
153230
Namespace: a.Namespace,
154231
},
155232
}
156-
resTest2, err := r.Reconcile(context.TODO(), reqTest2)
233+
resTest2, err := r1.Reconcile(context.TODO(), reqTest2)
234+
235+
//Instance 'a' reconciled without error
157236
assert.NoError(t, err)
158237
if resTest2.Requeue {
159238
t.Fatal("reconcile requeued request")
160239
}
161240

241+
reqTest3 := reconcile.Request{
242+
NamespacedName: types.NamespacedName{
243+
Name: b.Name,
244+
Namespace: b.Namespace,
245+
},
246+
}
247+
resTest3, err := r2.Reconcile(context.TODO(), reqTest3)
248+
//Instance 'b' is not reconciled and an error is expeced
249+
assert.Error(t, err)
250+
if resTest3.Requeue {
251+
t.Fatal("reconcile requeued request")
252+
}
253+
162254
}
163255

164256
func TestReconcileArgoCD_Reconcile_RemoveManagedByLabelOnArgocdDeletion(t *testing.T) {

0 commit comments

Comments
 (0)