Skip to content

Commit

Permalink
chore: added minimal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kvanzuijlen committed Dec 12, 2023
1 parent 26cc0d7 commit 1b87e15
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
64 changes: 64 additions & 0 deletions server/core/runtime/apply_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runtime_test

import (
"fmt"
vcsmocks "github.com/runatlantis/atlantis/server/events/vcs/mocks"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -73,6 +74,69 @@ func TestRun_Success(t *testing.T) {
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}
func TestRun_Success_WithApplyErrorLabel(t *testing.T) {
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, "workspace.tfplan")
err := os.WriteFile(planPath, nil, 0600)
logger := logging.NewNoopLogger(t)
ctx := command.ProjectContext{
Log: logger,
Workspace: "workspace",
RepoRelDir: ".",
EscapedCommentArgs: []string{"comment", "args"},
}
Ok(t, err)

RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
applyErrorLabel := "some-label"
vcsClient := vcsmocks.NewMockClient()
o := runtime.ApplyStepRunner{
TerraformExecutor: terraform,
ApplyErrorLabel: applyErrorLabel,
VcsClient: vcsClient,
}

When(terraform.RunCommandWithVersion(Any[command.ProjectContext](), Any[string](), Any[[]string](), Any[map[string]string](), Any[*version.Version](), Any[string]())).
ThenReturn("output", nil)
output, err := o.Run(ctx, []string{"extra", "args"}, tmpDir, map[string]string(nil))
Ok(t, err)
Equals(t, "output", output)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(ctx, tmpDir, []string{"apply", "-input=false", "extra", "args", "comment", "args", fmt.Sprintf("%q", planPath)}, map[string]string(nil), nil, "workspace")
vcsClient.VerifyWasNotCalled().AddPullLabel(ctx.Pull.BaseRepo, ctx.Pull, applyErrorLabel)

Check failure on line 106 in server/core/runtime/apply_step_runner_test.go

View workflow job for this annotation

GitHub Actions / Linting

[golangci-lint] reported by reviewdog 🐶 vcsClient.VerifyWasNotCalled undefined (type *"github.com/runatlantis/atlantis/server/events/vcs/mocks".MockClient has no field or method VerifyWasNotCalled) (typecheck) Raw Output: server/core/runtime/apply_step_runner_test.go:106:12: vcsClient.VerifyWasNotCalled undefined (type *"github.com/runatlantis/atlantis/server/events/vcs/mocks".MockClient has no field or method VerifyWasNotCalled) (typecheck) package runtime_test
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}

func TestRun_Fail_WithApplyErrorLabel(t *testing.T) {
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, "workspace.tfplan")
err := os.WriteFile(planPath, nil, 0600)
logger := logging.NewNoopLogger(t)
ctx := command.ProjectContext{
Log: logger,
Workspace: "workspace",
RepoRelDir: ".",
EscapedCommentArgs: []string{"comment", "args"},
}
Ok(t, err)

RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
applyErrorLabel := "some-label"
vcsClient := vcsmocks.NewMockClient()
o := runtime.ApplyStepRunner{
TerraformExecutor: terraform,
ApplyErrorLabel: applyErrorLabel,
VcsClient: vcsClient,
}
errString := "some-error"
When(terraform.RunCommandWithVersion(Any[command.ProjectContext](), Any[string](), Any[[]string](), Any[map[string]string](), Any[*version.Version](), Any[string]())).
ThenReturn("", errors.New(errString))
_, err = o.Run(ctx, nil, tmpDir, map[string]string(nil))
ErrEquals(t, errString, err)
vcsClient.VerifyWasCalledOnce().AddPullLabel(ctx.Pull.BaseRepo, ctx.Pull, applyErrorLabel)
}

func TestRun_AppliesCorrectProjectPlan(t *testing.T) {
// When running for a project, the planfile has a different name.
Expand Down
50 changes: 50 additions & 0 deletions server/events/vcs/mocks/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b87e15

Please sign in to comment.