Skip to content

Commit

Permalink
chore(test): add no test dir tests, rename testdata dir (#2986)
Browse files Browse the repository at this point in the history
* add web_templates render tests

* add ansic strip tests

* move fixtures into testdata dir which is golang specific test dir name

* add server/metrics tests

* add recovery test

* add runtime stats test
  • Loading branch information
krrrr38 authored Jan 16, 2023
1 parent 71738f1 commit da48fb5
Show file tree
Hide file tree
Showing 221 changed files with 506 additions and 276 deletions.
8 changes: 4 additions & 4 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"gopkg.in/yaml.v2"

"github.com/runatlantis/atlantis/server"
"github.com/runatlantis/atlantis/server/events/vcs/fixtures"
"github.com/runatlantis/atlantis/server/events/vcs/testdata"
"github.com/runatlantis/atlantis/server/logging"
. "github.com/runatlantis/atlantis/testing"
)
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestExecute_ValidateVCSConfig(t *testing.T) {
{
"just github app key set",
map[string]interface{}{
GHAppKeyFlag: fixtures.GithubPrivateKey,
GHAppKeyFlag: testdata.GithubPrivateKey,
},
true,
},
Expand Down Expand Up @@ -496,7 +496,7 @@ func TestExecute_ValidateVCSConfig(t *testing.T) {
"github app and key set and should be successful",
map[string]interface{}{
GHAppIDFlag: "1",
GHAppKeyFlag: fixtures.GithubPrivateKey,
GHAppKeyFlag: testdata.GithubPrivateKey,
},
false,
},
Expand Down Expand Up @@ -631,7 +631,7 @@ func TestExecute_GithubUser(t *testing.T) {
func TestExecute_GithubApp(t *testing.T) {
t.Log("Should remove the @ from the github username if it's passed.")
c := setup(map[string]interface{}{
GHAppKeyFlag: fixtures.GithubPrivateKey,
GHAppKeyFlag: testdata.GithubPrivateKey,
GHAppIDFlag: "1",
RepoAllowlistFlag: "*",
}, t)
Expand Down
18 changes: 9 additions & 9 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestGitHubWorkflow(t *testing.T) {

cases := []struct {
Description string
// RepoDir is relative to testfixtures/test-repos.
// RepoDir is relative to testdata/test-repos.
RepoDir string
// RepoConfigFile is path for atlantis.yaml
RepoConfigFile string
Expand Down Expand Up @@ -592,7 +592,7 @@ func TestSimpleWorkflow_terraformLockFile(t *testing.T) {

cases := []struct {
Description string
// RepoDir is relative to testfixtures/test-repos.
// RepoDir is relative to testdata/test-repos.
RepoDir string
// ModifiedFiles are the list of files that have been modified in this
// pull request.
Expand Down Expand Up @@ -664,7 +664,7 @@ func TestSimpleWorkflow_terraformLockFile(t *testing.T) {
// Set the repo to be cloned through the testing backdoor.
repoDir, headSHA := initializeRepo(t, c.RepoDir)

oldLockFilePath, err := filepath.Abs(filepath.Join("testfixtures", "null_provider_lockfile_old_version"))
oldLockFilePath, err := filepath.Abs(filepath.Join("testdata", "null_provider_lockfile_old_version"))
Ok(t, err)
oldLockFileContent, err := os.ReadFile(oldLockFilePath)
Ok(t, err)
Expand Down Expand Up @@ -759,7 +759,7 @@ func TestGitHubWorkflowWithPolicyCheck(t *testing.T) {

cases := []struct {
Description string
// RepoDir is relative to testfixtures/test-repos.
// RepoDir is relative to testdata/test-repos.
RepoDir string
// ModifiedFiles are the list of files that have been modified in this
// pull request.
Expand Down Expand Up @@ -1330,7 +1330,7 @@ func (w *mockWebhookSender) Send(log logging.SimpleLogging, result webhooks.Appl
}

func GitHubCommentEvent(t *testing.T, comment string) *http.Request {
requestJSON, err := os.ReadFile(filepath.Join("testfixtures", "githubIssueCommentEvent.json"))
requestJSON, err := os.ReadFile(filepath.Join("testdata", "githubIssueCommentEvent.json"))
Ok(t, err)
escapedComment, err := json.Marshal(comment)
Ok(t, err)
Expand All @@ -1343,7 +1343,7 @@ func GitHubCommentEvent(t *testing.T, comment string) *http.Request {
}

func GitHubPullRequestOpenedEvent(t *testing.T, headSHA string) *http.Request {
requestJSON, err := os.ReadFile(filepath.Join("testfixtures", "githubPullRequestOpenedEvent.json"))
requestJSON, err := os.ReadFile(filepath.Join("testdata", "githubPullRequestOpenedEvent.json"))
Ok(t, err)
// Replace sha with expected sha.
requestJSONStr := strings.Replace(string(requestJSON), "c31fd9ea6f557ad2ea659944c3844a059b83bc5d", headSHA, -1)
Expand All @@ -1355,7 +1355,7 @@ func GitHubPullRequestOpenedEvent(t *testing.T, headSHA string) *http.Request {
}

func GitHubPullRequestClosedEvent(t *testing.T) *http.Request {
requestJSON, err := os.ReadFile(filepath.Join("testfixtures", "githubPullRequestClosedEvent.json"))
requestJSON, err := os.ReadFile(filepath.Join("testdata", "githubPullRequestClosedEvent.json"))
Ok(t, err)
req, err := http.NewRequest("POST", "/events", bytes.NewBuffer(requestJSON))
Ok(t, err)
Expand Down Expand Up @@ -1396,12 +1396,12 @@ func GitHubPullRequestParsed(headSHA string) *github.PullRequest {

// absRepoPath returns the absolute path to the test repo under dir repoDir.
func absRepoPath(t *testing.T, repoDir string) string {
path, err := filepath.Abs(filepath.Join("testfixtures", "test-repos", repoDir))
path, err := filepath.Abs(filepath.Join("testdata", "test-repos", repoDir))
Ok(t, err)
return path
}

// initializeRepo copies the repo data from testfixtures and initializes a new
// initializeRepo copies the repo data from testdata and initializes a new
// git repo in a temp directory. It returns that directory and a function
// to run in a defer that will delete the dir.
// The purpose of this function is to create a real git repository with a branch
Expand Down
10 changes: 5 additions & 5 deletions server/controllers/events/events_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestPost_GitlabCommentNotAllowlisted(t *testing.T) {
RepoAllowlistChecker: &events.RepoAllowlistChecker{},
VCSClient: vcsClient,
}
requestJSON, err := os.ReadFile(filepath.Join("testfixtures", "gitlabMergeCommentEvent_notAllowlisted.json"))
requestJSON, err := os.ReadFile(filepath.Join("testdata", "gitlabMergeCommentEvent_notAllowlisted.json"))
Ok(t, err)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(requestJSON))
req.Header.Set(gitlabHeader, "Note Hook")
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestPost_GitlabCommentNotAllowlistedWithSilenceErrors(t *testing.T) {
VCSClient: vcsClient,
SilenceAllowlistErrors: true,
}
requestJSON, err := os.ReadFile(filepath.Join("testfixtures", "gitlabMergeCommentEvent_notAllowlisted.json"))
requestJSON, err := os.ReadFile(filepath.Join("testdata", "gitlabMergeCommentEvent_notAllowlisted.json"))
Ok(t, err)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(requestJSON))
req.Header.Set(gitlabHeader, "Note Hook")
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestPost_GithubCommentNotAllowlisted(t *testing.T) {
RepoAllowlistChecker: &events.RepoAllowlistChecker{},
VCSClient: vcsClient,
}
requestJSON, err := os.ReadFile(filepath.Join("testfixtures", "githubIssueCommentEvent_notAllowlisted.json"))
requestJSON, err := os.ReadFile(filepath.Join("testdata", "githubIssueCommentEvent_notAllowlisted.json"))
Ok(t, err)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(requestJSON))
req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestPost_GithubCommentNotAllowlistedWithSilenceErrors(t *testing.T) {
VCSClient: vcsClient,
SilenceAllowlistErrors: true,
}
requestJSON, err := os.ReadFile(filepath.Join("testfixtures", "githubIssueCommentEvent_notAllowlisted.json"))
requestJSON, err := os.ReadFile(filepath.Join("testdata", "githubIssueCommentEvent_notAllowlisted.json"))
Ok(t, err)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(requestJSON))
req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -709,7 +709,7 @@ func TestPost_BBServerPullClosed(t *testing.T) {
}

// Build HTTP request.
requestBytes, err := os.ReadFile(filepath.Join("testfixtures", "bb-server-pull-deleted-event.json"))
requestBytes, err := os.ReadFile(filepath.Join("testdata", "bb-server-pull-deleted-event.json"))
// Replace the eventKey field with our event type.
requestJSON := strings.Replace(string(requestBytes), `"eventKey":"pr:deleted",`, fmt.Sprintf(`"eventKey":"%s",`, c.header), -1)
Ok(t, err)
Expand Down
1 change: 0 additions & 1 deletion server/controllers/templates/web_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ type LockDetailData struct {
PullRequestLink string
LockedBy string
Workspace string
Time time.Time
AtlantisVersion string
// CleanedBasePath is the path Atlantis is accessible at externally. If
// not using a path-based proxy, this will be an empty string. Never ends
Expand Down
79 changes: 79 additions & 0 deletions server/controllers/templates/web_templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package templates

import (
"io"
"testing"
"time"

. "github.com/runatlantis/atlantis/testing"
)

func TestIndexTemplate(t *testing.T) {
err := IndexTemplate.Execute(io.Discard, IndexData{
Locks: []LockIndexData{
{
LockPath: "lock path",
RepoFullName: "repo full name",
PullNum: 1,
Path: "path",
Workspace: "workspace",
Time: time.Now(),
TimeFormatted: "02-01-2006 15:04:05",
},
},
ApplyLock: ApplyLockData{
Locked: true,
Time: time.Now(),
TimeFormatted: "02-01-2006 15:04:05",
},
AtlantisVersion: "v0.0.0",
CleanedBasePath: "/path",
})
Ok(t, err)
}

func TestLockTemplate(t *testing.T) {
err := LockTemplate.Execute(io.Discard, LockDetailData{
LockKeyEncoded: "lock key encoded",
LockKey: "lock key",
PullRequestLink: "https://example.com",
LockedBy: "locked by",
Workspace: "workspace",
AtlantisVersion: "v0.0.0",
CleanedBasePath: "/path",
RepoOwner: "repo owner",
RepoName: "repo name",
})
Ok(t, err)
}

func TestProjectJobsTemplate(t *testing.T) {
err := ProjectJobsTemplate.Execute(io.Discard, ProjectJobData{
AtlantisVersion: "v0.0.0",
ProjectPath: "project path",
CleanedBasePath: "/path",
})
Ok(t, err)
}

func TestProjectJobsErrorTemplate(t *testing.T) {
err := ProjectJobsTemplate.Execute(io.Discard, ProjectJobsError{
AtlantisVersion: "v0.0.0",
ProjectPath: "project path",
CleanedBasePath: "/path",
})
Ok(t, err)
}

func TestGithubAppSetupTemplate(t *testing.T) {
err := GithubAppSetupTemplate.Execute(io.Discard, GithubSetupData{
Target: "target",
Manifest: "manifest",
ID: 1,
Key: "key",
WebhookSecret: "webhook secret",
URL: "https://example.com",
CleanedBasePath: "/path",
})
Ok(t, err)
}
14 changes: 7 additions & 7 deletions server/events/apply_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/models/fixtures"
"github.com/runatlantis/atlantis/server/events/models/testdata"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/metrics"
)
Expand Down Expand Up @@ -53,23 +53,23 @@ func TestApplyCommandRunner_IsLocked(t *testing.T) {
pull := &github.PullRequest{
State: github.String("open"),
}
modelPull := models.PullRequest{BaseRepo: fixtures.GithubRepo, State: models.OpenPullState, Num: fixtures.Pull.Num}
When(githubGetter.GetPullRequest(fixtures.GithubRepo, fixtures.Pull.Num)).ThenReturn(pull, nil)
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, fixtures.GithubRepo, nil)
modelPull := models.PullRequest{BaseRepo: testdata.GithubRepo, State: models.OpenPullState, Num: testdata.Pull.Num}
When(githubGetter.GetPullRequest(testdata.GithubRepo, testdata.Pull.Num)).ThenReturn(pull, nil)
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, testdata.GithubRepo, nil)

ctx := &command.Context{
User: fixtures.User,
User: testdata.User,
Log: logging.NewNoopLogger(t),
Scope: scopeNull,
Pull: modelPull,
HeadRepo: fixtures.GithubRepo,
HeadRepo: testdata.GithubRepo,
Trigger: command.CommentTrigger,
}

When(applyLockChecker.CheckApplyLock()).ThenReturn(locking.ApplyCommandLock{Locked: c.ApplyLocked}, c.ApplyLockError)
applyCommandRunner.Run(ctx, &events.CommentCommand{Name: command.Apply})

vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, c.ExpComment, "apply")
vcsClient.VerifyWasCalledOnce().CreateComment(testdata.GithubRepo, modelPull.Num, c.ExpComment, "apply")
})
}
}
16 changes: 8 additions & 8 deletions server/events/approve_policies_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/models/fixtures"
"github.com/runatlantis/atlantis/server/events/models/testdata"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/metrics"
)
Expand All @@ -32,7 +32,7 @@ func TestApproveCommandRunner_IsOwner(t *testing.T) {
},
{
Description: "When user is an owner, approval succeeds",
OwnerUsers: []string{fixtures.User.Username},
OwnerUsers: []string{testdata.User.Username},
OwnerTeams: []string{},
ExpComment: "Approved Policies for 1 projects:\n\n1. dir: `` workspace: ``",
},
Expand All @@ -52,7 +52,7 @@ func TestApproveCommandRunner_IsOwner(t *testing.T) {
},
{
Description: "When user is an owner but not a team member, approval succeeds",
OwnerUsers: []string{fixtures.User.Username},
OwnerUsers: []string{testdata.User.Username},
OwnerTeams: []string{"SomeTeam"},
UserTeams: []string{"SomeOtherTeam"},
ExpComment: "Approved Policies for 1 projects:\n\n1. dir: `` workspace: ``",
Expand All @@ -65,14 +65,14 @@ func TestApproveCommandRunner_IsOwner(t *testing.T) {

scopeNull, _, _ := metrics.NewLoggingScope(logger, "atlantis")

modelPull := models.PullRequest{BaseRepo: fixtures.GithubRepo, State: models.OpenPullState, Num: fixtures.Pull.Num}
modelPull := models.PullRequest{BaseRepo: testdata.GithubRepo, State: models.OpenPullState, Num: testdata.Pull.Num}

ctx := &command.Context{
User: fixtures.User,
User: testdata.User,
Log: logging.NewNoopLogger(t),
Scope: scopeNull,
Pull: modelPull,
HeadRepo: fixtures.GithubRepo,
HeadRepo: testdata.GithubRepo,
Trigger: command.CommentTrigger,
}

Expand All @@ -87,11 +87,11 @@ func TestApproveCommandRunner_IsOwner(t *testing.T) {
},
},
}, nil)
When(vcsClient.GetTeamNamesForUser(fixtures.GithubRepo, fixtures.User)).ThenReturn(c.UserTeams, nil)
When(vcsClient.GetTeamNamesForUser(testdata.GithubRepo, testdata.User)).ThenReturn(c.UserTeams, nil)

approvePoliciesCommandRunner.Run(ctx, &events.CommentCommand{Name: command.ApprovePolicies})

vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, c.ExpComment, "approve_policies")
vcsClient.VerifyWasCalledOnce().CreateComment(testdata.GithubRepo, modelPull.Num, c.ExpComment, "approve_policies")
})
}
}
Loading

0 comments on commit da48fb5

Please sign in to comment.