github.com/verrazzano/verrazzano@v1.7.1/cluster-operator/apis/clusters/v1alpha1/oke_webhook_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 v1alpha1 5 6 import ( 7 _ "embed" 8 "github.com/oracle/oci-go-sdk/v53/core" 9 "github.com/stretchr/testify/assert" 10 ocifake "github.com/verrazzano/verrazzano/cluster-operator/controllers/quickcreate/controller/oci/fake" 11 "k8s.io/client-go/kubernetes/scheme" 12 "sigs.k8s.io/controller-runtime/pkg/client/fake" 13 "sigs.k8s.io/yaml" 14 "testing" 15 ) 16 17 var ( 18 //go:embed testdata/okequickcreate_valid.yaml 19 testValidOKECR []byte 20 //go:embed testdata/okequickcreate_invalid.yaml 21 testInvalidOKECR []byte 22 ) 23 24 func TestValidateCreateOKE(t *testing.T) { 25 cli := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build() 26 ociClient := &ocifake.ClientImpl{ 27 VCN: &core.Vcn{ 28 Id: &testID, 29 }, 30 } 31 32 var tests = []struct { 33 name string 34 crBytes []byte 35 hasError bool 36 }{ 37 { 38 "no error for valid CR", 39 testValidOKECR, 40 false, 41 }, 42 { 43 "error for invalid CR", 44 testInvalidOKECR, 45 true, 46 }, 47 } 48 for _, tt := range tests { 49 t.Run(tt.name, func(t *testing.T) { 50 defer func() { NewValidationContext = newValidationContext }() 51 NewValidationContext = func() (*validationContext, error) { 52 return testValidationContextWithOCIClient(cli, ociClient), nil 53 } 54 o := &OKEQuickCreate{} 55 err := yaml.Unmarshal(tt.crBytes, o) 56 assert.NoError(t, err) 57 err = o.ValidateCreate() 58 if tt.hasError { 59 assert.Error(t, err) 60 } else { 61 assert.NoError(t, err) 62 } 63 }) 64 } 65 } 66 67 func TestValidateUpdateOK(t *testing.T) { 68 o := &OKEQuickCreate{} 69 err := yaml.Unmarshal(testValidOCIOCNECR, o) 70 assert.NoError(t, err) 71 var tests = []struct { 72 name string 73 modifier func(o *OKEQuickCreate) 74 hasError bool 75 }{ 76 { 77 "no error when no update", 78 func(o *OKEQuickCreate) {}, 79 false, 80 }, 81 { 82 "error when spec update", 83 func(o *OKEQuickCreate) { 84 o.Spec.IdentityRef.Name = "foo" 85 }, 86 true, 87 }, 88 } 89 90 for _, tt := range tests { 91 t.Run(tt.name, func(t *testing.T) { 92 o1 := o.DeepCopy() 93 o2 := o.DeepCopy() 94 tt.modifier(o2) 95 err := o1.ValidateUpdate(o2) 96 if tt.hasError { 97 assert.Error(t, err) 98 } else { 99 assert.NoError(t, err) 100 } 101 }) 102 } 103 }