Skip to content

Commit

Permalink
feat: add execution number to executions (#1660)
Browse files Browse the repository at this point in the history
* feat: add execution number to executions

* fix: print execution number only if it is not 0, omit for the existing executions
  • Loading branch information
nicufk committed Jul 13, 2022
1 parent 3d8587c commit 8462f5b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 11 deletions.
17 changes: 12 additions & 5 deletions cmd/kubectl-testkube/commands/tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ import (
"reflect"
"time"

"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/renderer"
apiclientv1 "github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/executor/output"
"github.com/kubeshop/testkube/pkg/test/detector"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
)

func printExecutionDetails(execution testkube.Execution) {
ui.Warn("Type :", execution.TestType)
ui.Warn("Name :", execution.TestName)
ui.Warn("Type: ", execution.TestType)
ui.Warn("Name: ", execution.TestName)
if execution.Id != "" {
ui.Warn("Execution ID :", execution.Id)
ui.Warn("Execution name:", execution.Name)
ui.Warn("Execution ID: ", execution.Id)
if execution.Number != 0 {
ui.Warn("Execution number: ", fmt.Sprintf("%d", execution.Number))
}
ui.Warn("Status: ", string(*execution.ExecutionResult.Status))
ui.Warn("Start time: ", execution.StartTime.String())
ui.Warn("End time: ", execution.EndTime.String())
ui.Warn("Duration: ", execution.Duration)
}

renderer.RenderVariables(execution.Variables)
Expand Down
14 changes: 10 additions & 4 deletions cmd/kubectl-testkube/commands/tests/renderer/execution_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ func ExecutionRenderer(ui *ui.UI, obj interface{}) error {
return fmt.Errorf("can't render execution, expecrted obj to be testkube.Execution but got '%T'", obj)
}

ui.Warn("ID: ", execution.Id)
ui.Warn("Name: ", execution.Name)
ui.Warn("Type: ", execution.TestType)
ui.Warn("Duration: ", execution.Duration)
ui.Warn("ID: ", execution.Id)
if execution.Number != 0 {
ui.Warn("Number: ", fmt.Sprintf("%d", execution.Number))
}
ui.Warn("Test name: ", execution.TestName)
ui.Warn("Type: ", execution.TestType)
ui.Warn("Status: ", string(*execution.ExecutionResult.Status))
ui.Warn("Start time:", execution.StartTime.String())
ui.Warn("End time: ", execution.EndTime.String())
ui.Warn("Duration: ", execution.Duration)

if len(execution.Labels) > 0 {
ui.Warn("Labels: ", testkube.MapToString(execution.Labels))
Expand Down
16 changes: 16 additions & 0 deletions internal/app/api/v1/executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func (s TestkubeAPI) executeTest(ctx context.Context, test testkube.Test, reques
request.Name = rand.Name()
}

request.Number = s.getNextExecutionNumber(test.Name)

// test name + test execution name should be unique
execution, _ = s.ExecutionResults.GetByNameAndTest(ctx, request.Name, test.Name)
if execution.Name == request.Name {
Expand Down Expand Up @@ -569,6 +571,18 @@ func (s *TestkubeAPI) streamLogsFromJob(executionID string, w *bufio.Writer) {
}
}

func (s TestkubeAPI) getNextExecutionNumber(testName string) int {
execution, err := s.ExecutionResults.GetLatestByTest(context.Background(), testName, "number")
if err == mongo.ErrNoDocuments {
return 1
}
if err != nil {
s.Log.Errorw("retrieving latest execution", "error", err)
return 1
}
return execution.Number + 1
}

func mergeVariables(vars1 map[string]testkube.Variable, vars2 map[string]testkube.Variable) map[string]testkube.Variable {
variables := map[string]testkube.Variable{}
for k, v := range vars1 {
Expand All @@ -588,6 +602,7 @@ func newExecutionFromExecutionOptions(options client.ExecuteOptions) testkube.Ex
options.Request.TestSuiteName,
options.Request.Name,
options.TestSpec.Type_,
options.Request.Number,
testsmapper.MapTestContentFromSpec(options.TestSpec.Content),
testkube.NewRunningExecutionResult(),
options.Request.Variables,
Expand All @@ -609,6 +624,7 @@ func mapExecutionsToExecutionSummary(executions []testkube.Execution) []testkube
result[i] = testkube.ExecutionSummary{
Id: execution.Id,
Name: execution.Name,
Number: execution.Number,
TestName: execution.TestName,
TestType: execution.TestType,
Status: execution.ExecutionResult.Status,
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Execution struct {
TestType string `json:"testType,omitempty"`
// execution name
Name string `json:"name,omitempty"`
// execution number
Number int `json:"number,omitempty"`
// environment variables passed to executor
Envs map[string]string `json:"envs,omitempty"`
// additional arguments/flags passed to executor binary
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/v1/testkube/model_execution_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ func NewExecutionWithID(id, testType, testName string) Execution {
}
}

func NewExecution(testNamespace, testName, testSuiteName, executionName, testType string, content *TestContent, result ExecutionResult,
func NewExecution(testNamespace, testName, testSuiteName, executionName, testType string, executionNumber int, content *TestContent, result ExecutionResult,
variables map[string]Variable, testSecretUUID, testSuiteSecretUUID string, labels map[string]string) Execution {
return Execution{
Id: primitive.NewObjectID().Hex(),
TestName: testName,
TestSuiteName: testSuiteName,
TestNamespace: testNamespace,
Name: executionName,
Number: executionNumber,
TestType: testType,
ExecutionResult: &result,
Variables: variables,
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_execution_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type ExecutionRequest struct {
Name string `json:"name,omitempty"`
// unique test suite name (CRD Test suite name), if it's run as a part of test suite
TestSuiteName string `json:"testSuiteName,omitempty"`
// test execution number
Number int `json:"number,omitempty"`
// test kubernetes namespace (\"testkube\" when not set)
Namespace string `json:"namespace,omitempty"`
// variables file content - need to be in format for particular executor (e.g. postman envs file)
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_execution_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type ExecutionSummary struct {
Id string `json:"id"`
// execution name
Name string `json:"name"`
// execution number
Number int `json:"number,omitempty"`
// name of the test
TestName string `json:"testName"`
// name of the test
Expand Down
6 changes: 5 additions & 1 deletion pkg/mapper/executions/sumary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package executions
import (
"testing"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/stretchr/testify/assert"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
)

func TestMapToSummary(t *testing.T) {
Expand All @@ -18,6 +19,7 @@ func TestMapToSummary(t *testing.T) {
for i := 0; i < len(executions); i++ {
assert.Equal(t, result[i].Id, executions[i].Id)
assert.Equal(t, result[i].Name, executions[i].Name)
assert.Equal(t, result[i].Number, executions[i].Number)
assert.Equal(t, result[i].TestName, executions[i].TestName)
assert.Equal(t, result[i].TestType, executions[i].TestType)
assert.Equal(t, result[i].Status, executions[i].ExecutionResult.Status)
Expand All @@ -35,6 +37,7 @@ func getExecutions() testkube.Executions {
"testsuite1",
"execution1",
"test/test",
1,
testkube.NewStringTestContent(""),
*ex1,
map[string]testkube.Variable{"p": testkube.NewBasicVariable("p", "v1")},
Expand All @@ -52,6 +55,7 @@ func getExecutions() testkube.Executions {
"testsuite1",
"execution2",
"test/test",
2,
testkube.NewStringTestContent(""),
*ex2,
map[string]testkube.Variable{"p": testkube.NewBasicVariable("p", "v2")},
Expand Down
1 change: 1 addition & 0 deletions pkg/mapper/executions/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func MapToSummary(executions []testkube.Execution) []testkube.ExecutionSummary {
result[i] = testkube.ExecutionSummary{
Id: s.Id,
Name: s.Name,
Number: s.Number,
TestName: s.TestName,
TestType: s.TestType,
Status: s.ExecutionResult.Status,
Expand Down

0 comments on commit 8462f5b

Please sign in to comment.