github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/helpers/vzhelper_test.go (about) 1 // Copyright (c) 2022, 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 helpers 5 6 import ( 7 "bytes" 8 "github.com/stretchr/testify/assert" 9 "github.com/verrazzano/verrazzano/pkg/constants" 10 v1alpha1 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1" 11 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1" 12 testhelpers "github.com/verrazzano/verrazzano/tools/vz/test/helpers" 13 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 14 "k8s.io/apimachinery/pkg/runtime/schema" 15 "k8s.io/apimachinery/pkg/types" 16 "k8s.io/cli-runtime/pkg/genericclioptions" 17 "os" 18 "sigs.k8s.io/controller-runtime/pkg/client/fake" 19 "testing" 20 ) 21 22 // TestNewVerrazzanoForVersion 23 // GIVEN a schema.GroupVersion 24 // 25 // WHEN I call this function 26 // THEN expect it to return a function that returns a new Verrazzano of the appropriate version 27 func TestNewVerrazzanoForVersion(t *testing.T) { 28 var tests = []struct { 29 name string 30 gv schema.GroupVersion 31 o interface{} 32 }{ 33 { 34 "new v1alpha1 Verrazzano", 35 v1alpha1.SchemeGroupVersion, 36 &v1alpha1.Verrazzano{}, 37 }, 38 { 39 "new v1beta1 Verrazzano", 40 v1beta1.SchemeGroupVersion, 41 &v1beta1.Verrazzano{}, 42 }, 43 } 44 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 assert.Equal(t, tt.o, NewVerrazzanoForGroupVersion(tt.gv)()) 48 }) 49 } 50 } 51 52 // TestGetLatestReleaseVersion 53 // GIVEN a list of release versions 54 // 55 // WHEN I call this function 56 // THEN expect it to return the latest version string 57 func TestGetLatestReleaseVersion(t *testing.T) { 58 buf := new(bytes.Buffer) 59 errBuf := new(bytes.Buffer) 60 rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf}) 61 latestRelease, err := GetLatestReleaseVersion(rc.GetHTTPClient()) 62 assert.NoError(t, err) 63 assert.Equal(t, latestRelease, "v1.3.1") 64 } 65 66 // TestGetVerrazzanoResource 67 // GIVEN the namespace and name of a verrazzano resource 68 // 69 // WHEN I call GetVerrazzanoResource 70 // THEN expect it to return a verrazzano resource 71 func TestVerrazzanoResource(t *testing.T) { 72 client := fake.NewClientBuilder().WithScheme(NewScheme()).WithObjects( 73 &v1beta1.Verrazzano{ 74 ObjectMeta: metav1.ObjectMeta{ 75 Namespace: "default", 76 Name: "verrazzano", 77 }, 78 Status: v1beta1.VerrazzanoStatus{ 79 Components: map[string]*v1beta1.ComponentStatusDetails{"test_component": {Name: "grafana"}}, 80 }, 81 }).Build() 82 83 vz, err := GetVerrazzanoResource(client, types.NamespacedName{Namespace: "default", Name: "verrazzano"}) 84 assert.NoError(t, err) 85 assert.Equal(t, "default", vz.Namespace) 86 assert.Equal(t, "verrazzano", vz.Name) 87 assert.Nil(t, UpdateVerrazzanoResource(client, vz)) 88 assert.NotEmpty(t, GetNamespacesForAllComponents(vz)) 89 _, err = findVerazzanoResourceV1Alpha1(client) 90 assert.Error(t, failedToFindResourceError(err)) 91 } 92 93 // TestGetVerrazzanoResourceNotFound 94 // GIVEN the namespace and name of a verrazzano resource 95 // 96 // WHEN I call GetVerrazzanoResource 97 // THEN expect it to return an error 98 func TestGetVerrazzanoResourceNotFound(t *testing.T) { 99 client := fake.NewClientBuilder().WithScheme(NewScheme()).Build() 100 _, err := GetVerrazzanoResource(client, types.NamespacedName{Namespace: "default", Name: "verrazzano"}) 101 assert.EqualError(t, err, "Failed to get a Verrazzano install resource: verrazzanos.install.verrazzano.io \"verrazzano\" not found") 102 } 103 104 // TestFindVerrazzanoResource 105 // GIVEN a list of a verrazzano resources 106 // 107 // WHEN I call FindVerrazzanoResource 108 // THEN expect to find a single verrazzano resource 109 func TestFindVerrazzanoResource(t *testing.T) { 110 client := fake.NewClientBuilder().WithScheme(NewScheme()).WithObjects( 111 &v1beta1.Verrazzano{ 112 ObjectMeta: metav1.ObjectMeta{ 113 Namespace: "default", 114 Name: "verrazzano", 115 }, 116 }).Build() 117 118 vz, err := FindVerrazzanoResource(client) 119 assert.NoError(t, err) 120 assert.Equal(t, "default", vz.Namespace) 121 assert.Equal(t, "verrazzano", vz.Name) 122 } 123 124 // TestFindVerrazzanoResourceMultiple 125 // GIVEN a list of a verrazzano resources 126 // 127 // WHEN I call FindVerrazzanoResource 128 // THEN return an error when multiple verrazzano resources found 129 func TestFindVerrazzanoResourceMultiple(t *testing.T) { 130 client := fake.NewClientBuilder().WithScheme(NewScheme()).WithObjects( 131 &v1beta1.Verrazzano{ 132 ObjectMeta: metav1.ObjectMeta{ 133 Namespace: "default", 134 Name: "verrazzano", 135 }, 136 }, 137 &v1beta1.Verrazzano{ 138 ObjectMeta: metav1.ObjectMeta{ 139 Namespace: "default", 140 Name: "verrazzano2", 141 }, 142 }).Build() 143 144 _, err := FindVerrazzanoResource(client) 145 assert.EqualError(t, err, "Expected to only find one Verrazzano resource, but found 2") 146 } 147 148 // TestFindVerrazzanoResourceNone 149 // GIVEN a list of a verrazzano resources 150 // 151 // WHEN I call FindVerrazzanoResource 152 // THEN return an error when no verrazzano resources are found 153 func TestFindVerrazzanoResourceNone(t *testing.T) { 154 client := fake.NewClientBuilder().WithScheme(NewScheme()).Build() 155 156 _, err := FindVerrazzanoResource(client) 157 assert.EqualError(t, err, "Failed to find any Verrazzano resources") 158 } 159 160 // TestGetNamespacesForAllComponents 161 // GIVEN a Verrazzano resource 162 // 163 // WHEN I call GetNamespacesForAllComponents 164 // THEN return list of namespaces of all components in that Verrazzano resource 165 func TestGetNamespacesForAllComponents(t *testing.T) { 166 //components: 167 vzWithOneComponent := 168 v1beta1.Verrazzano{ 169 Status: v1beta1.VerrazzanoStatus{ 170 Components: v1beta1.ComponentStatusMap{ 171 constants.Grafana: &v1beta1.ComponentStatusDetails{ 172 Name: constants.Grafana, 173 State: v1beta1.CompStateReady, 174 }, 175 }, 176 }, 177 } 178 179 vzWithMultipleComponents := 180 v1beta1.Verrazzano{ 181 Status: v1beta1.VerrazzanoStatus{ 182 Components: v1beta1.ComponentStatusMap{ 183 constants.CertManager: &v1beta1.ComponentStatusDetails{ 184 Name: constants.CertManager, 185 State: v1beta1.CompStateReady, 186 }, 187 constants.Grafana: &v1beta1.ComponentStatusDetails{ 188 Name: constants.Grafana, 189 State: v1beta1.CompStateReady, 190 }, 191 constants.Istio: &v1beta1.ComponentStatusDetails{ 192 Name: constants.Istio, 193 State: v1beta1.CompStateReady, 194 }, 195 }, 196 }, 197 } 198 199 vzWithDisabledComponent := 200 v1beta1.Verrazzano{ 201 Status: v1beta1.VerrazzanoStatus{ 202 Components: v1beta1.ComponentStatusMap{ 203 constants.CertManager: &v1beta1.ComponentStatusDetails{ 204 Name: constants.CertManager, 205 State: v1beta1.CompStateReady, 206 }, 207 constants.Grafana: &v1beta1.ComponentStatusDetails{ 208 Name: constants.Grafana, 209 State: v1beta1.CompStateReady, 210 }, 211 constants.Istio: &v1beta1.ComponentStatusDetails{ 212 Name: constants.Istio, 213 State: v1beta1.CompStateDisabled, 214 }, 215 }, 216 }, 217 } 218 219 var tests = []struct { 220 name string 221 component v1beta1.Verrazzano 222 expected []string 223 }{ 224 { 225 "vz with one component", 226 vzWithOneComponent, 227 []string{constants.VerrazzanoSystemNamespace}, 228 }, 229 { 230 "vz with multiple components", 231 vzWithMultipleComponents, 232 []string{constants.IstioSystemNamespace, constants.VerrazzanoSystemNamespace, constants.CertManagerNamespace}, 233 }, 234 { 235 "vz with disabled component", 236 vzWithDisabledComponent, 237 []string{constants.VerrazzanoSystemNamespace, constants.CertManagerNamespace}, 238 }, 239 } 240 241 for _, tt := range tests { 242 t.Run(tt.name, func(t *testing.T) { 243 result := GetNamespacesForAllComponents(&tt.component) 244 for i := range tt.expected { 245 assert.Contains(t, result, tt.expected[i]) 246 } 247 assert.Equal(t, len(tt.expected), len(result)) 248 }) 249 } 250 } 251 252 // TestNewVerrazzanoForVZVersion 253 // GIVEN a version 254 // 255 // WHEN I call NewVerrazzanoForVersion 256 // THEN expect it to return a new Verrazzano of the appropriate version 257 func TestNewVerrazzanoForVZVersion(t *testing.T) { 258 var tests = []struct { 259 name string 260 version string 261 gv schema.GroupVersion 262 }{ 263 { 264 "new v1alpha1 Verrazzano with default version", 265 "1.3.0", 266 v1alpha1.SchemeGroupVersion, 267 }, 268 { 269 "new v1beta1 Verrazzano with min version", 270 "1.4.0", 271 v1beta1.SchemeGroupVersion, 272 }, 273 { 274 "new v1beta1 Verrazzano below min version", 275 "1.2.0", 276 v1alpha1.SchemeGroupVersion, 277 }, 278 } 279 280 for _, tt := range tests { 281 t.Run(tt.name, func(t *testing.T) { 282 gv, obj, _ := NewVerrazzanoForVZVersion(tt.version) 283 version := obj.GetObjectKind().GroupVersionKind().Version 284 assert.Equal(t, tt.gv.Version, gv.Version) 285 assert.Equal(t, tt.gv.Group, gv.Group) 286 assert.Equal(t, version, gv.Version) 287 }) 288 } 289 } 290 291 // TestUpdateVerrazzanoResource 292 // GIVEN a Verrazzano resource and specified group version 293 // 294 // WHEN I call UpdateVerrazzanoResource 295 // THEN expect it to update that version of the Verrazzano resource, an error will be returned if it's not supported 296 func TestUpdateVerrazzanoResource(t *testing.T) { 297 client := fake.NewClientBuilder().WithScheme(NewScheme()).WithObjects( 298 &v1beta1.Verrazzano{ 299 ObjectMeta: metav1.ObjectMeta{ 300 Namespace: "default", 301 Name: "verrazzano", 302 }, 303 }).Build() 304 vz, _ := GetVerrazzanoResource(client, types.NamespacedName{Namespace: "default", Name: "verrazzano"}) 305 err := UpdateVerrazzanoResource(client, vz) 306 assert.NoError(t, err) 307 308 } 309 310 // TestGetOperatorYaml 311 // GIVEN a specified cersion 312 // 313 // WHEN I call GetOperatorYaml 314 // THEN expect it to return Kubernetes manifests to deploy the Verrazzano platform operator 315 func TestGetOperatorYaml(t *testing.T) { 316 var tests = []struct { 317 name string 318 version string 319 expected string 320 }{ 321 { 322 "earlier than 1.4.0", 323 "1.3.0", 324 "https://github.com/verrazzano/verrazzano/releases/download/1.3.0/operator.yaml", 325 }, 326 { 327 "later than 1.4.0", 328 "1.5.0", 329 "https://github.com/verrazzano/verrazzano/releases/download/1.5.0/verrazzano-platform-operator.yaml", 330 }, 331 { 332 "equal to 1.4.0", 333 "1.4.0", 334 "https://github.com/verrazzano/verrazzano/releases/download/1.4.0/verrazzano-platform-operator.yaml", 335 }, 336 } 337 for _, tt := range tests { 338 t.Run(tt.name, func(t *testing.T) { 339 str, _ := GetOperatorYaml(tt.version) 340 assert.Equal(t, str, tt.expected) 341 }) 342 } 343 }