Skip to content

Commit 9ee56b4

Browse files
trdoyle81Triona Doyle
andauthored
Port 1-084_validate_prune_templates to Ginko (#1089)
* Port 1-084_validate_prune_templates to Ginko Signed-off-by: Triona Doyle <bot@example.com> * test: use typed Application schema in 1-084 prune test Signed-off-by: Triona Doyle <bot@example.com> --------- Signed-off-by: Triona Doyle <bot@example.com> Co-authored-by: Triona Doyle <bot@example.com>
1 parent 96a7a95 commit 9ee56b4

1 file changed

Lines changed: 206 additions & 0 deletions

File tree

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package sequential
2+
3+
import (
4+
"context"
5+
"os"
6+
"path/filepath"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
11+
argov1alpha1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
12+
"github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture"
13+
"github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/argocd"
14+
fixtureUtils "github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture/utils"
15+
16+
corev1 "k8s.io/api/core/v1"
17+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
18+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
20+
"k8s.io/apimachinery/pkg/runtime/schema"
21+
"sigs.k8s.io/controller-runtime/pkg/client"
22+
)
23+
24+
var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
25+
26+
Context("1-084_validate_prune_templates", func() {
27+
var (
28+
k8sClient client.Client
29+
ctx context.Context
30+
ns *corev1.Namespace
31+
cleanupFunc func()
32+
)
33+
34+
BeforeEach(func() {
35+
fixture.EnsureSequentialCleanSlate()
36+
k8sClient, _ = fixtureUtils.GetE2ETestKubeClient()
37+
ctx = context.Background()
38+
39+
ns, cleanupFunc = fixture.CreateRandomE2ETestNamespaceWithCleanupFunc()
40+
41+
// permissions
42+
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(ns), ns)
43+
Expect(err).ToNot(HaveOccurred())
44+
45+
if ns.Labels == nil {
46+
ns.Labels = make(map[string]string)
47+
}
48+
ns.Labels["argocd.argoproj.io/managed-by"] = "openshift-gitops"
49+
Expect(k8sClient.Update(ctx, ns)).To(Succeed())
50+
})
51+
52+
AfterEach(func() {
53+
defer cleanupFunc()
54+
fixture.OutputDebugOnFail(ns)
55+
})
56+
57+
It("validates that resources with duplicate GVKs can be pruned successfully with local sync", func() {
58+
By("creating a temp dir for git repo")
59+
workDir, err := os.MkdirTemp("", "gitops-prune-test")
60+
Expect(err).ToNot(HaveOccurred())
61+
DeferCleanup(func() {
62+
_ = os.RemoveAll(workDir)
63+
})
64+
65+
By("writing two OpenShift Templates (duplicate GVKs) to the working dir")
66+
template1 := `---
67+
apiVersion: template.openshift.io/v1
68+
kind: Template
69+
metadata:
70+
name: redis-template-gitops
71+
annotations:
72+
description: "Description"
73+
iconClass: "icon-redis"
74+
tags: "database,nosql"
75+
objects:
76+
- apiVersion: v1
77+
kind: Pod
78+
metadata:
79+
name: redis-master
80+
spec:
81+
containers:
82+
- env:
83+
- name: REDIS_PASSWORD
84+
value: xyz1234s
85+
image: dockerfile/redis
86+
name: master
87+
ports:
88+
- containerPort: 6379
89+
protocol: TCP
90+
parameters:
91+
- description: Password used for Redis authentication
92+
from: '[A-Z0-9]{8}'
93+
generate: expression
94+
name: REDIS_PASSWORD
95+
labels:
96+
redis: master
97+
`
98+
template2 := `---
99+
apiVersion: template.openshift.io/v1
100+
kind: Template
101+
metadata:
102+
name: redis-template-gitops2
103+
annotations:
104+
description: "Description"
105+
iconClass: "icon-redis"
106+
tags: "database,nosql"
107+
objects:
108+
- apiVersion: v1
109+
kind: Pod
110+
metadata:
111+
name: redis-master
112+
spec:
113+
containers:
114+
- env:
115+
- name: REDIS_PASSWORD
116+
value: xyz1234s
117+
image: dockerfile/redis
118+
name: master
119+
ports:
120+
- containerPort: 6379
121+
protocol: TCP
122+
parameters:
123+
- description: Password used for Redis authentication
124+
from: '[A-Z0-9]{8}'
125+
generate: expression
126+
name: REDIS_PASSWORD
127+
labels:
128+
redis: master
129+
`
130+
err = os.WriteFile(filepath.Join(workDir, "app-template.yaml"), []byte(template1), 0600)
131+
Expect(err).ToNot(HaveOccurred())
132+
133+
err = os.WriteFile(filepath.Join(workDir, "app-template2.yaml"), []byte(template2), 0600)
134+
Expect(err).ToNot(HaveOccurred())
135+
136+
By("logging into the Argo CD CLI")
137+
err = argocd.LogInToDefaultArgoCDInstance()
138+
Expect(err).ToNot(HaveOccurred(), "Failed to login to Argo CD")
139+
140+
By("Creating ArgoCD Application CR using the typed schema")
141+
appName := "app-kustomize-" + ns.Name
142+
143+
app := &argov1alpha1.Application{
144+
ObjectMeta: metav1.ObjectMeta{
145+
Name: appName,
146+
Namespace: "openshift-gitops",
147+
},
148+
Spec: argov1alpha1.ApplicationSpec{
149+
Project: "default",
150+
Source: &argov1alpha1.ApplicationSource{
151+
RepoURL: "file://" + workDir + ".git",
152+
Path: ".",
153+
TargetRevision: "HEAD",
154+
},
155+
Destination: argov1alpha1.ApplicationDestination{
156+
Server: "https://kubernetes.default.svc",
157+
Namespace: ns.Name,
158+
},
159+
SyncPolicy: &argov1alpha1.SyncPolicy{
160+
SyncOptions: argov1alpha1.SyncOptions{"PruneLast=true"},
161+
},
162+
},
163+
}
164+
165+
Expect(k8sClient.Create(ctx, app)).To(Succeed())
166+
167+
DeferCleanup(func() {
168+
_ = k8sClient.Delete(ctx, app)
169+
})
170+
171+
By("syncing the application using the local dir")
172+
out, err := argocd.RunArgoCDCLI("app", "sync", appName, "--local", workDir, "--timeout", "100")
173+
Expect(err).ToNot(HaveOccurred(), "Failed to sync app with local flag: %s", out)
174+
175+
By("verifying both templates were created")
176+
tmplObj := &unstructured.Unstructured{}
177+
tmplObj.SetGroupVersionKind(schema.GroupVersionKind{Group: "template.openshift.io", Version: "v1", Kind: "Template"})
178+
179+
Eventually(func() error {
180+
return k8sClient.Get(ctx, client.ObjectKey{Name: "redis-template-gitops", Namespace: ns.Name}, tmplObj)
181+
}, "2m", "5s").Should(Succeed())
182+
183+
Eventually(func() error {
184+
return k8sClient.Get(ctx, client.ObjectKey{Name: "redis-template-gitops2", Namespace: ns.Name}, tmplObj)
185+
}, "2m", "5s").Should(Succeed())
186+
187+
By("deleting one template from the local source directory")
188+
err = os.Remove(filepath.Join(workDir, "app-template.yaml"))
189+
Expect(err).ToNot(HaveOccurred())
190+
191+
By("syncing the application again this time with the prune flag enabled")
192+
out, err = argocd.RunArgoCDCLI("app", "sync", appName, "--local", workDir, "--prune", "--timeout", "100")
193+
Expect(err).ToNot(HaveOccurred(), "Failed to sync and prune app: %s", out)
194+
195+
By("verifying the deleted template was pruned from the cluster")
196+
Eventually(func() bool {
197+
err := k8sClient.Get(ctx, client.ObjectKey{Name: "redis-template-gitops", Namespace: ns.Name}, tmplObj)
198+
return k8serrors.IsNotFound(err)
199+
}, "2m", "5s").Should(BeTrue(), "Expected redis-template-gitops to be pruned, but it still exists")
200+
201+
By("verifying the remaining template still exists")
202+
err = k8sClient.Get(ctx, client.ObjectKey{Name: "redis-template-gitops2", Namespace: ns.Name}, tmplObj)
203+
Expect(err).ToNot(HaveOccurred(), "Expected redis-template-gitops2 to still exist")
204+
})
205+
})
206+
})

0 commit comments

Comments
 (0)