github.com/oam-dev/kubevela@v1.9.11/pkg/builtin/registry/registry_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  	"github.com/stretchr/testify/assert"
    23  
    24  	cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
    25  )
    26  
    27  var mock map[string]interface{}
    28  
    29  func mockTask(ctx CallCtx, params interface{}) error {
    30  	ppp := params.(map[string]interface{})
    31  	for k, v := range ppp {
    32  		mock[k] = v
    33  	}
    34  	return nil
    35  }
    36  
    37  func mockTask1(ctx CallCtx, params interface{}) error {
    38  	ppp := params.(map[string]interface{})
    39  	for k, v := range ppp {
    40  		mock[k] = v
    41  	}
    42  	return nil
    43  }
    44  
    45  func TestDoTasks(t *testing.T) {
    46  	RegisterTask("mock", mockTask)
    47  	RegisterTask("mock1", mockTask1)
    48  
    49  	testCases := map[string]struct {
    50  		expect     map[string]interface{}
    51  		input      map[string]interface{}
    52  		expectMock map[string]interface{}
    53  	}{
    54  		"one task exec with new value added": {
    55  			expect: map[string]interface{}{
    56  				"image": "testImage",
    57  			},
    58  			input: map[string]interface{}{
    59  				"image": "testImage",
    60  				"mock": map[string]interface{}{
    61  					"prams": "testValues",
    62  				},
    63  			},
    64  			expectMock: map[string]interface{}{
    65  				"prams": "testValues",
    66  			},
    67  		},
    68  
    69  		"No task exec No changes": {
    70  			expect: map[string]interface{}{
    71  				"image": "testImage",
    72  				"cmd":   []string{"sleep", "1000"},
    73  			},
    74  			expectMock: map[string]interface{}{},
    75  			input: map[string]interface{}{
    76  				"image": "testImage",
    77  				"cmd":   []string{"sleep", "1000"},
    78  			}},
    79  		"two tasks exec with new value added": {
    80  			expect: map[string]interface{}{
    81  				"image": "testImage",
    82  			},
    83  			expectMock: map[string]interface{}{
    84  				"prams":  "testValues",
    85  				"prams1": "testValues1",
    86  			},
    87  			input: map[string]interface{}{
    88  				"image": "testImage",
    89  				"mock": map[string]interface{}{
    90  					"prams": "testValues",
    91  				},
    92  				"mock1": map[string]interface{}{
    93  					"prams1": "testValues1",
    94  				},
    95  			}},
    96  	}
    97  
    98  	for _, tcase := range testCases {
    99  		mock = make(map[string]interface{})
   100  		ret, _ := Run(tcase.input, cmdutil.IOStreams{})
   101  		assert.Equal(t, tcase.expect, ret)
   102  		assert.Equal(t, tcase.expectMock, mock)
   103  	}
   104  }
   105  
   106  func TestRegisterTask(t *testing.T) {
   107  	RegisterTask("mock", mockTask)
   108  	RegisterTask("mock1", mockTask)
   109  	RegisterTask("mock2", mockTask)
   110  	RegisterTask("mock", mockTask)
   111  
   112  	expectTasks := []string{
   113  		"mock",
   114  		"mock1",
   115  		"mock2",
   116  	}
   117  	tasks := GetTasks()
   118  	for _, eTask := range expectTasks {
   119  		if _, ok := tasks[eTask]; !ok {
   120  			t.Errorf("task %s not register", eTask)
   121  		}
   122  	}
   123  }