github.com/verrazzano/verrazzano@v1.7.0/platform-operator/controllers/common/controller_utils_test.go (about) 1 // Copyright (c) 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package common 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 vzv1alpha1 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1" 11 "github.com/verrazzano/verrazzano/platform-operator/internal/config" 12 ) 13 14 // TestIsUpgradeRequired tests that isUpgradeRequired method tells us when an upgrade is required before we can apply 15 // any module updates 16 // GIVEN a call to isUpgradeRequired 17 // WHEN the Verrazzano spec version is out of sync with the BOM version 18 // THEN true is returned 19 func TestIsUpgradeRequired(t *testing.T) { 20 defaultTestBomFile := "./testdata/test_bom.json" 21 22 tests := []struct { 23 name string 24 actualCR *vzv1alpha1.Verrazzano 25 testBOMPath string 26 want bool 27 wantErr assert.ErrorAssertionFunc 28 }{ 29 { 30 name: "VerrazzanoCRIsNil", 31 wantErr: assert.Error, 32 }, 33 { 34 name: "BothEmpty", 35 actualCR: &vzv1alpha1.Verrazzano{ 36 Spec: vzv1alpha1.VerrazzanoSpec{}, 37 }, 38 }, 39 { 40 name: "UpdateInstall", 41 actualCR: &vzv1alpha1.Verrazzano{ 42 Spec: vzv1alpha1.VerrazzanoSpec{}, 43 Status: vzv1alpha1.VerrazzanoStatus{ 44 Version: "2.0.2", 45 }, 46 }, 47 }, 48 { 49 name: "UpgradeIsPending", 50 actualCR: &vzv1alpha1.Verrazzano{ 51 Spec: vzv1alpha1.VerrazzanoSpec{ 52 Version: "2.0.1", 53 }, 54 Status: vzv1alpha1.VerrazzanoStatus{ 55 Version: "2.0.1", 56 }, 57 }, 58 want: true, 59 }, 60 { 61 name: "UpgradeToBranchBuildPending", 62 actualCR: &vzv1alpha1.Verrazzano{ 63 Spec: vzv1alpha1.VerrazzanoSpec{ 64 Version: "2.0.1", 65 }, 66 Status: vzv1alpha1.VerrazzanoStatus{ 67 Version: "2.0.1", 68 }, 69 }, 70 testBOMPath: "./testdata/test_bom_buildver.json", 71 want: true, 72 }, 73 { 74 name: "UpgradeToBranchBuildSameMMP", 75 actualCR: &vzv1alpha1.Verrazzano{ 76 Spec: vzv1alpha1.VerrazzanoSpec{ 77 Version: "2.0.2", 78 }, 79 Status: vzv1alpha1.VerrazzanoStatus{ 80 Version: "2.0.2", 81 }, 82 }, 83 testBOMPath: "./testdata/test_bom_buildver.json", 84 want: true, 85 }, 86 { 87 name: "UpgradeToNewBranchBuild", 88 actualCR: &vzv1alpha1.Verrazzano{ 89 Spec: vzv1alpha1.VerrazzanoSpec{ 90 Version: "2.0.2-1+bbadkkad", 91 }, 92 Status: vzv1alpha1.VerrazzanoStatus{ 93 Version: "2.0.2-1+bbadkkad", 94 }, 95 }, 96 testBOMPath: "./testdata/test_bom_buildver.json", 97 want: true, 98 }, 99 { 100 name: "UpgradeToNewBranchBuild2", 101 actualCR: &vzv1alpha1.Verrazzano{ 102 Spec: vzv1alpha1.VerrazzanoSpec{ 103 Version: "2.0.2-2+caafdaad", 104 }, 105 Status: vzv1alpha1.VerrazzanoStatus{ 106 Version: "2.0.2-2+caafdaad", 107 }, 108 }, 109 testBOMPath: "./testdata/test_bom_buildver.json", 110 want: true, 111 }, 112 { 113 name: "UpgradeToNewBranchBuildFromInstall", 114 actualCR: &vzv1alpha1.Verrazzano{ 115 Status: vzv1alpha1.VerrazzanoStatus{ 116 Version: "2.0.2-1+bbadkkad", 117 }, 118 }, 119 testBOMPath: "./testdata/test_bom_buildver.json", 120 want: true, 121 }, 122 { 123 name: "UpgradeToNewBranchBuildFromInstall2", 124 actualCR: &vzv1alpha1.Verrazzano{ 125 Status: vzv1alpha1.VerrazzanoStatus{ 126 Version: "2.0.2-2+caafdaad", 127 }, 128 }, 129 testBOMPath: "./testdata/test_bom_buildver.json", 130 want: true, 131 }, 132 { 133 name: "UpgradeToBranchBuildCompleted", 134 actualCR: &vzv1alpha1.Verrazzano{ 135 Spec: vzv1alpha1.VerrazzanoSpec{ 136 Version: "2.0.2-1+asdfdf", 137 }, 138 Status: vzv1alpha1.VerrazzanoStatus{ 139 Version: "2.0.2-1+asdfdf", 140 }, 141 }, 142 testBOMPath: "./testdata/test_bom_buildver.json", 143 }, 144 { 145 name: "UpgradeInProgress", 146 actualCR: &vzv1alpha1.Verrazzano{ 147 Spec: vzv1alpha1.VerrazzanoSpec{ 148 Version: "2.0.2", 149 }, 150 Status: vzv1alpha1.VerrazzanoStatus{ 151 Version: "2.0.1", 152 }, 153 }, 154 }, 155 { 156 name: "UpgradeComplete", 157 actualCR: &vzv1alpha1.Verrazzano{ 158 Spec: vzv1alpha1.VerrazzanoSpec{ 159 Version: "2.0.2", 160 }, 161 Status: vzv1alpha1.VerrazzanoStatus{ 162 Version: "2.0.2", 163 }, 164 }, 165 }, 166 { 167 name: "BOM Error", 168 actualCR: &vzv1alpha1.Verrazzano{ 169 Spec: vzv1alpha1.VerrazzanoSpec{}, 170 Status: vzv1alpha1.VerrazzanoStatus{ 171 Version: "2.0.2", 172 }, 173 }, 174 testBOMPath: "badpath", 175 wantErr: assert.Error, 176 }, 177 } 178 for _, tt := range tests { 179 t.Run(tt.name, func(t *testing.T) { 180 // Set the test BOM path 181 testBOM := tt.testBOMPath 182 if len(testBOM) == 0 { 183 testBOM = defaultTestBomFile 184 } 185 config.SetDefaultBomFilePath(testBOM) 186 defer func() { config.SetDefaultBomFilePath("") }() 187 188 // Set up the err check assertion 189 wantErr := tt.wantErr 190 if wantErr == nil { 191 wantErr = assert.NoError 192 } 193 194 got, err := IsUpgradeRequired(tt.actualCR) 195 if !wantErr(t, err, "Did not get expected error result") { 196 return 197 } 198 assert.Equal(t, got, tt.want) 199 }) 200 } 201 }