From b79b5d5e8606f995f8153968010801bc5e3eb90d Mon Sep 17 00:00:00 2001 From: Max Jonas Werner Date: Mon, 13 Mar 2023 18:02:20 +0100 Subject: [PATCH] use spec-specific repo when testing deploy tokens Signed-off-by: Max Jonas Werner --- gitlab/client_repository_deploytoken.go | 2 +- gitlab/integration_test.go | 65 +++++++++++++++++-------- gitlab/resource_deploytoken.go | 2 +- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/gitlab/client_repository_deploytoken.go b/gitlab/client_repository_deploytoken.go index 0d0c91c2..b1d4bce0 100644 --- a/gitlab/client_repository_deploytoken.go +++ b/gitlab/client_repository_deploytoken.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Flux CD contributors. +Copyright 2023 The Flux CD contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/gitlab/integration_test.go b/gitlab/integration_test.go index 7bdd57e3..66c447f6 100644 --- a/gitlab/integration_test.go +++ b/gitlab/integration_test.go @@ -47,8 +47,7 @@ const ( // Include scheme if custom, e.g.: // gitlabDomain = "https://gitlab.acme.org/" // gitlabDomain = "https://gitlab.dev.wkp.weave.works" - // gitlabDomain = "gitlab.com" - gitlabDomain = "http://localhost:9042" + gitlabDomain = "gitlab.com" defaultDescription = "Foo description" defaultBranch = "main" @@ -186,6 +185,28 @@ var _ = Describe("GitLab Provider", func() { testOrgRepoName string = "test-org-repo" testRepoName string = "test-repo" testTreeRepoName string = "test-repo-tree" + + orgReposToCleanup []gitprovider.OrgRepositoryRef + + createRepo = func(ctx context.Context, repoRef gitprovider.OrgRepositoryRef) gitprovider.OrgRepository { + _, err := c.OrgRepositories().Get(ctx, repoRef) + Expect(errors.Is(err, gitprovider.ErrNotFound)).To(BeTrue()) + + // Create a new repo + _, err = c.OrgRepositories().Create(ctx, repoRef, gitprovider.RepositoryInfo{ + Description: gitprovider.StringVar(defaultDescription), + }, &gitprovider.RepositoryCreateOptions{ + AutoInit: gitprovider.BoolVar(true), + LicenseTemplate: gitprovider.LicenseTemplateVar(gitprovider.LicenseTemplateApache2), + }) + Expect(err).ToNot(HaveOccurred()) + orgReposToCleanup = append(orgReposToCleanup, repoRef) + + orgRepo, err := c.OrgRepositories().Get(ctx, repoRef) + Expect(err).ToNot(HaveOccurred()) + + return orgRepo + } ) BeforeSuite(func() { @@ -337,7 +358,7 @@ var _ = Describe("GitLab Provider", func() { Expect(err).ToNot(HaveOccurred()) Expect(getOrg2).ToNot(BeNil()) }) - Expect(hits).To(Equal(1)) + Expect(hits).To(Equal(1), "unexpected number of cache hits") }) It("should not fail when .Children is called", func() { @@ -629,11 +650,10 @@ var _ = Describe("GitLab Provider", func() { }) It("should create, delete and reconcile deploy tokens", func() { + orgRepo := createRepo(ctx, newOrgRepoRef( + testOrgName, + fmt.Sprintf("deploy-key-spec-repo-%03d", rand.Intn(1000)))) testDeployTokenName := "test-deploy-token" - repoRef := newOrgRepoRef(testOrgName, testSharedOrgRepoName) - - orgRepo, err := c.OrgRepositories().Get(ctx, repoRef) - Expect(err).ToNot(HaveOccurred()) dts, err := orgRepo.DeployTokens() Expect(err).ToNot(HaveOccurred()) @@ -1135,32 +1155,39 @@ var _ = Describe("GitLab Provider", func() { fmt.Println("Deleting the user repo: ", testRepoName) repoRef := newUserRepoRef(testUserName, testRepoName) repo, err := c.UserRepositories().Get(ctx, repoRef) - if errors.Is(err, gitprovider.ErrNotFound) { - return + if !errors.Is(err, gitprovider.ErrNotFound) { + Expect(err).ToNot(HaveOccurred()) + Expect(repo.Delete(ctx)).ToNot(HaveOccurred()) } - Expect(err).ToNot(HaveOccurred()) - Expect(repo.Delete(ctx)).ToNot(HaveOccurred()) // Delete the test org repo used fmt.Println("Deleting the org repo: ", testOrgRepoName) orgRepoRef := newOrgRepoRef(testOrgName, testOrgRepoName) repo, err = c.OrgRepositories().Get(ctx, orgRepoRef) - if errors.Is(err, gitprovider.ErrNotFound) { - return + if !errors.Is(err, gitprovider.ErrNotFound) { + Expect(err).ToNot(HaveOccurred()) + Expect(repo.Delete(ctx)).ToNot(HaveOccurred()) } - Expect(err).ToNot(HaveOccurred()) - Expect(repo.Delete(ctx)).ToNot(HaveOccurred()) // Delete the test shared org repo used fmt.Println("Deleting the shared org repo: ", testSharedOrgRepoName) sharedOrgRepoRef := newOrgRepoRef(testOrgName, testSharedOrgRepoName) repo, err = c.OrgRepositories().Get(ctx, sharedOrgRepoRef) - if errors.Is(err, gitprovider.ErrNotFound) { - return + if !errors.Is(err, gitprovider.ErrNotFound) { + Expect(err).ToNot(HaveOccurred()) + Expect(repo.Delete(ctx)).ToNot(HaveOccurred()) + } + + for _, ref := range orgReposToCleanup { + repo, err = c.OrgRepositories().Get(ctx, ref) + if errors.Is(err, gitprovider.ErrNotFound) { + continue + } + Expect(err).ToNot(HaveOccurred()) + Expect(repo.Delete(ctx)).ToNot(HaveOccurred(), "failed to delete repo %s", ref) } - Expect(err).ToNot(HaveOccurred()) - Expect(repo.Delete(ctx)).ToNot(HaveOccurred()) }) + }) func expectPRToBeMerged(ctx context.Context, userRepo gitprovider.UserRepository, prNumber int) { diff --git a/gitlab/resource_deploytoken.go b/gitlab/resource_deploytoken.go index b56be19b..d7ba790c 100644 --- a/gitlab/resource_deploytoken.go +++ b/gitlab/resource_deploytoken.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Flux CD contributors. +Copyright 2023 The Flux CD contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.