Skip to content

Commit

Permalink
Support running tests in forked repository workflow
Browse files Browse the repository at this point in the history
Changes the GITHUB_TOKEN environmental variable to GITPROVIDER_BOT_TOKEN
because github secrets cannot start with GITHUB_

Add an option to cleanup all test repos, the integration tests sometimes
fail to cleanup.

Signed-off-by: Paul Carlton <[email protected]>
  • Loading branch information
Paul Carlton committed Jun 22, 2021
1 parent 0155ef7 commit c447a8c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 11 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ jobs:
with:
go-version: 1.15.x
- name: Run tests
env:
GITHUB_TOKEN: ${{ secrets.GITPROVIDER_BOT_TOKEN }}
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
run: make test
run: |
[ -n "${{ secrets.GITLAB_TOKEN }}" ] && export GITLAB_TOKEN=${{ secrets.GITLAB_TOKEN }} || echo "using default GITLAB_TOKEN"
[ -n "${{ secrets.GITPROVIDER_BOT_TOKEN }}" ] && export GITHUB_TOKEN=${{ secrets.GITPROVIDER_BOT_TOKEN }} || echo "using default GITHUB_TOKEN"
[ -n "${{ secrets.GIT_PROVIDER_USER }}" ] && export GIT_PROVIDER_USER=${{ secrets.GIT_PROVIDER_USER }} || echo "using default GIT_PROVIDER_USER"
[ -n "${{ secrets.GIT_PROVIDER_ORGANIZATION }}" ] && export GIT_PROVIDER_ORGANIZATION=${{ secrets.GIT_PROVIDER_ORGANIZATION }} || echo "using default GIT_PROVIDER_ORGANIZATION"
[ -n "${{ secrets.GITLAB_TEST_TEAM_NAME }}" ] && export GITLAB_TEST_TEAM_NAME=${{ secrets.GITLAB_TEST_TEAM_NAME }} || echo "using default GITLAB_TEST_TEAM_NAME"
[ -n "${{ secrets.GITLAB_TEST_SUBGROUP }}" ] && export GITLAB_TEST_SUBGROUP=${{ secrets.GITLAB_TEST_SUBGROUP }} || echo "using default GITLAB_TEST_SUBGROUP"
[ -n "${{ secrets.TEST_VERBOSE }}" ] && export TEST_VERBOSE=${{ secrets.TEST_VERBOSE }} || echo "TEST_VERBOSE not set"
[ -n "${{ secrets.CLEANUP_ALL }}" ] && export CLEANUP_ALL=${{ secrets.CLEANUP_ALL }} || echo "CLEANUP_ALL not set"
make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage.txt

