github.com/kubeshop/testkube@v1.17.23/pkg/scheduler/test_scheduler_test.go (about)

     1  package scheduler
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/golang/mock/gomock"
     7  	"github.com/stretchr/testify/assert"
     8  	k8sv1 "k8s.io/api/core/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	v1 "github.com/kubeshop/testkube-operator/api/executor/v1"
    12  	testsv3 "github.com/kubeshop/testkube-operator/api/tests/v3"
    13  	executorsclientv1 "github.com/kubeshop/testkube-operator/pkg/client/executors/v1"
    14  	testsclientv3 "github.com/kubeshop/testkube-operator/pkg/client/tests/v3"
    15  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    16  	"github.com/kubeshop/testkube/pkg/configmap"
    17  	"github.com/kubeshop/testkube/pkg/executor/client"
    18  	"github.com/kubeshop/testkube/pkg/log"
    19  	"github.com/kubeshop/testkube/pkg/secret"
    20  )
    21  
    22  func TestParamsNilAssign(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	t.Run("merge two maps", func(t *testing.T) {
    26  		t.Parallel()
    27  
    28  		p1 := map[string]testkube.Variable{"p1": testkube.NewBasicVariable("p1", "1")}
    29  		p2 := map[string]testkube.Variable{"p2": testkube.NewBasicVariable("p2", "2")}
    30  
    31  		out := mergeVariables(p1, p2)
    32  
    33  		assert.Equal(t, 2, len(out))
    34  		assert.Equal(t, "1", out["p1"].Value)
    35  	})
    36  
    37  	t.Run("merge two maps with override", func(t *testing.T) {
    38  		t.Parallel()
    39  
    40  		p1 := map[string]testkube.Variable{"p1": testkube.NewBasicVariable("p1", "1")}
    41  		p2 := map[string]testkube.Variable{"p1": testkube.NewBasicVariable("p1", "2")}
    42  
    43  		out := mergeVariables(p1, p2)
    44  
    45  		assert.Equal(t, 1, len(out))
    46  		assert.Equal(t, "2", out["p1"].Value)
    47  	})
    48  
    49  	t.Run("merge with nil map", func(t *testing.T) {
    50  		t.Parallel()
    51  
    52  		p2 := map[string]testkube.Variable{"p2": testkube.NewBasicVariable("p2", "2")}
    53  
    54  		out := mergeVariables(nil, p2)
    55  
    56  		assert.Equal(t, 1, len(out))
    57  		assert.Equal(t, "2", out["p2"].Value)
    58  	})
    59  
    60  }
    61  
    62  func TestGetExecuteOptions(t *testing.T) {
    63  	t.Parallel()
    64  
    65  	mockCtrl := gomock.NewController(t)
    66  	defer mockCtrl.Finish()
    67  
    68  	mockTestsClient := testsclientv3.NewMockInterface(mockCtrl)
    69  	mockExecutorsClient := executorsclientv1.NewMockInterface(mockCtrl)
    70  	mockSecretClient := secret.NewMockInterface(mockCtrl)
    71  	mockConfigMapClient := configmap.NewMockInterface(mockCtrl)
    72  
    73  	sc := Scheduler{
    74  		testsClient:     mockTestsClient,
    75  		executorsClient: mockExecutorsClient,
    76  		logger:          log.DefaultLogger,
    77  		secretClient:    mockSecretClient,
    78  		configMapClient: mockConfigMapClient,
    79  	}
    80  
    81  	mockTest := testsv3.Test{
    82  		ObjectMeta: metav1.ObjectMeta{Namespace: "testkube", Name: "some-test"},
    83  		Spec: testsv3.TestSpec{
    84  			Type_: "cypress",
    85  			ExecutionRequest: &testsv3.ExecutionRequest{
    86  				Name:   "some-custom-execution",
    87  				Number: 1,
    88  				Image:  "test-image",
    89  			},
    90  		},
    91  	}
    92  	mockExecutorTypes := "cypress"
    93  	mockExecutor := v1.Executor{
    94  		TypeMeta:   metav1.TypeMeta{},
    95  		ObjectMeta: metav1.ObjectMeta{Namespace: "testkube", Name: "cypress"},
    96  		Spec: v1.ExecutorSpec{
    97  			Types:                  []string{mockExecutorTypes},
    98  			ExecutorType:           "job",
    99  			URI:                    "",
   100  			Image:                  "cypress",
   101  			Args:                   []string{},
   102  			Command:                []string{"run"},
   103  			ImagePullSecrets:       []k8sv1.LocalObjectReference{{Name: "secret-name1"}, {Name: "secret-name2"}},
   104  			Features:               nil,
   105  			ContentTypes:           nil,
   106  			JobTemplate:            "",
   107  			JobTemplateReference:   "",
   108  			Meta:                   nil,
   109  			UseDataDirAsWorkingDir: false,
   110  		},
   111  	}
   112  
   113  	mockTestsClient.EXPECT().Get("id").Return(&mockTest, nil).Times(1)
   114  	mockExecutorsClient.EXPECT().GetByType(mockExecutorTypes).Return(&mockExecutor, nil)
   115  	mockConfigMapClient.EXPECT().Get(gomock.Any(), "configmap", "namespace").Times(1)
   116  
   117  	req := testkube.ExecutionRequest{
   118  		Name:             "id-1",
   119  		Number:           1,
   120  		ExecutionLabels:  map[string]string{"label": "value"},
   121  		Namespace:        "namespace",
   122  		VariablesFile:    "",
   123  		Variables:        map[string]testkube.Variable{"var": {Name: "one"}},
   124  		Command:          []string{"run"},
   125  		Args:             []string{},
   126  		ArgsMode:         "",
   127  		Image:            "executor-image",
   128  		ImagePullSecrets: []testkube.LocalObjectReference{},
   129  		Envs: map[string]string{
   130  			"env": "var",
   131  		},
   132  		SecretEnvs: map[string]string{
   133  			"secretEnv": "secretVar",
   134  		},
   135  		Sync:                               false,
   136  		HttpProxy:                          "",
   137  		HttpsProxy:                         "",
   138  		Uploads:                            []string{},
   139  		ActiveDeadlineSeconds:              10,
   140  		ArtifactRequest:                    &testkube.ArtifactRequest{},
   141  		JobTemplate:                        "",
   142  		JobTemplateReference:               "",
   143  		CronJobTemplate:                    "",
   144  		CronJobTemplateReference:           "",
   145  		PreRunScript:                       "",
   146  		PostRunScript:                      "",
   147  		ExecutePostRunScriptBeforeScraping: true,
   148  		SourceScripts:                      true,
   149  		ScraperTemplate:                    "",
   150  		ScraperTemplateReference:           "",
   151  		PvcTemplate:                        "",
   152  		PvcTemplateReference:               "",
   153  		EnvConfigMaps: []testkube.EnvReference{
   154  			{
   155  				Reference: &testkube.LocalObjectReference{
   156  					Name: "configmap",
   157  				},
   158  				Mount:          true,
   159  				MountPath:      "/data",
   160  				MapToVariables: true,
   161  			},
   162  		},
   163  		EnvSecrets: []testkube.EnvReference{
   164  			{
   165  				Reference: &testkube.LocalObjectReference{
   166  					Name: "secret-1",
   167  				},
   168  				Mount:          true,
   169  				MountPath:      "/data",
   170  				MapToVariables: false,
   171  			},
   172  		},
   173  		RunningContext: &testkube.RunningContext{
   174  			Type_: string(testkube.RunningContextTypeUserCLI),
   175  		},
   176  		TestExecutionName: "",
   177  		SlavePodRequest:   &testkube.PodRequest{},
   178  	}
   179  
   180  	got, err := sc.getExecuteOptions("namespace", "id", req)
   181  	assert.NoError(t, err)
   182  
   183  	want := client.ExecuteOptions{
   184  		ID:                   "",
   185  		TestName:             "id",
   186  		Namespace:            "namespace",
   187  		TestSpec:             mockTest.Spec,
   188  		ExecutorName:         "cypress",
   189  		ExecutorSpec:         mockExecutor.Spec,
   190  		Request:              req,
   191  		Sync:                 false,
   192  		Labels:               map[string]string(nil),
   193  		ImagePullSecretNames: []string{"secret-name1", "secret-name2"},
   194  	}
   195  
   196  	assert.Equal(t, want, got)
   197  }