-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment_test.go
498 lines (437 loc) · 16.1 KB
/
environment_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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2024 HUMAN Security.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package envite
import (
"context"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
type mockComponent struct {
status ComponentStatus
shouldFail bool
prepareCalled bool
startCalled bool
stopCalled bool
cleanupCalled bool
onStart func()
onStop func()
w *Writer
}
func (m *mockComponent) Type() string {
return "mock"
}
func (m *mockComponent) AttachEnvironment(_ context.Context, _ *Environment, w *Writer) error {
m.w = w
return nil
}
func (m *mockComponent) Prepare(context.Context) error {
m.prepareCalled = true
return nil
}
func (m *mockComponent) Start(context.Context) error {
if m.shouldFail {
return errors.New("start error")
}
m.startCalled = true
m.status = ComponentStatusRunning
if m.onStart != nil {
m.onStart()
}
return nil
}
func (m *mockComponent) Stop(context.Context) error {
if m.shouldFail {
return errors.New("stop error")
}
m.stopCalled = true
m.status = ComponentStatusStopped
if m.onStop != nil {
m.onStop()
}
return nil
}
func (m *mockComponent) Cleanup(context.Context) error {
m.cleanupCalled = true
return nil
}
func (m *mockComponent) Status(context.Context) (ComponentStatus, error) {
return m.status, nil
}
func (m *mockComponent) Config() any {
return nil
}
func (m *mockComponent) EnvVars() map[string]string {
return nil
}
func (m *mockComponent) initFlags() {
m.prepareCalled = false
m.startCalled = false
m.stopCalled = false
m.cleanupCalled = false
}
func TestEnvironmentManagement(t *testing.T) {
// Create mock components
component1 := &mockComponent{}
component2 := &mockComponent{}
component3 := &mockComponent{}
component1.onStart = func() {
assert.True(t, component1.prepareCalled)
assert.True(t, component2.prepareCalled)
assert.True(t, component3.prepareCalled)
assert.True(t, component1.startCalled)
assert.False(t, component2.startCalled)
assert.False(t, component3.startCalled)
assert.False(t, component1.stopCalled)
assert.False(t, component2.stopCalled)
assert.False(t, component3.stopCalled)
assert.False(t, component1.cleanupCalled)
assert.False(t, component2.cleanupCalled)
assert.False(t, component3.cleanupCalled)
}
component2.onStart = func() {
assert.True(t, component1.prepareCalled)
assert.True(t, component2.prepareCalled)
assert.True(t, component3.prepareCalled)
assert.True(t, component1.startCalled)
assert.True(t, component2.startCalled)
assert.False(t, component3.startCalled)
assert.False(t, component1.stopCalled)
assert.False(t, component2.stopCalled)
assert.False(t, component3.stopCalled)
assert.False(t, component1.cleanupCalled)
assert.False(t, component2.cleanupCalled)
assert.False(t, component3.cleanupCalled)
}
component3.onStart = func() {
assert.True(t, component1.prepareCalled)
assert.True(t, component2.prepareCalled)
assert.True(t, component3.prepareCalled)
assert.True(t, component1.startCalled)
assert.True(t, component2.startCalled)
assert.True(t, component3.startCalled)
assert.False(t, component1.stopCalled)
assert.False(t, component2.stopCalled)
assert.False(t, component3.stopCalled)
assert.False(t, component1.cleanupCalled)
assert.False(t, component2.cleanupCalled)
assert.False(t, component3.cleanupCalled)
}
component1.onStop = func() {
assert.True(t, component1.prepareCalled)
assert.True(t, component2.prepareCalled)
assert.True(t, component3.prepareCalled)
assert.True(t, component1.startCalled)
assert.True(t, component2.startCalled)
assert.True(t, component3.startCalled)
assert.True(t, component1.stopCalled)
assert.True(t, component2.stopCalled)
assert.True(t, component3.stopCalled)
assert.False(t, component1.cleanupCalled)
assert.False(t, component2.cleanupCalled)
assert.False(t, component3.cleanupCalled)
}
component2.onStop = func() {
assert.True(t, component1.prepareCalled)
assert.True(t, component2.prepareCalled)
assert.True(t, component3.prepareCalled)
assert.True(t, component1.startCalled)
assert.True(t, component2.startCalled)
assert.True(t, component3.startCalled)
assert.False(t, component1.stopCalled)
assert.True(t, component2.stopCalled)
assert.True(t, component3.stopCalled)
assert.False(t, component1.cleanupCalled)
assert.False(t, component2.cleanupCalled)
assert.False(t, component3.cleanupCalled)
}
component3.onStop = func() {
assert.True(t, component1.prepareCalled)
assert.True(t, component2.prepareCalled)
assert.True(t, component3.prepareCalled)
assert.True(t, component1.startCalled)
assert.True(t, component2.startCalled)
assert.True(t, component3.startCalled)
assert.False(t, component1.stopCalled)
assert.False(t, component2.stopCalled)
assert.True(t, component3.stopCalled)
assert.False(t, component1.cleanupCalled)
assert.False(t, component2.cleanupCalled)
assert.False(t, component3.cleanupCalled)
}
// Create a mock environment
env, err := NewEnvironment(
"test-env",
NewComponentGraph().
AddLayer(map[string]Component{"component-1": component1}).
AddLayer(map[string]Component{"component-2": component2}).
AddLayer(map[string]Component{"component-3": component3}),
)
assert.NoError(t, err)
// Validate env components
components := env.Components()
assert.Len(t, components, 3)
assert.Contains(t, components, component1)
assert.Contains(t, components, component2)
assert.Contains(t, components, component3)
c, err := env.componentByID("component-1")
assert.NoError(t, err)
assert.Equal(t, component1, c)
c, err = env.componentByID("not exist")
assert.Nil(t, c)
assert.NotNil(t, err)
// Start
err = env.Apply(context.Background(), []string{"component-1", "component-2", "component-3"})
assert.NoError(t, err)
// Validate status
status, err := env.Status(context.Background())
assert.NoError(t, err)
assert.Equal(t, "test-env", status.ID)
assert.Len(t, status.Components, 3)
assert.Len(t, status.Components[0], 1)
assert.Equal(t, "component-1", status.Components[0][0].ID)
assert.Equal(t, "mock", status.Components[0][0].Type)
assert.Equal(t, ComponentStatusRunning, status.Components[0][0].Status)
assert.Equal(t, "component-2", status.Components[1][0].ID)
assert.Equal(t, "mock", status.Components[1][0].Type)
assert.Equal(t, ComponentStatusRunning, status.Components[1][0].Status)
assert.Equal(t, "component-3", status.Components[2][0].ID)
assert.Equal(t, "mock", status.Components[2][0].Type)
assert.Equal(t, ComponentStatusRunning, status.Components[2][0].Status)
// Stop and cleanup
err = env.StopAll(context.Background())
assert.NoError(t, err)
err = env.Cleanup(context.Background())
assert.NoError(t, err)
// Validate cleanup
assert.True(t, component1.prepareCalled)
assert.True(t, component2.prepareCalled)
assert.True(t, component3.prepareCalled)
assert.True(t, component1.startCalled)
assert.True(t, component2.startCalled)
assert.True(t, component3.startCalled)
assert.True(t, component1.stopCalled)
assert.True(t, component2.stopCalled)
assert.True(t, component3.stopCalled)
assert.True(t, component1.cleanupCalled)
assert.True(t, component2.cleanupCalled)
assert.True(t, component3.cleanupCalled)
// Validate status
status, err = env.Status(context.Background())
assert.NoError(t, err)
assert.Equal(t, "test-env", status.ID)
assert.Len(t, status.Components, 3)
assert.Len(t, status.Components[0], 1)
assert.Equal(t, "component-1", status.Components[0][0].ID)
assert.Equal(t, "mock", status.Components[0][0].Type)
assert.Equal(t, ComponentStatusStopped, status.Components[0][0].Status)
assert.Equal(t, "component-2", status.Components[1][0].ID)
assert.Equal(t, "mock", status.Components[1][0].Type)
assert.Equal(t, ComponentStatusStopped, status.Components[1][0].Status)
assert.Equal(t, "component-3", status.Components[2][0].ID)
assert.Equal(t, "mock", status.Components[2][0].Type)
assert.Equal(t, ComponentStatusStopped, status.Components[2][0].Status)
// Clear state
component1.initFlags()
component2.initFlags()
component3.initFlags()
// Validate full start
err = env.StartAll(context.Background())
assert.NoError(t, err)
assert.Equal(t, ComponentStatusRunning, component1.status)
assert.Equal(t, ComponentStatusRunning, component2.status)
assert.Equal(t, ComponentStatusRunning, component3.status)
component1.onStart = nil
component2.onStart = nil
component3.onStart = nil
component1.onStop = nil
component2.onStop = nil
component3.onStop = nil
// Validate manual stop
err = env.StopComponent(context.Background(), "component-1")
assert.NoError(t, err)
assert.Equal(t, ComponentStatusStopped, component1.status)
assert.Equal(t, ComponentStatusRunning, component2.status)
assert.Equal(t, ComponentStatusRunning, component3.status)
// Validate manual start
err = env.StartComponent(context.Background(), "component-1")
assert.NoError(t, err)
assert.Equal(t, ComponentStatusRunning, component1.status)
assert.Equal(t, ComponentStatusRunning, component2.status)
assert.Equal(t, ComponentStatusRunning, component3.status)
}
func TestSelectiveAndZeroComponentApplication(t *testing.T) {
// Setup
component1 := &mockComponent{}
component2 := &mockComponent{}
component3 := &mockComponent{}
env, err := NewEnvironment(
"test-env",
NewComponentGraph().
AddLayer(map[string]Component{"component-1": component1}).
AddLayer(map[string]Component{"component-2": component2}).
AddLayer(map[string]Component{"component-3": component3}),
)
assert.NoError(t, err)
// Start a subset of components
err = env.Apply(context.Background(), []string{"component-1", "component-2"})
assert.NoError(t, err)
// Assert only the specified components are started
assert.True(t, component1.startCalled, "Component 1 should be started")
assert.True(t, component2.startCalled, "Component 2 should be started")
assert.False(t, component3.startCalled, "Component 3 should not be started")
// Init flags
component1.initFlags()
component2.initFlags()
component3.initFlags()
// Now apply zero components and check if all are stopped
err = env.Apply(context.Background(), []string{})
assert.NoError(t, err)
// Validate that all components are stopped
assert.True(t, component1.stopCalled, "Component 1 should be stopped after applying zero components")
assert.True(t, component2.stopCalled, "Component 2 should be stopped after applying zero components")
assert.False(t, component3.startCalled, "Component 3 should remain not started")
}
func TestErrorHandlingDuringComponentManagement(t *testing.T) {
// Setup with multiple components, including one that will fail on start
componentFailOnStart := &mockComponent{shouldFail: true}
componentSuccess1 := &mockComponent{}
componentSuccess2 := &mockComponent{}
env, err := NewEnvironment(
"test-env",
NewComponentGraph().
AddLayer(map[string]Component{"success-1": componentSuccess1}).
// Ensure this component is in the middle to test error handling
AddLayer(map[string]Component{"fail-start": componentFailOnStart}).
AddLayer(map[string]Component{"success-2": componentSuccess2}),
)
assert.NoError(t, err)
// Test applying all components, expecting failure due to "fail-start" component
err = env.Apply(context.Background(), []string{"success-1", "fail-start", "success-2"})
assert.Error(t, err, "Apply should fail due to the 'fail-start' component")
assert.Contains(t, err.Error(), "start error", "Error should propagate when component fails to start")
// Ensure all prepare calls occurred
assert.True(t, componentSuccess1.prepareCalled, "Component 'success-1' should be prepared")
assert.True(t, componentFailOnStart.prepareCalled, "Component 'fail-start' should be prepared")
assert.True(t, componentSuccess2.prepareCalled, "Component 'success-2' should be prepared")
// Ensure that the failure does not affect the initiation of other components
assert.True(t, componentSuccess1.startCalled, "Component 'success-1' should be started before failure occurs")
assert.False(t, componentSuccess2.startCalled, "Component 'success-2' should not be started due to the failure in 'fail-start'")
// Test applying only the components that do not fail
err = env.Apply(context.Background(), []string{"success-1", "success-2"})
assert.Error(t, err, "Apply should fail due to the 'fail-start' component")
assert.Contains(t, err.Error(), "stop error", "Error should propagate when component fails to start")
}
func TestErrorHandlingDuringConstruction(t *testing.T) {
// Setup with empty ID
env, err := NewEnvironment(
"test-env",
NewComponentGraph().
AddLayer(map[string]Component{"valid": &mockComponent{}}).
AddLayer(map[string]Component{"": &mockComponent{}}),
)
assert.Error(t, err)
assert.Contains(t, err.Error(), "component id")
assert.Nil(t, env)
// Setup with duplicate ID
env, err = NewEnvironment(
"test-env",
NewComponentGraph().
AddLayer(map[string]Component{"valid": &mockComponent{}}).
AddLayer(map[string]Component{"valid": &mockComponent{}}),
)
assert.Error(t, err)
assert.Contains(t, err.Error(), "component id")
assert.Nil(t, env)
// Setup with empty env id
env, err = NewEnvironment(
"",
NewComponentGraph().AddLayer(map[string]Component{"valid": &mockComponent{}}),
)
assert.Equal(t, ErrEmptyEnvID, err)
assert.Nil(t, env)
// Setup with nil graph
env, err = NewEnvironment(
"test-env",
nil,
)
assert.Equal(t, ErrNilGraph, err)
assert.Nil(t, env)
}
func TestLogger(t *testing.T) {
logMessages := 0
env, err := NewEnvironment(
"test-env",
NewComponentGraph().AddLayer(map[string]Component{"valid": &mockComponent{}}),
WithLogger(func(level LogLevel, message string) {
logMessages++
}),
)
assert.NoError(t, err)
assert.NotNil(t, env)
// validate log messages
err = env.StartAll(context.Background())
assert.NoError(t, err)
assert.True(t, logMessages >= 2)
}
func TestOutput(t *testing.T) {
component := &mockComponent{}
env, err := NewEnvironment(
"test-env",
NewComponentGraph().AddLayer(map[string]Component{"component": component}),
)
assert.NoError(t, err)
assert.NotNil(t, env)
someTime := time.Date(2000, 0, 0, 0, 0, 0, 0, time.UTC)
// send messages
component.w.Write([]byte("bytes message\n"))
component.w.WriteString("string message\r\n")
component.w.WriteWithTime(someTime, []byte("bytes message with time"))
component.w.WriteStringWithTime(someTime, "string message with time")
component.w.WriteString(component.w.Color.Red("red message"))
component.w.WriteString(component.w.Color.Green("green message"))
component.w.WriteString(component.w.Color.Yellow("yellow message"))
component.w.WriteString(component.w.Color.Blue("blue message"))
component.w.WriteString(component.w.Color.Magenta("magenta message"))
component.w.WriteString(component.w.Color.Cyan("cyan message"))
// validate messages
thisYear := fmt.Sprintf("<component>component<time>%d", time.Now().Year())
someYear := fmt.Sprintf("<component>component<time>%d", someTime.Year())
reader := env.Output()
ch := reader.Chan()
msg := <-ch
assert.Contains(t, string(msg), thisYear)
assert.Contains(t, string(msg), "bytes message")
msg = <-ch
assert.Contains(t, string(msg), thisYear)
assert.Contains(t, string(msg), "string message")
msg = <-ch
assert.Contains(t, string(msg), someYear)
assert.Contains(t, string(msg), "bytes message with time")
msg = <-ch
assert.Contains(t, string(msg), someYear)
assert.Contains(t, string(msg), "string message with time")
msg = <-ch
assert.Contains(t, string(msg), thisYear)
assert.Contains(t, string(msg), "\u001B[31mred message\u001B[39m")
msg = <-ch
assert.Contains(t, string(msg), thisYear)
assert.Contains(t, string(msg), "\u001B[32mgreen message\u001B[39m")
msg = <-ch
assert.Contains(t, string(msg), thisYear)
assert.Contains(t, string(msg), "\u001B[33myellow message\u001B[39m")
msg = <-ch
assert.Contains(t, string(msg), thisYear)
assert.Contains(t, string(msg), "\u001B[34mblue message\u001B[39m")
msg = <-ch
assert.Contains(t, string(msg), thisYear)
assert.Contains(t, string(msg), "\u001B[35mmagenta message\u001B[39m")
msg = <-ch
assert.Contains(t, string(msg), thisYear)
assert.Contains(t, string(msg), "\u001B[36mcyan message\u001B[39m")
err = reader.Close()
assert.NoError(t, err)
}