github.com/kubevela/workflow@v0.6.0/pkg/cue/utils_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 cue 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 24 25 "github.com/kubevela/workflow/pkg/cue/model/value" 26 ) 27 28 func TestIntifyValues(t *testing.T) { 29 testcases := map[string]struct { 30 input interface{} 31 output interface{} 32 }{ 33 "default case": { 34 input: "string", 35 output: "string", 36 }, 37 "float64": { 38 input: float64(1), 39 output: 1, 40 }, 41 "array": { 42 input: []interface{}{float64(1), float64(2)}, 43 output: []interface{}{1, 2}, 44 }, 45 "map": { 46 input: map[string]interface{}{"a": float64(1), "b": float64(2)}, 47 output: map[string]interface{}{"a": 1, "b": 2}, 48 }, 49 } 50 for name, testcase := range testcases { 51 t.Run(name, func(t *testing.T) { 52 r := require.New(t) 53 result := IntifyValues(testcase.input) 54 r.Equal(testcase.output, result) 55 }) 56 } 57 } 58 59 func TestFillUnstructuredObject(t *testing.T) { 60 testcases := map[string]struct { 61 obj *unstructured.Unstructured 62 json string 63 }{ 64 "test unstructured object with nil value": { 65 obj: &unstructured.Unstructured{ 66 Object: map[string]interface{}{ 67 "apiVersion": "apps/v1", 68 "kind": "Deployment", 69 "spec": map[string]interface{}{ 70 "template": map[string]interface{}{ 71 "metadata": map[string]interface{}{ 72 "creationTimestamp": nil, 73 }, 74 }, 75 }, 76 }, 77 }, 78 json: `{"object":{"apiVersion":"apps/v1","kind":"Deployment","spec":{"template":{"metadata":{"creationTimestamp":null}}}}}`, 79 }, 80 "test unstructured object without nil value": { 81 obj: &unstructured.Unstructured{ 82 Object: map[string]interface{}{ 83 "apiVersion": "apps/v1", 84 "kind": "Deployment", 85 "metadata": map[string]interface{}{ 86 "creationTimestamp": "2022-05-25T12:07:02Z", 87 }, 88 }, 89 }, 90 json: `{"object":{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"creationTimestamp":"2022-05-25T12:07:02Z"}}}`, 91 }, 92 } 93 94 for name, testcase := range testcases { 95 t.Run(name, func(t *testing.T) { 96 r := require.New(t) 97 value, err := value.NewValue("", nil, "") 98 r.NoError(err) 99 err = FillUnstructuredObject(value, testcase.obj, "object") 100 r.NoError(err) 101 json, err := value.CueValue().MarshalJSON() 102 r.NoError(err) 103 r.Equal(testcase.json, string(json)) 104 }) 105 } 106 } 107 108 func TestSubstituteUnstructuredObject(t *testing.T) { 109 testcases := map[string]struct { 110 obj *unstructured.Unstructured 111 json string 112 }{ 113 "test unstructured object with nil value": { 114 obj: &unstructured.Unstructured{ 115 Object: map[string]interface{}{ 116 "apiVersion": "apps/v1", 117 "kind": "Deployment", 118 "spec": map[string]interface{}{ 119 "template": map[string]interface{}{ 120 "metadata": map[string]interface{}{ 121 "creationTimestamp": nil, 122 }, 123 }, 124 }, 125 }, 126 }, 127 json: `{"object":{"apiVersion":"apps/v1","kind":"Deployment","spec":{"template":{"metadata":{"creationTimestamp":null}}}}}`, 128 }, 129 "test unstructured object without nil value": { 130 obj: &unstructured.Unstructured{ 131 Object: map[string]interface{}{ 132 "apiVersion": "apps/v1", 133 "kind": "Deployment", 134 "metadata": map[string]interface{}{ 135 "creationTimestamp": "2022-05-25T12:07:02Z", 136 }, 137 }, 138 }, 139 json: `{"object":{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"creationTimestamp":"2022-05-25T12:07:02Z"}}}`, 140 }, 141 } 142 143 for name, testcase := range testcases { 144 t.Run(name, func(t *testing.T) { 145 r := require.New(t) 146 value, err := value.NewValue(`object:{"test": "test"}`, nil, "") 147 r.NoError(err) 148 err = SetUnstructuredObject(value, testcase.obj, "object") 149 r.NoError(err) 150 json, err := value.CueValue().MarshalJSON() 151 r.NoError(err) 152 r.Equal(testcase.json, string(json)) 153 }) 154 } 155 }