Files
kubevela/pkg/dsl/process/handle_test.go

77 lines
1.5 KiB
Go

package process
import (
"testing"
"cuelang.org/go/cue"
"github.com/bmizerany/assert"
"github.com/oam-dev/kubevela/pkg/dsl/model"
)
func TestContext(t *testing.T) {
baseTemplate := `
image: "myserver"
`
var r cue.Runtime
inst, err := r.Compile("-", baseTemplate)
if err != nil {
t.Error(err)
return
}
base, err := model.NewBase(inst.Value())
if err != nil {
t.Error(err)
return
}
serviceTemplate := `
apiVersion: "v1"
kind: "ConfigMap"
`
svcInst, err := r.Compile("-", serviceTemplate)
if err != nil {
t.Error(err)
return
}
svcIns, err := model.NewOther(svcInst.Value())
if err != nil {
t.Error(err)
return
}
svcAux := Auxiliary{
Ins: svcIns,
Name: "service",
}
ctx := NewContext("mycomp", "myapp")
ctx.SetBase(base)
ctx.AppendAuxiliaries(svcAux)
ctxInst, err := r.Compile("-", ctx.BaseContextFile())
if err != nil {
t.Error(err)
return
}
gName, err := ctxInst.Lookup("context", ContextName).String()
assert.Equal(t, nil, err)
assert.Equal(t, "mycomp", gName)
myAppName, err := ctxInst.Lookup("context", ContextAppName).String()
assert.Equal(t, nil, err)
assert.Equal(t, "myapp", myAppName)
inputJs, err := ctxInst.Lookup("context", OutputFieldName).MarshalJSON()
assert.Equal(t, nil, err)
assert.Equal(t, `{"image":"myserver"}`, string(inputJs))
outputsJs, err := ctxInst.Lookup("context", OutputsFieldName, "service").MarshalJSON()
assert.Equal(t, nil, err)
assert.Equal(t, "{\"apiVersion\":\"v1\",\"kind\":\"ConfigMap\"}", string(outputsJs))
}