...

Source file src/k8s.io/kubernetes/pkg/apis/batch/v1beta1/defaults_test.go

Documentation: k8s.io/kubernetes/pkg/apis/batch/v1beta1

     1  /*
     2  Copyright 2017 The Kubernetes 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 v1beta1_test
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	batchv1beta1 "k8s.io/api/batch/v1beta1"
    24  
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    27  	_ "k8s.io/kubernetes/pkg/apis/batch/install"
    28  	. "k8s.io/kubernetes/pkg/apis/batch/v1beta1"
    29  	_ "k8s.io/kubernetes/pkg/apis/core/install"
    30  	utilpointer "k8s.io/utils/pointer"
    31  )
    32  
    33  func TestSetDefaultCronJob(t *testing.T) {
    34  	tests := map[string]struct {
    35  		original *batchv1beta1.CronJob
    36  		expected *batchv1beta1.CronJob
    37  	}{
    38  		"empty batchv1beta1.CronJob should default batchv1beta1.ConcurrencyPolicy and Suspend": {
    39  			original: &batchv1beta1.CronJob{},
    40  			expected: &batchv1beta1.CronJob{
    41  				Spec: batchv1beta1.CronJobSpec{
    42  					ConcurrencyPolicy:          batchv1beta1.AllowConcurrent,
    43  					Suspend:                    newBool(false),
    44  					SuccessfulJobsHistoryLimit: utilpointer.Int32(3),
    45  					FailedJobsHistoryLimit:     utilpointer.Int32(1),
    46  				},
    47  			},
    48  		},
    49  		"set fields should not be defaulted": {
    50  			original: &batchv1beta1.CronJob{
    51  				Spec: batchv1beta1.CronJobSpec{
    52  					ConcurrencyPolicy:          batchv1beta1.ForbidConcurrent,
    53  					Suspend:                    newBool(true),
    54  					SuccessfulJobsHistoryLimit: utilpointer.Int32(5),
    55  					FailedJobsHistoryLimit:     utilpointer.Int32(5),
    56  				},
    57  			},
    58  			expected: &batchv1beta1.CronJob{
    59  				Spec: batchv1beta1.CronJobSpec{
    60  					ConcurrencyPolicy:          batchv1beta1.ForbidConcurrent,
    61  					Suspend:                    newBool(true),
    62  					SuccessfulJobsHistoryLimit: utilpointer.Int32(5),
    63  					FailedJobsHistoryLimit:     utilpointer.Int32(5),
    64  				},
    65  			},
    66  		},
    67  	}
    68  
    69  	for name, test := range tests {
    70  		original := test.original
    71  		expected := test.expected
    72  		obj2 := roundTrip(t, runtime.Object(original))
    73  		actual, ok := obj2.(*batchv1beta1.CronJob)
    74  		if !ok {
    75  			t.Errorf("%s: unexpected object: %v", name, actual)
    76  			t.FailNow()
    77  		}
    78  		if actual.Spec.ConcurrencyPolicy != expected.Spec.ConcurrencyPolicy {
    79  			t.Errorf("%s: got different concurrencyPolicy than expected: %v %v", name, actual.Spec.ConcurrencyPolicy, expected.Spec.ConcurrencyPolicy)
    80  		}
    81  		if *actual.Spec.Suspend != *expected.Spec.Suspend {
    82  			t.Errorf("%s: got different suspend than expected: %v %v", name, *actual.Spec.Suspend, *expected.Spec.Suspend)
    83  		}
    84  		if *actual.Spec.SuccessfulJobsHistoryLimit != *expected.Spec.SuccessfulJobsHistoryLimit {
    85  			t.Errorf("%s: got different successfulJobsHistoryLimit than expected: %v %v", name, *actual.Spec.SuccessfulJobsHistoryLimit, *expected.Spec.SuccessfulJobsHistoryLimit)
    86  		}
    87  		if *actual.Spec.FailedJobsHistoryLimit != *expected.Spec.FailedJobsHistoryLimit {
    88  			t.Errorf("%s: got different failedJobsHistoryLimit than expected: %v %v", name, *actual.Spec.FailedJobsHistoryLimit, *expected.Spec.FailedJobsHistoryLimit)
    89  		}
    90  	}
    91  }
    92  
    93  func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
    94  	data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
    95  	if err != nil {
    96  		t.Errorf("%v\n %#v", err, obj)
    97  		return nil
    98  	}
    99  	obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
   100  	if err != nil {
   101  		t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
   102  		return nil
   103  	}
   104  	obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
   105  	err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
   106  	if err != nil {
   107  		t.Errorf("%v\nSource: %#v", err, obj2)
   108  		return nil
   109  	}
   110  	return obj3
   111  }
   112  
   113  func newBool(val bool) *bool {
   114  	p := new(bool)
   115  	*p = val
   116  	return p
   117  }
   118  

View as plain text