Files
kubevela/pkg/workflow/hooks/data_passing_test.go
Tianxin Dong f7be1cd2fc Feat: support more expressions in if and op.Fail (#4151)
* Feat: support more expressions in if

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix cache overrides context

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix skip status

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* add tests in tasks

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* Feat: add fail action

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix annotation in tests

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* add failed reason in substeps

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix look up nil value

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* add tests in workflow

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* add tests in application controller

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* optimize the code

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* add status in if

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix dash case

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
2022-06-22 14:51:23 +08:00

105 lines
2.7 KiB
Go

/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hooks
import (
"context"
"testing"
"github.com/crossplane/crossplane-runtime/pkg/test"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/stretchr/testify/require"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/cue/model/value"
wfContext "github.com/oam-dev/kubevela/pkg/workflow/context"
)
func TestInput(t *testing.T) {
wfCtx := mockContext(t)
r := require.New(t)
paramValue, err := wfCtx.MakeParameter(map[string]interface{}{
"name": "foo",
})
r.NoError(err)
score, err := paramValue.MakeValue(`score: 99`)
r.NoError(err)
err = wfCtx.SetVar(score, "foo")
r.NoError(err)
err = Input(wfCtx, paramValue, v1beta1.WorkflowStep{
DependsOn: []string{"mystep"},
Inputs: common.StepInputs{{
From: "foo.score",
ParameterKey: "myscore",
}},
})
r.NoError(err)
result, err := paramValue.LookupValue("myscore")
r.NoError(err)
s, err := result.String()
r.NoError(err)
r.Equal(s, `99
`)
}
func TestOutput(t *testing.T) {
wfCtx := mockContext(t)
r := require.New(t)
taskValue, err := value.NewValue(`
output: score: 99
`, nil, "")
r.NoError(err)
err = Output(wfCtx, taskValue, v1beta1.WorkflowStep{
Properties: &runtime.RawExtension{
Raw: []byte("{\"name\":\"mystep\"}"),
},
Outputs: common.StepOutputs{{
ValueFrom: "output.score",
Name: "myscore",
}},
}, common.StepStatus{
Phase: common.WorkflowStepPhaseSucceeded,
})
r.NoError(err)
result, err := wfCtx.GetVar("myscore")
r.NoError(err)
s, err := result.String()
r.NoError(err)
r.Equal(s, `99
`)
}
func mockContext(t *testing.T) wfContext.Context {
cli := &test.MockClient{
MockCreate: func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return nil
},
MockUpdate: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return nil
},
MockGet: func(ctx context.Context, key client.ObjectKey, obj client.Object) error {
return nil
},
}
wfCtx, err := wfContext.NewContext(cli, "default", "v1", "testuid")
require.NoError(t, err)
return wfCtx
}