...
1
16
17 package resourceslice
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 slice = &resource.ResourceSlice{
28 ObjectMeta: metav1.ObjectMeta{
29 Name: "valid-class",
30 },
31 NodeName: "valid-node-name",
32 DriverName: "testdriver.example.com",
33 ResourceModel: resource.ResourceModel{
34 NamedResources: &resource.NamedResourcesResources{},
35 },
36 }
37
38 func TestClassStrategy(t *testing.T) {
39 if Strategy.NamespaceScoped() {
40 t.Errorf("ResourceSlice must not be namespace scoped")
41 }
42 if Strategy.AllowCreateOnUpdate() {
43 t.Errorf("ResourceSlice should not allow create on update")
44 }
45 }
46
47 func TestClassStrategyCreate(t *testing.T) {
48 ctx := genericapirequest.NewDefaultContext()
49 slice := slice.DeepCopy()
50
51 Strategy.PrepareForCreate(ctx, slice)
52 errs := Strategy.Validate(ctx, slice)
53 if len(errs) != 0 {
54 t.Errorf("unexpected error validating for create %v", errs)
55 }
56 }
57
58 func TestClassStrategyUpdate(t *testing.T) {
59 t.Run("no-changes-okay", func(t *testing.T) {
60 ctx := genericapirequest.NewDefaultContext()
61 slice := slice.DeepCopy()
62 newClass := slice.DeepCopy()
63 newClass.ResourceVersion = "4"
64
65 Strategy.PrepareForUpdate(ctx, newClass, slice)
66 errs := Strategy.ValidateUpdate(ctx, newClass, slice)
67 if len(errs) != 0 {
68 t.Errorf("unexpected validation errors: %v", errs)
69 }
70 })
71
72 t.Run("name-change-not-allowed", func(t *testing.T) {
73 ctx := genericapirequest.NewDefaultContext()
74 slice := slice.DeepCopy()
75 newClass := slice.DeepCopy()
76 newClass.Name = "valid-class-2"
77 newClass.ResourceVersion = "4"
78
79 Strategy.PrepareForUpdate(ctx, newClass, slice)
80 errs := Strategy.ValidateUpdate(ctx, newClass, slice)
81 if len(errs) == 0 {
82 t.Errorf("expected a validation error")
83 }
84 })
85 }
86
View as plain text