2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
version: latest
args: release --release-notes=/tmp/release.txt --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITPROVIDER_BOT_TOKEN }}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ meeting](https://docs.google.com/document/d/1l_M0om0qUEN_NNiGgpqJ2tvsF2iioHkaARD
To run the e2e tests you'll have to provide GitHub and GitLab tokens:

```sh
export GITHUB_TOKEN=YOUR-GH-ADMIN-TOKEN
export GITPROVIDER_BOT_TOKEN=YOUR-GH-ADMIN-TOKEN
export GITLAB_TOKEN=YOUR-GL-ADMIN-TOKEN
```

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VER?=0.0.1
TEST_VERBOSE?=

all: test

Expand All @@ -12,7 +13,7 @@ vet:
go vet ./...

test: tidy fmt vet
go test -race -coverprofile=coverage.txt -covermode=atomic ./...
go test ${TEST_VERBOSE} -race -coverprofile=coverage.txt -covermode=atomic ./...

release:
git checkout main
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ If you need to run `make test` for your fork/branch you may need to supply the f
- GIT_PROVIDER_USER : This should be the same username for both GitHub and GitLab.
- GITLAB_TEST_TEAM_NAME : An existing GitLab group.
- GITLAB_TEST_SUBGROUP : An existing GitLab subgroup of the GIT_PROVIDER_ORGANIZATION top-level group.
- GITHUB_TOKEN : A GitHub token with `repo`, `admin:org` and `delete_repo` permissions.
- GITPROVIDER_BOT_TOKEN : A GitHub token with `repo`, `admin:org` and `delete_repo` permissions.
- GITLAB_TOKEN: A GitLab token with `api` scope.
- TEST_VERBOSE: Set to '-v' to emit test output for debugging purposes
- CLEANUP_ALL: Set to delete all test repos after testing.

## Maintainers

Expand Down
49 changes: 46 additions & 3 deletions github/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math/rand"
"net/http"
"os"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -164,6 +165,38 @@ var _ = Describe("GitHub Provider", func() {
Expect(err).ToNot(HaveOccurred())
})

cleanupOrgRepos := func(prefix string) {
fmt.Printf("Deleting repos starting with %s in org: %s\n", prefix, testOrgName)
repos, err := c.OrgRepositories().List(ctx, newOrgRef(testOrgName))
Expect(err).ToNot(HaveOccurred())
for _, repo := range repos {
// Delete the test org repo used
name := repo.Repository().GetRepository()
if !strings.HasPrefix(name, prefix) {
continue
}
fmt.Printf("Deleting the org repo: %s\n", name)
repo.Delete(ctx)
Expect(err).ToNot(HaveOccurred())
}
}

cleanupUserRepos := func(prefix string) {
fmt.Printf("Deleting repos starting with %s for user: %s\n", prefix, testUser)
repos, err := c.UserRepositories().List(ctx, newUserRef(testUser))
Expect(err).ToNot(HaveOccurred())
for _, repo := range repos {
name := repo.Repository().GetRepository()
if !strings.HasPrefix(name, prefix) {
fmt.Printf("Skipping the org repo: %s\n", name)
continue
}
fmt.Printf("Deleting the org repo: %s\n", name)
repo.Delete(ctx)
Expect(err).ToNot(HaveOccurred())
}
}

It("should list the available organizations the user has access to", func() {
// Get a list of all organizations the user is part of
orgs, err := c.Organizations().List(ctx)
Expand Down Expand Up @@ -418,11 +451,21 @@ var _ = Describe("GitHub Provider", func() {
if c == nil {
return
}

if len(os.Getenv("CLEANUP_ALL")) > 0 {
defer cleanupOrgRepos("test-repo")
defer cleanupUserRepos("test-repo")
}

// Delete the org test repo used
orgRepo, err := c.OrgRepositories().Get(ctx, newOrgRepoRef(testOrgName, testOrgRepoName))
Expect(err).ToNot(HaveOccurred())
Expect(orgRepo.Delete(ctx)).ToNot(HaveOccurred())

if err != nil && len(os.Getenv("CLEANUP_ALL")) > 0 {
fmt.Printf("failed to get repo: %s in org: %s, error: %s\n", testOrgRepoName, testOrgName, err)
fmt.Printf("CLEANUP_ALL set so continuing\n")
} else {
Expect(err).ToNot(HaveOccurred())
Expect(orgRepo.Delete(ctx)).ToNot(HaveOccurred())
}
// Delete the user test repo used
userRepo, err := c.UserRepositories().Get(ctx, newUserRepoRef(testUser, testUserRepoName))
Expect(err).ToNot(HaveOccurred())
Expand Down
38 changes: 38 additions & 0 deletions gitlab/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,38 @@ var _ = Describe("GitLab Provider", func() {
Expect(*info.DefaultBranch).To(Equal(internal.DefaultBranch))
}

cleanupOrgRepos := func(prefix string) {
fmt.Printf("Deleting repos starting with %s in org: %s\n", prefix, testOrgName)
repos, err := c.OrgRepositories().List(ctx, newOrgRef(testOrgName))
Expect(err).ToNot(HaveOccurred())
for _, repo := range repos {
// Delete the test org repo used
name := repo.Repository().GetRepository()
if !strings.HasPrefix(name, prefix) {
continue
}
fmt.Printf("Deleting the org repo: %s\n", name)
repo.Delete(ctx)
Expect(err).ToNot(HaveOccurred())
}
}

cleanupUserRepos := func(prefix string) {
fmt.Printf("Deleting repos starting with %s for user: %s\n", prefix, testUserName)
repos, err := c.UserRepositories().List(ctx, newUserRef(testUserName))
Expect(err).ToNot(HaveOccurred())
for _, repo := range repos {
// Delete the test org repo used
name := repo.Repository().GetRepository()
if !strings.HasPrefix(name, prefix) {
continue
}
fmt.Printf("Deleting the org repo: %s\n", name)
repo.Delete(ctx)
Expect(err).ToNot(HaveOccurred())
}
}

It("should list the available organizations the user has access to", func() {
// Get a list of all organizations the user is part of
orgs, err := c.Organizations().List(ctx)
Expand Down Expand Up @@ -743,6 +775,12 @@ var _ = Describe("GitLab Provider", func() {
if c == nil {
return
}

if len(os.Getenv("CLEANUP_ALL")) > 0 {
defer cleanupOrgRepos("test-org-repo")
defer cleanupOrgRepos("test-shared-org-repo")
defer cleanupUserRepos("test-repo")
}
// Delete the test repo used
fmt.Println("Deleting the user repo: ", testRepoName)
repoRef := newUserRepoRef(testUserName, testRepoName)
Expand Down

0 comments on commit c447a8c

Please sign in to comment.