k8s.io/kubernetes@v1.29.3/pkg/api/job/warnings_test.go (about)

     1  /*
     2  Copyright 2023 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 job
    18  
    19  import (
    20  	"testing"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/kubernetes/pkg/apis/batch"
    24  	"k8s.io/kubernetes/pkg/apis/core"
    25  	"k8s.io/kubernetes/test/utils/ktesting"
    26  	"k8s.io/utils/pointer"
    27  )
    28  
    29  var (
    30  	validSelector = &metav1.LabelSelector{
    31  		MatchLabels: map[string]string{"a": "b"},
    32  	}
    33  
    34  	validPodTemplate = core.PodTemplateSpec{
    35  		ObjectMeta: metav1.ObjectMeta{
    36  			Labels: validSelector.MatchLabels,
    37  		},
    38  		Spec: core.PodSpec{
    39  			RestartPolicy: core.RestartPolicyOnFailure,
    40  			DNSPolicy:     core.DNSClusterFirst,
    41  			Containers:    []core.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: core.TerminationMessageReadFile}},
    42  		},
    43  	}
    44  )
    45  
    46  func TestWarningsForJobSpec(t *testing.T) {
    47  	cases := map[string]struct {
    48  		spec              *batch.JobSpec
    49  		wantWarningsCount int
    50  	}{
    51  		"valid NonIndexed": {
    52  			spec: &batch.JobSpec{
    53  				CompletionMode: completionModePtr(batch.NonIndexedCompletion),
    54  				Template:       validPodTemplate,
    55  			},
    56  		},
    57  		"NondIndexed with high completions and parallelism": {
    58  			spec: &batch.JobSpec{
    59  				CompletionMode: completionModePtr(batch.NonIndexedCompletion),
    60  				Template:       validPodTemplate,
    61  				Parallelism:    pointer.Int32(1_000_000_000),
    62  				Completions:    pointer.Int32(1_000_000_000),
    63  			},
    64  		},
    65  		"invalid PodTemplate": {
    66  			spec: &batch.JobSpec{
    67  				CompletionMode: completionModePtr(batch.NonIndexedCompletion),
    68  				Template: core.PodTemplateSpec{
    69  					Spec: core.PodSpec{ImagePullSecrets: []core.LocalObjectReference{{Name: ""}}},
    70  				},
    71  			},
    72  			wantWarningsCount: 1,
    73  		},
    74  		"valid Indexed low completions low parallelism": {
    75  			spec: &batch.JobSpec{
    76  				CompletionMode: completionModePtr(batch.IndexedCompletion),
    77  				Completions:    pointer.Int32(10_000),
    78  				Parallelism:    pointer.Int32(10_000),
    79  				Template:       validPodTemplate,
    80  			},
    81  		},
    82  		"valid Indexed high completions low parallelism": {
    83  			spec: &batch.JobSpec{
    84  				CompletionMode: completionModePtr(batch.IndexedCompletion),
    85  				Completions:    pointer.Int32(1000_000_000),
    86  				Parallelism:    pointer.Int32(10_000),
    87  				Template:       validPodTemplate,
    88  			},
    89  		},
    90  		"valid Indexed medium completions medium parallelism": {
    91  			spec: &batch.JobSpec{
    92  				CompletionMode: completionModePtr(batch.IndexedCompletion),
    93  				Completions:    pointer.Int32(100_000),
    94  				Parallelism:    pointer.Int32(100_000),
    95  				Template:       validPodTemplate,
    96  			},
    97  		},
    98  		"invalid Indexed high completions high parallelism": {
    99  			spec: &batch.JobSpec{
   100  				CompletionMode: completionModePtr(batch.IndexedCompletion),
   101  				Completions:    pointer.Int32(100_001),
   102  				Parallelism:    pointer.Int32(10_001),
   103  				Template:       validPodTemplate,
   104  			},
   105  			wantWarningsCount: 1,
   106  		},
   107  	}
   108  	for name, tc := range cases {
   109  		t.Run(name, func(t *testing.T) {
   110  			_, ctx := ktesting.NewTestContext(t)
   111  			warnings := WarningsForJobSpec(ctx, nil, tc.spec, nil)
   112  			if len(warnings) != tc.wantWarningsCount {
   113  				t.Errorf("Got %d warnings, want %d.\nWarnings: %v", len(warnings), tc.wantWarningsCount, warnings)
   114  			}
   115  		})
   116  	}
   117  }
   118  
   119  func completionModePtr(m batch.CompletionMode) *batch.CompletionMode {
   120  	return &m
   121  }