github.com/kubevela/workflow@v0.6.0/pkg/debug/context_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 debug
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/crossplane/crossplane-runtime/pkg/test"
    24  	"github.com/stretchr/testify/require"
    25  	corev1 "k8s.io/api/core/v1"
    26  	kerrors "k8s.io/apimachinery/pkg/api/errors"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  
    30  	"github.com/kubevela/workflow/pkg/cue/model/value"
    31  	"github.com/kubevela/workflow/pkg/types"
    32  )
    33  
    34  func TestSetContext(t *testing.T) {
    35  	r := require.New(t)
    36  	cli := newCliForTest(&corev1.ConfigMap{
    37  		ObjectMeta: metav1.ObjectMeta{
    38  			Name: GenerateContextName("test", "step1", "123456"),
    39  		},
    40  		Data: map[string]string{
    41  			"debug": "test",
    42  		},
    43  	})
    44  	// test update
    45  	debugCtx := NewContext(cli, &types.WorkflowInstance{
    46  		WorkflowMeta: types.WorkflowMeta{
    47  			Name: "test",
    48  		},
    49  	}, "step1")
    50  	v, err := value.NewValue(`
    51  test: test
    52  `, nil, "")
    53  	r.NoError(err)
    54  	err = debugCtx.Set(v)
    55  	r.NoError(err)
    56  	// test create
    57  	debugCtx = NewContext(cli, &types.WorkflowInstance{
    58  		WorkflowMeta: types.WorkflowMeta{
    59  			Name: "test",
    60  		},
    61  	}, "step2")
    62  	v, err = value.NewValue(`
    63  test: test
    64  `, nil, "")
    65  	r.NoError(err)
    66  	err = debugCtx.Set(v)
    67  	r.NoError(err)
    68  }
    69  
    70  func newCliForTest(wfCm *corev1.ConfigMap) *test.MockClient {
    71  	return &test.MockClient{
    72  		MockGet: func(ctx context.Context, key client.ObjectKey, obj client.Object) error {
    73  			o, ok := obj.(*corev1.ConfigMap)
    74  			if ok {
    75  				switch key.Name {
    76  				case GenerateContextName("test", "step1", "123456"):
    77  					if wfCm != nil {
    78  						*o = *wfCm
    79  						return nil
    80  					}
    81  				default:
    82  					return kerrors.NewNotFound(corev1.Resource("configMap"), o.Name)
    83  				}
    84  			}
    85  			return kerrors.NewNotFound(corev1.Resource("configMap"), key.Name)
    86  		},
    87  		MockUpdate: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
    88  			o, ok := obj.(*corev1.ConfigMap)
    89  			if ok {
    90  				if wfCm == nil {
    91  					return kerrors.NewNotFound(corev1.Resource("configMap"), o.Name)
    92  				}
    93  				*wfCm = *o
    94  			}
    95  			return nil
    96  		},
    97  		MockCreate: func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
    98  			return nil
    99  		},
   100  	}
   101  }