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 , //stored at v1alpha1
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
+ err := SafeStorageVersionUpgrade (existingCRD , newCRD )
61
+ // expect error, since crd upgrade is not safe
62
+ require .Error (t , err , "expected error for unsafe CRD upgrade" )
63
+
64
+ // error should be related to the storage upgrade removing a version
65
+ require .Contains (t , err .Error (), "new CRD removes version" , "expected storage version error" )
66
+ }
67
+
68
+ func TestSafeStorageVersionUpgradeSuccess (t * testing.T ) {
69
+ existingCRD := & apiextensionsv1.CustomResourceDefinition {
70
+ ObjectMeta : metav1.ObjectMeta {
71
+ Name : crdName ,
72
+ },
73
+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
74
+ Group : "cluster.com" ,
75
+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
76
+ {
77
+ Name : "v1alpha2" ,
78
+ Served : true ,
79
+ Storage : false ,
80
+ Schema : & apiextensionsv1.CustomResourceValidation {
81
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
82
+ Type : "object" ,
83
+ Description : "my crd schema" ,
84
+ },
85
+ },
86
+ },
87
+ {
88
+ Name : "v1alpha3" ,
89
+ Served : true ,
90
+ Storage : true , //stored at v1alpha1
91
+ Schema : & apiextensionsv1.CustomResourceValidation {
92
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
93
+ Type : "object" ,
94
+ Description : "my crd schema" ,
95
+ },
96
+ },
97
+ },
98
+ },
99
+ },
100
+ Status : apiextensionsv1.CustomResourceDefinitionStatus {
101
+ StoredVersions : []string {"v1alpha2" , "v1alpha3" },
102
+ },
103
+ }
104
+
105
+ newCRD := & apiextensionsv1.CustomResourceDefinition {
106
+ ObjectMeta : metav1.ObjectMeta {
107
+ Name : crdName ,
108
+ },
109
+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
110
+ Group : "cluster.com" ,
111
+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
112
+ {
113
+ Name : "v1alpha2" ,
114
+ Served : true ,
115
+ Storage : false ,
116
+ Schema : & apiextensionsv1.CustomResourceValidation {
117
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
118
+ Type : "object" ,
119
+ Description : "my crd schema" ,
120
+ },
121
+ },
122
+ },
123
+ {
124
+ Name : "v1alpha3" ,
125
+ Served : true ,
126
+ Storage : true ,
127
+ Schema : & apiextensionsv1.CustomResourceValidation {
128
+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
129
+ Type : "object" ,
130
+ Description : "my crd schema" ,
131
+ },
132
+ },
133
+ },
134
+ },
135
+ },
136
+ }
137
+
138
+ err := SafeStorageVersionUpgrade (existingCRD , newCRD )
139
+ // expect error, since crd upgrade is not safe
140
+ require .NoError (t , err , "did not expect error for safe CRD upgrade" )
141
+ }
0 commit comments