1
+ package crd
2
+
3
+ import (
4
+ "github.com/stretchr/testify/require"
5
+ apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
6
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7
+ "testing"
8
+ )
9
+
10
+ const crdName = "test"
11
+
12
+ func TestSafeStorageVersionUpgradeFailure (t * testing.T ) {
13
+ existingCRD := & apiextensionsv1.CustomResourceDefinition {
14
+ ObjectMeta : metav1.ObjectMeta {
15
+ Name : crdName ,
16
+ },
17
+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
18
+ Group : "cluster.com" ,
19
+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
20
+ {
21
+ Name : "v1alpha2" ,
22
+ Served : true ,
23
+ Storage : true ,
24
+ Schema : & apiextensionsv1.CustomResourceValidation {
25
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
26
+ Type : "object" ,
27
+ Description : "my crd schema" ,
28
+ },
29
+ },
30
+ },
31
+ },
32
+ },
33
+ Status : apiextensionsv1.CustomResourceDefinitionStatus {
34
+ StoredVersions : []string {"v1alpha2" },
35
+ },
36
+ }
37
+
38
+ newCRD := & apiextensionsv1.CustomResourceDefinition {
39
+ ObjectMeta : metav1.ObjectMeta {
40
+ Name : crdName ,
41
+ },
42
+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
43
+ Group : "cluster.com" ,
44
+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
45
+ {
46
+ Name : "v1alpha3" ,
47
+ Served : true ,
48
+ Storage : true ,
49
+ Schema : & apiextensionsv1.CustomResourceValidation {
50
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
51
+ Type : "object" ,
52
+ Description : "my crd schema" ,
53
+ },
54
+ },
55
+ },
56
+ },
57
+ },
58
+ }
59
+
60
+ safe , err := SafeStorageVersionUpgrade (existingCRD , newCRD )
61
+ // expect safe to be false, since crd upgrade is not safe
62
+ require .False (t , safe )
63
+ // expect error, since crd upgrade is not safe
64
+ require .Error (t , err , "expected error for unsafe CRD upgrade" )
65
+ // error should be related to the storage upgrade removing a version
66
+ require .Contains (t , err .Error (), "new CRD removes version" , "expected storage version error" )
67
+ }
68
+
69
+ func TestSafeStorageVersionUpgradeSuccess (t * testing.T ) {
70
+ existingCRD := & apiextensionsv1.CustomResourceDefinition {
71
+ ObjectMeta : metav1.ObjectMeta {
72
+ Name : crdName ,
73
+ },
74
+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
75
+ Group : "cluster.com" ,
76
+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
77
+ {
78
+ Name : "v1alpha2" ,
79
+ Served : true ,
80
+ Storage : false ,
81
+ Schema : & apiextensionsv1.CustomResourceValidation {
82
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
83
+ Type : "object" ,
84
+ Description : "my crd schema" ,
85
+ },
86
+ },
87
+ },
88
+ {
89
+ Name : "v1alpha3" ,
90
+ Served : true ,
91
+ Storage : true ,
92
+ Schema : & apiextensionsv1.CustomResourceValidation {
93
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
94
+ Type : "object" ,
95
+ Description : "my crd schema" ,
96
+ },
97
+ },
98
+ },
99
+ },
100
+ },
101
+ Status : apiextensionsv1.CustomResourceDefinitionStatus {
102
+ StoredVersions : []string {"v1alpha2" , "v1alpha3" },
103
+ },
104
+ }
105
+
106
+ newCRD := & apiextensionsv1.CustomResourceDefinition {
107
+ ObjectMeta : metav1.ObjectMeta {
108
+ Name : crdName ,
109
+ },
110
+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
111
+ Group : "cluster.com" ,
112
+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
113
+ {
114
+ Name : "v1alpha2" ,
115
+ Served : true ,
116
+ Storage : false ,
117
+ Schema : & apiextensionsv1.CustomResourceValidation {
118
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
119
+ Type : "object" ,
120
+ Description : "my crd schema" ,
121
+ },
122
+ },
123
+ },
124
+ {
125
+ Name : "v1alpha3" ,
126
+ Served : true ,
127
+ Storage : true ,
128
+ Schema : & apiextensionsv1.CustomResourceValidation {
129
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
130
+ Type : "object" ,
131
+ Description : "my crd schema" ,
132
+ },
133
+ },
134
+ },
135
+ },
136
+ },
137
+ }
138
+
139
+ safe , err := SafeStorageVersionUpgrade (existingCRD , newCRD )
140
+ // expect safe to be true, since crd upgrade is safe
141
+ require .True (t , safe )
142
+ // expect no error, since crd upgrade is safe
143
+ require .NoError (t , err , "did not expect error for safe CRD upgrade" )
144
+ }
0 commit comments