github.com/oam-dev/kubevela@v1.9.11/pkg/appfile/unit_test.go (about) 1 /* 2 Copyright 2023 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 appfile 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 24 "github.com/stretchr/testify/require" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/runtime" 27 "sigs.k8s.io/controller-runtime/pkg/client/fake" 28 29 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 30 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 31 ) 32 33 func TestIsNotFoundInAppFile(t *testing.T) { 34 require.True(t, IsNotFoundInAppFile(fmt.Errorf("ComponentDefinition XXX not found in appfile"))) 35 } 36 37 func TestIsNotFoundInAppRevision(t *testing.T) { 38 require.True(t, IsNotFoundInAppRevision(fmt.Errorf("ComponentDefinition XXX not found in app revision"))) 39 } 40 41 func TestParseComponentFromRevisionAndClient(t *testing.T) { 42 ctx := context.Background() 43 cli := fake.NewClientBuilder().WithScheme(scheme).Build() 44 p := &Parser{ 45 client: cli, 46 tmplLoader: LoadTemplate, 47 } 48 comp := common.ApplicationComponent{ 49 Name: "test", 50 Type: "test", 51 Properties: &runtime.RawExtension{Raw: []byte(`{}`)}, 52 Traits: []common.ApplicationTrait{{ 53 Type: "tr", 54 Properties: &runtime.RawExtension{Raw: []byte(`{}`)}, 55 }, { 56 Type: "internal", 57 Properties: &runtime.RawExtension{Raw: []byte(`{}`)}, 58 }}, 59 } 60 appRev := &v1beta1.ApplicationRevision{} 61 cd := &v1beta1.ComponentDefinition{ObjectMeta: metav1.ObjectMeta{Name: "test"}} 62 td := &v1beta1.TraitDefinition{ObjectMeta: metav1.ObjectMeta{Name: "tr"}} 63 require.NoError(t, cli.Create(ctx, cd)) 64 require.NoError(t, cli.Create(ctx, td)) 65 appRev.Spec.TraitDefinitions = map[string]*v1beta1.TraitDefinition{"internal": {}} 66 _, err := p.ParseComponentFromRevisionAndClient(ctx, comp, appRev) 67 require.NoError(t, err) 68 69 _comp1 := comp.DeepCopy() 70 _comp1.Type = "bad" 71 _, err = p.ParseComponentFromRevisionAndClient(ctx, *_comp1, appRev) 72 require.Error(t, err) 73 74 _comp2 := comp.DeepCopy() 75 _comp2.Traits[0].Type = "bad" 76 _, err = p.ParseComponentFromRevisionAndClient(ctx, *_comp2, appRev) 77 require.Error(t, err) 78 79 _comp3 := comp.DeepCopy() 80 _comp3.Traits[0].Properties = &runtime.RawExtension{Raw: []byte(`bad`)} 81 _, err = p.ParseComponentFromRevisionAndClient(ctx, *_comp3, appRev) 82 require.Error(t, err) 83 }