Skip to content

Commit

Permalink
feat: [TKC-3016] add defaultConfigs for TestWorkflow executions
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasv committed Dec 18, 2024
1 parent 378ab7e commit f3974cc
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 5 deletions.
3 changes: 3 additions & 0 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9534,6 +9534,9 @@ components:
defaultValue:
type: string
description: configuration value default
truncated:
type: boolean
description: indicates if the value is truncated


TestWorkflowConfigValue:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ type TestWorkflowExecutionConfigValue struct {
Value string `json:"value,omitempty"`
// configuration value default
DefaultValue string `json:"defaultValue,omitempty"`
// indicates if the value is truncated
Truncated bool `json:"truncated,omitempty"`
}
70 changes: 65 additions & 5 deletions pkg/repository/testworkflow/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ type MongoRepositoryOpt func(*MongoRepository)

func (r *MongoRepository) Get(ctx context.Context, id string) (result testkube.TestWorkflowExecution, err error) {
err = r.Coll.FindOne(ctx, bson.M{"$or": bson.A{bson.M{"id": id}, bson.M{"name": id}}}).Decode(&result)

if result.ResolvedWorkflow != nil && result.ResolvedWorkflow.Spec != nil {
for k, v := range result.ResolvedWorkflow.Spec.Config {
if v.Default_ != nil {
if result.ConfigParams == nil {
result.ConfigParams = make(map[string]testkube.TestWorkflowExecutionConfigValue)
}
if _, ok := result.ConfigParams[k]; !ok {
result.ConfigParams[k] = testkube.TestWorkflowExecutionConfigValue{
DefaultValue: v.Default_.Value,
}
} else {
value := result.ConfigParams[k].Value
truncated := false
if len(value) > 100 {
value = value[:100]
truncated = true
}
result.ConfigParams[k] = testkube.TestWorkflowExecutionConfigValue{
DefaultValue: v.Default_.Value,
Value: value,
Truncated: truncated,
}
}
}
}
}

return *result.UnscapeDots(), err
}

Expand Down Expand Up @@ -241,8 +269,13 @@ func (r *MongoRepository) GetExecutions(ctx context.Context, filter Filter) (res
return
}

type TestWorkflowExecutionSummaryWithResolvedWorkflow struct {
testkube.TestWorkflowExecutionSummary `json:",inline" bson:",inline"`
ResolvedWorkflow *testkube.TestWorkflow `json:"resolvedWorkflow,omitempty"`
}

func (r *MongoRepository) GetExecutionsSummary(ctx context.Context, filter Filter) (result []testkube.TestWorkflowExecutionSummary, err error) {
result = make([]testkube.TestWorkflowExecutionSummary, 0)
executions := make([]TestWorkflowExecutionSummaryWithResolvedWorkflow, 0)
query, opts := composeQueryAndOpts(filter)
if r.allowDiskUse {
opts.SetAllowDiskUse(r.allowDiskUse)
Expand All @@ -255,16 +288,43 @@ func (r *MongoRepository) GetExecutionsSummary(ctx context.Context, filter Filte
"result.steps": 0,
"result.initialization": 0,
"workflow.spec": 0,
"resolvedWorkflow": 0,
})
cursor, err := r.Coll.Find(ctx, query, opts)
if err != nil {
return
}
err = cursor.All(ctx, &result)
err = cursor.All(ctx, &executions)
result = make([]testkube.TestWorkflowExecutionSummary, len(executions))
for i := range executions {
executions[i].UnscapeDots()

for i := range result {
result[i].UnscapeDots()
if executions[i].ResolvedWorkflow != nil && executions[i].ResolvedWorkflow.Spec != nil {
for k, v := range executions[i].ResolvedWorkflow.Spec.Config {
if v.Default_ != nil {
if executions[i].ConfigParams == nil {
executions[i].ConfigParams = make(map[string]testkube.TestWorkflowExecutionConfigValue)
}
if _, ok := executions[i].ConfigParams[k]; !ok {
executions[i].ConfigParams[k] = testkube.TestWorkflowExecutionConfigValue{
DefaultValue: v.Default_.Value,
}
} else {
value := executions[i].ConfigParams[k].Value
truncated := false
if len(value) > 100 {
value = value[:100]
truncated = true
}
executions[i].ConfigParams[k] = testkube.TestWorkflowExecutionConfigValue{
DefaultValue: v.Default_.Value,
Value: value,
Truncated: truncated,
}
}
}
}
}
result[i] = executions[i].TestWorkflowExecutionSummary
}
return
}
Expand Down
130 changes: 130 additions & 0 deletions pkg/repository/testworkflow/mongo_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,133 @@ func TestNewMongoRepository_GetExecutions_Actor_Integration(t *testing.T) {

assert.Len(t, res, 0)
}

func TestNewMongoRepository_GetExecutionsSummary_Integration(t *testing.T) {
test.IntegrationTest(t)
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.APIMongoDSN))
if err != nil {
t.Fatalf("error connecting to mongo: %v", err)
}
db := client.Database("testworkflow-executions-summary-mongo-repository-test")
t.Cleanup(func() {
db.Drop(ctx)
})
repo := NewMongoRepository(db, false)

