Skip to content

Commit

Permalink
chore: do not store context in struct for e2e tests (runatlantis#4731)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemassa authored Jul 7, 2024
1 parent e538430 commit 2341891
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
19 changes: 10 additions & 9 deletions e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"context"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -45,7 +46,7 @@ resource "null_resource" "hello" {
`

// nolint: gosec
func (t *E2ETester) Start() (*E2EResult, error) {
func (t *E2ETester) Start(ctx context.Context) (*E2EResult, error) {
cloneDir := fmt.Sprintf("%s/%s-test", t.cloneDirRoot, t.projectType.Name)
branchName := fmt.Sprintf("%s-%s", t.projectType.Name, time.Now().Format("20060102150405"))
testFileName := fmt.Sprintf("%s.tf", t.projectType.Name)
Expand Down Expand Up @@ -113,7 +114,7 @@ func (t *E2ETester) Start() (*E2EResult, error) {
base := "main"
newPullRequest := &github.NewPullRequest{Title: &title, Head: &head, Body: &body, Base: &base}

pull, _, err := t.githubClient.client.PullRequests.Create(t.githubClient.ctx, t.ownerName, t.repoName, newPullRequest)
pull, _, err := t.githubClient.client.PullRequests.Create(ctx, t.ownerName, t.repoName, newPullRequest)
if err != nil {
return e2eResult, fmt.Errorf("error while creating new pull request: %v", err)
}
Expand All @@ -124,7 +125,7 @@ func (t *E2ETester) Start() (*E2EResult, error) {
log.Printf("created pull request %s", pull.GetHTMLURL())

// defer closing pull request and delete remote branch
defer cleanUp(t, pull.GetNumber(), branchName) // nolint: errcheck
defer cleanUp(ctx, t, pull.GetNumber(), branchName) // nolint: errcheck

// wait for atlantis to respond to webhook and autoplan.
time.Sleep(2 * time.Second)
Expand All @@ -135,7 +136,7 @@ func (t *E2ETester) Start() (*E2EResult, error) {
i := 0
for ; i < maxLoops && checkStatus(state); i++ {
time.Sleep(2 * time.Second)
state, _ = getAtlantisStatus(t, branchName)
state, _ = getAtlantisStatus(ctx, t, branchName)
if state == "" {
log.Println("atlantis run hasn't started")
continue
Expand All @@ -156,9 +157,9 @@ func (t *E2ETester) Start() (*E2EResult, error) {
return e2eResult, nil
}

func getAtlantisStatus(t *E2ETester, branchName string) (string, error) {
func getAtlantisStatus(ctx context.Context, t *E2ETester, branchName string) (string, error) {
// check repo status
combinedStatus, _, err := t.githubClient.client.Repositories.GetCombinedStatus(t.githubClient.ctx, t.ownerName, t.repoName, branchName, nil)
combinedStatus, _, err := t.githubClient.client.Repositories.GetCombinedStatus(ctx, t.ownerName, t.repoName, branchName, nil)
if err != nil {
return "", err
}
Expand All @@ -181,16 +182,16 @@ func checkStatus(state string) bool {
return true
}

func cleanUp(t *E2ETester, pullRequestNumber int, branchName string) error {
func cleanUp(ctx context.Context, t *E2ETester, pullRequestNumber int, branchName string) error {
// clean up
pullClosed, _, err := t.githubClient.client.PullRequests.Edit(t.githubClient.ctx, t.ownerName, t.repoName, pullRequestNumber, &github.PullRequest{State: github.String("closed")})
pullClosed, _, err := t.githubClient.client.PullRequests.Edit(ctx, t.ownerName, t.repoName, pullRequestNumber, &github.PullRequest{State: github.String("closed")})
if err != nil {
return fmt.Errorf("error while closing new pull request: %v", err)
}
log.Printf("closed pull request %d", pullClosed.GetNumber())

deleteBranchName := fmt.Sprintf("%s/%s", "heads", branchName)
_, err = t.githubClient.client.Git.DeleteRef(t.githubClient.ctx, t.ownerName, t.repoName, deleteBranchName)
_, err = t.githubClient.client.Git.DeleteRef(ctx, t.ownerName, t.repoName, deleteBranchName)
if err != nil {
return fmt.Errorf("error while deleting branch %s: %v", deleteBranchName, err)
}
Expand Down
3 changes: 0 additions & 3 deletions e2e/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
package main

import (
"context"

"github.com/google/go-github/v59/github"
)

type GithubClient struct {
client *github.Client
ctx context.Context
username string
}
22 changes: 11 additions & 11 deletions e2e/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func main() {
}
ghClient := github.NewClient(tp.Client())

githubClient := &GithubClient{client: ghClient, ctx: context.Background(), username: githubUsername}

githubClient := &GithubClient{client: ghClient, username: githubUsername}
ctx := context.Background()
// we create atlantis hook once for the repo, since the atlantis server can handle multiple requests
log.Printf("creating atlantis webhook with %s url", atlantisURL)
hookID, err := createAtlantisWebhook(githubClient, ownerName, repoName, atlantisURL)
hookID, err := createAtlantisWebhook(ctx, githubClient, ownerName, repoName, atlantisURL)
if err != nil {
log.Fatalf("error creating atlantis webhook: %v", err)
}
Expand All @@ -101,7 +101,7 @@ func main() {
}

// start e2e tests
results, err := startTests(e2e)
results, err := startTests(ctx, e2e)
log.Printf("Test Results\n---------------------------\n")
for _, result := range results {
fmt.Printf("Project Type: %s \n", result.projectType)
Expand All @@ -115,7 +115,7 @@ func main() {

}

func createAtlantisWebhook(g *GithubClient, ownerName string, repoName string, hookURL string) (int64, error) {
func createAtlantisWebhook(ctx context.Context, g *GithubClient, ownerName string, repoName string, hookURL string) (int64, error) {
// create atlantis hook
atlantisHook := &github.Hook{
Events: []string{"issue_comment", "pull_request", "push"},
Expand All @@ -127,7 +127,7 @@ func createAtlantisWebhook(g *GithubClient, ownerName string, repoName string, h
}

// moved to github.go
hook, _, err := g.client.Repositories.CreateHook(g.ctx, ownerName, repoName, atlantisHook)
hook, _, err := g.client.Repositories.CreateHook(ctx, ownerName, repoName, atlantisHook)
if err != nil {
return 0, err
}
Expand All @@ -136,8 +136,8 @@ func createAtlantisWebhook(g *GithubClient, ownerName string, repoName string, h
return hook.GetID(), nil
}

func deleteAtlantisHook(g *GithubClient, ownerName string, repoName string, hookID int64) error {
_, err := g.client.Repositories.DeleteHook(g.ctx, ownerName, repoName, hookID)
func deleteAtlantisHook(ctx context.Context, g *GithubClient, ownerName string, repoName string, hookID int64) error {
_, err := g.client.Repositories.DeleteHook(ctx, ownerName, repoName, hookID)
if err != nil {
return err
}
Expand All @@ -150,17 +150,17 @@ func cleanDir(path string) error {
return os.RemoveAll(path)
}

func startTests(e2e E2ETester) ([]*E2EResult, error) {
func startTests(ctx context.Context, e2e E2ETester) ([]*E2EResult, error) {
var testResults []*E2EResult
var testErrors *multierror.Error
// delete webhook when we are done running tests
defer deleteAtlantisHook(e2e.githubClient, e2e.ownerName, e2e.repoName, e2e.hookID) // nolint: errcheck
defer deleteAtlantisHook(ctx, e2e.githubClient, e2e.ownerName, e2e.repoName, e2e.hookID) // nolint: errcheck

for _, projectType := range projectTypes {
log.Printf("starting e2e test for project type %q", projectType.Name)
e2e.projectType = projectType
// start e2e test
result, err := e2e.Start()
result, err := e2e.Start(ctx)
testResults = append(testResults, result)
testErrors = multierror.Append(testErrors, err)
}
Expand Down

0 comments on commit 2341891

Please sign in to comment.