Skip to content

Commit f2c0229

Browse files
committed
add test demonstrating consistency between yaml.v2 and yaml.v3
1 parent 20d25fa commit f2c0229

File tree

1 file changed

+70
-12
lines changed

1 file changed

+70
-12
lines changed

yaml_test.go

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package yaml
1818

1919
import (
20+
"bytes"
2021
"encoding/json"
2122
"fmt"
2223
"math"
@@ -26,7 +27,8 @@ import (
2627
"testing"
2728

2829
"github.com/google/go-cmp/cmp"
29-
yaml "sigs.k8s.io/yaml/goyaml.v2"
30+
yamlv2 "sigs.k8s.io/yaml/goyaml.v2"
31+
yamlv3 "sigs.k8s.io/yaml/goyaml.v3"
3032
)
3133

3234
/* Test helper functions */
@@ -128,7 +130,7 @@ func testYAMLToJSON(t *testing.T, f testYAMLToJSONFunc, tests map[string]yamlToJ
128130
// Convert Yaml to Json
129131
jsonBytes, err := f([]byte(test.yaml))
130132
if err != nil && test.err == noErrorsType {
131-
t.Errorf("Failed to convert YAML to JSON, yaml: `%s`, err: %v", test.yaml, err)
133+
t.Errorf("Failed to convert YAML to JSON, yamlv2: `%s`, err: %v", test.yaml, err)
132134
}
133135
if err == nil && test.err&fatalErrorsType != 0 {
134136
t.Errorf("expected a fatal error, but no fatal error was returned, yaml: `%s`", test.yaml)
@@ -785,10 +787,10 @@ func TestJSONObjectToYAMLObject(t *testing.T) {
785787
tests := []struct {
786788
name string
787789
input map[string]interface{}
788-
expected yaml.MapSlice
790+
expected yamlv2.MapSlice
789791
}{
790-
{name: "nil", expected: yaml.MapSlice(nil)},
791-
{name: "empty", input: map[string]interface{}{}, expected: yaml.MapSlice(nil)},
792+
{name: "nil", expected: yamlv2.MapSlice(nil)},
793+
{name: "empty", input: map[string]interface{}{}, expected: yamlv2.MapSlice(nil)},
792794
{
793795
name: "values",
794796
input: map[string]interface{}{
@@ -808,19 +810,19 @@ func TestJSONObjectToYAMLObject(t *testing.T) {
808810
"string": string("foo"),
809811
"uint64 big": bigUint64,
810812
},
811-
expected: yaml.MapSlice{
813+
expected: yamlv2.MapSlice{
812814
{Key: "nil slice"},
813815
{Key: "nil map"},
814816
{Key: "empty slice", Value: []interface{}{}},
815-
{Key: "empty map", Value: yaml.MapSlice(nil)},
817+
{Key: "empty map", Value: yamlv2.MapSlice(nil)},
816818
{Key: "bool", Value: true},
817819
{Key: "float64", Value: float64(42.1)},
818820
{Key: "fractionless", Value: int(42)},
819821
{Key: "int", Value: int(42)},
820822
{Key: "int64", Value: int(42)},
821823
{Key: "int64 big", Value: intOrInt64(int64(1) << 62)},
822824
{Key: "negative int64 big", Value: intOrInt64(-(1 << 62))},
823-
{Key: "map", Value: yaml.MapSlice{{Key: "foo", Value: "bar"}}},
825+
{Key: "map", Value: yamlv2.MapSlice{{Key: "foo", Value: "bar"}}},
824826
{Key: "slice", Value: []interface{}{"foo", "bar"}},
825827
{Key: "string", Value: string("foo")},
826828
{Key: "uint64 big", Value: bigUint64},
@@ -840,12 +842,12 @@ func TestJSONObjectToYAMLObject(t *testing.T) {
840842
if err != nil {
841843
t.Fatalf("unexpected json.Marshal error: %v", err)
842844
}
843-
var gotByRoundtrip yaml.MapSlice
844-
if err := yaml.Unmarshal(jsonBytes, &gotByRoundtrip); err != nil {
845+
var gotByRoundtrip yamlv2.MapSlice
846+
if err := yamlv2.Unmarshal(jsonBytes, &gotByRoundtrip); err != nil {
845847
t.Fatalf("unexpected yaml.Unmarshal error: %v", err)
846848
}
847849

848-
// yaml.Unmarshal loses precision, it's rounding to the 4th last digit.
850+
// yamlv2.Unmarshal loses precision, it's rounding to the 4th last digit.
849851
// Replicate this here in the test, but don't change the type.
850852
for i := range got {
851853
switch got[i].Key {
@@ -885,9 +887,65 @@ func sortMapSlicesInPlace(x interface{}) {
885887
for i := range x {
886888
sortMapSlicesInPlace(x[i])
887889
}
888-
case yaml.MapSlice:
890+
case yamlv2.MapSlice:
889891
sort.Slice(x, func(a, b int) bool {
890892
return x[a].Key.(string) < x[b].Key.(string)
891893
})
892894
}
893895
}
896+
897+
func TestPatchedYamlV3AndUpstream(t *testing.T) {
898+
input := `group: apps
899+
apiVersion: v1
900+
kind: Deployment
901+
metadata:
902+
name: deploy1
903+
spec:
904+
template:
905+
spec:
906+
containers:
907+
- image: nginx:1.7.9
908+
name: nginx-tagged
909+
- image: nginx:latest
910+
name: nginx-latest
911+
- image: foobar:1
912+
name: replaced-with-digest
913+
- image: postgres:1.8.0
914+
name: postgresdb
915+
initContainers:
916+
- image: nginx
917+
name: nginx-notag
918+
- image: nginx@sha256:111111111111111111
919+
name: nginx-sha256
920+
- image: alpine:1.8.0
921+
name: init-alpine
922+
`
923+
924+
var v3Map map[string]interface{}
925+
var v2Map map[string]interface{}
926+
927+
// unmarshal the input into the two maps
928+
if err := yamlv3.Unmarshal([]byte(input), &v3Map); err != nil {
929+
t.Fatal(err)
930+
}
931+
if err := yamlv2.Unmarshal([]byte(input), &v2Map); err != nil {
932+
t.Fatal(err)
933+
}
934+
935+
// marshal using non-default settings from the yaml v3 fork
936+
var buf bytes.Buffer
937+
enc := yamlv3.NewEncoder(&buf)
938+
enc.CompactSeqIndent()
939+
enc.SetIndent(2)
940+
err := enc.Encode(v3Map)
941+
v3output := buf.String()
942+
943+
// marshal using the yaml v2 fork
944+
v2output, err := yamlv2.Marshal(v2Map)
945+
if err != nil {
946+
t.Fatal(err)
947+
}
948+
if v3output != string(v2output) {
949+
t.Fatalf("expected\n%s\ngot\n%s", string(v2output), v3output)
950+
}
951+
}

0 commit comments

Comments
 (0)