// Insert test data
execution := testkube.TestWorkflowExecution{
Id: "test-id-1",
Name: "test-name-1",
Workflow: &testkube.TestWorkflow{
Name: "test-workflow-1",
Spec: &testkube.TestWorkflowSpec{},
},
ResolvedWorkflow: &testkube.TestWorkflow{
Name: "test-workflow-1",
Spec: &testkube.TestWorkflowSpec{
Config: map[string]testkube.TestWorkflowParameterSchema{
"param1": {
Default_: &testkube.BoxedString{
Value: "default",
},
},
},
},
},
}
err = repo.Insert(ctx, execution)
assert.NoError(t, err)

execution2 := testkube.TestWorkflowExecution{
Id: "test-id-1",
Name: "test-name-2",
Workflow: &testkube.TestWorkflow{
Name: "test-workflow-2",
Spec: &testkube.TestWorkflowSpec{},
},
ResolvedWorkflow: &testkube.TestWorkflow{
Name: "test-workflow-2",
Spec: &testkube.TestWorkflowSpec{
Config: map[string]testkube.TestWorkflowParameterSchema{
"param1": {
Default_: &testkube.BoxedString{
Value: "default",
},
},
},
},
},
ConfigParams: map[string]testkube.TestWorkflowExecutionConfigValue{
"param1": {
Value: "custom-value",
},
},
}
err = repo.Insert(ctx, execution2)
assert.NoError(t, err)

// Test GetExecutionsSummary
filter := NewExecutionsFilter().WithName("test-workflow-1")
result, err := repo.GetExecutionsSummary(ctx, filter)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, "test-name-1", result[0].Name)
assert.Equal(t, "default", result[0].ConfigParams["param1"].DefaultValue)

filter = NewExecutionsFilter().WithName("test-workflow-2")
result, err = repo.GetExecutionsSummary(ctx, filter)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, "test-name-2", result[0].Name)
assert.Equal(t, "default", result[0].ConfigParams["param1"].DefaultValue)
assert.Equal(t, "custom-value", result[0].ConfigParams["param1"].Value)
}

func TestNewMongoRepository_Get_Integration(t *testing.T) {
test.IntegrationTest(t)

ctx := context.Background()

client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.APIMongoDSN))
if err != nil {
t.Fatalf("error connecting to mongo: %v", err)
}
db := client.Database("testworkflow-get-mongo-repository-test")
t.Cleanup(func() {
db.Drop(ctx)
})

repo := NewMongoRepository(db, false)
execution := testkube.TestWorkflowExecution{
Id: "test-id-1",
Name: "test-name-1",
Workflow: &testkube.TestWorkflow{
Name: "test-workflow-1",
Spec: &testkube.TestWorkflowSpec{},
},
ResolvedWorkflow: &testkube.TestWorkflow{
Name: "test-workflow-1",
Spec: &testkube.TestWorkflowSpec{
Config: map[string]testkube.TestWorkflowParameterSchema{
"param1": {
Default_: &testkube.BoxedString{
Value: "default",
},
},
},
},
},
}
err = repo.Insert(ctx, execution)
assert.NoError(t, err)

result, err := repo.Get(ctx, "test-id-1")
assert.NoError(t, err)

assert.Equal(t, execution.Id, result.Id)
assert.Equal(t, execution.Name, result.Name)
assert.Equal(t, "default", result.ConfigParams["param1"].DefaultValue)
assert.Equal(t, false, result.ConfigParams["param1"].Truncated)

}

0 comments on commit f3974cc

Please sign in to comment.