Skip to content

Commit

Permalink
chore: Make e2e tests more vcs agnostic (#4836)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemassa authored Aug 13, 2024
1 parent f7829b0 commit 9f887f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
20 changes: 5 additions & 15 deletions e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (t *E2ETester) Start(ctx context.Context) (*E2EResult, error) {
// waiting for atlantis run and finish
maxLoops := 20
i := 0
for ; i < maxLoops && checkStatus(state); i++ {
for ; i < maxLoops && t.vcsClient.IsAtlantisInProgress(state); i++ {
time.Sleep(2 * time.Second)
state, _ = t.vcsClient.GetAtlantisStatus(ctx, branchName)
if state == "" {
Expand All @@ -144,22 +144,13 @@ func (t *E2ETester) Start(ctx context.Context) (*E2EResult, error) {
log.Printf("atlantis run finished with status %q", state)
e2eResult.testResult = state
// check if atlantis run was a success
if state != "success" {
if !t.vcsClient.DidAtlantisSucceed(state) {
return e2eResult, fmt.Errorf("atlantis run project type %q failed with %q status", t.projectType.Name, state)
}

return e2eResult, nil
}

func checkStatus(state string) bool {
for _, s := range []string{"success", "error", "failure"} {
if state == s {
return false
}
}
return true
}

func cleanUp(ctx context.Context, t *E2ETester, pullRequestNumber int, branchName string) error {
// clean up
err := t.vcsClient.ClosePullRequest(ctx, pullRequestNumber)
Expand All @@ -168,12 +159,11 @@ func cleanUp(ctx context.Context, t *E2ETester, pullRequestNumber int, branchNam
}
log.Printf("closed pull request %d", pullRequestNumber)

deleteBranchName := fmt.Sprintf("%s/%s", "heads", branchName)
err = t.vcsClient.DeleteBranch(ctx, deleteBranchName)
err = t.vcsClient.DeleteBranch(ctx, branchName)
if err != nil {
return fmt.Errorf("error while deleting branch %s: %v", deleteBranchName, err)
return fmt.Errorf("error while deleting branch %s: %v", branchName, err)
}
log.Printf("deleted branch %s", deleteBranchName)
log.Printf("deleted branch %s", branchName)

return nil
}
16 changes: 15 additions & 1 deletion e2e/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,23 @@ func (g GithubClient) ClosePullRequest(ctx context.Context, pullRequestNumber in
}
func (g GithubClient) DeleteBranch(ctx context.Context, branchName string) error {

_, err := g.client.Git.DeleteRef(ctx, g.ownerName, g.repoName, branchName)
deleteBranchName := fmt.Sprintf("%s/%s", "heads", branchName)
_, err := g.client.Git.DeleteRef(ctx, g.ownerName, g.repoName, deleteBranchName)
if err != nil {
return fmt.Errorf("error while deleting branch %s: %v", branchName, err)
}
return nil
}

func (g GithubClient) IsAtlantisInProgress(state string) bool {
for _, s := range []string{"success", "error", "failure"} {
if state == s {
return false
}
}
return true
}

func (g GithubClient) DidAtlantisSucceed(state string) bool {
return state == "success"
}
2 changes: 2 additions & 0 deletions e2e/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ type VCSClient interface {
GetAtlantisStatus(ctx context.Context, branchName string) (string, error)
ClosePullRequest(ctx context.Context, pullRequestNumber int) error
DeleteBranch(ctx context.Context, branchName string) error
IsAtlantisInProgress(state string) bool
DidAtlantisSucceed(state string) bool
}

0 comments on commit 9f887f8

Please sign in to comment.