github.com/oam-dev/kubevela@v1.9.11/pkg/policy/override_test.go (about) 1 /* 2 Copyright 2021 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package policy 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "sigs.k8s.io/controller-runtime/pkg/client/fake" 27 28 oamutil "github.com/oam-dev/kubevela/pkg/oam/util" 29 30 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 31 "github.com/oam-dev/kubevela/pkg/oam" 32 "github.com/oam-dev/kubevela/pkg/utils/common" 33 ) 34 35 func TestParseOverridePolicyRelatedDefinitions(t *testing.T) { 36 cli := fake.NewClientBuilder().WithScheme(common.Scheme).WithObjects(&v1beta1.ComponentDefinition{ 37 ObjectMeta: v1.ObjectMeta{Name: "comp", Namespace: oam.SystemDefinitionNamespace}, 38 }, &v1beta1.TraitDefinition{ 39 ObjectMeta: v1.ObjectMeta{Name: "trait", Namespace: "test"}, 40 }).Build() 41 r := require.New(t) 42 app := &v1beta1.Application{} 43 app.SetNamespace("test") 44 ctx := oamutil.SetNamespaceInCtx(context.Background(), "test") 45 testCases := map[string]struct { 46 Policy v1beta1.AppPolicy 47 ComponentDefs []*v1beta1.ComponentDefinition 48 TraitDefs []*v1beta1.TraitDefinition 49 Error string 50 }{ 51 "normal": { 52 Policy: v1beta1.AppPolicy{Properties: &runtime.RawExtension{Raw: []byte(`{"components":[{"type":"comp","traits":[{"type":"trait"}]}]}`)}}, 53 ComponentDefs: []*v1beta1.ComponentDefinition{{ObjectMeta: v1.ObjectMeta{Name: "comp", Namespace: oam.SystemDefinitionNamespace}}}, 54 TraitDefs: []*v1beta1.TraitDefinition{{ObjectMeta: v1.ObjectMeta{Name: "trait", Namespace: "test"}}}, 55 }, 56 "invalid-override-policy": { 57 Policy: v1beta1.AppPolicy{Properties: &runtime.RawExtension{Raw: []byte(`{bad value}`)}}, 58 Error: "invalid override policy spec", 59 }, 60 "comp-def-not-found": { 61 Policy: v1beta1.AppPolicy{Properties: &runtime.RawExtension{Raw: []byte(`{"components":[{"type":"comp-404","traits":[{"type":"trait"}]}]}`)}}, 62 Error: "failed to get component definition", 63 }, 64 "trait-def-not-found": { 65 Policy: v1beta1.AppPolicy{Properties: &runtime.RawExtension{Raw: []byte(`{"components":[{"type":"comp","traits":[{"type":"trait-404"}]}]}`)}}, 66 Error: "failed to get trait definition", 67 }, 68 "empty-policy": { 69 Policy: v1beta1.AppPolicy{Properties: nil}, 70 ComponentDefs: nil, 71 TraitDefs: nil, 72 Error: "have empty properties", 73 }, 74 } 75 for name, tt := range testCases { 76 t.Run(name, func(t *testing.T) { 77 compDefs, traitDefs, err := ParseOverridePolicyRelatedDefinitions(ctx, cli, app, tt.Policy) 78 if tt.Error != "" { 79 r.NotNil(err) 80 r.Contains(err.Error(), tt.Error) 81 } else { 82 r.NoError(err) 83 r.Equal(len(tt.ComponentDefs), len(compDefs)) 84 for i := range tt.ComponentDefs { 85 r.Equal(tt.ComponentDefs[i].Name, compDefs[i].Name) 86 r.Equal(tt.ComponentDefs[i].Namespace, compDefs[i].Namespace) 87 } 88 r.Equal(len(tt.TraitDefs), len(traitDefs)) 89 for i := range tt.TraitDefs { 90 r.Equal(tt.TraitDefs[i].Name, traitDefs[i].Name) 91 r.Equal(tt.TraitDefs[i].Namespace, traitDefs[i].Namespace) 92 } 93 } 94 }) 95 } 96 }