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