Skip to content

Commit

Permalink
fix: mapping for cd events for test workflow running context
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Sukhin <[email protected]>
  • Loading branch information
vsukhin committed Jul 9, 2024
1 parent 48042d7 commit c5d9641
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
41 changes: 33 additions & 8 deletions pkg/mapper/cdevents/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
})
}
}
Expand Down Expand Up @@ -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),
})
}
}
Expand Down Expand Up @@ -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),
})
}
}
Expand Down Expand Up @@ -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),
})
}
}
Expand Down
37 changes: 25 additions & 12 deletions pkg/mapper/cdevents/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -511,8 +512,10 @@ func TestMapTestkubeEventQueuedTestWorkflowTestToCDEvent(t *testing.T) {
},
},
},
RunningContext: &testkube.RunningContext{
Type_: "scheduler",
RunningContext: []testkube.TestWorkflowRunningContext{
{
Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor),
},
},
},
}
Expand Down Expand Up @@ -595,8 +598,10 @@ func TestMapTestkubeEventQueuedTestWorkflowTestSuiteToCDEvent(t *testing.T) {
},
},
},
RunningContext: &testkube.RunningContext{
Type_: "scheduler",
RunningContext: []testkube.TestWorkflowRunningContext{
{
Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor),
},
},
},
}
Expand Down Expand Up @@ -670,8 +675,10 @@ func TestMapTestkubeEventStartTestWorkflowTestToCDEvent(t *testing.T) {
},
},
},
RunningContext: &testkube.RunningContext{
Type_: "scheduler",
RunningContext: []testkube.TestWorkflowRunningContext{
{
Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor),
},
},
},
}
Expand Down Expand Up @@ -755,8 +762,10 @@ func TestMapTestkubeEventStartTestWorkflowTestSuiteToCDEvent(t *testing.T) {
},
},
},
RunningContext: &testkube.RunningContext{
Type_: "scheduler",
RunningContext: []testkube.TestWorkflowRunningContext{
{
Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor),
},
},
},
}
Expand Down Expand Up @@ -839,8 +848,10 @@ func TestMapTestkubeEventFinishTestWorkflowTestToCDEvent(t *testing.T) {
},
},
},
RunningContext: &testkube.RunningContext{
Type_: "scheduler",
RunningContext: []testkube.TestWorkflowRunningContext{
{
Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor),
},
},
},
}
Expand Down Expand Up @@ -942,8 +953,10 @@ func TestMapTestkubeEventFinishTestWorkflowTestSuiteToCDEvent(t *testing.T) {
},
},
},
RunningContext: &testkube.RunningContext{
Type_: "scheduler",
RunningContext: []testkube.TestWorkflowRunningContext{
{
Actor: common.Ptr(testkube.CRON_TestWorkflowRunningContextActor),
},
},
},
}
Expand Down

0 comments on commit c5d9641

Please sign in to comment.