github.com/crossplane-contrib/function-cue@v0.2.2-0.20240508161918-5100fcb5a058/internal/fn/debug_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package fn 19 20 import ( 21 "encoding/base64" 22 "encoding/json" 23 "fmt" 24 "testing" 25 26 "cuelang.org/go/cue/cuecontext" 27 28 "github.com/ghodss/yaml" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func yaml2Object(t *testing.T, s string) any { 34 var data any 35 err := yaml.Unmarshal([]byte(s), &data) 36 require.NoError(t, err) 37 return data 38 } 39 40 func TestDebugRemoveNoise(t *testing.T) { 41 f, err := New(Options{}) 42 require.NoError(t, err) 43 inputYAML := ` 44 - apiVersion: v1 45 kind: ConfigMap 46 metadata: 47 name: foobar 48 annotations: 49 "kubectl.kubernetes.io/last-applied-configuration": "{}" 50 managedFields: [] 51 creationTimestamp: "XXX" 52 generation: 1204 53 resourceVersion: "yyy" 54 uid: "zzz" 55 data: 56 foo: bar 57 ` 58 cleanedYAML := ` 59 - apiVersion: v1 60 kind: ConfigMap 61 metadata: 62 name: foobar 63 annotations: {} 64 data: 65 foo: bar 66 ` 67 input := yaml2Object(t, inputYAML) 68 cleaned := yaml2Object(t, cleanedYAML) 69 inBytes, err := json.MarshalIndent(input, "", " ") 70 require.NoError(t, err) 71 72 t.Run("re-serialize clean", func(t *testing.T) { 73 out, err := f.reserialize(inBytes, false) 74 require.NoError(t, err) 75 actual := yaml2Object(t, string(out)) 76 assert.EqualValues(t, cleaned, actual) 77 }) 78 79 t.Run("re-serialize cue", func(t *testing.T) { 80 cueString := f.getDebugString(inBytes, false) 81 cc := cuecontext.New() 82 val := cc.CompileString(cueString) 83 require.NoError(t, val.Err()) 84 b, err := val.MarshalJSON() 85 require.NoError(t, err) 86 actual := yaml2Object(t, string(b)) 87 assert.EqualValues(t, cleaned, actual) 88 }) 89 90 t.Run("re-serialize raw", func(t *testing.T) { 91 out, err := f.reserialize(inBytes, true) 92 require.NoError(t, err) 93 actual := yaml2Object(t, string(out)) 94 assert.EqualValues(t, input, actual) 95 }) 96 97 t.Run("re-serialize cue raw", func(t *testing.T) { 98 cueString := f.getDebugString(inBytes, true) 99 cc := cuecontext.New() 100 val := cc.CompileString(cueString) 101 require.NoError(t, val.Err()) 102 b, err := val.MarshalJSON() 103 require.NoError(t, err) 104 actual := yaml2Object(t, string(b)) 105 assert.EqualValues(t, input, actual) 106 }) 107 } 108 109 func TestDebugRemoveConnectionDetails(t *testing.T) { 110 f, err := New(Options{}) 111 require.NoError(t, err) 112 inputYAML := ` 113 - resource: 114 resource: 115 apiVersion: v1 116 kind: ConfigMap 117 metadata: 118 name: foobar 119 data: 120 foo: bar 121 connectionDetails: 122 secret1: super-secret 123 secret2: even-more-secret 124 ` 125 redacted := []byte(base64.StdEncoding.EncodeToString([]byte("<redacted>"))) 126 cleanedYAML := fmt.Sprintf(` 127 - resource: 128 resource: 129 apiVersion: v1 130 kind: ConfigMap 131 metadata: 132 name: foobar 133 data: 134 foo: bar 135 connectionDetails: 136 secret1: %s 137 secret2: %s 138 `, redacted, redacted) 139 input := yaml2Object(t, inputYAML) 140 cleaned := yaml2Object(t, cleanedYAML) 141 inBytes, err := json.MarshalIndent(input, "", " ") 142 require.NoError(t, err) 143 out, err := f.reserialize(inBytes, false) 144 require.NoError(t, err) 145 actual := yaml2Object(t, string(out)) 146 assert.EqualValues(t, cleaned, actual) 147 } 148 149 func TestDebugBadJSON(t *testing.T) { 150 f, err := New(Options{}) 151 require.NoError(t, err) 152 s := f.getDebugString([]byte("{ foo:"), true) 153 assert.EqualValues(t, "{ foo:", s) 154 }