backend/remote: exit with 1 when a run is canceled

This commit is contained in:
Sander van Harmelen 2019-02-26 19:12:53 +01:00
parent 2b9e2b4c2b
commit 01f17fa0ca
4 changed files with 55 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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