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

     1  package testkube
     2  
     3  import (
     4  	"encoding/csv"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  )
     9  
    10  const (
    11  	//TestLabelTestType is a test label for a test type
    12  	TestLabelTestType = "test-type"
    13  	// TestLabelExecutor is a test label for an executor
    14  	TestLabelExecutor = "executor"
    15  	// TestLabelTestName is a test label for a test name
    16  	TestLabelTestName = "test-name"
    17  )
    18  
    19  type Tests []Test
    20  
    21  func (t Tests) Table() (header []string, output [][]string) {
    22  	header = []string{"Name", "Description", "Type", "Created", "Labels", "Schedule"}
    23  	for _, e := range t {
    24  		output = append(output, []string{
    25  			e.Name,
    26  			e.Description,
    27  			e.Type_,
    28  			e.Created.String(),
    29  			MapToString(e.Labels),
    30  			e.Schedule,
    31  		})
    32  	}
    33  
    34  	return
    35  }
    36  
    37  func (t Test) GetObjectRef(namespace string) *ObjectRef {
    38  	return &ObjectRef{
    39  		Name:      t.Name,
    40  		Namespace: namespace,
    41  	}
    42  }
    43  
    44  func PrepareExecutorArgs(binaryArgs []string) ([]string, error) {
    45  	executorArgs := make([]string, 0)
    46  	for _, arg := range binaryArgs {
    47  		r := csv.NewReader(strings.NewReader(arg))
    48  		r.Comma = ' '
    49  		r.LazyQuotes = true
    50  		r.TrimLeadingSpace = true
    51  
    52  		records, err := r.ReadAll()
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  
    57  		if len(records) != 1 {
    58  			return nil, errors.New("single string expected")
    59  		}
    60  
    61  		executorArgs = append(executorArgs, records[0]...)
    62  	}
    63  
    64  	return executorArgs, nil
    65  }
    66  
    67  func (test *Test) QuoteTestTextFields() {
    68  	if test.Description != "" {
    69  		test.Description = fmt.Sprintf("%q", test.Description)
    70  	}
    71  
    72  	if test.Content != nil && test.Content.Data != "" {
    73  		test.Content.Data = fmt.Sprintf("%q", test.Content.Data)
    74  	}
    75  
    76  	if test.Schedule != "" {
    77  		test.Schedule = fmt.Sprintf("%q", test.Schedule)
    78  	}
    79  
    80  	if test.ExecutionRequest != nil {
    81  		var fields = []*string{
    82  			&test.ExecutionRequest.VariablesFile,
    83  			&test.ExecutionRequest.JobTemplate,
    84  			&test.ExecutionRequest.CronJobTemplate,
    85  			&test.ExecutionRequest.PreRunScript,
    86  			&test.ExecutionRequest.PostRunScript,
    87  			&test.ExecutionRequest.PvcTemplate,
    88  			&test.ExecutionRequest.ScraperTemplate,
    89  		}
    90  
    91  		for _, field := range fields {
    92  			if *field != "" {
    93  				*field = fmt.Sprintf("%q", *field)
    94  			}
    95  		}
    96  
    97  		for key, value := range test.ExecutionRequest.Envs {
    98  			if value != "" {
    99  				test.ExecutionRequest.Envs[key] = fmt.Sprintf("%q", value)
   100  			}
   101  		}
   102  
   103  		for key, value := range test.ExecutionRequest.SecretEnvs {
   104  			if value != "" {
   105  				test.ExecutionRequest.SecretEnvs[key] = fmt.Sprintf("%q", value)
   106  			}
   107  		}
   108  
   109  		for key, value := range test.ExecutionRequest.Variables {
   110  			if value.Value != "" {
   111  				value.Value = fmt.Sprintf("%q", value.Value)
   112  				test.ExecutionRequest.Variables[key] = value
   113  			}
   114  		}
   115  
   116  		for i := range test.ExecutionRequest.Args {
   117  			if test.ExecutionRequest.Args[i] != "" {
   118  				test.ExecutionRequest.Args[i] = fmt.Sprintf("%q", test.ExecutionRequest.Args[i])
   119  			}
   120  		}
   121  
   122  		for i := range test.ExecutionRequest.Command {
   123  			if test.ExecutionRequest.Command[i] != "" {
   124  				test.ExecutionRequest.Command[i] = fmt.Sprintf("%q", test.ExecutionRequest.Command[i])
   125  			}
   126  		}
   127  
   128  		if test.ExecutionRequest.SlavePodRequest != nil && test.ExecutionRequest.SlavePodRequest.PodTemplate != "" {
   129  			test.ExecutionRequest.SlavePodRequest.PodTemplate = fmt.Sprintf("%q", test.ExecutionRequest.SlavePodRequest.PodTemplate)
   130  		}
   131  	}
   132  }