...
1
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
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
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 }
91
View as plain text