diff --git a/db/db.go b/db/db.go index 26268f0a7..9eba92e0c 100644 --- a/db/db.go +++ b/db/db.go @@ -102,18 +102,11 @@ func get(ctx context.Context, db *sql.DB, query string, args ...interface{}) (st buf := []byte{} err := row.Scan(&buf) - if err == nil { - return string(buf), nil - } - - if err != sql.ErrNoRows { - err = errors.Wrap(err, "SELECT") + if err != nil { logger.Error(err) - } else { - err = nil + return "", errors.Wrap(err, "SELECT") } - - return "", err + return string(buf), nil } // buildGetCondition builds a where condition string in the format "column_name = 'field_value' AND" diff --git a/db/template.go b/db/template.go index dc656e0e5..47e0d1cc8 100644 --- a/db/template.go +++ b/db/template.go @@ -49,7 +49,7 @@ func (d TinkDB) CreateTemplate(ctx context.Context, name string, data string, id func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string) (string, string, string, error) { getCondition, err := buildGetCondition(fields) if err != nil { - return "", "", "", errors.Wrap(err, "failed to build get condition") + return "", "", "", errors.Wrap(err, "failed to get template") } query := ` diff --git a/db/workflow.go b/db/workflow.go index 64d68baae..7190e0fea 100644 --- a/db/workflow.go +++ b/db/workflow.go @@ -40,12 +40,12 @@ func (d TinkDB) CreateWorkflow(ctx context.Context, wf Workflow, data string, id err = insertActionList(ctx, d.instance, data, id, tx) if err != nil { - return errors.Wrap(err, "Failed to insert in workflow_state") + return errors.Wrap(err, "failed to create workflow") } err = insertInWorkflow(ctx, d.instance, wf, tx) if err != nil { - return errors.Wrap(err, "Failed to workflow") + return errors.Wrap(err, "failed to create workflow") } err = tx.Commit() @@ -109,9 +109,7 @@ func insertActionList(ctx context.Context, db *sql.DB, yamlData string, id uuid. workerID, err := getWorkerID(ctx, db, task.WorkerAddr) if err != nil { - return err - } else if workerID == "" { - return fmt.Errorf("hardware mentioned with reference %s not found", task.WorkerAddr) + return errors.WithMessage(err, "unable to insert into action list") } workerUID, err := uuid.Parse(workerID) if err != nil { @@ -689,7 +687,11 @@ func getWorkerIDbyMac(ctx context.Context, db *sql.DB, mac string) (string, erro data @> $1 ` - return get(ctx, db, query, arg) + id, err := get(ctx, db, query, arg) + if errors.Cause(err) == sql.ErrNoRows { + err = errors.WithMessage(errors.New(mac), "mac") + } + return id, err } func getWorkerIDbyIP(ctx context.Context, db *sql.DB, ip string) (string, error) { @@ -733,7 +735,11 @@ func getWorkerIDbyIP(ctx context.Context, db *sql.DB, ip string) (string, error) ) ` - return get(ctx, db, query, instance, hardwareOrManagement) + id, err := get(ctx, db, query, instance, hardwareOrManagement) + if errors.Cause(err) == sql.ErrNoRows { + err = errors.WithMessage(errors.New(ip), "ip") + } + return id, err } func getWorkerID(ctx context.Context, db *sql.DB, addr string) (string, error) { @@ -743,10 +749,12 @@ func getWorkerID(ctx context.Context, db *sql.DB, addr string) (string, error) { if ip == nil || ip.To4() == nil { return "", fmt.Errorf("invalid worker address: %s", addr) } - return getWorkerIDbyIP(ctx, db, addr) + id, err := getWorkerIDbyIP(ctx, db, addr) + return id, errors.WithMessage(err, "no worker found") } - return getWorkerIDbyMac(ctx, db, addr) + id, err := getWorkerIDbyMac(ctx, db, addr) + return id, errors.WithMessage(err, "no worker found") } func init() { diff --git a/grpc-server/template_test.go b/grpc-server/template_test.go index 09c19dc1a..b2f059594 100644 --- a/grpc-server/template_test.go +++ b/grpc-server/template_test.go @@ -11,6 +11,19 @@ import ( ) const ( + templateNotFoundID = "abstract-beef-beyond-meat-abominations" + templateNotFoundName = "my-awesome-mock-name" + templateNotFoundTemplate = `version: "0.1" +name: not_found_workflow +global_timeout: 600 +tasks: + - name: "not found" + worker: "{{.device_1}}" + actions: + - name: "not_found" + image: not-found + timeout: 60` + templateID1 = "7cd79119-1959-44eb-8b82-bc15bad4888e" templateName1 = "template_1" template1 = `version: "0.1" @@ -124,6 +137,9 @@ func TestGetTemplate(t *testing.T) { ) testCases := map[string]struct { args args + id string + name string + data string err bool }{ "SuccessfulTemplateGet_Name": { @@ -135,18 +151,18 @@ func TestGetTemplate(t *testing.T) { GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) { t.Log("in get template func") - if fields["id"] == templateID1 { - return templateID1, templateName1, template1, nil - } - if fields["name"] == templateName1 { + if fields["id"] == templateID1 || fields["name"] == templateName1 { return templateID1, templateName1, template1, nil } - return "", "", "", errors.New("failed to get template") + return templateNotFoundID, templateNotFoundName, templateNotFoundTemplate, errors.New("failed to get template") }, }, getRequest: &pb.GetRequest{GetBy: &pb.GetRequest_Name{Name: templateName1}}, }, - err: false, + id: templateID1, + name: templateName1, + data: template1, + err: false, }, "FailedTemplateGet_Name": { @@ -158,18 +174,18 @@ func TestGetTemplate(t *testing.T) { GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) { t.Log("in get template func") - if fields["id"] == templateID1 { - return templateID1, templateName1, template1, nil - } - if fields["name"] == templateName1 { + if fields["id"] == templateID1 || fields["name"] == templateName1 { return templateID1, templateName1, template1, nil } - return "", "", "", errors.New("failed to get template") + return templateNotFoundID, templateNotFoundName, templateNotFoundTemplate, errors.New("failed to get template") }, }, getRequest: &pb.GetRequest{GetBy: &pb.GetRequest_Name{Name: templateName2}}, }, - err: true, + id: templateNotFoundID, + name: templateNotFoundName, + data: templateNotFoundTemplate, + err: true, }, "SuccessfulTemplateGet_ID": { @@ -181,18 +197,18 @@ func TestGetTemplate(t *testing.T) { GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) { t.Log("in get template func") - if fields["id"] == templateID1 { - return templateID1, templateName1, template1, nil - } - if fields["name"] == templateName1 { + if fields["id"] == templateID1 || fields["name"] == templateName1 { return templateID1, templateName1, template1, nil } - return "", "", "", errors.New("failed to get template") + return templateNotFoundID, templateNotFoundName, templateNotFoundTemplate, errors.New("failed to get template") }, }, getRequest: &pb.GetRequest{GetBy: &pb.GetRequest_Id{Id: templateID1}}, }, - err: false, + id: templateID1, + name: templateName1, + data: template1, + err: false, }, "FailedTemplateGet_ID": { @@ -204,18 +220,18 @@ func TestGetTemplate(t *testing.T) { GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) { t.Log("in get template func") - if fields["id"] == templateID1 { - return templateID1, templateName1, template1, nil - } - if fields["name"] == templateName1 { + if fields["id"] == templateID1 || fields["name"] == templateName1 { return templateID1, templateName1, template1, nil } - return "", "", "", errors.New("failed to get template") + return templateNotFoundID, templateNotFoundName, templateNotFoundTemplate, errors.New("failed to get template") }, }, getRequest: &pb.GetRequest{GetBy: &pb.GetRequest_Id{Id: templateID2}}, }, - err: true, + id: templateNotFoundID, + name: templateNotFoundName, + data: templateNotFoundTemplate, + err: true, }, "FailedTemplateGet_EmptyRequest": { @@ -227,18 +243,18 @@ func TestGetTemplate(t *testing.T) { GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) { t.Log("in get template func") - if fields["id"] == templateID1 { + if fields["id"] == templateID1 || fields["name"] == templateName1 { return templateID1, templateName1, template1, nil } - if fields["name"] == templateName1 { - return templateID1, templateName1, template1, nil - } - return "", "", "", errors.New("failed to get template") + return templateNotFoundID, templateNotFoundName, templateNotFoundTemplate, errors.New("failed to get template") }, }, getRequest: &pb.GetRequest{}, }, - err: true, + id: templateNotFoundID, + name: templateNotFoundName, + data: templateNotFoundTemplate, + err: true, }, "FailedTemplateGet_NilRequest": { @@ -250,17 +266,17 @@ func TestGetTemplate(t *testing.T) { GetTemplateFunc: func(ctx context.Context, fields map[string]string) (string, string, string, error) { t.Log("in get template func") - if fields["id"] == templateID1 { - return templateID1, templateName1, template1, nil - } - if fields["name"] == templateName1 { + if fields["id"] == templateID1 || fields["name"] == templateName1 { return templateID1, templateName1, template1, nil } - return "", "", "", errors.New("failed to get template") + return templateNotFoundID, templateNotFoundName, templateNotFoundTemplate, errors.New("failed to get template") }, }, }, - err: true, + id: templateNotFoundID, + name: templateNotFoundName, + data: templateNotFoundTemplate, + err: true, }, } @@ -278,6 +294,9 @@ func TestGetTemplate(t *testing.T) { assert.Nil(t, err) assert.NotEmpty(t, res) } + assert.Equal(t, res.Id, tc.id) + assert.Equal(t, res.Name, tc.name) + assert.Equal(t, res.Data, tc.data) }) } } diff --git a/grpc-server/workflow.go b/grpc-server/workflow.go index d0a466d8c..d2523be6f 100644 --- a/grpc-server/workflow.go +++ b/grpc-server/workflow.go @@ -25,8 +25,8 @@ var state = map[int32]workflow.State{ } const ( - errFailedToGetTemplate = "failed to get template with ID: %s" - errTemplateParsing = "failed to parse template with ID: %s" + errFailedToGetTemplate = "failed to get template with ID %s" + errTemplateParsing = "failed to parse template with ID %s" ) // CreateWorkflow implements workflow.CreateWorkflow