Skip to content

Commit

Permalink
fix: [TKC-2168] id sequence (#5681)
Browse files Browse the repository at this point in the history
* fix: change sequence for numbers

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add line

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add sequence repository

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add sequence repository

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add sequence for test suites

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: rename option method

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add sequence for test workflows

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: skip crd deletion

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fixL use insert instead of update

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: integration test sync

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: intergrattion test

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: parallel integration test

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: use default value

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: typo nil check

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: change prefix

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: test suite prefix

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: new cloud methods

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: dep update

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

---------

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>
  • Loading branch information
vsukhin authored Jul 29, 2024
1 parent dd0316d commit 435e213
Show file tree
Hide file tree
Showing 30 changed files with 751 additions and 221 deletions.
16 changes: 16 additions & 0 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ paths:
- api
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/SkipDeleteCRD"
summary: "Delete test suite"
description: "Deletes a test suite"
operationId: deleteTestSuite
Expand Down Expand Up @@ -1664,6 +1665,7 @@ paths:
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/SkipDeleteExecutions"
- $ref: "#/components/parameters/SkipDeleteCRD"
summary: "Delete test"
description: "Deletes a test"
operationId: deleteTest
Expand Down Expand Up @@ -3351,6 +3353,7 @@ paths:
- api
parameters:
- $ref: "#/components/parameters/Namespace"
- $ref: "#/components/parameters/ID"
summary: "Delete secret"
description: "Delete secret in the cluster"
operationId: deleteSecret
Expand Down Expand Up @@ -3393,6 +3396,8 @@ paths:
tags:
- secrets
- api
parameters:
- $ref: "#/components/parameters/ID"
summary: "Update secret"
description: "Update secret in the cluster"
operationId: updateSecret
Expand Down Expand Up @@ -3446,6 +3451,8 @@ paths:
tags:
- secrets
- api
parameters:
- $ref: "#/components/parameters/ID"
summary: "Get secret"
description: "Get secret in the cluster"
operationId: getSecret
Expand Down Expand Up @@ -4424,6 +4431,7 @@ paths:
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/SkipDeleteExecutions"
- $ref: "#/components/parameters/SkipDeleteCRD"
summary: Delete test workflow
description: Delete test workflow from the kubernetes cluster
operationId: deleteTestWorkflow
Expand Down Expand Up @@ -10473,6 +10481,14 @@ components:
default: false
description: flag to request all resources
required: false
SkipDeleteCRD:
in: query
name: skipDeleteCRD
schema:
type: boolean
default: false
description: dont delete CRD
required: false
requestBodies:
UploadsBody:
description: "Upload files request body data"
Expand Down
12 changes: 8 additions & 4 deletions cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/kubeshop/testkube/pkg/cloud"
configrepository "github.com/kubeshop/testkube/pkg/repository/config"
"github.com/kubeshop/testkube/pkg/repository/result"
"github.com/kubeshop/testkube/pkg/repository/sequence"
"github.com/kubeshop/testkube/pkg/repository/storage"
"github.com/kubeshop/testkube/pkg/repository/testresult"

Expand Down Expand Up @@ -289,10 +290,14 @@ func main() {
db, err := storage.GetMongoDatabase(cfg.APIMongoDSN, cfg.APIMongoDB, cfg.APIMongoDBType, cfg.APIMongoAllowTLS, mongoSSLConfig)
exitOnError("Getting mongo database", err)
isDocDb := cfg.APIMongoDBType == storage.TypeDocDB
mongoResultsRepository := result.NewMongoRepository(db, cfg.APIMongoAllowDiskUse, isDocDb, result.WithFeatureFlags(features), result.WithLogsClient(logGrpcClient))
sequenceRepository := sequence.NewMongoRepository(db)
mongoResultsRepository := result.NewMongoRepository(db, cfg.APIMongoAllowDiskUse, isDocDb, result.WithFeatureFlags(features),
result.WithLogsClient(logGrpcClient), result.WithMongoRepositorySequence(sequenceRepository))
resultsRepository = mongoResultsRepository
testResultsRepository = testresult.NewMongoRepository(db, cfg.APIMongoAllowDiskUse, isDocDb)
testWorkflowResultsRepository = testworkflow2.NewMongoRepository(db, cfg.APIMongoAllowDiskUse)
testResultsRepository = testresult.NewMongoRepository(db, cfg.APIMongoAllowDiskUse, isDocDb,
testresult.WithMongoRepositorySequence(sequenceRepository))
testWorkflowResultsRepository = testworkflow2.NewMongoRepository(db, cfg.APIMongoAllowDiskUse,
testworkflow2.WithMongoRepositorySequence(sequenceRepository))
configRepository = configrepository.NewMongoRepository(db)
triggerLeaseBackend = triggers.NewMongoLeaseBackend(db)
minioClient := newStorageClient(cfg)
Expand Down Expand Up @@ -570,7 +575,6 @@ func main() {
testWorkflowTemplatesClient,
testWorkflowProcessor,
configMapConfig,
resultsRepository,
testWorkflowExecutionsClient,
testWorkflowsClient,
metrics,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/gofiber/websocket/v2 v2.1.1
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.6.0
github.com/google/go-containerregistry v0.20.1
github.com/google/uuid v1.5.0
github.com/gookit/color v1.5.4
github.com/gorilla/websocket v1.5.0
Expand All @@ -33,7 +34,7 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/kelseyhightower/envconfig v1.4.0
github.com/kubepug/kubepug v1.7.1
github.com/kubeshop/testkube-operator v1.15.2-beta1.0.20240719112653-d677ef7c3437
github.com/kubeshop/testkube-operator v1.15.2-beta1.0.20240729112855-d7bd9c5f64a9
github.com/minio/minio-go/v7 v7.0.47
github.com/montanaflynn/stats v0.6.6
github.com/moogar0880/problems v0.1.1
Expand Down Expand Up @@ -121,7 +122,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-containerregistry v0.20.1 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubepug/kubepug v1.7.1 h1:LKhfSxS8Y5mXs50v+3Lpyec+cogErDLcV7CMUuiaisw=
github.com/kubepug/kubepug v1.7.1/go.mod h1:lv+HxD0oTFL7ZWjj0u6HKhMbbTIId3eG7aWIW0gyF8g=
github.com/kubeshop/testkube-operator v1.15.2-beta1.0.20240719112653-d677ef7c3437 h1:Ov4w/ozhVUwdOekhD3YzFQ3IYwKkmILGRAy3LdRFGck=
github.com/kubeshop/testkube-operator v1.15.2-beta1.0.20240719112653-d677ef7c3437/go.mod h1:P47tw1nKQFufdsZndyq2HG2MSa0zK/lU0XpRfZtEmIk=
github.com/kubeshop/testkube-operator v1.15.2-beta1.0.20240729112855-d7bd9c5f64a9 h1:Zg+hTPkHi504Nwp3M9kM9+DPX7OdiYJBJL2hJCE5Jhg=
github.com/kubeshop/testkube-operator v1.15.2-beta1.0.20240729112855-d7bd9c5f64a9/go.mod h1:P47tw1nKQFufdsZndyq2HG2MSa0zK/lU0XpRfZtEmIk=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
Expand Down Expand Up @@ -1028,6 +1028,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
24 changes: 13 additions & 11 deletions internal/app/api/v1/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,23 +475,25 @@ func (s TestkubeAPI) DeleteTestHandler() fiber.Handler {
return s.Error(c, http.StatusBadRequest, fmt.Errorf("failed to delete test: id cannot be empty"))
}
errPrefix := fmt.Sprintf("failed to delete test %s", name)
err := s.TestsClient.Delete(name)
if err != nil {
if errors.IsNotFound(err) {
return s.Warn(c, http.StatusNotFound, fmt.Errorf("%s: client could not find test: %w", errPrefix, err))
}
skipCRD := c.Query("skipDeleteCRD", "")
if skipCRD != "true" {
err := s.TestsClient.Delete(name)
if err != nil {
if errors.IsNotFound(err) {
return s.Warn(c, http.StatusNotFound, fmt.Errorf("%s: client could not find test: %w", errPrefix, err))
}

if _, ok := err.(*testsclientv3.DeleteDependenciesError); ok {
return s.Warn(c, http.StatusInternalServerError, fmt.Errorf("client deleted test %s but deleting test dependencies(secrets) returned errors: %w", name, err))
}
if _, ok := err.(*testsclientv3.DeleteDependenciesError); ok {
return s.Warn(c, http.StatusInternalServerError, fmt.Errorf("client deleted test %s but deleting test dependencies(secrets) returned errors: %w", name, err))
}

return s.Error(c, http.StatusInternalServerError, fmt.Errorf("%s: client could not delete test: %w", errPrefix, err))
return s.Error(c, http.StatusInternalServerError, fmt.Errorf("%s: client could not delete test: %w", errPrefix, err))
}
}

skipExecutions := c.Query("skipDeleteExecutions", "")
if skipExecutions != "true" {
// delete executions for test
if err = s.ExecutionResults.DeleteByTest(c.Context(), name); err != nil {
if err := s.ExecutionResults.DeleteByTest(c.Context(), name); err != nil {
return s.Warn(c, http.StatusInternalServerError, fmt.Errorf("test %s was deleted but deleting test executions returned error: %w", name, err))
}
}
Expand Down
18 changes: 10 additions & 8 deletions internal/app/api/v1/testsuites.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,23 +246,25 @@ func (s TestkubeAPI) DeleteTestSuiteHandler() fiber.Handler {
return func(c *fiber.Ctx) error {
name := c.Params("id")
errPrefix := fmt.Sprintf("failed to delete test suite %s", name)
skipCRD := c.Query("skipDeleteCRD", "")
if skipCRD != "true" {
err := s.TestsSuitesClient.Delete(name)
if err != nil {
if errors.IsNotFound(err) {
return s.Warn(c, http.StatusNotFound, fmt.Errorf("%s: test suite not found: %w", errPrefix, err))
}

err := s.TestsSuitesClient.Delete(name)
if err != nil {
if errors.IsNotFound(err) {
return s.Warn(c, http.StatusNotFound, fmt.Errorf("%s: test suite not found: %w", errPrefix, err))
return s.Error(c, http.StatusBadGateway, fmt.Errorf("%s: client could not delete test suite: %w", errPrefix, err))
}

return s.Error(c, http.StatusBadGateway, fmt.Errorf("%s: client could not delete test suite: %w", errPrefix, err))
}

// delete executions for test
if err = s.ExecutionResults.DeleteByTestSuite(c.Context(), name); err != nil {
if err := s.ExecutionResults.DeleteByTestSuite(c.Context(), name); err != nil {
return s.Error(c, http.StatusBadGateway, fmt.Errorf("%s: client could not delete test suite test executions: %w", errPrefix, err))
}

// delete executions for test suite
if err = s.TestExecutionResults.DeleteByTestSuite(c.Context(), name); err != nil {
if err := s.TestExecutionResults.DeleteByTestSuite(c.Context(), name); err != nil {
return s.Error(c, http.StatusBadGateway, fmt.Errorf("%s: client could not delete test suite executions: %w", errPrefix, err))
}

Expand Down
13 changes: 8 additions & 5 deletions internal/app/api/v1/testworkflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ func (s *TestkubeAPI) DeleteTestWorkflowHandler() fiber.Handler {
return func(c *fiber.Ctx) error {
name := c.Params("id")
errPrefix := fmt.Sprintf("failed to delete test workflow '%s'", name)
err := s.TestWorkflowsClient.Delete(name)
s.Metrics.IncDeleteTestWorkflow(err)
if err != nil {
return s.ClientError(c, errPrefix, err)
skipCRD := c.Query("skipDeleteCRD", "")
if skipCRD != "true" {
err := s.TestWorkflowsClient.Delete(name)
s.Metrics.IncDeleteTestWorkflow(err)
if err != nil {
return s.ClientError(c, errPrefix, err)
}
}
skipExecutions := c.Query("skipDeleteExecutions", "")
if skipExecutions != "true" {
err = s.TestWorkflowOutput.DeleteOutputByTestWorkflow(context.Background(), name)
err := s.TestWorkflowOutput.DeleteOutputByTestWorkflow(context.Background(), name)
if err != nil {
return s.ClientError(c, "deleting executions output", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cloud/data/testresult/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ const (
CmdTestResultDeleteAll executor.Command = "test_result_delete_all"
CmdTestResultDeleteByTestSuites executor.Command = "test_result_delete_by_test_suites"
CmdTestResultGetTestSuiteMetrics executor.Command = "test_result_get_test_suite_metrics"
CmdTestResultGetNextExecutionNumber executor.Command = "test_result_get_next_execution_number"
)
13 changes: 13 additions & 0 deletions pkg/cloud/data/testresult/testresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,16 @@ func (r *CloudRepository) GetPreviousFinishedState(ctx context.Context, testSuit
}
return commandResponse.Result, nil
}

func (r *CloudRepository) GetNextExecutionNumber(ctx context.Context, testSuiteName string) (number int32, err error) {
req := NextExecutionNumberRequest{TestSuiteName: testSuiteName}
response, err := r.executor.Execute(ctx, CmdTestResultGetNextExecutionNumber, req)
if err != nil {
return 0, err
}
var commandResponse NextExecutionNumberResponse
if err := json.Unmarshal(response, &commandResponse); err != nil {
return 0, err
}
return commandResponse.TestSuiteNumber, nil
}
8 changes: 8 additions & 0 deletions pkg/cloud/data/testresult/testresult_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ type GetTestSuiteMetricsRequest struct {
type GetTestSuiteMetricsResponse struct {
Metrics testkube.ExecutionsMetrics `json:"metrics"`
}

type NextExecutionNumberRequest struct {
TestSuiteName string `json:"testSuiteName"`
}

type NextExecutionNumberResponse struct {
TestSuiteNumber int32 `json:"testSuiteNumber"`
}
23 changes: 23 additions & 0 deletions pkg/cloud/data/testresult/testresult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,26 @@ func TestCloudResultRepository_GetPreviousFinishedState(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, *expectedStatus, status)
}

func TestCloudResultRepository_GetNextExecutionNumber(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockExecutor := executor.NewMockExecutor(ctrl)

testSuiteName := "testsuite-1"
var testSuiteNumber int32 = 3

// Setup expectations for the mockedExecutor.Execute method
expectedReq := NextExecutionNumberRequest{TestSuiteName: testSuiteName}
expectedResponse, _ := json.Marshal(&NextExecutionNumberResponse{TestSuiteNumber: testSuiteNumber})
mockExecutor.EXPECT().Execute(gomock.Any(), CmdTestResultGetNextExecutionNumber, expectedReq).Return(expectedResponse, nil)

r := &CloudRepository{executor: mockExecutor}

result, err := r.GetNextExecutionNumber(ctx, testSuiteName)
assert.NoError(t, err)
assert.Equal(t, testSuiteNumber, result)
}
3 changes: 3 additions & 0 deletions pkg/cloud/data/testworkflow/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
CmdTestWorkflowExecutionDeleteAll executor.Command = "workflow_execution_delete_all"
CmdTestWorkflowExecutionDeleteByWorkflows executor.Command = "workflow_execution_delete_by_workflows"
CmdTestWorkflowExecutionGetWorkflowMetrics executor.Command = "workflow_execution_get_workflow_metrics"
CmdTestWorkflowExecutionGetNextExecutionNumber executor.Command = "workflow_execution_get_next_execution_number"

CmdTestWorkflowOutputPresignSaveLog executor.Command = "workflow_output_presign_save_log"
CmdTestWorkflowOutputPresignReadLog executor.Command = "workflow_output_presign_read_log"
Expand Down Expand Up @@ -65,6 +66,8 @@ func command(v interface{}) executor.Command {
return CmdTestWorkflowExecutionDeleteByWorkflows
case ExecutionGetWorkflowMetricsRequest:
return CmdTestWorkflowExecutionGetWorkflowMetrics
case ExecutionGetNextExecutionNumberRequest:
return CmdTestWorkflowExecutionGetNextExecutionNumber

case OutputPresignSaveLogRequest:
return CmdTestWorkflowOutputPresignSaveLog
Expand Down
13 changes: 13 additions & 0 deletions pkg/cloud/data/testworkflow/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,16 @@ func (r *CloudRepository) GetPreviousFinishedState(ctx context.Context, workflow
}
return commandResponse.Result, nil
}

func (r *CloudRepository) GetNextExecutionNumber(ctx context.Context, testWorkflowName string) (number int32, err error) {
req := ExecutionGetNextExecutionNumberRequest{TestWorkflowName: testWorkflowName}
response, err := r.executor.Execute(ctx, CmdTestWorkflowExecutionGetNextExecutionNumber, req)
if err != nil {
return 0, err
}
var commandResponse ExecutionGetNextExecutionNumberResponse
if err := json.Unmarshal(response, &commandResponse); err != nil {
return 0, err
}
return commandResponse.TestWorkflowNumber, nil
}
8 changes: 8 additions & 0 deletions pkg/cloud/data/testworkflow/execution_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,11 @@ type ExecutionsAddReportRequest struct {
}

type ExecutionsAddReportResponse struct{}

type ExecutionGetNextExecutionNumberRequest struct {
TestWorkflowName string `json:"testWorkflowName"`
}

type ExecutionGetNextExecutionNumberResponse struct {
TestWorkflowNumber int32 `json:"testWorkflowNumber"`
}
4 changes: 2 additions & 2 deletions pkg/repository/result/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ type Repository interface {
}

type Sequences interface {
// GetNextExecutionNumber gets next execution number by test name
GetNextExecutionNumber(ctx context.Context, testName string) (number int32, err error)
// GetNextExecutionNumber gets next execution number by name
GetNextExecutionNumber(ctx context.Context, name string) (number int32, err error)
}

//go:generate mockgen -destination=./mock_output_repository.go -package=result "github.com/kubeshop/testkube/pkg/repository/result" OutputRepository
Expand Down
Loading

0 comments on commit 435e213

Please sign in to comment.