-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode_test.go
237 lines (211 loc) · 5.75 KB
/
node_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
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
237
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package eventlogger
import (
"context"
"errors"
"testing"
"github.com/go-test/deep"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestLinkNodes ensures that we are able to create a graph of linked nodes correctly.
// NOTE: This test should not be run in parallel as it sets a package level variable
// on 'deep' to ensure we compare unexported fields too.
func TestLinkNodes(t *testing.T) {
n1, n2, n3 := &Filter{Predicate: nil}, &JSONFormatter{}, &FileSink{Path: "test.log"}
root, err := linkNodes([]Node{n1, n2, n3}, []NodeID{"1", "2", "3"})
if err != nil {
t.Fatal(err)
}
expected := &linkedNode{
node: n1,
nodeID: "1",
next: []*linkedNode{{
node: n2,
nodeID: "2",
next: []*linkedNode{{
node: n3,
nodeID: "3",
}},
}},
}
deep.CompareUnexportedFields = true
t.Cleanup(func() { deep.CompareUnexportedFields = false })
if diff := deep.Equal(root, expected); len(diff) > 0 {
t.Fatal(diff)
}
}
// TestLinkNodesErrors attempts to exercise the linkNodes func such that we hit
// the early return error checking on the incoming parameters.
func TestLinkNodesErrors(t *testing.T) {
tests := map[string]struct {
nodes []Node
ids []NodeID
wantErrorMessage string
}{
"nil-nodes": {
nodes: nil,
ids: []NodeID{"1", "2", "3"},
wantErrorMessage: "no nodes given",
},
"no-nodes": {
nodes: []Node{},
ids: []NodeID{"1", "2", "3"},
wantErrorMessage: "no nodes given",
},
"nil-ids": {
nodes: []Node{
&Filter{Predicate: nil}, &JSONFormatter{}, &FileSink{Path: "test.log"},
},
ids: nil,
wantErrorMessage: "no IDs given",
},
"no-ids": {
nodes: []Node{
&Filter{Predicate: nil}, &JSONFormatter{}, &FileSink{Path: "test.log"},
},
ids: []NodeID{},
wantErrorMessage: "no IDs given",
},
"more-nodes-than-ids": {
nodes: []Node{
&Filter{Predicate: nil}, &JSONFormatter{}, &FileSink{Path: "test.log"},
},
ids: []NodeID{"1", "2"},
wantErrorMessage: "number of nodes does not match number of IDs",
},
"less-nodes-than-ids": {
nodes: []Node{
&Filter{Predicate: nil}, &JSONFormatter{}, &FileSink{Path: "test.log"},
},
ids: []NodeID{"1", "2", "3", "4"},
wantErrorMessage: "number of nodes does not match number of IDs",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
_, err := linkNodes(tc.nodes, tc.ids)
require.Error(t, err)
require.EqualError(t, err, tc.wantErrorMessage)
})
}
}
// TestFlattenNodes tests that given a 'root' node we can correctly flatten it
// out to retrieve the NodeIDs of linked nodes.
func TestFlattenNodes_LinkNodes(t *testing.T) {
ids := []NodeID{"1", "2", "3"}
nodes := []Node{
&Filter{Predicate: nil}, &JSONFormatter{}, &FileSink{Path: "test.log"},
}
linkedNodes, err := linkNodes(nodes, ids)
require.NoError(t, err)
flatNodes := linkedNodes.flatten()
require.Contains(t, flatNodes, NodeID("1"))
require.Contains(t, flatNodes, NodeID("2"))
require.Contains(t, flatNodes, NodeID("3"))
require.Equal(t, 3, len(flatNodes))
}
// TestFlattenNodes_LinkNodesAndSinks tests that given a more complex set of linked
// nodes we can still get the right set of registered nodes.
func TestFlattenNodes_LinkNodesAndSinks(t *testing.T) {
ids := []NodeID{"1", "2"}
nodes := []Node{
&Filter{Predicate: nil}, &JSONFormatter{},
}
sinkIds := []NodeID{"x", "y", "z"}
sinkNodes := []Node{
&FileSink{Path: "test.log"}, &FileSink{Path: "foo.log"}, &FileSink{Path: "bar.log"},
}
linkedNodes, err := linkNodesAndSinks(nodes, sinkNodes, ids, sinkIds)
require.NoError(t, err)
flatNodes := linkedNodes.flatten()
require.Contains(t, flatNodes, NodeID("1"))
require.Contains(t, flatNodes, NodeID("2"))
require.Contains(t, flatNodes, NodeID("x"))
require.Contains(t, flatNodes, NodeID("y"))
require.Contains(t, flatNodes, NodeID("z"))
require.Equal(t, 5, len(flatNodes))
}
func TestNodeController(t *testing.T) {
t.Parallel()
ctx := context.Background()
tests := []struct {
name string
node Node
wantClosed bool
wantErrContains string
}{
{
name: "closer",
node: &mockCloser{},
},
{
name: "closer-with-error",
node: &mockCloser{closeErr: errors.New("closer error")},
wantErrContains: "closer error",
},
{
name: "unwrapped",
node: &mockCloserWithWrapper{
n: &mockCloser{},
},
wantClosed: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
nc := NewNodeController(tc.node)
err := nc.Close(ctx)
if tc.wantErrContains != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.wantErrContains)
return
}
require.NoError(t, err)
if tc.wantClosed {
switch tp := tc.node.(type) {
case *mockCloser:
assert.True(t, tp.closed)
case *mockCloserWithWrapper:
assert.True(t, tp.n.closed)
default:
t.Errorf("unexpected type: %t", tc.node)
}
}
})
}
}
var (
_ Closer = (*mockCloser)(nil)
_ NodeUnwrapper = (*mockCloserWithWrapper)(nil)
)
type mockCloser struct {
Node
closeErr error
closed bool
}
func (m *mockCloser) Close(ctx context.Context) error {
switch {
case m.closeErr != nil:
return m.closeErr
default:
m.closed = true
return nil
}
}
type mockCloserWithWrapper struct {
n *mockCloser
}
func (m *mockCloserWithWrapper) Unwrap() Node {
return m.n
}
func (m *mockCloserWithWrapper) Process(ctx context.Context, e *Event) (*Event, error) {
panic("unimplemented")
}
func (m *mockCloserWithWrapper) Reopen() error {
panic("unimplemented")
}
func (m *mockCloserWithWrapper) Type() NodeType {
return NodeTypeSink
}