|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package parallel |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + argov1beta1api "github.com/argoproj-labs/argocd-operator/api/v1beta1" |
| 23 | + argocdv1alpha1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" |
| 24 | + "github.com/argoproj/gitops-engine/pkg/health" |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + |
| 31 | + "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture" |
| 32 | + applicationFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/application" |
| 33 | + argocdFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/argocd" |
| 34 | + k8sFixture "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/k8s" |
| 35 | + fixtureUtils "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/utils" |
| 36 | +) |
| 37 | + |
| 38 | +var _ = Describe("GitOps Operator Parallel E2E Tests", func() { |
| 39 | + |
| 40 | + Context("1-122_validate_user_defined_appproject", func() { |
| 41 | + |
| 42 | + var ( |
| 43 | + k8sClient client.Client |
| 44 | + ctx context.Context |
| 45 | + ns *corev1.Namespace |
| 46 | + cleanupFunc func() |
| 47 | + cleanupFuncs []func() |
| 48 | + ) |
| 49 | + |
| 50 | + BeforeEach(func() { |
| 51 | + fixture.EnsureParallelCleanSlate() |
| 52 | + |
| 53 | + k8sClient, _ = fixtureUtils.GetE2ETestKubeClient() |
| 54 | + ctx = context.Background() |
| 55 | + }) |
| 56 | + |
| 57 | + AfterEach(func() { |
| 58 | + // Clean up all additional namespaces |
| 59 | + for _, cleanup := range cleanupFuncs { |
| 60 | + if cleanup != nil { |
| 61 | + cleanup() |
| 62 | + } |
| 63 | + } |
| 64 | + cleanupFuncs = nil |
| 65 | + |
| 66 | + if cleanupFunc != nil { |
| 67 | + cleanupFunc() |
| 68 | + } |
| 69 | + |
| 70 | + fixture.OutputDebugOnFail(ns) |
| 71 | + }) |
| 72 | + |
| 73 | + It("verifies creating and configuring a user-defined AppProject instance with target namespaces and Application CR referencing it", func() { |
| 74 | + |
| 75 | + By("creating namespace-scoped Argo CD instance") |
| 76 | + ns, cleanupFunc = fixture.CreateRandomE2ETestNamespaceWithCleanupFunc() |
| 77 | + |
| 78 | + argoCD := &argov1beta1api.ArgoCD{ |
| 79 | + ObjectMeta: metav1.ObjectMeta{Name: "argocd", Namespace: ns.Name}, |
| 80 | + Spec: argov1beta1api.ArgoCDSpec{}, |
| 81 | + } |
| 82 | + Expect(k8sClient.Create(ctx, argoCD)).To(Succeed()) |
| 83 | + |
| 84 | + By("waiting for ArgoCD CR to be reconciled and the instance to be ready") |
| 85 | + Eventually(argoCD, "8m", "10s").Should(argocdFixture.BeAvailable()) |
| 86 | + |
| 87 | + By("creating target namespace for deployment") |
| 88 | + var targetNS *corev1.Namespace |
| 89 | + targetNS, targetCleanup := fixture.CreateManagedNamespaceWithCleanupFunc(ns.Name+"-target", ns.Name) |
| 90 | + cleanupFuncs = append(cleanupFuncs, targetCleanup) |
| 91 | + |
| 92 | + By("creating and configuring a user-defined AppProject instance with the target namespace") |
| 93 | + appProjectName := "user-defined-project" |
| 94 | + appProject := &argocdv1alpha1.AppProject{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Name: appProjectName, |
| 97 | + Namespace: ns.Name, |
| 98 | + }, |
| 99 | + Spec: argocdv1alpha1.AppProjectSpec{ |
| 100 | + SourceRepos: []string{"*"}, |
| 101 | + Destinations: []argocdv1alpha1.ApplicationDestination{ |
| 102 | + { |
| 103 | + Server: "https://kubernetes.default.svc", |
| 104 | + Namespace: targetNS.Name, |
| 105 | + }, |
| 106 | + }, |
| 107 | + ClusterResourceWhitelist: []metav1.GroupKind{ |
| 108 | + { |
| 109 | + Group: "*", |
| 110 | + Kind: "*", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | + Expect(k8sClient.Create(ctx, appProject)).To(Succeed()) |
| 116 | + |
| 117 | + By("verifying AppProject exists and is configured correctly") |
| 118 | + Eventually(appProject, "2m", "5s").Should(k8sFixture.ExistByName(), "AppProject did not exist within timeout") |
| 119 | + |
| 120 | + // Verify AppProject configuration |
| 121 | + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(appProject), appProject)).To(Succeed()) |
| 122 | + Expect(len(appProject.Spec.Destinations)).To(BeNumerically(">", 0), "AppProject should have at least one destination configured") |
| 123 | + Expect(appProject.Spec.Destinations[0].Namespace).To(Equal(targetNS.Name), "AppProject destination should match target namespace") |
| 124 | + Expect(appProject.Spec.Destinations[0].Server).To(Equal("https://kubernetes.default.svc"), "AppProject destination server should be configured") |
| 125 | + |
| 126 | + By("creating and configuring the Application CR to reference the target namespace and user-defined AppProject instance") |
| 127 | + app := &argocdv1alpha1.Application{ |
| 128 | + ObjectMeta: metav1.ObjectMeta{ |
| 129 | + Name: "test-app", |
| 130 | + Namespace: ns.Name, |
| 131 | + }, |
| 132 | + Spec: argocdv1alpha1.ApplicationSpec{ |
| 133 | + Project: appProjectName, |
| 134 | + Source: &argocdv1alpha1.ApplicationSource{ |
| 135 | + RepoURL: "https://github.com/redhat-developer/gitops-operator", |
| 136 | + Path: "test/examples/nginx", |
| 137 | + TargetRevision: "HEAD", |
| 138 | + }, |
| 139 | + Destination: argocdv1alpha1.ApplicationDestination{ |
| 140 | + Server: "https://kubernetes.default.svc", |
| 141 | + Namespace: targetNS.Name, |
| 142 | + }, |
| 143 | + SyncPolicy: &argocdv1alpha1.SyncPolicy{ |
| 144 | + Automated: &argocdv1alpha1.SyncPolicyAutomated{}, |
| 145 | + }, |
| 146 | + }, |
| 147 | + } |
| 148 | + Expect(k8sClient.Create(ctx, app)).To(Succeed()) |
| 149 | + |
| 150 | + By("verifying Application is healthy and syncs successfully") |
| 151 | + Eventually(app, "8m", "10s").Should(applicationFixture.HaveHealthStatusCode(health.HealthStatusHealthy), "Application did not reach healthy status within timeout") |
| 152 | + Eventually(app, "8m", "10s").Should(applicationFixture.HaveSyncStatusCode(argocdv1alpha1.SyncStatusCodeSynced), "Application did not sync within timeout") |
| 153 | + |
| 154 | + By("verifying Application references the user-defined AppProject") |
| 155 | + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(app), app)).To(Succeed()) |
| 156 | + Expect(app.Spec.Project).To(Equal(appProjectName), "Application should reference the user-defined AppProject") |
| 157 | + |
| 158 | + By("verifying Application targets the correct namespace") |
| 159 | + Expect(app.Spec.Destination.Namespace).To(Equal(targetNS.Name), "Application should target the configured namespace") |
| 160 | + |
| 161 | + }) |
| 162 | + }) |
| 163 | +}) |
0 commit comments