Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove sandbox/id sequence #5696

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading