From d1ca1bf1f365f6d3438944eeb3e15adfa93d01d2 Mon Sep 17 00:00:00 2001 From: parauliya Date: Wed, 14 Apr 2021 17:43:01 +0530 Subject: [PATCH 1/3] Fixed #477 : Corrected state for get and get by id command. Signed-off-by: parauliya --- grpc-server/workflow.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/grpc-server/workflow.go b/grpc-server/workflow.go index cab36c733..86850abfd 100644 --- a/grpc-server/workflow.go +++ b/grpc-server/workflow.go @@ -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 @@ -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) @@ -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) }) @@ -279,3 +273,17 @@ func (s *server) ShowWorkflowEvents(req *workflow.GetRequest, stream workflow.Wo metrics.CacheHits.With(labels).Inc() return nil } + +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 { + wfacts, _ := db.GetWorkflowActions(ctx, id) + if isLastAction(wfctx, wfacts) { + return workflow.State_STATE_SUCCESS + } else { + return workflow.State_STATE_RUNNING + } + } +} From 004d13171380b4724f998be08716fd193a8660fe Mon Sep 17 00:00:00 2001 From: parauliya Date: Thu, 15 Apr 2021 17:06:15 +0530 Subject: [PATCH 2/3] Fixed review comments and unit tests Signed-off-by: parauliya --- grpc-server/workflow.go | 15 ++++++++++----- grpc-server/workflow_test.go | 8 ++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/grpc-server/workflow.go b/grpc-server/workflow.go index 86850abfd..1dcb07d66 100644 --- a/grpc-server/workflow.go +++ b/grpc-server/workflow.go @@ -274,13 +274,18 @@ func (s *server) ShowWorkflowEvents(req *workflow.GetRequest, stream workflow.Wo 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 Tailed 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 + wfCtx, _ := db.GetWorkflowContexts(ctx, id) + if wfCtx.CurrentActionState != workflow.State_STATE_SUCCESS { + return wfCtx.CurrentActionState } else { - wfacts, _ := db.GetWorkflowActions(ctx, id) - if isLastAction(wfctx, wfacts) { + //wfacts, _ := db.GetWorkflowActions(ctx, id) + if wfCtx.GetCurrentActionIndex() == wfCtx.GetTotalNumberOfActions()-1 { return workflow.State_STATE_SUCCESS } else { return workflow.State_STATE_RUNNING diff --git a/grpc-server/workflow_test.go b/grpc-server/workflow_test.go index 8efcdca8c..334c653ae 100644 --- a/grpc-server/workflow_test.go +++ b/grpc-server/workflow_test.go @@ -136,6 +136,14 @@ 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 }, From 9b817cea89f352ebf5053befd399503120f02b75 Mon Sep 17 00:00:00 2001 From: parauliya Date: Mon, 19 Apr 2021 12:22:26 +0530 Subject: [PATCH 3/3] Added a unit test for workflow state in get workflow command Signed-off-by: parauliya --- grpc-server/workflow.go | 3 +-- grpc-server/workflow_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/grpc-server/workflow.go b/grpc-server/workflow.go index 1dcb07d66..1167e25e1 100644 --- a/grpc-server/workflow.go +++ b/grpc-server/workflow.go @@ -275,7 +275,7 @@ func (s *server) ShowWorkflowEvents(req *workflow.GetRequest, stream workflow.Wo } // This function will provide the workflow state on the basis of the state of the actions -// For e.g. : If an action has Tailed or Timeout then the workflow state will also be +// 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. @@ -284,7 +284,6 @@ func getWorkflowState(db db.Database, ctx context.Context, id string) workflow.S if wfCtx.CurrentActionState != workflow.State_STATE_SUCCESS { return wfCtx.CurrentActionState } else { - //wfacts, _ := db.GetWorkflowActions(ctx, id) if wfCtx.GetCurrentActionIndex() == wfCtx.GetTotalNumberOfActions()-1 { return workflow.State_STATE_SUCCESS } else { diff --git a/grpc-server/workflow_test.go b/grpc-server/workflow_test.go index 334c653ae..32e666cdf 100644 --- a/grpc-server/workflow_test.go +++ b/grpc-server/workflow_test.go @@ -118,6 +118,7 @@ func TestGetWorkflow(t *testing.T) { args struct { db mock.DB wfTemplate, wfHardware string + state workflow.State } want struct { expectedError bool @@ -148,6 +149,7 @@ func TestGetWorkflow(t *testing.T) { return "", "", templateData, nil }, }, + state: workflow.State_STATE_SUCCESS, wfTemplate: templateID, wfHardware: hw, }, @@ -167,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) @@ -183,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)