k8s.io/kubernetes@v1.29.3/pkg/registry/core/podtemplate/strategy_test.go (about) 1 /* 2 Copyright 2021 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 podtemplate 18 19 import ( 20 "testing" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 genericapirequest "k8s.io/apiserver/pkg/endpoints/request" 24 api "k8s.io/kubernetes/pkg/apis/core" 25 ) 26 27 func TestStrategy(t *testing.T) { 28 ctx := genericapirequest.NewDefaultContext() 29 if !Strategy.NamespaceScoped() { 30 t.Errorf("must be namespace scoped") 31 } 32 if Strategy.AllowCreateOnUpdate() { 33 t.Errorf("should not allow create on update") 34 } 35 36 podTemplate := &api.PodTemplate{ 37 ObjectMeta: metav1.ObjectMeta{ 38 Name: "mytemplate", 39 Namespace: metav1.NamespaceDefault, 40 Generation: 999, 41 }, 42 Template: api.PodTemplateSpec{ 43 Spec: api.PodSpec{ 44 RestartPolicy: api.RestartPolicyOnFailure, 45 DNSPolicy: api.DNSClusterFirst, 46 Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: api.TerminationMessageReadFile}}, 47 }, 48 }, 49 } 50 51 Strategy.PrepareForCreate(ctx, podTemplate) 52 if podTemplate.Generation != 1 { 53 t.Errorf("expected Generation=1, got %d", podTemplate.Generation) 54 } 55 errs := Strategy.Validate(ctx, podTemplate) 56 if len(errs) != 0 { 57 t.Errorf("Unexpected error validating %v", errs) 58 } 59 60 // ensure we do not change generation for non-spec updates 61 updatedLabel := podTemplate.DeepCopy() 62 updatedLabel.Labels = map[string]string{"a": "true"} 63 Strategy.PrepareForUpdate(ctx, updatedLabel, podTemplate) 64 if updatedLabel.Generation != 1 { 65 t.Errorf("expected Generation=1, got %d", updatedLabel.Generation) 66 } 67 68 updatedTemplate := podTemplate.DeepCopy() 69 updatedTemplate.ResourceVersion = "10" 70 updatedTemplate.Generation = 999 71 updatedTemplate.Template.Spec.RestartPolicy = api.RestartPolicyNever 72 73 // ensure generation is updated for spec changes 74 Strategy.PrepareForUpdate(ctx, updatedTemplate, podTemplate) 75 if updatedTemplate.Generation != 2 { 76 t.Errorf("expected Generation=2, got %d", updatedTemplate.Generation) 77 } 78 errs = Strategy.ValidateUpdate(ctx, updatedTemplate, podTemplate) 79 if len(errs) != 0 { 80 t.Errorf("Unexpected error validating %v", errs) 81 } 82 83 invalidUpdatedTemplate := updatedTemplate.DeepCopy() 84 invalidUpdatedTemplate.Name = "changed" 85 Strategy.PrepareForUpdate(ctx, invalidUpdatedTemplate, podTemplate) 86 errs = Strategy.ValidateUpdate(ctx, invalidUpdatedTemplate, podTemplate) 87 if len(errs) == 0 { 88 t.Errorf("expected error validating, got none") 89 } 90 }