github.com/oam-dev/kubevela@v1.9.11/pkg/builtin/registry/registry_runner_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 registry 18 19 import ( 20 "testing" 21 22 "cuelang.org/go/cue" 23 "cuelang.org/go/cue/cuecontext" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestContext(t *testing.T) { 28 lpV := `test: "just a test"` 29 inst := cuecontext.New().CompileString(lpV) 30 ctx := Meta{Obj: inst} 31 val := ctx.Lookup("test") 32 assert.Equal(t, true, val.Exists()) 33 34 intV := `iTest: 64` 35 iInst := cuecontext.New().CompileString(intV) 36 iCtx := Meta{Obj: iInst.Value()} 37 iVal := iCtx.Int64("iTest") 38 assert.Equal(t, int64(64), iVal) 39 } 40 41 func TestRunner(t *testing.T) { 42 key := "mock" 43 RegisterRunner(key, newMockRunner) 44 45 task := LookupRunner(key) 46 if task == nil { 47 t.Errorf("there is no task %s", key) 48 } 49 runner, err := task(cue.Value{}) 50 if err != nil { 51 t.Errorf("fail to get runner, %v", err) 52 } 53 rs, err := runner.Run(&Meta{Obj: cue.Value{}}) 54 assert.Equal(t, nil, err) 55 assert.Equal(t, "mock", rs) 56 } 57 58 func newMockRunner(v cue.Value) (Runner, error) { 59 return &MockRunner{name: "mock"}, nil 60 } 61 62 type MockRunner struct { 63 name string 64 } 65 66 func (r *MockRunner) Run(meta *Meta) (res interface{}, err error) { 67 return r.name, nil 68 }