Fixup the tests

This commit is contained in:
Sander van Harmelen 2019-04-25 09:53:12 +02:00
parent 7cf744241a
commit bb12206bca
7 changed files with 25 additions and 52 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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) {

View File

@ -3,4 +3,4 @@
+---------+------+-----+-------------+----------------------+
+---------+------+-----+-------------+----------------------+
| TOTAL | $0.000 USD / 720 HRS |
+---------+------+-----+-------------+----------------------+
+---------+------+-----+-------------+----------------------+