Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit 6cc27d1

Browse files
committed
chore: add e2e tests with muliplt objects
1 parent 25e05ef commit 6cc27d1

2 files changed

Lines changed: 130 additions & 36 deletions

File tree

controllers/gitopsapi_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ func GetKustomizaton(fs billy.Filesystem, path string) (*types.Kustomization, er
144144
if err != nil {
145145
return nil, err
146146
}
147-
existingKustomization, _ := ioutil.ReadAll(existing)
147+
existingKustomization, err := ioutil.ReadAll(existing)
148+
if err != nil {
149+
return nil, err
150+
}
148151
if err := yaml.Unmarshal(existingKustomization, &kustomization); err != nil {
149152
return nil, err
150153
}

test/e2e.go

Lines changed: 126 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ var (
4646
k8s *kubernetes.Clientset
4747
crdK8s crdclient.Client
4848
tests = map[string]Test{
49-
"git-operator-is-running": TestGitOperatorIsRunning,
50-
"github-branch-sync": TestGithubBranchSync,
51-
"github-pr-github-sync": TestGithubPRSync,
52-
"github-pr-crd-sync": TestGithubPRCRDSync,
53-
"github-gitops-api-create": TestGitopsAPICreate,
54-
"github-gitops-api-update": TestGitopsAPIUpdate,
55-
"github-gitops-api-delete": TestGitopsAPIDelete,
49+
"git-operator-is-running": TestGitOperatorIsRunning,
50+
"github-branch-sync": TestGithubBranchSync,
51+
"github-pr-github-sync": TestGithubPRSync,
52+
"github-pr-crd-sync": TestGithubPRCRDSync,
53+
"github-gitops-api-create": TestGitopsAPICreate,
54+
"github-gitops-api-update": TestGitopsAPIOrUpdate,
55+
"github-gitops-api-delete-multiple": TestGitopsAPIDeleteMultiple,
56+
"github-gitops-api-delete": TestGitopsAPIDelete,
5657
}
5758
scheme = runtime.NewScheme()
5859
log = ctrl.Log.WithName("e2e")
@@ -199,7 +200,7 @@ func TestGitopsAPICreate(ctx context.Context, test *console.TestResults) error {
199200
return err
200201
}
201202

202-
func TestGitopsAPIUpdate(ctx context.Context, test *console.TestResults) error {
203+
func TestGitopsAPIOrUpdate(ctx context.Context, test *console.TestResults) error {
203204
git, err := connectors.NewConnector(ctx, crdK8s, k8s, log, "platform-system", "https://github.com/"+repository, &v1.LocalObjectReference{
204205
Name: "github",
205206
})
@@ -208,19 +209,41 @@ func TestGitopsAPIUpdate(ctx context.Context, test *console.TestResults) error {
208209
}
209210
branchName := getBranchName("test")
210211
body := `
211-
[
212-
{
213-
"apiVersion": "v1",
214-
"data": {
215-
"some-key": "some-value",
216-
"new-key": "new-value"
217-
},
218-
"kind": "ConfigMap",
219-
"metadata": {
220-
"name": "test-configmap",
221-
"namespace": "default"
212+
[
213+
{
214+
"apiVersion": "v1",
215+
"data": {
216+
"some-key": "some-value",
217+
"new-key": "new-value"
218+
},
219+
"kind": "ConfigMap",
220+
"metadata": {
221+
"name": "test-configmap",
222+
"namespace": "default"
223+
}
224+
},
225+
{
226+
"apiVersion": "acmp.corp/v1",
227+
"kind": "NamespaceRequest",
228+
"metadata": {
229+
"name": "tenant8"
230+
},
231+
"spec": {
232+
"cluster": "dev01",
233+
"memory": 11,
234+
"some-new-key": "test-value"
235+
}
236+
},
237+
{
238+
"apiVersion": "v1",
239+
"data": {
240+
"some-key": "some-value"
241+
},
242+
"kind": "ConfigMap",
243+
"metadata": {
244+
"name": "sample-configmap"
245+
}
222246
}
223-
}
224247
]
225248
`
226249
api := &gitv1.GitopsAPI{
@@ -230,7 +253,7 @@ func TestGitopsAPIUpdate(ctx context.Context, test *console.TestResults) error {
230253
SearchPath: "resources/",
231254
Kustomization: "resources/kustomization.yaml",
232255
PullRequest: &gitv1.PullRequestTemplate{
233-
Title: "Automated PR: Updated existing object {{.metadata.name}}",
256+
Title: "Updating/Creating multiple objects",
234257
Body: "Somebody created a new PR {{.metadata.name}}",
235258
},
236259
},
@@ -270,18 +293,86 @@ func TestGitopsAPIDelete(ctx context.Context, test *console.TestResults) error {
270293
branchName := getBranchName("test")
271294
body := `
272295
[
273-
{
274-
"apiVersion": "v1",
275-
"data": {
276-
"some-key": "some-value",
277-
"new-key": "new-value"
278-
},
279-
"kind": "ConfigMap",
280-
"metadata": {
281-
"name": "test-configmap",
282-
"namespace": "default"
296+
{
297+
"apiVersion": "v1",
298+
"data": {
299+
"some-key": "some-value"
300+
},
301+
"kind": "ConfigMap",
302+
"metadata": {
303+
"name": "some-configmap"
304+
}
283305
}
306+
]
307+
`
308+
log.Info("json", "value", body)
309+
api := &gitv1.GitopsAPI{
310+
Spec: gitv1.GitopsAPISpec{
311+
GitRepository: repository,
312+
Branch: branchName,
313+
SearchPath: "resources/",
314+
PullRequest: &gitv1.PullRequestTemplate{
315+
Title: "Automated PR: Delete single object",
316+
Body: "Somebody created a new PR {{.metadata.name}}",
317+
},
318+
},
319+
}
320+
work, title, err := controllers.DeleteObject(ctx, log, git, api, bytes.NewReader([]byte(body)), "application/json")
321+
if err != nil {
322+
return err
323+
}
324+
_, err = controllers.CreateCommit(api, work, title)
325+
if err != nil {
326+
return err
284327
}
328+
329+
if err = git.Push(ctx, fmt.Sprintf("%s:%s", api.Spec.Branch, api.Spec.Base)); err != nil {
330+
return err
331+
}
332+
pr, err := git.OpenPullRequest(ctx, api.Spec.Base, api.Spec.Branch, api.Spec.PullRequest)
333+
if err != nil {
334+
return err
335+
}
336+
if pr != 0 {
337+
if err := git.ClosePullRequest(ctx, pr); err != nil {
338+
return err
339+
}
340+
}
341+
return err
342+
}
343+
344+
func TestGitopsAPIDeleteMultiple(ctx context.Context, test *console.TestResults) error {
345+
git, err := connectors.NewConnector(ctx, crdK8s, k8s, log, "platform-system", "https://github.com/"+repository, &v1.LocalObjectReference{
346+
Name: "github",
347+
})
348+
if err != nil {
349+
return err
350+
}
351+
branchName := getBranchName("test")
352+
body := `
353+
[
354+
{
355+
"apiVersion": "v1",
356+
"data": {
357+
"some-key": "some-value"
358+
},
359+
"kind": "ConfigMap",
360+
"metadata": {
361+
"name": "some-configmap"
362+
}
363+
},
364+
{
365+
"apiVersion": "acmp.corp/v1",
366+
"kind": "NamespaceRequest",
367+
"metadata": {
368+
"name": "tenant8"
369+
},
370+
"spec": {
371+
"cluster": "dev01",
372+
"memory": 11,
373+
"some-new-key": "test-value"
374+
}
375+
}
285376
]
286377
`
287378
log.Info("json", "value", body)
@@ -291,7 +382,7 @@ func TestGitopsAPIDelete(ctx context.Context, test *console.TestResults) error {
291382
Branch: branchName,
292383
SearchPath: "resources/",
293384
PullRequest: &gitv1.PullRequestTemplate{
294-
Title: "Automated PR: Delete object {{.metadata.name}}",
385+
Title: "Automated PR: Delete multiple objects",
295386
Body: "Somebody created a new PR {{.metadata.name}}",
296387
},
297388
},
@@ -345,7 +436,7 @@ func TestGithubBranchSync(ctx context.Context, test *console.TestResults) error
345436
}
346437
test.Passf("TestGithubBranchSync", "Successfully created branch %s", branchName)
347438

348-
gitBranchGetCtx, cancelFunc := context.WithTimeout(ctx, 3*time.Minute)
439+
gitBranchGetCtx, cancelFunc := context.WithTimeout(ctx, 4*time.Minute)
349440
defer cancelFunc()
350441
crdName := fmt.Sprintf("gitrepository-sample-%s", branchName)
351442
gitBranch, err := waitForGitBranch(gitBranchGetCtx, crdName)
@@ -407,7 +498,7 @@ func TestGithubPRSync(ctx context.Context, test *console.TestResults) error {
407498
return err
408499
}
409500

410-
gitPRGetCtx, cancelFunc := context.WithTimeout(ctx, 3*time.Minute)
501+
gitPRGetCtx, cancelFunc := context.WithTimeout(ctx, 4*time.Minute)
411502
defer cancelFunc()
412503
crdName := fmt.Sprintf("gitrepository-sample-%d", *pr.Number)
413504
gitPR, err := waitForGitPullRequest(gitPRGetCtx, crdName)
@@ -502,7 +593,7 @@ func TestGithubPRCRDSync(ctx context.Context, test *console.TestResults) error {
502593
return err
503594
}
504595

505-
gitPRGetCtx, cancelFunc := context.WithTimeout(ctx, 3*time.Minute)
596+
gitPRGetCtx, cancelFunc := context.WithTimeout(ctx, 4*time.Minute)
506597
defer cancelFunc()
507598
gitPR, err := waitForGitPullRequestFromCrd(gitPRGetCtx, branchName)
508599
if err != nil {

0 commit comments

Comments
 (0)