github.com/kubevela/workflow@v0.6.0/pkg/tasks/template/load_test.go (about) 1 /* 2 Copyright 2022 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 template 18 19 import ( 20 "context" 21 "encoding/json" 22 "os" 23 "testing" 24 25 "github.com/crossplane/crossplane-runtime/pkg/test" 26 "github.com/stretchr/testify/require" 27 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 "sigs.k8s.io/yaml" 30 ) 31 32 func TestLoad(t *testing.T) { 33 cli := &test.MockClient{ 34 MockGet: func(ctx context.Context, key client.ObjectKey, obj client.Object) error { 35 o, ok := obj.(*unstructured.Unstructured) 36 if !ok { 37 return nil 38 } 39 var d map[string]interface{} 40 js, err := yaml.YAMLToJSON([]byte(stepDefYaml)) 41 if err != nil { 42 return err 43 } 44 if err := json.Unmarshal(js, &d); err != nil { 45 return err 46 } 47 o.Object = d 48 return nil 49 }, 50 } 51 loader := NewWorkflowStepTemplateLoader(cli) 52 53 r := require.New(t) 54 tmpl, err := loader.LoadTemplate(context.Background(), "builtin-apply-component") 55 r.NoError(err) 56 expected, err := os.ReadFile("./static/builtin-apply-component.cue") 57 r.NoError(err) 58 r.Equal(tmpl, string(expected)) 59 60 tmpl, err = loader.LoadTemplate(context.Background(), "apply-oam-component") 61 r.NoError(err) 62 r.Equal(tmpl, `import ( 63 "vela/op" 64 ) 65 66 // apply components and traits 67 apply: op.#ApplyComponent & { 68 component: parameter.component 69 } 70 parameter: { 71 // +usage=Declare the name of the component 72 component: string 73 }`) 74 } 75 76 var ( 77 stepDefYaml = `apiVersion: core.oam.dev/v1beta1 78 kind: WorkflowStepDefinition 79 metadata: 80 annotations: 81 definition.oam.dev/description: Apply components and traits for your workflow steps 82 name: apply-oam-component 83 namespace: vela-system 84 spec: 85 schematic: 86 cue: 87 template: | 88 import ( 89 "vela/op" 90 ) 91 92 // apply components and traits 93 apply: op.#ApplyComponent & { 94 component: parameter.component 95 } 96 parameter: { 97 // +usage=Declare the name of the component 98 component: string 99 }` 100 )