Skip to content

Commit

Permalink
Add logging mocks for testing Terraform client
Browse files Browse the repository at this point in the history
Signed-off-by: Leandro López (inkel) <[email protected]>
  • Loading branch information
inkel committed Jun 9, 2023
1 parent d5eb004 commit 2b09c44
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions server/core/terraform/terraform_client_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"testing"

version "github.com/hashicorp/go-version"
. "github.com/petergtz/pegomock"
runtimemodels "github.com/runatlantis/atlantis/server/core/runtime/models"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
jobmocks "github.com/runatlantis/atlantis/server/jobs/mocks"
"github.com/runatlantis/atlantis/server/logging"
logmocks "github.com/runatlantis/atlantis/server/logging/mocks"
. "github.com/runatlantis/atlantis/testing"
)

Expand Down Expand Up @@ -168,10 +170,12 @@ func TestDefaultClient_RunCommandWithVersion_Error(t *testing.T) {
}

func TestDefaultClient_RunCommandAsync_Success(t *testing.T) {
RegisterMockTestingT(t)
v, err := version.NewVersion("0.11.11")
Ok(t, err)
tmp := t.TempDir()
logger := logging.NewNoopLogger(t)
logger := logmocks.NewMockSimpleLogging()
When(logger.With(AnyString(), AnyInterface())).ThenReturn(logger)
projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()

ctx := command.ProjectContext{
Expand Down Expand Up @@ -211,13 +215,17 @@ func TestDefaultClient_RunCommandAsync_Success(t *testing.T) {
Ok(t, err)
exp := fmt.Sprintf("TF_IN_AUTOMATION=true TF_PLUGIN_CACHE_DIR=%s WORKSPACE=workspace ATLANTIS_TERRAFORM_VERSION=0.11.11 DIR=%s", tmp, tmp)
Equals(t, exp, out)

logger.VerifyWasCalledOnce().With(EqString("duration"), AnyInterface())
}

func TestDefaultClient_RunCommandAsync_BigOutput(t *testing.T) {
RegisterMockTestingT(t)
v, err := version.NewVersion("0.11.11")
Ok(t, err)
tmp := t.TempDir()
logger := logging.NewNoopLogger(t)
logger := logmocks.NewMockSimpleLogging()
When(logger.With(AnyString(), AnyInterface())).ThenReturn(logger)
projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()

ctx := command.ProjectContext{
Expand Down Expand Up @@ -258,13 +266,17 @@ func TestDefaultClient_RunCommandAsync_BigOutput(t *testing.T) {
out, err := waitCh(outCh)
Ok(t, err)
Equals(t, strings.TrimRight(exp, "\n"), out)

logger.VerifyWasCalledOnce().With(EqString("duration"), AnyInterface())
}

func TestDefaultClient_RunCommandAsync_StderrOutput(t *testing.T) {
RegisterMockTestingT(t)
v, err := version.NewVersion("0.11.11")
Ok(t, err)
tmp := t.TempDir()
logger := logging.NewNoopLogger(t)
logger := logmocks.NewMockSimpleLogging()
When(logger.With(AnyString(), AnyInterface())).ThenReturn(logger)
projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()

ctx := command.ProjectContext{
Expand Down Expand Up @@ -294,13 +306,17 @@ func TestDefaultClient_RunCommandAsync_StderrOutput(t *testing.T) {
out, err := waitCh(outCh)
Ok(t, err)
Equals(t, "stderr", out)

logger.VerifyWasCalledOnce().With(EqString("duration"), AnyInterface())
}

func TestDefaultClient_RunCommandAsync_ExitOne(t *testing.T) {
RegisterMockTestingT(t)
v, err := version.NewVersion("0.11.11")
Ok(t, err)
tmp := t.TempDir()
logger := logging.NewNoopLogger(t)
logger := logmocks.NewMockSimpleLogging()
When(logger.With(AnyString(), AnyInterface())).ThenReturn(logger)
projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()

ctx := command.ProjectContext{
Expand Down Expand Up @@ -331,13 +347,17 @@ func TestDefaultClient_RunCommandAsync_ExitOne(t *testing.T) {
ErrEquals(t, fmt.Sprintf(`running "echo dying && exit 1" in %q: exit status 1`, tmp), err)
// Test that we still get our output.
Equals(t, "dying", out)

logger.VerifyWasCalledOnce().With(EqString("duration"), AnyInterface())
}

func TestDefaultClient_RunCommandAsync_Input(t *testing.T) {
RegisterMockTestingT(t)
v, err := version.NewVersion("0.11.11")
Ok(t, err)
tmp := t.TempDir()
logger := logging.NewNoopLogger(t)
logger := logmocks.NewMockSimpleLogging()
When(logger.With(AnyString(), AnyInterface())).ThenReturn(logger)
projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()

ctx := command.ProjectContext{
Expand Down Expand Up @@ -369,6 +389,8 @@ func TestDefaultClient_RunCommandAsync_Input(t *testing.T) {
out, err := waitCh(outCh)
Ok(t, err)
Equals(t, "echo me", out)

logger.VerifyWasCalledOnce().With(EqString("duration"), AnyInterface())
}

func waitCh(ch <-chan runtimemodels.Line) (string, error) {
Expand Down

0 comments on commit 2b09c44

Please sign in to comment.