knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/podspec_defaults_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  	"strings"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	corev1 "k8s.io/api/core/v1"
    26  )
    27  
    28  func TestPodSpecDefaulting(t *testing.T) {
    29  	p := WithPod{
    30  		Spec: WithPodSpec{
    31  			Template: PodSpecable{
    32  				Spec: corev1.PodSpec{
    33  					Containers: []corev1.Container{{
    34  						Name:  "blah",
    35  						Image: "busybox",
    36  					}},
    37  				},
    38  			},
    39  		},
    40  	}
    41  
    42  	tests := []struct {
    43  		name string
    44  		with func(context.Context) context.Context
    45  		want *WithPod
    46  	}{{
    47  		name: "no check",
    48  		with: func(ctx context.Context) context.Context {
    49  			return ctx
    50  		},
    51  		want: p.DeepCopy(),
    52  	}, {
    53  		name: "no change",
    54  		with: func(ctx context.Context) context.Context {
    55  			return WithPodSpecDefaulter(ctx, func(ctx context.Context, wp *WithPod) {
    56  			})
    57  		},
    58  		want: p.DeepCopy(),
    59  	}, {
    60  		name: "no busybox",
    61  		with: func(ctx context.Context) context.Context {
    62  			return WithPodSpecDefaulter(ctx, func(ctx context.Context, wp *WithPod) {
    63  				for i, c := range wp.Spec.Template.Spec.InitContainers {
    64  					if !strings.Contains(c.Image, "@") {
    65  						wp.Spec.Template.Spec.InitContainers[i].Image = c.Image + "@sha256:deadbeef"
    66  					}
    67  				}
    68  				for i, c := range wp.Spec.Template.Spec.Containers {
    69  					if !strings.Contains(c.Image, "@") {
    70  						wp.Spec.Template.Spec.Containers[i].Image = c.Image + "@sha256:deadbeef"
    71  					}
    72  				}
    73  			})
    74  		},
    75  		want: &WithPod{
    76  			Spec: WithPodSpec{
    77  				Template: PodSpecable{
    78  					Spec: corev1.PodSpec{
    79  						Containers: []corev1.Container{{
    80  							Name:  "blah",
    81  							Image: "busybox@sha256:deadbeef",
    82  						}},
    83  					},
    84  				},
    85  			},
    86  		},
    87  	}}
    88  
    89  	for _, test := range tests {
    90  		t.Run(test.name, func(t *testing.T) {
    91  			ctx := test.with(context.Background())
    92  			got := p.DeepCopy()
    93  			got.SetDefaults(ctx)
    94  			if !cmp.Equal(test.want, got) {
    95  				t.Errorf("SetDefaults (-want, +got) = %s", cmp.Diff(test.want, got))
    96  			}
    97  		})
    98  	}
    99  }
   100  
   101  func TestPodDefaulting(t *testing.T) {
   102  	p := Pod{
   103  		Spec: corev1.PodSpec{
   104  			Containers: []corev1.Container{{
   105  				Name:  "blah",
   106  				Image: "busybox",
   107  			}},
   108  		},
   109  	}
   110  
   111  	tests := []struct {
   112  		name string
   113  		with func(context.Context) context.Context
   114  		want *Pod
   115  	}{{
   116  		name: "no check",
   117  		with: func(ctx context.Context) context.Context {
   118  			return ctx
   119  		},
   120  		want: p.DeepCopy(),
   121  	}, {
   122  		name: "no change",
   123  		with: func(ctx context.Context) context.Context {
   124  			return WithPodDefaulter(ctx, func(ctx context.Context, wp *Pod) {
   125  			})
   126  		},
   127  		want: p.DeepCopy(),
   128  	}, {
   129  		name: "no busybox",
   130  		with: func(ctx context.Context) context.Context {
   131  			return WithPodDefaulter(ctx, func(ctx context.Context, wp *Pod) {
   132  				for i, c := range wp.Spec.InitContainers {
   133  					if !strings.Contains(c.Image, "@") {
   134  						wp.Spec.InitContainers[i].Image = c.Image + "@sha256:deadbeef"
   135  					}
   136  				}
   137  				for i, c := range wp.Spec.Containers {
   138  					if !strings.Contains(c.Image, "@") {
   139  						wp.Spec.Containers[i].Image = c.Image + "@sha256:deadbeef"
   140  					}
   141  				}
   142  			})
   143  		},
   144  		want: &Pod{
   145  			Spec: corev1.PodSpec{
   146  				Containers: []corev1.Container{{
   147  					Name:  "blah",
   148  					Image: "busybox@sha256:deadbeef",
   149  				}},
   150  			},
   151  		},
   152  	}}
   153  
   154  	for _, test := range tests {
   155  		t.Run(test.name, func(t *testing.T) {
   156  			ctx := test.with(context.Background())
   157  			got := p.DeepCopy()
   158  			got.SetDefaults(ctx)
   159  			if !cmp.Equal(test.want, got) {
   160  				t.Errorf("SetDefaults (-want, +got) = %s", cmp.Diff(test.want, got))
   161  			}
   162  		})
   163  	}
   164  }