-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathgraph_node_test.go
365 lines (307 loc) · 13.8 KB
/
graph_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package resource_test
import (
"context"
"testing"
"time"
"github.com/pkg/errors"
"go.viam.com/test"
"go.viam.com/rdk/components/generic"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/testutils"
"go.viam.com/rdk/utils"
)
func withTestLogger(t *testing.T, node *resource.GraphNode) *resource.GraphNode {
logger := logging.NewTestLogger(t)
node.InitializeLogger(logger, "testnode")
return node
}
func TestUninitializedLifecycle(t *testing.T) {
// empty
node := withTestLogger(t, resource.NewUninitializedNode())
test.That(t, node.IsUninitialized(), test.ShouldBeTrue)
test.That(t, node.UpdatedAt(), test.ShouldEqual, 0)
_, err := node.Resource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "not initialized")
_, err = node.UnsafeResource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "not initialized")
test.That(t, node.ResourceModel(), test.ShouldResemble, resource.Model{})
test.That(t, node.HasResource(), test.ShouldBeFalse)
test.That(t, node.Config(), test.ShouldResemble, resource.Config{})
test.That(t, node.NeedsReconfigure(), test.ShouldBeFalse)
expectedState := resource.NodeStateUnconfigured
test.That(t, node.State(), test.ShouldEqual, expectedState)
status := node.Status()
test.That(t, status.State, test.ShouldResemble, expectedState)
lifecycleTest(t, node, []string(nil))
}
func TestUnconfiguredLifecycle(t *testing.T) {
someConf := resource.Config{
Name: "foo",
Attributes: utils.AttributeMap{"3": 4},
}
initialDeps := []string{"dep1", "dep2"}
node := withTestLogger(t, resource.NewUnconfiguredGraphNode(someConf, initialDeps))
test.That(t, node.IsUninitialized(), test.ShouldBeTrue)
test.That(t, node.UpdatedAt(), test.ShouldEqual, 0)
_, err := node.Resource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "not initialized")
_, err = node.UnsafeResource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "not initialized")
test.That(t, node.ResourceModel(), test.ShouldResemble, resource.Model{})
test.That(t, node.HasResource(), test.ShouldBeFalse)
test.That(t, node.Config(), test.ShouldResemble, someConf)
test.That(t, node.NeedsReconfigure(), test.ShouldBeTrue)
test.That(t, node.UnresolvedDependencies(), test.ShouldResemble, initialDeps)
expectedState := resource.NodeStateConfiguring
test.That(t, node.State(), test.ShouldEqual, expectedState)
status := node.Status()
test.That(t, status.Name.Name, test.ShouldEqual, "")
test.That(t, status.State, test.ShouldResemble, expectedState)
lifecycleTest(t, node, initialDeps)
}
func TestConfiguredLifecycle(t *testing.T) {
someConf := resource.Config{Attributes: utils.AttributeMap{"3": 4}}
resName := generic.Named("some")
ourRes := &someResource{Resource: testutils.NewUnimplementedResource(resName)}
node := withTestLogger(t, resource.NewConfiguredGraphNode(someConf, ourRes, resource.DefaultModelFamily.WithModel("bar")))
test.That(t, node.IsUninitialized(), test.ShouldBeFalse)
test.That(t, node.UpdatedAt(), test.ShouldEqual, 0)
res, err := node.Resource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldEqual, res)
res, err = node.UnsafeResource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldEqual, res)
test.That(t, node.ResourceModel(), test.ShouldResemble, resource.DefaultModelFamily.WithModel("bar"))
test.That(t, node.HasResource(), test.ShouldBeTrue)
test.That(t, node.Config(), test.ShouldResemble, someConf)
test.That(t, node.NeedsReconfigure(), test.ShouldBeFalse)
test.That(t, node.UnresolvedDependencies(), test.ShouldBeEmpty)
expectedState := resource.NodeStateReady
test.That(t, node.State(), test.ShouldEqual, expectedState)
status := node.Status()
test.That(t, status.Name, test.ShouldResemble, resource.Name{})
test.That(t, status.State, test.ShouldResemble, resource.NodeStateReady)
lifecycleTest(t, node, []string(nil))
}
func lifecycleTest(t *testing.T, node *resource.GraphNode, initialDeps []string) {
// get initial state
state, transitionedAt := node.State(), node.TransitionedAt()
verifyStateTransition := func(t *testing.T, node *resource.GraphNode, expectedState resource.NodeState) {
t.Helper()
toState, toTransitionedAt := node.State(), node.TransitionedAt()
test.That(t, toState, test.ShouldEqual, expectedState)
test.That(t, toTransitionedAt.UnixNano(), test.ShouldBeGreaterThan, transitionedAt.UnixNano())
state, transitionedAt = toState, toTransitionedAt
}
verifySameState := func(t *testing.T, node *resource.GraphNode) {
t.Helper()
toState, toTransitionedAt := node.State(), node.TransitionedAt()
test.That(t, toState, test.ShouldEqual, state)
test.That(t, toTransitionedAt.UnixNano(), test.ShouldEqual, transitionedAt.UnixNano())
state, transitionedAt = toState, toTransitionedAt
}
// mark it as [NodeStateUnhealthy]
ourErr := errors.New("whoops")
node.LogAndSetLastError(ourErr)
_, err := node.Resource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "whoops")
verifyStateTransition(t, node, resource.NodeStateUnhealthy)
// mark it for removal
test.That(t, node.MarkedForRemoval(), test.ShouldBeFalse)
node.MarkForRemoval()
test.That(t, node.MarkedForRemoval(), test.ShouldBeTrue)
// Attempt to change status to [NodeStateUnhealthy]
ourErr = errors.New("whoops")
node.LogAndSetLastError(ourErr)
status := node.Status()
// Ensure that error is set and node stays in [NodeStateUnhealthy]
// since state transition [NodeStateUnhealthy] -> [NodeStateRemoving] is blocked
test.That(t, status.Error.Error(), test.ShouldContainSubstring, "whoops")
test.That(t, node.MarkedForRemoval(), test.ShouldBeTrue)
_, err = node.Resource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "pending removal")
verifyStateTransition(t, node, resource.NodeStateRemoving)
test.That(t, node.UnresolvedDependencies(), test.ShouldResemble, initialDeps)
// but we end up configuring it
ourRes := &someResource{Resource: testutils.NewUnimplementedResource(generic.Named("foo"))}
node.SwapResource(ourRes, resource.DefaultModelFamily.WithModel("bar"), nil)
test.That(t, node.ResourceModel(), test.ShouldResemble, resource.DefaultModelFamily.WithModel("bar"))
test.That(t, node.MarkedForRemoval(), test.ShouldBeFalse)
test.That(t, node.IsUninitialized(), test.ShouldBeFalse)
res, err := node.Resource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldEqual, ourRes)
verifyStateTransition(t, node, resource.NodeStateReady)
// now it needs update
node.SetNeedsUpdate()
res, err = node.Resource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldEqual, ourRes)
test.That(t, node.MarkedForRemoval(), test.ShouldBeFalse)
verifyStateTransition(t, node, resource.NodeStateConfiguring)
// but an error happened
node.LogAndSetLastError(ourErr)
_, err = node.Resource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, ourErr.Error())
res, err = node.UnsafeResource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldEqual, ourRes)
test.That(t, node.IsUninitialized(), test.ShouldBeFalse)
verifyStateTransition(t, node, resource.NodeStateUnhealthy)
// it reconfigured
ourRes2 := &someResource{Resource: testutils.NewUnimplementedResource(generic.Named("foo"))}
node.SwapResource(ourRes2, resource.DefaultModelFamily.WithModel("baz"), nil)
test.That(t, node.ResourceModel(), test.ShouldResemble, resource.DefaultModelFamily.WithModel("baz"))
res, err = node.Resource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldNotEqual, ourRes)
test.That(t, res, test.ShouldEqual, ourRes2)
test.That(t, node.MarkedForRemoval(), test.ShouldBeFalse)
verifyStateTransition(t, node, resource.NodeStateReady)
// it needs a new config
ourConf := resource.Config{Attributes: utils.AttributeMap{"1": 2}}
node.SetNewConfig(ourConf, []string{"3", "4", "5"})
res, err = node.Resource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldEqual, ourRes2)
test.That(t, node.NeedsReconfigure(), test.ShouldBeTrue)
test.That(t, node.Config(), test.ShouldResemble, resource.Config{Attributes: utils.AttributeMap{"1": 2}})
test.That(t, node.UnresolvedDependencies(), test.ShouldResemble, []string{"3", "4", "5"})
verifyStateTransition(t, node, resource.NodeStateConfiguring)
node.SetNeedsUpdate() // noop
res, err = node.Resource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldEqual, ourRes2)
test.That(t, node.NeedsReconfigure(), test.ShouldBeTrue)
test.That(t, node.Config(), test.ShouldResemble, resource.Config{Attributes: utils.AttributeMap{"1": 2}})
test.That(t, node.UnresolvedDependencies(), test.ShouldResemble, []string{"3", "4", "5"})
verifySameState(t, node)
// but an error happened
node.LogAndSetLastError(ourErr)
_, err = node.Resource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, ourErr.Error())
res, err = node.UnsafeResource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldEqual, ourRes2)
verifyStateTransition(t, node, resource.NodeStateUnhealthy)
// it reconfigured
ourRes3 := &someResource{Resource: testutils.NewUnimplementedResource(generic.Named("fooa"))}
node.SwapResource(ourRes3, resource.DefaultModelFamily.WithModel("bazz"), nil)
test.That(t, node.ResourceModel(), test.ShouldResemble, resource.DefaultModelFamily.WithModel("bazz"))
res, err = node.Resource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldNotEqual, ourRes2)
test.That(t, res, test.ShouldEqual, ourRes3)
test.That(t, node.MarkedForRemoval(), test.ShouldBeFalse)
test.That(t, node.IsUninitialized(), test.ShouldBeFalse)
test.That(t, node.Config(), test.ShouldResemble, resource.Config{Attributes: utils.AttributeMap{"1": 2}})
test.That(t, node.UnresolvedDependencies(), test.ShouldBeEmpty)
verifyStateTransition(t, node, resource.NodeStateReady)
//nolint
test.That(t, node.Close(context.WithValue(context.Background(), "foo", "hi")), test.ShouldBeNil)
test.That(t, ourRes.closeCap, test.ShouldBeEmpty)
test.That(t, ourRes2.closeCap, test.ShouldBeEmpty)
test.That(t, ourRes3.closeCap, test.ShouldHaveLength, 1)
test.That(t, ourRes3.closeCap, test.ShouldResemble, []interface{}{"hi"})
test.That(t, node.IsUninitialized(), test.ShouldBeTrue)
_, err = node.Resource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "not initialized")
// TODO(RSDK-7928): we might want to make this transition a node to an "unconfigured"
// or "removing" state.
verifySameState(t, node)
ourRes4 := &someResource{Resource: testutils.NewUnimplementedResource(generic.Named("foob")), shouldErr: true}
node.SwapResource(ourRes4, resource.DefaultModelFamily.WithModel("bazzz"), nil)
test.That(t, node.ResourceModel(), test.ShouldResemble, resource.DefaultModelFamily.WithModel("bazzz"))
res, err = node.Resource()
test.That(t, err, test.ShouldBeNil)
test.That(t, res, test.ShouldNotEqual, ourRes3)
test.That(t, res, test.ShouldEqual, ourRes4)
test.That(t, node.MarkedForRemoval(), test.ShouldBeFalse)
test.That(t, node.IsUninitialized(), test.ShouldBeFalse)
verifySameState(t, node)
//nolint
err = node.Close(context.WithValue(context.Background(), "foo", "bye"))
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "bad close")
test.That(t, ourRes.closeCap, test.ShouldBeEmpty)
test.That(t, ourRes2.closeCap, test.ShouldBeEmpty)
test.That(t, ourRes3.closeCap, test.ShouldHaveLength, 1)
test.That(t, ourRes4.closeCap, test.ShouldHaveLength, 1)
test.That(t, ourRes4.closeCap, test.ShouldResemble, []interface{}{"bye"})
test.That(t, node.IsUninitialized(), test.ShouldBeTrue)
_, err = node.Resource()
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "not initialized")
// TODO(RSDK-7928): we might want to make this transition a node to an "unconfigured"
// or "removing" state.
verifySameState(t, node)
}
type someResource struct {
resource.Resource
closeCap []interface{}
shouldErr bool
}
func (s *someResource) Close(ctx context.Context) error {
s.closeCap = append(s.closeCap, ctx.Value("foo"))
if s.shouldErr {
return errors.New("bad close")
}
return nil
}
type anotherResource struct {
resource.Resource
CloseFunc func(ctx context.Context) error
}
// Close calls the injected Close or the real version.
func (a *anotherResource) Close(ctx context.Context) error {
if a.CloseFunc == nil {
return errors.New("oops")
}
return a.CloseFunc(ctx)
}
func TestClose(t *testing.T) {
// Tests that Close does not deadlock by calling graphNode.Close() inside a resource Close().
ourRes := &anotherResource{}
node := resource.NewConfiguredGraphNode(
resource.Config{},
ourRes,
resource.DefaultModelFamily.WithModel("bar"),
)
ourRes.CloseFunc = func(ctx context.Context) error {
return node.Close(ctx)
}
// This pattern fails the test faster on deadlocks instead of having to wait for the full
// test timeout.
errCh := make(chan error)
go func() {
errCh <- node.Close(context.Background())
}()
select {
case err := <-errCh:
test.That(t, err, test.ShouldBeNil)
case <-time.After(time.Second * 20):
t.Fatal("node took too long to close, might be a deadlock")
}
}
// TestTransitionToBlocking ensures a node marked removing cannot transition to [NodeStateUnhealthy] state.
func TestTransitionToBlocking(t *testing.T) {
node := withTestLogger(t, resource.NewUninitializedNode())
// Set state removing
node.MarkForRemoval()
test.That(t, node.MarkedForRemoval(), test.ShouldBeTrue)
// Attempt to set state to [NodeStateUnhealthy]
node.LogAndSetLastError(errors.New("Its error time"))
// Node should stay still be in state removing
test.That(t, node.MarkedForRemoval(), test.ShouldBeTrue)
}