knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/cronjob_validation_test.go (about) 1 /* 2 Copyright 2021 The Knative 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 v1 18 19 import ( 20 "context" 21 "testing" 22 23 batchv1 "k8s.io/api/batch/v1" 24 corev1 "k8s.io/api/core/v1" 25 "knative.dev/pkg/apis" 26 ) 27 28 func TestCronJobValidation(t *testing.T) { 29 tests := []struct { 30 name string 31 with func(context.Context) context.Context 32 want *apis.FieldError 33 }{{ 34 name: "no check", 35 with: func(ctx context.Context) context.Context { 36 return ctx 37 }, 38 want: nil, 39 }, { 40 name: "no error", 41 with: func(ctx context.Context) context.Context { 42 return WithCronJobValidator(ctx, func(ctx context.Context, c *CronJob) *apis.FieldError { 43 return nil 44 }) 45 }, 46 want: nil, 47 }, { 48 name: "no busybox", 49 with: func(ctx context.Context) context.Context { 50 return WithCronJobValidator(ctx, func(ctx context.Context, c *CronJob) *apis.FieldError { 51 for i, con := range c.Spec.JobTemplate.Spec.Template.Spec.InitContainers { 52 if con.Image == "busybox" { 53 return apis.ErrInvalidValue(con.Image, "image").ViaFieldIndex("spec.template.spec.initContainers", i) 54 } 55 } 56 for i, con := range c.Spec.JobTemplate.Spec.Template.Spec.Containers { 57 if con.Image == "busybox" { 58 return apis.ErrInvalidValue(con.Image, "image").ViaFieldIndex("spec.template.spec.containers", i) 59 } 60 } 61 return nil 62 }) 63 }, 64 want: apis.ErrInvalidValue("busybox", "spec.template.spec.containers[0].image"), 65 }} 66 67 for _, test := range tests { 68 t.Run(test.name, func(t *testing.T) { 69 c := CronJob{ 70 Spec: batchv1.CronJobSpec{ 71 JobTemplate: batchv1.JobTemplateSpec{ 72 Spec: batchv1.JobSpec{ 73 Template: corev1.PodTemplateSpec{ 74 Spec: corev1.PodSpec{ 75 Containers: []corev1.Container{{ 76 Name: "blah", 77 Image: "busybox", 78 }}, 79 }, 80 }, 81 }, 82 }, 83 }, 84 } 85 ctx := test.with(context.Background()) 86 got := c.Validate(ctx) 87 if test.want.Error() != got.Error() { 88 t.Errorf("Validate() = %v, wanted %v", got, test.want) 89 } 90 }) 91 } 92 }