Skip to content

Commit

Permalink
use spec-specific repo when testing deploy tokens
Browse files Browse the repository at this point in the history
Signed-off-by: Max Jonas Werner <[email protected]>
  • Loading branch information
makkes committed Mar 14, 2023
1 parent a464dac commit b79b5d5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gitlab/client_repository_deploytoken.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
65 changes: 46 additions & 19 deletions gitlab/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion gitlab/resource_deploytoken.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit b79b5d5

Please sign in to comment.