From a0ed48ae2796dbfbfce1e024d661005fea3ba4c7 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Thu, 4 Jul 2024 15:37:55 +0300 Subject: [PATCH] feat: add running context to test workflow execution models Signed-off-by: Vladislav Sukhin --- .../renderer/testworkflowexecution_obj.go | 5 ++++ .../commands/testworkflows/run.go | 12 ++++++--- .../testworkflow-toolkit/commands/execute.go | 2 +- docs/docs/articles/webhooks.mdx | 1 + .../model_running_context_extended.go | 18 +++++++------ pkg/mapper/cdevents/mapper.go | 26 ++++++++++++++++++- pkg/mapper/cdevents/mapper_test.go | 18 +++++++++++++ pkg/triggers/executor.go | 4 +++ 8 files changed, 73 insertions(+), 13 deletions(-) diff --git a/cmd/kubectl-testkube/commands/testworkflows/renderer/testworkflowexecution_obj.go b/cmd/kubectl-testkube/commands/testworkflows/renderer/testworkflowexecution_obj.go index 05076da5af9..f321030606a 100644 --- a/cmd/kubectl-testkube/commands/testworkflows/renderer/testworkflowexecution_obj.go +++ b/cmd/kubectl-testkube/commands/testworkflows/renderer/testworkflowexecution_obj.go @@ -58,6 +58,11 @@ func printPrettyOutput(ui *ui.UI, execution testkube.TestWorkflowExecution) { ui.Warn("Execution number: ", fmt.Sprintf("%d", execution.Number)) } ui.Warn("Requested at: ", execution.ScheduledAt.String()) + if execution.RunningContext != nil { + ui.Warn("Running context: ") + ui.Warn("Type: ", execution.RunningContext.Type_) + ui.Warn("Context: ", execution.RunningContext.Context) + } if execution.Result != nil && execution.Result.Status != nil { ui.Warn("Status: ", string(*execution.Result.Status)) if !execution.Result.QueuedAt.IsZero() { diff --git a/cmd/kubectl-testkube/commands/testworkflows/run.go b/cmd/kubectl-testkube/commands/testworkflows/run.go index 7a42b731e6e..7cce049dfad 100644 --- a/cmd/kubectl-testkube/commands/testworkflows/run.go +++ b/cmd/kubectl-testkube/commands/testworkflows/run.go @@ -31,9 +31,10 @@ var ( func NewRunTestWorkflowCmd() *cobra.Command { var ( - executionName string - config map[string]string - watchEnabled bool + executionName string + config map[string]string + watchEnabled bool + runningContext string ) cmd := &cobra.Command{ @@ -71,6 +72,10 @@ func NewRunTestWorkflowCmd() *cobra.Command { Name: executionName, Config: config, DisableWebhooks: disableWebhooks, + RunningContext: &testkube.RunningContext{ + Type_: string(testkube.RunningContextTypeUserCLI), + Context: runningContext, + }, }) if err != nil { // User friendly Open Source operation error @@ -116,6 +121,7 @@ func NewRunTestWorkflowCmd() *cobra.Command { cmd.Flags().BoolVarP(&watchEnabled, "watch", "f", false, "watch for changes after start") cmd.Flags().Bool("disable-webhooks", false, "disable webhooks for this execution") cmd.Flags().Bool("enable-webhooks", false, "enable webhooks for this execution") + cmd.Flags().StringVar(&runningContext, "context", "", "running context description for test execution") return cmd } diff --git a/cmd/tcl/testworkflow-toolkit/commands/execute.go b/cmd/tcl/testworkflow-toolkit/commands/execute.go index 8d8aa468ffd..bd93fc72e52 100644 --- a/cmd/tcl/testworkflow-toolkit/commands/execute.go +++ b/cmd/tcl/testworkflow-toolkit/commands/execute.go @@ -62,7 +62,7 @@ func buildTestExecution(test testworkflowsv1.StepExecuteTest, async, disableWebh exec, err := c.ExecuteTest(test.Name, test.ExecutionRequest.Name, client.ExecuteTestOptions{ RunningContext: &testkube.RunningContext{ - Type_: "testworkflow", + Type_: string(testkube.RunningContextTypeTestWorkflow), Context: fmt.Sprintf("%s/executions/%s", env.WorkflowName(), env.ExecutionId()), }, IsVariablesFileUploaded: test.ExecutionRequest.IsVariablesFileUploaded, diff --git a/docs/docs/articles/webhooks.mdx b/docs/docs/articles/webhooks.mdx index ea0830839e3..807758610fa 100644 --- a/docs/docs/articles/webhooks.mdx +++ b/docs/docs/articles/webhooks.mdx @@ -407,6 +407,7 @@ The full TestSuiteExecution data model can be found [here](https://github.com/ku - `Output` - Additional information from the steps, like referenced executed tests or artifacts - `Workflow` - TestWorkflow definition - `ResolvedWorkflow` - TestWorkflow definition with resolved fields +- `RunningContext` - Running context - how the test workflow has been triggered (for example, `user-ui`) The full TestWorkflowExecution data model can be found [here](https://github.com/kubeshop/testkube/blob/main/pkg/api/v1/testkube/model_test_workflow_execution.go). diff --git a/pkg/api/v1/testkube/model_running_context_extended.go b/pkg/api/v1/testkube/model_running_context_extended.go index b1b4db2a0ae..26e90d52e99 100644 --- a/pkg/api/v1/testkube/model_running_context_extended.go +++ b/pkg/api/v1/testkube/model_running_context_extended.go @@ -3,12 +3,14 @@ package testkube type RunningContextType string const ( - RunningContextTypeUserCLI RunningContextType = "user-cli" - RunningContextTypeUserUI RunningContextType = "user-ui" - RunningContextTypeTestSuite RunningContextType = "testsuite" - RunningContextTypeTestTrigger RunningContextType = "testtrigger" - RunningContextTypeScheduler RunningContextType = "scheduler" - RunningContextTypeTestExecution RunningContextType = "testexecution" - RunningContextTypeTestSuiteExecution RunningContextType = "testsuiteexecution" - RunningContextTypeEmpty RunningContextType = "" + RunningContextTypeUserCLI RunningContextType = "user-cli" + RunningContextTypeUserUI RunningContextType = "user-ui" + RunningContextTypeTestSuite RunningContextType = "testsuite" + RunningContextTypeTestWorkflow RunningContextType = "testworkflow" + RunningContextTypeTestTrigger RunningContextType = "testtrigger" + RunningContextTypeScheduler RunningContextType = "scheduler" + RunningContextTypeTestExecution RunningContextType = "testexecution" + RunningContextTypeTestSuiteExecution RunningContextType = "testsuiteexecution" + RunningContextTypeTestWorkflowExecution RunningContextType = "testworkflowexecution" + RunningContextTypeEmpty RunningContextType = "" ) diff --git a/pkg/mapper/cdevents/mapper.go b/pkg/mapper/cdevents/mapper.go index 27a166ab9bb..3c85a10e8f5 100644 --- a/pkg/mapper/cdevents/mapper.go +++ b/pkg/mapper/cdevents/mapper.go @@ -414,7 +414,7 @@ func MapTestkubeRunningContextTypeToCDEventTiggerType(contextType string) string switch testkube.RunningContextType(contextType) { case testkube.RunningContextTypeUserCLI, testkube.RunningContextTypeUserUI: return "manual" - case testkube.RunningContextTypeTestTrigger, testkube.RunningContextTypeTestSuite: + case testkube.RunningContextTypeTestTrigger, testkube.RunningContextTypeTestSuite, testkube.RunningContextTypeTestWorkflow: return "event" case testkube.RunningContextTypeScheduler: return "schedule" @@ -522,6 +522,12 @@ func MapTestkubeEventQueuedTestWorkflowTestToCDEvent(event testkube.Event, clust Id: namespace, Source: clusterID, }) + + if event.TestWorkflowExecution.RunningContext != nil { + ev.SetSubjectTrigger(&cdevents.TestCaseRunQueuedSubjectContentTrigger{ + Type: MapTestkubeRunningContextTypeToCDEventTiggerType(event.TestWorkflowExecution.RunningContext.Type_), + }) + } } return ev, nil @@ -561,6 +567,12 @@ func MapTestkubeEventQueuedTestWorkflowTestSuiteToCDEvent(event testkube.Event, Id: namespace, Source: clusterID, }) + + if event.TestWorkflowExecution.RunningContext != nil { + ev.SetSubjectTrigger(&cdevents.TestSuiteRunQueuedSubjectContentTrigger{ + Type: MapTestkubeRunningContextTypeToCDEventTiggerType(event.TestWorkflowExecution.RunningContext.Type_), + }) + } } return ev, nil @@ -601,6 +613,12 @@ func MapTestkubeEventStartTestWorkflowTestToCDEvent(event testkube.Event, cluste Id: namespace, Source: clusterID, }) + + if event.TestWorkflowExecution.RunningContext != nil { + ev.SetSubjectTrigger(&cdevents.TestCaseRunStartedSubjectContentTrigger{ + Type: MapTestkubeRunningContextTypeToCDEventTiggerType(event.TestWorkflowExecution.RunningContext.Type_), + }) + } } return ev, nil @@ -640,6 +658,12 @@ func MapTestkubeEventStartTestWorkflowTestSuiteToCDEvent(event testkube.Event, c Id: namespace, Source: clusterID, }) + + if event.TestWorkflowExecution.RunningContext != nil { + ev.SetSubjectTrigger(&cdevents.TestSuiteRunStartedSubjectContentTrigger{ + Type: MapTestkubeRunningContextTypeToCDEventTiggerType(event.TestWorkflowExecution.RunningContext.Type_), + }) + } } return ev, nil diff --git a/pkg/mapper/cdevents/mapper_test.go b/pkg/mapper/cdevents/mapper_test.go index e66146e1ed7..0045e9990ab 100644 --- a/pkg/mapper/cdevents/mapper_test.go +++ b/pkg/mapper/cdevents/mapper_test.go @@ -511,6 +511,9 @@ func TestMapTestkubeEventQueuedTestWorkflowTestToCDEvent(t *testing.T) { }, }, }, + RunningContext: &testkube.RunningContext{ + Type_: "scheduler", + }, }, } clusterID := "cluster-1" @@ -592,6 +595,9 @@ func TestMapTestkubeEventQueuedTestWorkflowTestSuiteToCDEvent(t *testing.T) { }, }, }, + RunningContext: &testkube.RunningContext{ + Type_: "scheduler", + }, }, } clusterID := "cluster-1" @@ -664,6 +670,9 @@ func TestMapTestkubeEventStartTestWorkflowTestToCDEvent(t *testing.T) { }, }, }, + RunningContext: &testkube.RunningContext{ + Type_: "scheduler", + }, }, } clusterID := "cluster-1" @@ -746,6 +755,9 @@ func TestMapTestkubeEventStartTestWorkflowTestSuiteToCDEvent(t *testing.T) { }, }, }, + RunningContext: &testkube.RunningContext{ + Type_: "scheduler", + }, }, } clusterID := "cluster-1" @@ -827,6 +839,9 @@ func TestMapTestkubeEventFinishTestWorkflowTestToCDEvent(t *testing.T) { }, }, }, + RunningContext: &testkube.RunningContext{ + Type_: "scheduler", + }, }, } clusterID := "cluster-1" @@ -927,6 +942,9 @@ func TestMapTestkubeEventFinishTestWorkflowTestSuiteToCDEvent(t *testing.T) { }, }, }, + RunningContext: &testkube.RunningContext{ + Type_: "scheduler", + }, }, } clusterID := "cluster-1" diff --git a/pkg/triggers/executor.go b/pkg/triggers/executor.go index 9ab3cc46bb5..eda3a639c6a 100644 --- a/pkg/triggers/executor.go +++ b/pkg/triggers/executor.go @@ -135,6 +135,10 @@ func (s *Service) execute(ctx context.Context, e *watcherEvent, t *testtriggersv request := testkube.TestWorkflowExecutionRequest{ Config: make(map[string]string, len(variables)), + RunningContext: &testkube.RunningContext{ + Type_: string(testkube.RunningContextTypeTestTrigger), + Context: t.Name, + }, } for _, variable := range variables {