From c5d96418b5c9101adc023d819e1d11f56ca52116 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Tue, 9 Jul 2024 16:27:05 +0300 Subject: [PATCH] fix: mapping for cd events for test workflow running context Signed-off-by: Vladislav Sukhin --- pkg/mapper/cdevents/mapper.go | 41 ++++++++++++++++++++++++------ pkg/mapper/cdevents/mapper_test.go | 37 ++++++++++++++++++--------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/pkg/mapper/cdevents/mapper.go b/pkg/mapper/cdevents/mapper.go index 3c85a10e8f5..5389f78aad8 100644 --- a/pkg/mapper/cdevents/mapper.go +++ b/pkg/mapper/cdevents/mapper.go @@ -423,6 +423,21 @@ func MapTestkubeRunningContextTypeToCDEventTiggerType(contextType string) string return "other" } +// MapTestkubeTestWorkflowRunningContextActorToCDEventTiggerType maps OpenAPI spec Test Workflow Running Context Actor to CDEvent Trigger Type +func MapTestkubeTestWorkflowRunningContextActorToCDEventTiggerType(actor testkube.TestWorkflowRunningContextActor) string { + switch actor { + case testkube.USER_TestWorkflowRunningContextActor: + return "manual" + case testkube.TESTWORKFLOW_TestWorkflowRunningContextActor, testkube.TESTWORKFLOWEXECUTION_TestWorkflowRunningContextActor, + testkube.TESTRIGGER_TestWorkflowRunningContextActor: + return "event" + case testkube.CRON_TestWorkflowRunningContextActor: + return "schedule" + } + + return "other" +} + // MapTestkubeTestTypeToCDEventTestCaseType maps OpenAPI spec Test Type to CDEvent Test Case Type func MapTestkubeTestTypeToCDEventTestCaseType(testType string) string { var types = map[string]string{ @@ -487,6 +502,16 @@ func MapMimeTypeToCDEventOutputType(mimeType string) string { return "other" } +func getActor(runningContext []testkube.TestWorkflowRunningContext) *testkube.TestWorkflowRunningContextActor { + for _, ctx := range runningContext { + if ctx.Actor != nil { + return ctx.Actor + } + } + + return nil +} + // MapTestkubeEventQueuedTestWorkflowTestToCDEvent maps OpenAPI spec Queued Test Workflow Test Event to CDEvent CDEventReader func MapTestkubeEventQueuedTestWorkflowTestToCDEvent(event testkube.Event, clusterID, defaultNamespace, dashboardURI string) (cdevents.CDEventReader, error) { // Create the base event @@ -523,9 +548,9 @@ func MapTestkubeEventQueuedTestWorkflowTestToCDEvent(event testkube.Event, clust Source: clusterID, }) - if event.TestWorkflowExecution.RunningContext != nil { + if actor := getActor(event.TestWorkflowExecution.RunningContext); actor != nil { ev.SetSubjectTrigger(&cdevents.TestCaseRunQueuedSubjectContentTrigger{ - Type: MapTestkubeRunningContextTypeToCDEventTiggerType(event.TestWorkflowExecution.RunningContext.Type_), + Type: MapTestkubeTestWorkflowRunningContextActorToCDEventTiggerType(*actor), }) } } @@ -568,9 +593,9 @@ func MapTestkubeEventQueuedTestWorkflowTestSuiteToCDEvent(event testkube.Event, Source: clusterID, }) - if event.TestWorkflowExecution.RunningContext != nil { + if actor := getActor(event.TestWorkflowExecution.RunningContext); actor != nil { ev.SetSubjectTrigger(&cdevents.TestSuiteRunQueuedSubjectContentTrigger{ - Type: MapTestkubeRunningContextTypeToCDEventTiggerType(event.TestWorkflowExecution.RunningContext.Type_), + Type: MapTestkubeTestWorkflowRunningContextActorToCDEventTiggerType(*actor), }) } } @@ -614,9 +639,9 @@ func MapTestkubeEventStartTestWorkflowTestToCDEvent(event testkube.Event, cluste Source: clusterID, }) - if event.TestWorkflowExecution.RunningContext != nil { + if actor := getActor(event.TestWorkflowExecution.RunningContext); actor != nil { ev.SetSubjectTrigger(&cdevents.TestCaseRunStartedSubjectContentTrigger{ - Type: MapTestkubeRunningContextTypeToCDEventTiggerType(event.TestWorkflowExecution.RunningContext.Type_), + Type: MapTestkubeTestWorkflowRunningContextActorToCDEventTiggerType(*actor), }) } } @@ -659,9 +684,9 @@ func MapTestkubeEventStartTestWorkflowTestSuiteToCDEvent(event testkube.Event, c Source: clusterID, }) - if event.TestWorkflowExecution.RunningContext != nil { + if actor := getActor(event.TestWorkflowExecution.RunningContext); actor != nil { ev.SetSubjectTrigger(&cdevents.TestSuiteRunStartedSubjectContentTrigger{ - Type: MapTestkubeRunningContextTypeToCDEventTiggerType(event.TestWorkflowExecution.RunningContext.Type_), + Type: MapTestkubeTestWorkflowRunningContextActorToCDEventTiggerType(*actor), }) } } diff --git a/pkg/mapper/cdevents/mapper_test.go b/pkg/mapper/cdevents/mapper_test.go index 0045e9990ab..6c50aad0fc4 100644 --- a/pkg/mapper/cdevents/mapper_test.go +++ b/pkg/mapper/cdevents/mapper_test.go @@ -7,6 +7,7 @@ import ( cdevents "github.com/cdevents/sdk-go/pkg/api" "github.com/stretchr/testify/assert" + "github.com/kubeshop/testkube/internal/common" "github.com/kubeshop/testkube/pkg/api/v1/testkube" ) @@ -511,8 +512,10 @@ func TestMapTestkubeEventQueuedTestWorkflowTestToCDEvent(t *testing.T) { }, }, }, - RunningContext: &testkube.RunningContext{ - Type_: "scheduler", + RunningContext: []testkube.TestWorkflowRunningContext{ + { + Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor), + }, }, }, } @@ -595,8 +598,10 @@ func TestMapTestkubeEventQueuedTestWorkflowTestSuiteToCDEvent(t *testing.T) { }, }, }, - RunningContext: &testkube.RunningContext{ - Type_: "scheduler", + RunningContext: []testkube.TestWorkflowRunningContext{ + { + Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor), + }, }, }, } @@ -670,8 +675,10 @@ func TestMapTestkubeEventStartTestWorkflowTestToCDEvent(t *testing.T) { }, }, }, - RunningContext: &testkube.RunningContext{ - Type_: "scheduler", + RunningContext: []testkube.TestWorkflowRunningContext{ + { + Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor), + }, }, }, } @@ -755,8 +762,10 @@ func TestMapTestkubeEventStartTestWorkflowTestSuiteToCDEvent(t *testing.T) { }, }, }, - RunningContext: &testkube.RunningContext{ - Type_: "scheduler", + RunningContext: []testkube.TestWorkflowRunningContext{ + { + Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor), + }, }, }, } @@ -839,8 +848,10 @@ func TestMapTestkubeEventFinishTestWorkflowTestToCDEvent(t *testing.T) { }, }, }, - RunningContext: &testkube.RunningContext{ - Type_: "scheduler", + RunningContext: []testkube.TestWorkflowRunningContext{ + { + Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor), + }, }, }, } @@ -942,8 +953,10 @@ func TestMapTestkubeEventFinishTestWorkflowTestSuiteToCDEvent(t *testing.T) { }, }, }, - RunningContext: &testkube.RunningContext{ - Type_: "scheduler", + RunningContext: []testkube.TestWorkflowRunningContext{ + { + Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor), + }, }, }, }