...
1
16
17 package podschedulingcontext
18
19 import (
20 "testing"
21
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
24 "k8s.io/kubernetes/pkg/apis/resource"
25 )
26
27 var schedulingCtx = &resource.PodSchedulingContext{
28 ObjectMeta: metav1.ObjectMeta{
29 Name: "valid-pod",
30 Namespace: "default",
31 },
32 Spec: resource.PodSchedulingContextSpec{
33 SelectedNode: "worker",
34 },
35 }
36
37 func TestPodSchedulingStrategy(t *testing.T) {
38 if !Strategy.NamespaceScoped() {
39 t.Errorf("PodSchedulingContext must be namespace scoped")
40 }
41 if Strategy.AllowCreateOnUpdate() {
42 t.Errorf("PodSchedulingContext should not allow create on update")
43 }
44 }
45
46 func TestPodSchedulingStrategyCreate(t *testing.T) {
47 ctx := genericapirequest.NewDefaultContext()
48 schedulingCtx := schedulingCtx.DeepCopy()
49
50 Strategy.PrepareForCreate(ctx, schedulingCtx)
51 errs := Strategy.Validate(ctx, schedulingCtx)
52 if len(errs) != 0 {
53 t.Errorf("unexpected error validating for create %v", errs)
54 }
55 }
56
57 func TestPodSchedulingStrategyUpdate(t *testing.T) {
58 t.Run("no-changes-okay", func(t *testing.T) {
59 ctx := genericapirequest.NewDefaultContext()
60 schedulingCtx := schedulingCtx.DeepCopy()
61 newSchedulingCtx := schedulingCtx.DeepCopy()
62 newSchedulingCtx.ResourceVersion = "4"
63
64 Strategy.PrepareForUpdate(ctx, newSchedulingCtx, schedulingCtx)
65 errs := Strategy.ValidateUpdate(ctx, newSchedulingCtx, schedulingCtx)
66 if len(errs) != 0 {
67 t.Errorf("unexpected validation errors: %v", errs)
68 }
69 })
70
71 t.Run("name-change-not-allowed", func(t *testing.T) {
72 ctx := genericapirequest.NewDefaultContext()
73 schedulingCtx := schedulingCtx.DeepCopy()
74 newSchedulingCtx := schedulingCtx.DeepCopy()
75 newSchedulingCtx.Name = "valid-claim-2"
76 newSchedulingCtx.ResourceVersion = "4"
77
78 Strategy.PrepareForUpdate(ctx, newSchedulingCtx, schedulingCtx)
79 errs := Strategy.ValidateUpdate(ctx, newSchedulingCtx, schedulingCtx)
80 if len(errs) == 0 {
81 t.Errorf("expected a validation error")
82 }
83 })
84 }
85
View as plain text