|
1 | 1 | package openshift |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "sort" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | corev1 "k8s.io/api/core/v1" |
7 | 9 | rbacv1 "k8s.io/api/rbac/v1" |
8 | 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/client-go/kubernetes/scheme" |
9 | 12 |
|
| 13 | + argoapp "github.com/argoproj-labs/argocd-operator/api/v1beta1" |
10 | 14 | "github.com/stretchr/testify/assert" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
11 | 18 | ) |
12 | 19 |
|
13 | 20 | func TestReconcileArgoCD_reconcileApplicableClusterRole(t *testing.T) { |
@@ -207,3 +214,104 @@ func TestReconcileArgoCD_reconcileSecrets(t *testing.T) { |
207 | 214 | assert.NoError(t, ReconcilerHook(a, testSecret, "")) |
208 | 215 | assert.Equal(t, string(testSecret.Data["namespaces"]), "someRandomNamespace") |
209 | 216 | } |
| 217 | + |
| 218 | +func TestAdminClusterRoleMapper(t *testing.T) { |
| 219 | + s := scheme.Scheme |
| 220 | + s.AddKnownTypes(argoapp.GroupVersion, &argoapp.ArgoCD{}, &argoapp.ArgoCDList{}) |
| 221 | + |
| 222 | + t.Run("non-admin object returns empty result", func(t *testing.T) { |
| 223 | + fakeClient := fake.NewClientBuilder().WithScheme(s).Build() |
| 224 | + |
| 225 | + mapFunc := adminClusterRoleMapper(fakeClient) |
| 226 | + |
| 227 | + nonAdminClusterRole := &rbacv1.ClusterRole{ |
| 228 | + ObjectMeta: metav1.ObjectMeta{ |
| 229 | + Name: "not-admin", |
| 230 | + }, |
| 231 | + } |
| 232 | + |
| 233 | + result := mapFunc(context.TODO(), nonAdminClusterRole) |
| 234 | + |
| 235 | + assert.Empty(t, result) |
| 236 | + }) |
| 237 | + |
| 238 | + t.Run("admin object with no Argo CD instances returns empty result", func(t *testing.T) { |
| 239 | + fakeClient := fake.NewClientBuilder().WithScheme(s).Build() |
| 240 | + |
| 241 | + mapFunc := adminClusterRoleMapper(fakeClient) |
| 242 | + |
| 243 | + // Create admin cluster role |
| 244 | + adminClusterRole := &rbacv1.ClusterRole{ |
| 245 | + ObjectMeta: metav1.ObjectMeta{ |
| 246 | + Name: "admin", |
| 247 | + }, |
| 248 | + } |
| 249 | + |
| 250 | + result := mapFunc(context.TODO(), adminClusterRole) |
| 251 | + |
| 252 | + assert.Empty(t, result) |
| 253 | + }) |
| 254 | + |
| 255 | + t.Run("admin object with Argo CD instances returns reconcile requests", func(t *testing.T) { |
| 256 | + // Create test Argo CD instances |
| 257 | + argocd1 := &argoapp.ArgoCD{ |
| 258 | + ObjectMeta: metav1.ObjectMeta{ |
| 259 | + Name: "argocd-1", |
| 260 | + Namespace: "namespace-1", |
| 261 | + }, |
| 262 | + } |
| 263 | + |
| 264 | + argocd2 := &argoapp.ArgoCD{ |
| 265 | + ObjectMeta: metav1.ObjectMeta{ |
| 266 | + Name: "argocd-2", |
| 267 | + Namespace: "namespace-2", |
| 268 | + }, |
| 269 | + } |
| 270 | + |
| 271 | + // Create fake client with Argo CD instances |
| 272 | + fakeClient := fake.NewClientBuilder(). |
| 273 | + WithScheme(s). |
| 274 | + WithObjects(argocd1, argocd2). |
| 275 | + Build() |
| 276 | + |
| 277 | + mapFunc := adminClusterRoleMapper(fakeClient) |
| 278 | + |
| 279 | + // Create admin cluster role |
| 280 | + adminClusterRole := &rbacv1.ClusterRole{ |
| 281 | + ObjectMeta: metav1.ObjectMeta{ |
| 282 | + Name: "admin", |
| 283 | + }, |
| 284 | + } |
| 285 | + |
| 286 | + result := mapFunc(context.TODO(), adminClusterRole) |
| 287 | + |
| 288 | + // Should return reconcile requests for both Argo CD instances |
| 289 | + assert.Len(t, result, 2) |
| 290 | + |
| 291 | + // Check that the reconcile requests contain the correct namespaced names |
| 292 | + expectedRequests := []reconcile.Request{ |
| 293 | + { |
| 294 | + NamespacedName: client.ObjectKey{ |
| 295 | + Name: "argocd-1", |
| 296 | + Namespace: "namespace-1", |
| 297 | + }, |
| 298 | + }, |
| 299 | + { |
| 300 | + NamespacedName: client.ObjectKey{ |
| 301 | + Name: "argocd-2", |
| 302 | + Namespace: "namespace-2", |
| 303 | + }, |
| 304 | + }, |
| 305 | + } |
| 306 | + |
| 307 | + // Sort both slices to ensure consistent comparison |
| 308 | + sort.Slice(result, func(i, j int) bool { |
| 309 | + return result[i].NamespacedName.Name < result[j].NamespacedName.Name |
| 310 | + }) |
| 311 | + sort.Slice(expectedRequests, func(i, j int) bool { |
| 312 | + return expectedRequests[i].NamespacedName.Name < expectedRequests[j].NamespacedName.Name |
| 313 | + }) |
| 314 | + |
| 315 | + assert.Equal(t, expectedRequests, result) |
| 316 | + }) |
| 317 | +} |
0 commit comments