-
Notifications
You must be signed in to change notification settings - Fork 89
/
yaml_test.go
191 lines (167 loc) · 4.97 KB
/
yaml_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils
import (
"io/ioutil"
"os"
"path/filepath"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
)
type yamlSuite struct {
}
var _ = gc.Suite(&yamlSuite{})
func (*yamlSuite) TestYamlRoundTrip(c *gc.C) {
// test happy path of round tripping an object via yaml
type T struct {
A int `yaml:"a"`
B bool `yaml:"deleted"`
C string `yaml:"omitempty"`
D string
}
v := T{A: 1, B: true, C: "", D: ""}
f, err := ioutil.TempFile(c.MkDir(), "yaml")
c.Assert(err, gc.IsNil)
path := f.Name()
f.Close()
err = WriteYaml(path, v)
c.Assert(err, gc.IsNil)
var v2 T
err = ReadYaml(path, &v2)
c.Assert(err, gc.IsNil)
c.Assert(v, gc.Equals, v2)
}
func (*yamlSuite) TestReadYamlReturnsNotFound(c *gc.C) {
// The contract for ReadYaml requires it returns an error
// that can be inspected by os.IsNotExist. Notably, we cannot
// use juju/errors gift wrapping.
f, err := ioutil.TempFile(c.MkDir(), "yaml")
c.Assert(err, gc.IsNil)
path := f.Name()
err = os.Remove(path)
c.Assert(err, gc.IsNil)
err = ReadYaml(path, nil)
// assert that the error is reported as NotExist
c.Assert(os.IsNotExist(err), gc.Equals, true)
}
func (*yamlSuite) TestWriteYamlMissingDirectory(c *gc.C) {
// WriteYaml tries to create a temporary file in the same
// directory as the target. Test what happens if the path's
// directory is missing
root := c.MkDir()
missing := filepath.Join(root, "missing", "filename")
v := struct{ A, B int }{1, 2}
err := WriteYaml(missing, v)
c.Assert(err, gc.NotNil)
}
func (*yamlSuite) TestWriteYamlWriteGarbage(c *gc.C) {
c.Skip("https://github.com/go-yaml/yaml/issues/144")
// some things cannot be marshalled into yaml, check that
// WriteYaml detects this.
root := c.MkDir()
path := filepath.Join(root, "f")
v := struct{ A, B [10]bool }{}
err := WriteYaml(path, v)
c.Assert(err, gc.NotNil)
}
type ConformSuite struct{}
var _ = gc.Suite(&ConformSuite{})
func (s *ConformSuite) TestConformYAML(c *gc.C) {
var goodInterfaceTests = []struct {
description string
inputInterface interface{}
expectedInterface map[string]interface{}
expectedError string
}{{
description: "An interface requiring no changes.",
inputInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[string]interface{}{
"foo1": "val1",
"foo2": "val2"}},
expectedInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[string]interface{}{
"foo1": "val1",
"foo2": "val2"}},
}, {
description: "Substitute a single inner map[i]i.",
inputInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[interface{}]interface{}{
"foo1": "val1",
"foo2": "val2"}},
expectedInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[string]interface{}{
"foo1": "val1",
"foo2": "val2"}},
}, {
description: "Substitute nested inner map[i]i.",
inputInterface: map[string]interface{}{
"key1a": "val1a",
"key2a": "val2a",
"key3a": map[interface{}]interface{}{
"key1b": "val1b",
"key2b": map[interface{}]interface{}{
"key1c": "val1c"}}},
expectedInterface: map[string]interface{}{
"key1a": "val1a",
"key2a": "val2a",
"key3a": map[string]interface{}{
"key1b": "val1b",
"key2b": map[string]interface{}{
"key1c": "val1c"}}},
}, {
description: "Substitute nested map[i]i within []i.",
inputInterface: map[string]interface{}{
"key1a": "val1a",
"key2a": []interface{}{5, "foo", map[string]interface{}{
"key1b": "val1b",
"key2b": map[interface{}]interface{}{
"key1c": "val1c"}}}},
expectedInterface: map[string]interface{}{
"key1a": "val1a",
"key2a": []interface{}{5, "foo", map[string]interface{}{
"key1b": "val1b",
"key2b": map[string]interface{}{
"key1c": "val1c"}}}},
}, {
description: "An inner map[interface{}]interface{} with an int key.",
inputInterface: map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": map[interface{}]interface{}{
"foo1": "val1",
5: "val2"}},
expectedError: "map keyed with non-string value",
}, {
description: "An inner []interface{} containing a map[i]i with an int key.",
inputInterface: map[string]interface{}{
"key1a": "val1b",
"key2a": "val2b",
"key3a": []interface{}{"foo1", 5, map[interface{}]interface{}{
"key1b": "val1b",
"key2b": map[interface{}]interface{}{
"key1c": "val1c",
5: "val2c"}}}},
expectedError: "map keyed with non-string value",
}}
for i, test := range goodInterfaceTests {
c.Logf("test %d: %s", i, test.description)
input := test.inputInterface
cleansedInterfaceMap, err := ConformYAML(input)
if test.expectedError == "" {
if !c.Check(err, jc.ErrorIsNil) {
continue
}
c.Check(cleansedInterfaceMap, jc.DeepEquals, test.expectedInterface)
} else {
c.Check(err, gc.ErrorMatches, test.expectedError)
}
}
}