github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/kubernetes/provider/specs/decode_test.go (about) 1 // Copyright 2019 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package specs_test 5 6 import ( 7 "strings" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 k8sspces "github.com/juju/juju/caas/kubernetes/provider/specs" 13 "github.com/juju/juju/testing" 14 ) 15 16 type decoderSuite struct { 17 testing.BaseSuite 18 } 19 20 var _ = gc.Suite(&decoderSuite{}) 21 22 func (s *decoderSuite) TestYAMLOrJSONDecoder(c *gc.C) { 23 type tS struct { 24 Foo string `json:"foo,omitempty" yaml:"foo,omitempty"` 25 Bar string `json:"bar,omitempty" yaml:"bar,omitempty"` 26 } 27 28 var in string 29 var out tS 30 31 in = ` 32 foo: foo1 33 bar: bar1` 34 // decode YAML in strict mode - good. 35 decoder := k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), true) 36 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 37 c.Assert(out, gc.DeepEquals, tS{ 38 Foo: "foo1", 39 Bar: "bar1", 40 }) 41 // decode YAML in non-strict mode - good. 42 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), false) 43 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 44 c.Assert(out, gc.DeepEquals, tS{ 45 Foo: "foo1", 46 Bar: "bar1", 47 }) 48 49 in = ` 50 { 51 "foo": "foo1", 52 "bar": "bar1" 53 }` 54 // decode JSON in strict mode - good. 55 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), true) 56 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 57 c.Assert(out, gc.DeepEquals, tS{ 58 Foo: "foo1", 59 Bar: "bar1", 60 }) 61 // decode JSON in non-strict mode - good. 62 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), false) 63 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 64 c.Assert(out, gc.DeepEquals, tS{ 65 Foo: "foo1", 66 Bar: "bar1", 67 }) 68 69 in = ` 70 foo: foo1 71 bar: bar1 72 unknownkey: ops` 73 // decode YAML in strict mode - bad. 74 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), true) 75 c.Assert(decoder.Decode(&out), gc.ErrorMatches, `json: unknown field "unknownkey"`) 76 // decode YAML in non-strict mode - unknown field ignored. 77 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), false) 78 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 79 c.Assert(out, gc.DeepEquals, tS{ 80 Foo: "foo1", 81 Bar: "bar1", 82 }) 83 84 in = ` 85 { 86 "foo": "foo1", 87 "bar": "bar1", 88 "unknownkey": "ops" 89 }` 90 // decode JSON in strict mode - bad. 91 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), true) 92 c.Assert(decoder.Decode(&out), gc.ErrorMatches, `json: unknown field "unknownkey"`) 93 // decode JSON in non-strict mode - unknown field ignored. 94 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), false) 95 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 96 c.Assert(out, gc.DeepEquals, tS{ 97 Foo: "foo1", 98 Bar: "bar1", 99 }) 100 101 in = ` 102 { 103 "foo": "foo1" 104 } 105 { 106 "bar": "bar1" 107 }` 108 // decode JSON in strict mode - good. 109 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), true) 110 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 111 c.Assert(out, gc.DeepEquals, tS{ 112 Foo: "foo1", 113 Bar: "bar1", 114 }) 115 // decode JSON in non-strict mode - good. 116 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), false) 117 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 118 c.Assert(out, gc.DeepEquals, tS{ 119 Foo: "foo1", 120 Bar: "bar1", 121 }) 122 123 in = ` 124 { 125 "foo": "foo1" 126 } 127 { 128 "bar": "bar1", 129 "unknownkey": "ops" 130 }` 131 // decode JSON in strict mode - bad. 132 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), true) 133 c.Assert(decoder.Decode(&out), gc.ErrorMatches, `json: unknown field "unknownkey"`) 134 // decode JSON in non-strict mode - unknown field ignored. 135 decoder = k8sspces.NewYAMLOrJSONDecoder(strings.NewReader(in), len(in), false) 136 c.Assert(decoder.Decode(&out), jc.ErrorIsNil) 137 c.Assert(out, gc.DeepEquals, tS{ 138 Foo: "foo1", 139 Bar: "bar1", 140 }) 141 }