@@ -17,6 +17,7 @@ limitations under the License.
17
17
package yaml
18
18
19
19
import (
20
+ "bytes"
20
21
"encoding/json"
21
22
"fmt"
22
23
"math"
@@ -26,7 +27,8 @@ import (
26
27
"testing"
27
28
28
29
"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"
30
32
)
31
33
32
34
/* Test helper functions */
@@ -128,7 +130,7 @@ func testYAMLToJSON(t *testing.T, f testYAMLToJSONFunc, tests map[string]yamlToJ
128
130
// Convert Yaml to Json
129
131
jsonBytes , err := f ([]byte (test .yaml ))
130
132
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 )
132
134
}
133
135
if err == nil && test .err & fatalErrorsType != 0 {
134
136
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) {
785
787
tests := []struct {
786
788
name string
787
789
input map [string ]interface {}
788
- expected yaml .MapSlice
790
+ expected yamlv2 .MapSlice
789
791
}{
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 )},
792
794
{
793
795
name : "values" ,
794
796
input : map [string ]interface {}{
@@ -808,19 +810,19 @@ func TestJSONObjectToYAMLObject(t *testing.T) {
808
810
"string" : string ("foo" ),
809
811
"uint64 big" : bigUint64 ,
810
812
},
811
- expected : yaml .MapSlice {
813
+ expected : yamlv2 .MapSlice {
812
814
{Key : "nil slice" },
813
815
{Key : "nil map" },
814
816
{Key : "empty slice" , Value : []interface {}{}},
815
- {Key : "empty map" , Value : yaml .MapSlice (nil )},
817
+ {Key : "empty map" , Value : yamlv2 .MapSlice (nil )},
816
818
{Key : "bool" , Value : true },
817
819
{Key : "float64" , Value : float64 (42.1 )},
818
820
{Key : "fractionless" , Value : int (42 )},
819
821
{Key : "int" , Value : int (42 )},
820
822
{Key : "int64" , Value : int (42 )},
821
823
{Key : "int64 big" , Value : intOrInt64 (int64 (1 ) << 62 )},
822
824
{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" }}},
824
826
{Key : "slice" , Value : []interface {}{"foo" , "bar" }},
825
827
{Key : "string" , Value : string ("foo" )},
826
828
{Key : "uint64 big" , Value : bigUint64 },
@@ -840,12 +842,12 @@ func TestJSONObjectToYAMLObject(t *testing.T) {
840
842
if err != nil {
841
843
t .Fatalf ("unexpected json.Marshal error: %v" , err )
842
844
}
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 {
845
847
t .Fatalf ("unexpected yaml.Unmarshal error: %v" , err )
846
848
}
847
849
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.
849
851
// Replicate this here in the test, but don't change the type.
850
852
for i := range got {
851
853
switch got [i ].Key {
@@ -885,9 +887,65 @@ func sortMapSlicesInPlace(x interface{}) {
885
887
for i := range x {
886
888
sortMapSlicesInPlace (x [i ])
887
889
}
888
- case yaml .MapSlice :
890
+ case yamlv2 .MapSlice :
889
891
sort .Slice (x , func (a , b int ) bool {
890
892
return x [a ].Key .(string ) < x [b ].Key .(string )
891
893
})
892
894
}
893
895
}
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\n got\n %s" , string (v2output ), v3output )
950
+ }
951
+ }
0 commit comments