| 1 |
/* |
| 2 |
Copyright 2022 The Kubernetes 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 jsontesting |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"encoding/json" |
| 22 |
"fmt" |
| 23 |
"reflect" |
| 24 |
"strings" |
| 25 |
|
| 26 |
kjson "sigs.k8s.io/json" |
| 27 |
|
| 28 |
"k8s.io/kube-openapi/pkg/internal" |
| 29 |
|
| 30 |
"github.com/go-openapi/jsonreference" |
| 31 |
"github.com/google/go-cmp/cmp" |
| 32 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 33 |
) |
| 34 |
|
| 35 |
func JsonCompare(got, want []byte) error { |
| 36 |
if d := cmp.Diff(got, want, cmp.Transformer("JSONBytes", func(in []byte) (out interface{}) { |
| 37 |
if strictErrors, err := kjson.UnmarshalStrict(in, &out); strictErrors != nil || err != nil { |
| 38 |
return in |
| 39 |
} |
| 40 |
return out |
| 41 |
})); d != "" { |
| 42 |
return fmt.Errorf("JSON mismatch (-got +want):\n%s", d) |
| 43 |
} |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
type RoundTripTestCase struct { |
| 48 |
Name string |
| 49 |
JSON string |
| 50 |
Object json.Marshaler |
| 51 |
|
| 52 |
// An error that is expected when `Object` is marshalled to json |
| 53 |
// If `Object` does not exist, then it is inferred from the provided JSON |
| 54 |
ExpectedMarshalError string |
| 55 |
|
| 56 |
// An error that is expected when the provided JSON is unmarshalled |
| 57 |
// If `JSON` does not exist then this it is inferred from the provided `Object` |
| 58 |
ExpectedUnmarshalError string |
| 59 |
} |
| 60 |
|
| 61 |
type MarshalerUnmarshaler interface { |
| 62 |
json.Unmarshaler |
| 63 |
json.Marshaler |
| 64 |
} |
| 65 |
|
| 66 |
func (t RoundTripTestCase) RoundTripTest(example MarshalerUnmarshaler) error { |
| 67 |
var jsonBytes []byte |
| 68 |
var err error |
| 69 |
|
| 70 |
// Tests whether the provided error matches the given pattern, and says |
| 71 |
// whether the test is finished, and the error to return |
| 72 |
expectError := func(e error, name string, expected string) (testFinished bool, err error) { |
| 73 |
if len(expected) > 0 { |
| 74 |
if e == nil || !strings.Contains(e.Error(), expected) { |
| 75 |
return true, fmt.Errorf("expected %v error containing substring: '%s'. but got actual error '%v'", name, expected, e) |
| 76 |
} |
| 77 |
|
| 78 |
// If an error was expected and achieved, we stop the test |
| 79 |
// since it cannot be continued. But the return nil error since it |
| 80 |
// was expected. |
| 81 |
return true, nil |
| 82 |
} else if e != nil { |
| 83 |
return true, fmt.Errorf("unexpected %v error: %w", name, e) |
| 84 |
} |
| 85 |
|
| 86 |
return false, nil |
| 87 |
} |
| 88 |
|
| 89 |
// If user did not provide JSON and instead provided Object, infer JSON |
| 90 |
// from the provided object. |
| 91 |
if len(t.JSON) == 0 { |
| 92 |
jsonBytes, err = json.Marshal(t.Object) |
| 93 |
if testFinished, err := expectError(err, "marshal", t.ExpectedMarshalError); testFinished { |
| 94 |
return err |
| 95 |
} |
| 96 |
|
| 97 |
// If no JSON fixture was provided, ensure optimized marshaling produces identical json to unoptimized |
| 98 |
original := internal.UseOptimizedJSONMarshaling |
| 99 |
originalV3 := internal.UseOptimizedJSONMarshalingV3 |
| 100 |
internal.UseOptimizedJSONMarshaling = false |
| 101 |
internal.UseOptimizedJSONMarshalingV3 = false |
| 102 |
unoptimizedJSONBytes, err := json.Marshal(t.Object) |
| 103 |
if err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
internal.UseOptimizedJSONMarshaling = original |
| 107 |
internal.UseOptimizedJSONMarshalingV3 = originalV3 |
| 108 |
|
| 109 |
// round-trip to normalize order |
| 110 |
optimizedObj := map[string]any{} |
| 111 |
if err := json.Unmarshal(jsonBytes, &optimizedObj); err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
optimizedBytes, err := json.Marshal(optimizedObj) |
| 115 |
if err != nil { |
| 116 |
return err |
| 117 |
} |
| 118 |
unoptimizedObj := map[string]any{} |
| 119 |
if err := json.Unmarshal(unoptimizedJSONBytes, &unoptimizedObj); err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
unoptimizedBytes, err := json.Marshal(unoptimizedObj) |
| 123 |
if err != nil { |
| 124 |
return err |
| 125 |
} |
| 126 |
|
| 127 |
// Ensure optimized bytes match unoptimized bytes |
| 128 |
if !bytes.Equal(optimizedBytes, unoptimizedBytes) { |
| 129 |
return fmt.Errorf("optimized marshal differs from unoptimized marshal:\n%s", cmp.Diff(string(optimizedBytes), string(unoptimizedBytes))) |
| 130 |
} |
| 131 |
} else { |
| 132 |
jsonBytes = []byte(t.JSON) |
| 133 |
} |
| 134 |
|
| 135 |
err = example.UnmarshalJSON(jsonBytes) |
| 136 |
if testFinished, err := expectError(err, "unmarshal", t.ExpectedUnmarshalError); testFinished { |
| 137 |
return err |
| 138 |
} |
| 139 |
|
| 140 |
if t.Object != nil && !reflect.DeepEqual(t.Object, example) { |
| 141 |
return fmt.Errorf("test case expected to unmarshal to specific value: %v", cmp.Diff(t.Object, example, cmpopts.IgnoreUnexported(jsonreference.Ref{}))) |
| 142 |
} |
| 143 |
|
| 144 |
reEncoded, err := json.Marshal(example) |
| 145 |
if err != nil { |
| 146 |
return fmt.Errorf("failed to marshal decoded value: %w", err) |
| 147 |
} |
| 148 |
|
| 149 |
// Check expected marshal error if it has not yet been checked |
| 150 |
// (for case where JSON is provided, and object is not) |
| 151 |
if testFinished, err := expectError(err, "marshal", t.ExpectedMarshalError); testFinished { |
| 152 |
return err |
| 153 |
} |
| 154 |
// Marshal both re-encoded, and original JSON into interface |
| 155 |
// to compare them without ordering issues |
| 156 |
var expected map[string]interface{} |
| 157 |
var actual map[string]interface{} |
| 158 |
|
| 159 |
if err = json.Unmarshal(jsonBytes, &expected); err != nil { |
| 160 |
return fmt.Errorf("failed to unmarshal test json: %w", err) |
| 161 |
} |
| 162 |
|
| 163 |
if err = json.Unmarshal(reEncoded, &actual); err != nil { |
| 164 |
return fmt.Errorf("failed to unmarshal actual data: %w", err) |
| 165 |
} |
| 166 |
|
| 167 |
if !reflect.DeepEqual(expected, actual) { |
| 168 |
return fmt.Errorf("expected equal values: %v", cmp.Diff(expected, actual)) |
| 169 |
} |
| 170 |
|
| 171 |
return nil |
| 172 |
} |