volcano.sh/volcano@v1.9.0/pkg/webhooks/admission/jobs/mutate/mutate_job_test.go (about)

     1  /*
     2  Copyright 2019 The Volcano Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mutate
    18  
    19  import (
    20  	"testing"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  
    25  	"volcano.sh/apis/pkg/apis/batch/v1alpha1"
    26  )
    27  
    28  func TestCreatePatchExecution(t *testing.T) {
    29  
    30  	namespace := "test"
    31  
    32  	testCase := struct {
    33  		Name      string
    34  		Job       v1alpha1.Job
    35  		operation patchOperation
    36  	}{
    37  		Name: "patch default task name",
    38  		Job: v1alpha1.Job{
    39  			ObjectMeta: metav1.ObjectMeta{
    40  				Name:      "path-task-name",
    41  				Namespace: namespace,
    42  			},
    43  			Spec: v1alpha1.JobSpec{
    44  				MinAvailable: 1,
    45  				Tasks: []v1alpha1.TaskSpec{
    46  					{
    47  						Replicas: 1,
    48  						Template: v1.PodTemplateSpec{
    49  							ObjectMeta: metav1.ObjectMeta{
    50  								Labels: map[string]string{"name": "test"},
    51  							},
    52  							Spec: v1.PodSpec{
    53  								Containers: []v1.Container{
    54  									{
    55  										Name:  "fake-name",
    56  										Image: "busybox:1.24",
    57  									},
    58  								},
    59  							},
    60  						},
    61  					},
    62  					{
    63  						Replicas: 1,
    64  						Template: v1.PodTemplateSpec{
    65  							ObjectMeta: metav1.ObjectMeta{
    66  								Labels: map[string]string{"name": "test"},
    67  							},
    68  							Spec: v1.PodSpec{
    69  								Containers: []v1.Container{
    70  									{
    71  										Name:  "fake-name",
    72  										Image: "busybox:1.24",
    73  									},
    74  								},
    75  							},
    76  						},
    77  					},
    78  				},
    79  			},
    80  		},
    81  		operation: patchOperation{
    82  			Op:   "replace",
    83  			Path: "/spec/tasks",
    84  			Value: []v1alpha1.TaskSpec{
    85  				{
    86  					Name:     v1alpha1.DefaultTaskSpec + "0",
    87  					Replicas: 1,
    88  					Template: v1.PodTemplateSpec{
    89  						ObjectMeta: metav1.ObjectMeta{
    90  							Labels: map[string]string{"name": "test"},
    91  						},
    92  						Spec: v1.PodSpec{
    93  							Containers: []v1.Container{
    94  								{
    95  									Name:  "fake-name",
    96  									Image: "busybox:1.24",
    97  								},
    98  							},
    99  						},
   100  					},
   101  				},
   102  				{
   103  					Name:     v1alpha1.DefaultTaskSpec + "1",
   104  					Replicas: 1,
   105  					Template: v1.PodTemplateSpec{
   106  						ObjectMeta: metav1.ObjectMeta{
   107  							Labels: map[string]string{"name": "test"},
   108  						},
   109  						Spec: v1.PodSpec{
   110  							Containers: []v1.Container{
   111  								{
   112  									Name:  "fake-name",
   113  									Image: "busybox:1.24",
   114  								},
   115  							},
   116  						},
   117  					},
   118  				},
   119  			},
   120  		},
   121  	}
   122  
   123  	ret := mutateSpec(testCase.Job.Spec.Tasks, "/spec/tasks", &testCase.Job)
   124  	if ret.Path != testCase.operation.Path || ret.Op != testCase.operation.Op {
   125  		t.Errorf("testCase %s's expected patch operation %v, but got %v",
   126  			testCase.Name, testCase.operation, *ret)
   127  	}
   128  
   129  	actualTasks, ok := ret.Value.([]v1alpha1.TaskSpec)
   130  	if !ok {
   131  		t.Errorf("testCase '%s' path value expected to be '[]v1alpha1.TaskSpec', but negative",
   132  			testCase.Name)
   133  	}
   134  	expectedTasks, _ := testCase.operation.Value.([]v1alpha1.TaskSpec)
   135  	for index, task := range expectedTasks {
   136  		aTask := actualTasks[index]
   137  		if aTask.Name != task.Name {
   138  			t.Errorf("testCase '%s's expected patch operation with value %v, but got %v",
   139  				testCase.Name, testCase.operation.Value, ret.Value)
   140  		}
   141  		if aTask.MaxRetry != defaultMaxRetry {
   142  			t.Errorf("testCase '%s's expected patch 'task.MaxRetry' with value %v, but got %v",
   143  				testCase.Name, defaultMaxRetry, aTask.MaxRetry)
   144  		}
   145  	}
   146  
   147  }