diff --git a/backend/remote/backend_common.go b/backend/remote/backend_common.go index 836ce85b31..34c8514672 100644 --- a/backend/remote/backend_common.go +++ b/backend/remote/backend_common.go @@ -228,7 +228,7 @@ func (b *Remote) parseVariableValues(op *backend.Operation) (terraform.InputValu } func (b *Remote) costEstimation(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run) error { - if r.CostEstimation != nil { + if r.CostEstimation == nil { return nil } diff --git a/backend/remote/backend_mock.go b/backend/remote/backend_mock.go index 59a1a13932..31b63131bd 100644 --- a/backend/remote/backend_mock.go +++ b/backend/remote/backend_mock.go @@ -228,8 +228,8 @@ func newMockCostEstimations(client *mockClient) *mockCostEstimations { } } -// create is a helper function to create a mock plan that uses the configured -// working directory to find the logfile. +// create is a helper function to create a mock cost estimation that uses the +// configured working directory to find the logfile. func (m *mockCostEstimations) create(cvID, workspaceID string) (*tfe.CostEstimation, error) { id := generateID("ce-") @@ -264,28 +264,6 @@ func (m *mockCostEstimations) Read(ctx context.Context, costEstimationID string) if !ok { return nil, tfe.ErrResourceNotFound } - - logfile, ok := m.logs[ce.ID] - if !ok { - return nil, tfe.ErrResourceNotFound - } - - if _, err := os.Stat(logfile); os.IsNotExist(err) { - return nil, fmt.Errorf("logfile does not exist") - } - - logs, err := ioutil.ReadFile(logfile) - if err != nil { - return nil, err - } - - if bytes.Contains(logs, []byte("SKU")) { - ce.Status = tfe.CostEstimationFinished - } else { - // As this is an unexpected state, we say the estimation errored. - ce.Status = tfe.CostEstimationErrored - } - return ce, nil } @@ -309,12 +287,7 @@ func (m *mockCostEstimations) Logs(ctx context.Context, costEstimationID string) return nil, err } - if bytes.Contains(logs, []byte("SKU")) { - ce.Status = tfe.CostEstimationFinished - } else { - // As this is an unexpected state, we say the estimation errored. - ce.Status = tfe.CostEstimationErrored - } + ce.Status = tfe.CostEstimationFinished return bytes.NewBuffer(logs), nil } @@ -1151,10 +1124,6 @@ func (m *mockWorkspaces) UnassignSSHKey(ctx context.Context, workspaceID string) panic("not implemented") } -func (m *mockWorkspaces) RemoveVCSConnection(ctx context.Context, organizationID string, workspaceID string) (*tfe.Workspace, error) { - panic("not implemented") -} - const alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" func generateID(s string) string { diff --git a/backend/remote/backend_plan.go b/backend/remote/backend_plan.go index 72508c6392..db5495b166 100644 --- a/backend/remote/backend_plan.go +++ b/backend/remote/backend_plan.go @@ -290,7 +290,7 @@ func (b *Remote) plan(stopCtx, cancelCtx context.Context, op *backend.Operation, return r, nil } - // Show Cost Estimation + // Show any cost estimation output. if r.CostEstimation != nil { err = b.costEstimation(stopCtx, cancelCtx, op, r) if err != nil { diff --git a/backend/remote/backend_plan_test.go b/backend/remote/backend_plan_test.go index d89ef86855..acf3c4abb6 100644 --- a/backend/remote/backend_plan_test.go +++ b/backend/remote/backend_plan_test.go @@ -655,10 +655,11 @@ func TestRemote_planWithWorkingDirectory(t *testing.T) { } } -func TestRemote_costEstimationFinish(t *testing.T) { - b := testBackendDefault(t) +func TestRemote_costEstimation(t *testing.T) { + b, bCleanup := testBackendDefault(t) + defer bCleanup() - op, configCleanup := testOperationPlan(t, "./test-fixtures/cost-estimation") + op, configCleanup := testOperationPlan(t, "./test-fixtures/plan-cost-estimation") defer configCleanup() op.Workspace = backend.DefaultStateName @@ -672,17 +673,20 @@ func TestRemote_costEstimationFinish(t *testing.T) { if run.Result != backend.OperationSuccess { t.Fatalf("operation failed: %s", b.CLI.(*cli.MockUi).ErrorWriter.String()) } + if run.PlanEmpty { + t.Fatalf("expected a non-empty plan") + } output := b.CLI.(*cli.MockUi).OutputWriter.String() if !strings.Contains(output, "Running plan in the remote backend") { t.Fatalf("expected remote backend header in output: %s", output) } - if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") { - t.Fatalf("expected plan summary in output: %s", output) - } if !strings.Contains(output, "SKU") { t.Fatalf("expected cost estimation result in output: %s", output) } + if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") { + t.Fatalf("expected plan summary in output: %s", output) + } } func TestRemote_planPolicyPass(t *testing.T) { @@ -711,12 +715,12 @@ func TestRemote_planPolicyPass(t *testing.T) { if !strings.Contains(output, "Running plan in the remote backend") { t.Fatalf("expected remote backend header in output: %s", output) } - if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") { - t.Fatalf("expected plan summery in output: %s", output) - } if !strings.Contains(output, "Sentinel Result: true") { t.Fatalf("expected policy check result in output: %s", output) } + if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") { + t.Fatalf("expected plan summery in output: %s", output) + } } func TestRemote_planPolicyHardFail(t *testing.T) { @@ -750,12 +754,12 @@ func TestRemote_planPolicyHardFail(t *testing.T) { if !strings.Contains(output, "Running plan in the remote backend") { t.Fatalf("expected remote backend header in output: %s", output) } - if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") { - t.Fatalf("expected plan summery in output: %s", output) - } if !strings.Contains(output, "Sentinel Result: false") { t.Fatalf("expected policy check result in output: %s", output) } + if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") { + t.Fatalf("expected plan summery in output: %s", output) + } } func TestRemote_planPolicySoftFail(t *testing.T) { @@ -789,12 +793,12 @@ func TestRemote_planPolicySoftFail(t *testing.T) { if !strings.Contains(output, "Running plan in the remote backend") { t.Fatalf("expected remote backend header in output: %s", output) } - if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") { - t.Fatalf("expected plan summery in output: %s", output) - } if !strings.Contains(output, "Sentinel Result: false") { t.Fatalf("expected policy check result in output: %s", output) } + if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") { + t.Fatalf("expected plan summery in output: %s", output) + } } func TestRemote_planWithRemoteError(t *testing.T) { diff --git a/backend/remote/test-fixtures/cost-estimation/ce.log b/backend/remote/test-fixtures/plan-cost-estimation/ce.log similarity index 83% rename from backend/remote/test-fixtures/cost-estimation/ce.log rename to backend/remote/test-fixtures/plan-cost-estimation/ce.log index d73c8915b7..e51fef1edc 100644 --- a/backend/remote/test-fixtures/cost-estimation/ce.log +++ b/backend/remote/test-fixtures/plan-cost-estimation/ce.log @@ -3,4 +3,4 @@ +---------+------+-----+-------------+----------------------+ +---------+------+-----+-------------+----------------------+ | TOTAL | $0.000 USD / 720 HRS | -+---------+------+-----+-------------+----------------------+ \ No newline at end of file ++---------+------+-----+-------------+----------------------+ diff --git a/backend/remote/test-fixtures/cost-estimation/main.tf b/backend/remote/test-fixtures/plan-cost-estimation/main.tf similarity index 100% rename from backend/remote/test-fixtures/cost-estimation/main.tf rename to backend/remote/test-fixtures/plan-cost-estimation/main.tf diff --git a/backend/remote/test-fixtures/cost-estimation/plan.log b/backend/remote/test-fixtures/plan-cost-estimation/plan.log similarity index 100% rename from backend/remote/test-fixtures/cost-estimation/plan.log rename to backend/remote/test-fixtures/plan-cost-estimation/plan.log