github.com/kubevela/workflow@v0.6.0/pkg/tasks/discover_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 tasks
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/pkg/errors"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/kubevela/workflow/pkg/cue/process"
    27  	"github.com/kubevela/workflow/pkg/tasks/builtin"
    28  	"github.com/kubevela/workflow/pkg/tasks/custom"
    29  	"github.com/kubevela/workflow/pkg/types"
    30  )
    31  
    32  func TestDiscover(t *testing.T) {
    33  	r := require.New(t)
    34  	makeErr := func(name string) error {
    35  		return errors.Errorf("template %s not found", name)
    36  	}
    37  
    38  	loadTemplate := func(ctx context.Context, name string) (string, error) {
    39  		switch name {
    40  		case "foo":
    41  			return "", nil
    42  		case "crazy":
    43  			return "", nil
    44  		default:
    45  			return "", makeErr(name)
    46  		}
    47  	}
    48  	pCtx := process.NewContext(process.ContextData{
    49  		Namespace: "default",
    50  	})
    51  	discover := &taskDiscover{
    52  		builtin: map[string]types.TaskGenerator{
    53  			"stepGroup": builtin.StepGroup,
    54  		},
    55  		customTaskDiscover: custom.NewTaskLoader(loadTemplate, nil, nil, 0, pCtx),
    56  	}
    57  
    58  	_, err := discover.GetTaskGenerator(context.Background(), "stepGroup")
    59  	r.NoError(err)
    60  	_, err = discover.GetTaskGenerator(context.Background(), "foo")
    61  	r.NoError(err)
    62  	_, err = discover.GetTaskGenerator(context.Background(), "crazy")
    63  	r.NoError(err)
    64  	_, err = discover.GetTaskGenerator(context.Background(), "fly")
    65  	r.Equal(err.Error(), makeErr("fly").Error())
    66  
    67  }