github.com/kubeshop/testkube@v1.17.23/pkg/api/v1/testkube/model_test_upsert_request_extended.go (about)

     1  package testkube
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/adhocore/gronx"
     8  )
     9  
    10  func (test *TestUpsertRequest) QuoteTestTextFields() {
    11  	if test.Description != "" {
    12  		test.Description = fmt.Sprintf("%q", test.Description)
    13  	}
    14  
    15  	if test.Content != nil && test.Content.Data != "" {
    16  		test.Content.Data = fmt.Sprintf("%q", test.Content.Data)
    17  	}
    18  
    19  	if test.Schedule != "" {
    20  		test.Schedule = fmt.Sprintf("%q", test.Schedule)
    21  	}
    22  
    23  	if test.ExecutionRequest != nil {
    24  		var fields = []*string{
    25  			&test.ExecutionRequest.VariablesFile,
    26  			&test.ExecutionRequest.JobTemplate,
    27  			&test.ExecutionRequest.CronJobTemplate,
    28  			&test.ExecutionRequest.PreRunScript,
    29  			&test.ExecutionRequest.PostRunScript,
    30  			&test.ExecutionRequest.PvcTemplate,
    31  			&test.ExecutionRequest.ScraperTemplate,
    32  		}
    33  
    34  		for _, field := range fields {
    35  			if *field != "" {
    36  				*field = fmt.Sprintf("%q", *field)
    37  			}
    38  		}
    39  
    40  		for key, value := range test.ExecutionRequest.Envs {
    41  			if value != "" {
    42  				test.ExecutionRequest.Envs[key] = fmt.Sprintf("%q", value)
    43  			}
    44  		}
    45  
    46  		for key, value := range test.ExecutionRequest.SecretEnvs {
    47  			if value != "" {
    48  				test.ExecutionRequest.SecretEnvs[key] = fmt.Sprintf("%q", value)
    49  			}
    50  		}
    51  
    52  		for key, value := range test.ExecutionRequest.Variables {
    53  			if value.Value != "" {
    54  				value.Value = fmt.Sprintf("%q", value.Value)
    55  				test.ExecutionRequest.Variables[key] = value
    56  			}
    57  		}
    58  
    59  		for i := range test.ExecutionRequest.Args {
    60  			if test.ExecutionRequest.Args[i] != "" {
    61  				test.ExecutionRequest.Args[i] = fmt.Sprintf("%q", test.ExecutionRequest.Args[i])
    62  			}
    63  		}
    64  
    65  		for i := range test.ExecutionRequest.Command {
    66  			if test.ExecutionRequest.Command[i] != "" {
    67  				test.ExecutionRequest.Command[i] = fmt.Sprintf("%q", test.ExecutionRequest.Command[i])
    68  			}
    69  		}
    70  
    71  		if test.ExecutionRequest.SlavePodRequest != nil && test.ExecutionRequest.SlavePodRequest.PodTemplate != "" {
    72  			test.ExecutionRequest.SlavePodRequest.PodTemplate = fmt.Sprintf("%q", test.ExecutionRequest.SlavePodRequest.PodTemplate)
    73  		}
    74  	}
    75  }
    76  
    77  func ValidateUpsertTestRequest(test TestUpsertRequest) error {
    78  	if test.Name == "" {
    79  		return errors.New("test name cannot be empty")
    80  	}
    81  	if test.Type_ == "" {
    82  		return errors.New("test type cannot be empty")
    83  	}
    84  	if test.Content == nil {
    85  		return errors.New("test content cannot be empty")
    86  	}
    87  	if test.Schedule != "" {
    88  		gron := gronx.New()
    89  		if !gron.IsValid(test.Schedule) {
    90  			return errors.New("invalin cron expression in test schedule")
    91  		}
    92  	}
    93  	return nil
    94  }