From 01f17fa0ca7fa2221565dc058bdffb23549a3855 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Tue, 26 Feb 2019 19:12:53 +0100 Subject: [PATCH] backend/remote: exit with 1 when a run is canceled --- backend/remote/backend.go | 7 ++++++- backend/remote/backend_apply_test.go | 25 ++++++++++++++++++++++++- backend/remote/backend_mock.go | 4 ++-- backend/remote/backend_plan_test.go | 23 +++++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/backend/remote/backend.go b/backend/remote/backend.go index f4fd3eb5b..ef67b59d5 100644 --- a/backend/remote/backend.go +++ b/backend/remote/backend.go @@ -692,6 +692,11 @@ func (b *Remote) Operation(ctx context.Context, op *backend.Operation) (*backend return } + if r == nil && opErr == context.Canceled { + runningOp.Result = backend.OperationFailure + return + } + if r != nil { // Retrieve the run to get its current status. r, err := b.client.Runs.Read(cancelCtx, r.ID) @@ -710,7 +715,7 @@ func (b *Remote) Operation(ctx context.Context, op *backend.Operation) (*backend } } - if r.Status == tfe.RunErrored { + if r.Status == tfe.RunCanceled || r.Status == tfe.RunErrored { runningOp.Result = backend.OperationFailure } } diff --git a/backend/remote/backend_apply_test.go b/backend/remote/backend_apply_test.go index c27b747ef..a9e7e46d1 100644 --- a/backend/remote/backend_apply_test.go +++ b/backend/remote/backend_apply_test.go @@ -76,6 +76,29 @@ func TestRemote_applyBasic(t *testing.T) { } } +func TestRemote_applyCanceled(t *testing.T) { + b, bCleanup := testBackendDefault(t) + defer bCleanup() + + op, configCleanup := testOperationApply(t, "./test-fixtures/apply") + defer configCleanup() + + op.Workspace = backend.DefaultStateName + + run, err := b.Operation(context.Background(), op) + if err != nil { + t.Fatalf("error starting operation: %v", err) + } + + // Stop the run to simulate a Ctrl-C. + run.Stop() + + <-run.Done() + if run.Result == backend.OperationSuccess { + t.Fatal("expected apply operation to fail") + } +} + func TestRemote_applyWithoutPermissions(t *testing.T) { b, bCleanup := testBackendNoDefault(t) defer bCleanup() @@ -91,7 +114,7 @@ func TestRemote_applyWithoutPermissions(t *testing.T) { if err != nil { t.Fatalf("error creating named workspace: %v", err) } - w.Permissions.CanUpdate = false + w.Permissions.CanQueueApply = false op, configCleanup := testOperationApply(t, "./test-fixtures/apply") defer configCleanup() diff --git a/backend/remote/backend_mock.go b/backend/remote/backend_mock.go index dfd9079bd..4fe547477 100644 --- a/backend/remote/backend_mock.go +++ b/backend/remote/backend_mock.go @@ -919,8 +919,8 @@ func (m *mockWorkspaces) Create(ctx context.Context, organization string, option Name: *options.Name, Operations: !strings.HasSuffix(*options.Name, "no-operations"), Permissions: &tfe.WorkspacePermissions{ - CanQueueRun: true, - CanUpdate: true, + CanQueueApply: true, + CanQueueRun: true, }, } if options.AutoApply != nil { diff --git a/backend/remote/backend_plan_test.go b/backend/remote/backend_plan_test.go index 8cd465969..389444200 100644 --- a/backend/remote/backend_plan_test.go +++ b/backend/remote/backend_plan_test.go @@ -63,6 +63,29 @@ func TestRemote_planBasic(t *testing.T) { } } +func TestRemote_planCanceled(t *testing.T) { + b, bCleanup := testBackendDefault(t) + defer bCleanup() + + op, configCleanup := testOperationPlan(t, "./test-fixtures/plan") + defer configCleanup() + + op.Workspace = backend.DefaultStateName + + run, err := b.Operation(context.Background(), op) + if err != nil { + t.Fatalf("error starting operation: %v", err) + } + + // Stop the run to simulate a Ctrl-C. + run.Stop() + + <-run.Done() + if run.Result == backend.OperationSuccess { + t.Fatal("expected plan operation to fail") + } +} + func TestRemote_planLongLine(t *testing.T) { b, bCleanup := testBackendDefault(t) defer bCleanup()