-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_test.go
More file actions
236 lines (178 loc) · 4.86 KB
/
stream_test.go
File metadata and controls
236 lines (178 loc) · 4.86 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package yamlstream_test
import (
"os"
"testing"
yamlstream "github.com/jdockerty/yaml-stream"
"github.com/stretchr/testify/assert"
)
const (
simpleYAML = "testdata/simple.yaml"
simpleYAMLStream = "testdata/simple_stream.yaml"
simpleYAMLStreamOne = "testdata/simple_stream_one.yaml"
simpleYAMLStreamTwo = "testdata/simple_stream_two.yaml"
simpleYAMLStreamThree = "testdata/simple_stream_three.yaml"
complexYAML = "testdata/complex.yaml"
)
func TestNewWithDefaults(t *testing.T) {
ys := yamlstream.New()
assert.IsType(t, ys, &yamlstream.Stream{})
assert.Equal(t, ys, &yamlstream.Stream{Count: 0, Stream: nil})
}
func TestStreamCount(t *testing.T) {
tests := []struct {
Name string
Filename string
Expected int
}{
{
Name: "is correct for simple yaml file",
Filename: simpleYAML,
Expected: 1,
},
{
Name: "is correct for simple yaml stream file",
Filename: simpleYAMLStream,
Expected: 3,
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
ys := yamlstream.New()
f, _ := os.Open(tc.Filename)
defer f.Close()
err := ys.Read(f)
assert.Nil(t, err)
assert.Equal(t, tc.Expected, ys.Count)
})
}
}
func TestBytes(t *testing.T) {
ys := yamlstream.New()
f, _ := os.Open(simpleYAMLStream)
defer f.Close()
_ = ys.Read(f)
assert.IsType(t, ys.Bytes(), make([]byte, 0))
}
func TestReadEquality(t *testing.T) {
tests := []struct {
Name string
Filename string
}{
{
Name: "is equal for yaml stream",
Filename: simpleYAMLStream,
},
{
Name: "is equal for regular yaml file",
Filename: simpleYAML,
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
ys := yamlstream.New()
f, _ := os.Open(tc.Filename)
defer f.Close()
err := ys.Read(f)
assert.Nil(t, err)
fileAsBytes, _ := os.ReadFile(tc.Filename)
assert.Equal(t, fileAsBytes, ys.Bytes())
})
}
}
func TestReadWithOpen(t *testing.T) {
ys := yamlstream.New()
err := ys.ReadWithOpen(simpleYAMLStream)
assert.Nil(t, err)
assert.Equal(t, 3, ys.Count)
}
func TestStreamGet(t *testing.T) {
tests := []struct {
Name string
YAMLStreamFilename string
SegmentedFilename string
Index int
}{
{
Name: "is correct for 0th index",
YAMLStreamFilename: simpleYAMLStream,
SegmentedFilename: simpleYAMLStreamOne,
Index: 0,
},
{
Name: "is correct for 1st index",
YAMLStreamFilename: simpleYAMLStream,
SegmentedFilename: simpleYAMLStreamTwo,
Index: 1,
},
{
Name: "is correct for 2nd index",
YAMLStreamFilename: simpleYAMLStream,
SegmentedFilename: simpleYAMLStreamThree,
Index: 2,
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
ys := yamlstream.New()
f, _ := os.Open(tc.YAMLStreamFilename)
defer f.Close()
err := ys.Read(f)
assert.Nil(t, err)
expectedAsBytes, _ := os.ReadFile(tc.SegmentedFilename)
yamlDoc := ys.Get(tc.Index)
assert.Equal(t, string(expectedAsBytes), yamlDoc.String())
})
}
}
func TestStreamGetUnmarshal(t *testing.T) {
ys := yamlstream.New()
f, _ := os.Open(simpleYAMLStream)
err := ys.Read(f)
assert.Nil(t, err)
var streamOne map[string]int
err = ys.GetUnmarshal(0, &streamOne)
assert.Nil(t, err)
assert.Equal(t, 1, streamOne["stream_number"])
var streamTwo map[string]int
err = ys.GetUnmarshal(1, &streamTwo)
assert.Nil(t, err)
assert.Equal(t, 2, streamTwo["stream_number"])
}
func TestStreamGetUnmarshalWithComplexType(t *testing.T) {
ys := yamlstream.New()
f, _ := os.Open(complexYAML)
err := ys.Read(f)
assert.Nil(t, err)
// Unfortunately we cannot use the actually appsv1.Deployment here as the
// Kubernetes objects have specific runtime information encoded into them;
// however, we can still use it as a partial struct in order to create the
// case of a more complicated YAML document.
//
// This is also a manifest I control within the jdockerty/oomer project.
var partialDeployment struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata struct {
Namespace string `yaml:"namespace"`
Name string `yaml:"name"`
Labels struct{}
} `yaml:"metadata"`
Spec struct {
Replicas int `yaml:"replicas"`
} `yaml:"spec"`
}
err = ys.GetUnmarshal(0, &partialDeployment)
assert.Equal(t, "apps/v1", partialDeployment.ApiVersion)
assert.Equal(t, "Deployment", partialDeployment.Kind)
assert.Equal(t, "oomkilled", partialDeployment.Metadata.Namespace)
assert.Equal(t, 2, partialDeployment.Spec.Replicas)
}
func TestStreamGetUnmarshalFailsWithoutPointer(t *testing.T) {
ys := yamlstream.New()
f, _ := os.Open(simpleYAMLStreamOne)
err := ys.Read(f)
assert.Nil(t, err)
var streamOne map[string]string
err = ys.GetUnmarshal(0, streamOne)
assert.Error(t, err)
}