Skip to content

Commit

Permalink
Merge pull request #482 from parauliya/Fix_477
Browse files Browse the repository at this point in the history
Fixed #477 : Corrected state for get and get by id command.
  • Loading branch information
parauliya authored Apr 20, 2021
2 parents d13a368 + 9b817ce commit 5d58c4b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
30 changes: 21 additions & 9 deletions grpc-server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ import (
wkf "github.com/tinkerbell/tink/workflow"
)

var state = map[int32]workflow.State{
0: workflow.State_STATE_PENDING,
1: workflow.State_STATE_RUNNING,
2: workflow.State_STATE_FAILED,
3: workflow.State_STATE_TIMEOUT,
4: workflow.State_STATE_SUCCESS,
}

const errFailedToGetTemplate = "failed to get template with ID %s"

// CreateWorkflow implements workflow.CreateWorkflow
Expand Down Expand Up @@ -115,11 +107,12 @@ func (s *server) GetWorkflow(ctx context.Context, in *workflow.GetRequest) (*wor
if err != nil {
return &workflow.Workflow{}, err
}

wf := &workflow.Workflow{
Id: w.ID,
Template: w.Template,
Hardware: w.Hardware,
State: state[w.State],
State: getWorkflowState(s.db, ctx, in.Id),
Data: data,
}
l := s.logger.With("workflowID", w.ID)
Expand Down Expand Up @@ -181,6 +174,7 @@ func (s *server) ListWorkflows(_ *workflow.Empty, stream workflow.WorkflowServic
Hardware: w.Hardware,
CreatedAt: w.CreatedAt,
UpdatedAt: w.UpdatedAt,
State: getWorkflowState(s.db, stream.Context(), w.ID),
}
return stream.Send(wf)
})
Expand Down Expand Up @@ -279,3 +273,21 @@ func (s *server) ShowWorkflowEvents(req *workflow.GetRequest, stream workflow.Wo
metrics.CacheHits.With(labels).Inc()
return nil
}

// This function will provide the workflow state on the basis of the state of the actions
// For e.g. : If an action has Failed or Timeout then the workflow state will also be
// considered as Failed/Timeout. And If an action is successful then the workflow state
// will be considered as Running until the last action of the workflow is executed successfully.

func getWorkflowState(db db.Database, ctx context.Context, id string) workflow.State {
wfCtx, _ := db.GetWorkflowContexts(ctx, id)
if wfCtx.CurrentActionState != workflow.State_STATE_SUCCESS {
return wfCtx.CurrentActionState
} else {
if wfCtx.GetCurrentActionIndex() == wfCtx.GetTotalNumberOfActions()-1 {
return workflow.State_STATE_SUCCESS
} else {
return workflow.State_STATE_RUNNING
}
}
}
40 changes: 40 additions & 0 deletions grpc-server/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestGetWorkflow(t *testing.T) {
args struct {
db mock.DB
wfTemplate, wfHardware string
state workflow.State
}
want struct {
expectedError bool
Expand All @@ -136,10 +137,19 @@ func TestGetWorkflow(t *testing.T) {
Template: templateID,
Hardware: hw}, nil
},
GetWorkflowContextsFunc: func(ctx context.Context, wfID string) (*workflow.WorkflowContext, error) {
return &workflow.WorkflowContext{
WorkflowId: wfID,
CurrentActionState: workflow.State_STATE_SUCCESS,
CurrentActionIndex: 0,
TotalNumberOfActions: 1,
}, nil
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string, deleted bool) (string, string, string, error) {
return "", "", templateData, nil
},
},
state: workflow.State_STATE_SUCCESS,
wfTemplate: templateID,
wfHardware: hw,
},
Expand All @@ -159,6 +169,35 @@ func TestGetWorkflow(t *testing.T) {
expectedError: true,
},
},
"GetWorkflowState": {
args: args{
db: mock.DB{
GetWorkflowFunc: func(ctx context.Context, workflowID string) (db.Workflow, error) {
return db.Workflow{
ID: workflowID,
Template: templateID,
Hardware: hw}, nil
},
GetWorkflowContextsFunc: func(ctx context.Context, wfID string) (*workflow.WorkflowContext, error) {
return &workflow.WorkflowContext{
WorkflowId: wfID,
CurrentActionState: workflow.State_STATE_SUCCESS,
CurrentActionIndex: 0,
TotalNumberOfActions: 2,
}, nil
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string, deleted bool) (string, string, string, error) {
return "", "", templateData, nil
},
},
state: workflow.State_STATE_RUNNING,
wfTemplate: templateID,
wfHardware: hw,
},
want: want{
expectedError: false,
},
},
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand All @@ -175,6 +214,7 @@ func TestGetWorkflow(t *testing.T) {
assert.True(t, tc.want.expectedError)
return
}
assert.Equal(t, tc.args.state, res.State)
assert.NoError(t, err)
assert.NotEmpty(t, res)
assert.False(t, tc.want.expectedError)
Expand Down

0 comments on commit 5d58c4b

Please sign in to comment.