Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #381: 'tink workflow state' command returns an error if workflow does not exist #447

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions db/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,13 @@ func (d TinkDB) GetWorkflow(ctx context.Context, id string) (Workflow, error) {
if err == nil {
return Workflow{ID: id, Template: tmp, Hardware: tar}, nil
}

if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
d.logger.Error(err)
return Workflow{}, err
}

return Workflow{}, nil
return Workflow{}, errors.New("Workflow with id " + id + " does not exist")
}

// DeleteWorkflow deletes a workflow
Expand Down Expand Up @@ -549,8 +549,9 @@ func (d TinkDB) GetWorkflowContexts(ctx context.Context, wfID string) (*pb.Workf
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT from worflow_state")
d.logger.Error(err)
return &pb.WorkflowContext{}, err
}
return &pb.WorkflowContext{}, nil
return &pb.WorkflowContext{}, errors.New("Workflow with id " + wfID + " does not exist")
}

// GetWorkflowActions : gives you the action list of workflow
Expand Down
5 changes: 3 additions & 2 deletions grpc-server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (s *server) GetWorkflow(ctx context.Context, in *workflow.GetRequest) (*wor
l = l.With("detail", pqErr.Detail, "where", pqErr.Where)
}
l.Error(err)
return &workflow.Workflow{}, err
}

fields := map[string]string{
"id": w.Template,
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func (s *server) GetWorkflowContext(ctx context.Context, in *workflow.GetRequest
metrics.CacheInFlight.With(labels).Inc()
defer metrics.CacheInFlight.With(labels).Dec()

const msg = "getting a workflow"
const msg = "getting a workflow context"
labels["op"] = "get"

metrics.CacheTotals.With(labels).Inc()
Expand All @@ -216,6 +216,7 @@ func (s *server) GetWorkflowContext(ctx context.Context, in *workflow.GetRequest
l = l.With("detail", pqErr.Detail, "where", pqErr.Where)
}
l.Error(err)
return &workflow.WorkflowContext{}, err
}
wf := &workflow.WorkflowContext{
WorkflowId: w.WorkflowId,
Expand Down
61 changes: 61 additions & 0 deletions grpc-server/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ func TestGetWorkflow(t *testing.T) {
expectedError: false,
},
},
"WorkflowDoesNotExist": {
args: args{
db: mock.DB{
GetWorkflowFunc: func(ctx context.Context, workflowID string) (db.Workflow, error) {
return db.Workflow{}, errors.New("Workflow with id " + workflowID + " does not exist")
},
},
},
want: want{
expectedError: true,
},
},
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand All @@ -169,3 +181,52 @@ func TestGetWorkflow(t *testing.T) {
})
}
}

func TestGetWorkflowContext(t *testing.T) {
type (
args struct {
db mock.DB
}
want struct {
expectedError bool
}
)
testCases := map[string]struct {
args args
want want
}{
"WorkflowDoesNotExist": {
args: args{
db: mock.DB{
GetWorkflowContextsFunc: func(ctx context.Context, workflowID string) (*workflow.WorkflowContext, error) {
w := workflow.WorkflowContext{}
return &w, errors.New("Workflow with id " + workflowID + " does not exist")
},
},
},
want: want{
expectedError: true,
},
},
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
s := testServer(t, tc.args.db)
res, err := s.GetWorkflowContext(ctx, &workflow.GetRequest{
Id: workflowID,
})
if err != nil {
assert.Error(t, err)
assert.Empty(t, res)
assert.True(t, tc.want.expectedError)
return
}
assert.NoError(t, err)
assert.NotEmpty(t, res)
assert.False(t, tc.want.expectedError)
})
}
}