knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/podspec_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  	corev1 "k8s.io/api/core/v1"
    24  	"knative.dev/pkg/apis"
    25  )
    26  
    27  func TestPodSpecValidation(t *testing.T) {
    28  	tests := []struct {
    29  		name string
    30  		with func(context.Context) context.Context
    31  		want *apis.FieldError
    32  	}{{
    33  		name: "no check",
    34  		with: func(ctx context.Context) context.Context {
    35  			return ctx
    36  		},
    37  		want: nil,
    38  	}, {
    39  		name: "no error",
    40  		with: func(ctx context.Context) context.Context {
    41  			return WithPodSpecValidator(ctx, func(ctx context.Context, wp *WithPod) *apis.FieldError {
    42  				return nil
    43  			})
    44  		},
    45  		want: nil,
    46  	}, {
    47  		name: "no busybox",
    48  		with: func(ctx context.Context) context.Context {
    49  			return WithPodSpecValidator(ctx, func(ctx context.Context, wp *WithPod) *apis.FieldError {
    50  				for i, c := range wp.Spec.Template.Spec.InitContainers {
    51  					if c.Image == "busybox" {
    52  						return apis.ErrInvalidValue(c.Image, "image").ViaFieldIndex("spec.template.spec.initContainers", i)
    53  					}
    54  				}
    55  				for i, c := range wp.Spec.Template.Spec.Containers {
    56  					if c.Image == "busybox" {
    57  						return apis.ErrInvalidValue(c.Image, "image").ViaFieldIndex("spec.template.spec.containers", i)
    58  					}
    59  				}
    60  				return nil
    61  			})
    62  		},
    63  		want: apis.ErrInvalidValue("busybox", "spec.template.spec.containers[0].image"),
    64  	}}
    65  
    66  	for _, test := range tests {
    67  		t.Run(test.name, func(t *testing.T) {
    68  			p := WithPod{
    69  				Spec: WithPodSpec{
    70  					Template: PodSpecable{
    71  						Spec: corev1.PodSpec{
    72  							Containers: []corev1.Container{{
    73  								Name:  "blah",
    74  								Image: "busybox",
    75  							}},
    76  						},
    77  					},
    78  				},
    79  			}
    80  			ctx := test.with(context.Background())
    81  			got := p.Validate(ctx)
    82  			if test.want.Error() != got.Error() {
    83  				t.Errorf("Validate() = %v, wanted %v", got, test.want)
    84  			}
    85  		})
    86  	}
    87  }
    88  
    89  func TestPodValidation(t *testing.T) {
    90  	tests := []struct {
    91  		name string
    92  		with func(context.Context) context.Context
    93  		want *apis.FieldError
    94  	}{{
    95  		name: "no check",
    96  		with: func(ctx context.Context) context.Context {
    97  			return ctx
    98  		},
    99  		want: nil,
   100  	}, {
   101  		name: "no error",
   102  		with: func(ctx context.Context) context.Context {
   103  			return WithPodValidator(ctx, func(ctx context.Context, wp *Pod) *apis.FieldError {
   104  				return nil
   105  			})
   106  		},
   107  		want: nil,
   108  	}, {
   109  		name: "no busybox",
   110  		with: func(ctx context.Context) context.Context {
   111  			return WithPodValidator(ctx, func(ctx context.Context, wp *Pod) *apis.FieldError {
   112  				for i, c := range wp.Spec.InitContainers {
   113  					if c.Image == "busybox" {
   114  						return apis.ErrInvalidValue(c.Image, "image").ViaFieldIndex("spec.template.spec.initContainers", i)
   115  					}
   116  				}
   117  				for i, c := range wp.Spec.Containers {
   118  					if c.Image == "busybox" {
   119  						return apis.ErrInvalidValue(c.Image, "image").ViaFieldIndex("spec.template.spec.containers", i)
   120  					}
   121  				}
   122  				return nil
   123  			})
   124  		},
   125  		want: apis.ErrInvalidValue("busybox", "spec.template.spec.containers[0].image"),
   126  	}}
   127  
   128  	for _, test := range tests {
   129  		t.Run(test.name, func(t *testing.T) {
   130  			p := Pod{
   131  				Spec: corev1.PodSpec{
   132  					Containers: []corev1.Container{{
   133  						Name:  "blah",
   134  						Image: "busybox",
   135  					}},
   136  				},
   137  			}
   138  			ctx := test.with(context.Background())
   139  			got := p.Validate(ctx)
   140  			if test.want.Error() != got.Error() {
   141  				t.Errorf("Validate() = %v, wanted %v", got, test.want)
   142  			}
   143  		})
   144  	}
   145  }