From c0f111881e32a91f56e4d4b709ee3a0f6ec393c5 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Mon, 11 Jan 2021 12:45:25 +0530 Subject: [PATCH] TestGetWorkflow Signed-off-by: Gaurav Gahlot --- db/workflow_test.go | 125 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/db/workflow_test.go b/db/workflow_test.go index 6d2d6b9e9..477151f06 100644 --- a/db/workflow_test.go +++ b/db/workflow_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/google/uuid" + "github.com/stretchr/testify/assert" "github.com/tinkerbell/tink/db" "github.com/tinkerbell/tink/protos/hardware" pb "github.com/tinkerbell/tink/protos/workflow" @@ -225,6 +226,130 @@ func TestDeleteWorkflow(t *testing.T) { } } +func TestGetWorkflow(t *testing.T) { + ctx := context.Background() + tests := []struct { + // Name identifies the single test in a table test scenario + Name string + // GetAsync if set to true gets all the workflows concurrently + GetAsync bool + // Input is a struct that will be used to create a workflow and pre-populate the database + Input *input + // Expectation is the function used to apply the assertions. + // You can use it to validate if you get workflow you expected + Expectation func(t *testing.T, tinkDB *db.TinkDB, id string) + }{ + { + Name: "get-workflow", + Input: &input{ + devices: "{\"device_1\":\"08:00:27:00:00:01\"}", + hardware: readHardwareData("./testdata/hardware.json"), + template: func() *workflow.Workflow { + tmp := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") + tmp.ID = uuid.New().String() + tmp.Name = fmt.Sprintf("id_%d", rand.Int()) + return tmp + }(), + }, + Expectation: func(t *testing.T, tinkDB *db.TinkDB, id string) { + _, err := tinkDB.GetWorkflow(ctx, id) + if err != nil { + t.Error(err) + } + }, + }, + { + Name: "stress-get-workflow", + GetAsync: true, + Input: &input{ + workflowCount: 20, + devices: "{\"device_1\":\"08:00:27:00:00:01\"}", + hardware: readHardwareData("./testdata/hardware.json"), + template: func() *workflow.Workflow { + tmp := workflow.MustParseFromFile("./testdata/template_happy_path_1.yaml") + tmp.ID = uuid.New().String() + tmp.Name = fmt.Sprintf("id_%d", rand.Int()) + return tmp + }(), + }, + Expectation: func(t *testing.T, tinkDB *db.TinkDB, id string) { + _, err := tinkDB.GetWorkflow(ctx, id) + if err != nil { + t.Error(err) + } + }, + }, + } + + for _, s := range tests { + t.Run(s.Name, func(t *testing.T) { + t.Parallel() + _, tinkDB, cl := NewPostgresDatabaseClient(t, ctx, NewPostgresDatabaseRequest{ + ApplyMigration: true, + }) + defer func() { + err := cl() + if err != nil { + t.Error(err) + } + }() + + err := createHardware(ctx, tinkDB, s.Input.hardware) + if err != nil { + t.Error(err) + } + err = createTemplateFromWorkflowType(ctx, tinkDB, s.Input.template) + if err != nil { + t.Error(err) + } + + wfIDs := []string{} + for i := 0; i < s.Input.workflowCount; i++ { + id, err := createWorkflow(ctx, tinkDB, s.Input) + if err != nil { + t.Error(err) + } + wfIDs = append(wfIDs, id) + } + + var wg sync.WaitGroup + wg.Add(s.Input.workflowCount) + for i := 0; i < s.Input.workflowCount; i++ { + if s.GetAsync { + go func(t *testing.T, tinkDB *db.TinkDB, wfID string) { + defer wg.Done() + s.Expectation(t, tinkDB, wfID) + }(t, tinkDB, wfIDs[i]) + } else { + wg.Done() + s.Expectation(t, tinkDB, wfIDs[i]) + } + } + wg.Wait() + }) + } +} + +func TestGetWorkflow_WithNonExistingID(t *testing.T) { + t.Parallel() + ctx := context.Background() + _, tinkDB, cl := NewPostgresDatabaseClient(t, ctx, NewPostgresDatabaseRequest{ + ApplyMigration: true, + }) + defer func() { + err := cl() + if err != nil { + t.Error(err) + } + }() + + wf, err := tinkDB.GetWorkflow(ctx, uuid.New().String()) + if err != nil { + t.Error(err) + } + assert.Empty(t, wf) +} + func createWorkflow(ctx context.Context, tinkDB *db.TinkDB, in *input) (string, error) { _, _, tmpData, err := tinkDB.GetTemplate(context.Background(), map[string]string{"id": in.template.ID}, false) if err != nil {