-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcontainer_status_test.go
More file actions
584 lines (540 loc) · 14 KB
/
Copy pathcontainer_status_test.go
File metadata and controls
584 lines (540 loc) · 14 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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package session
import (
"errors"
"reflect"
"testing"
"time"
"github.com/nicholas-fedor/watchtower/pkg/types"
)
func TestContainerStatus_ID(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
want types.ContainerID
}{
{
name: "valid container ID",
u: &ContainerStatus{containerID: "cont1"},
want: "cont1",
},
{
name: "empty container ID",
u: &ContainerStatus{containerID: ""},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.ID(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ContainerStatus.ID() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainerStatus_Name(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
want string
}{
{
name: "valid container name",
u: &ContainerStatus{containerName: "my-container"},
want: "my-container",
},
{
name: "empty container name",
u: &ContainerStatus{containerName: ""},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Name(); got != tt.want {
t.Errorf("ContainerStatus.Name() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainerStatus_CurrentImageID(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
want types.ImageID
}{
{
name: "valid current image ID",
u: &ContainerStatus{oldImage: "img123"},
want: "img123",
},
{
name: "empty current image ID",
u: &ContainerStatus{oldImage: ""},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.CurrentImageID(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ContainerStatus.CurrentImageID() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainerStatus_LatestImageID(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
want types.ImageID
}{
{
name: "valid latest image ID",
u: &ContainerStatus{newImage: "img456"},
want: "img456",
},
{
name: "empty latest image ID",
u: &ContainerStatus{newImage: ""},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.LatestImageID(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ContainerStatus.LatestImageID() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainerStatus_ImageName(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
want string
}{
{
name: "valid image name",
u: &ContainerStatus{imageName: "myimage:latest"},
want: "myimage:latest",
},
{
name: "empty image name",
u: &ContainerStatus{imageName: ""},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.ImageName(); got != tt.want {
t.Errorf("ContainerStatus.ImageName() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainerStatus_Error(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
want string
}{
{
name: "no error",
u: &ContainerStatus{containerError: nil},
want: "",
},
{
name: "with error",
u: &ContainerStatus{containerError: errors.New("update failed")},
want: "update failed",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Error(); got != tt.want {
t.Errorf("ContainerStatus.Error() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainerStatus_State(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
want string
}{
{
name: "unknown state",
u: &ContainerStatus{state: UnknownState},
want: "Unknown",
},
{
name: "skipped state",
u: &ContainerStatus{state: SkippedState},
want: "Skipped",
},
{
name: "scanned state",
u: &ContainerStatus{state: ScannedState},
want: "Scanned",
},
{
name: "updated state",
u: &ContainerStatus{state: UpdatedState},
want: "Updated",
},
{
name: "failed state",
u: &ContainerStatus{state: FailedState},
want: "Failed",
},
{
name: "fresh state",
u: &ContainerStatus{state: FreshState},
want: "Fresh",
},
{
name: "stale state",
u: &ContainerStatus{state: StaleState},
want: "Stale",
},
{
name: "restarted state",
u: &ContainerStatus{state: RestartedState},
want: RestartedStateString,
},
{
name: "invalid state",
u: &ContainerStatus{state: State(999)}, // Beyond defined states
want: "Unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.State(); got != tt.want {
t.Errorf("ContainerStatus.State() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainerStatus_RestartedStateBehavior(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
}{
{
name: "restarted state with all fields",
u: &ContainerStatus{
containerID: "cont1",
oldImage: "img123",
newImage: "img456",
containerName: "my-container",
imageName: "nginx:latest",
containerError: nil,
state: RestartedState,
monitorOnly: false,
newContainerID: "newcont1",
},
},
{
name: "restarted state with minimal fields",
u: &ContainerStatus{
containerID: "cont1",
state: RestartedState,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Verify state is Restarted
if got := tt.u.State(); got != RestartedStateString {
t.Errorf("ContainerStatus.State() = %v, want %v", got, RestartedStateString)
}
// Verify other methods return expected values
if got := tt.u.ID(); got != tt.u.containerID {
t.Errorf("ContainerStatus.ID() = %v, want %v", got, tt.u.containerID)
}
if got := tt.u.Name(); got != tt.u.containerName {
t.Errorf("ContainerStatus.Name() = %v, want %v", got, tt.u.containerName)
}
if got := tt.u.CurrentImageID(); got != tt.u.oldImage {
t.Errorf("ContainerStatus.CurrentImageID() = %v, want %v", got, tt.u.oldImage)
}
if got := tt.u.LatestImageID(); got != tt.u.newImage {
t.Errorf("ContainerStatus.LatestImageID() = %v, want %v", got, tt.u.newImage)
}
if got := tt.u.ImageName(); got != tt.u.imageName {
t.Errorf("ContainerStatus.ImageName() = %v, want %v", got, tt.u.imageName)
}
if got := tt.u.IsMonitorOnly(); got != tt.u.monitorOnly {
t.Errorf("ContainerStatus.IsMonitorOnly() = %v, want %v", got, tt.u.monitorOnly)
}
if got := tt.u.NewContainerID(); got != tt.u.newContainerID {
t.Errorf("ContainerStatus.NewContainerID() = %v, want %v", got, tt.u.newContainerID)
}
})
}
}
func TestContainerStatus_RestartedStateErrorHandling(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
want string
}{
{
name: "restarted state with no error",
u: &ContainerStatus{
state: RestartedState,
containerError: nil,
},
want: "",
},
{
name: "restarted state with error",
u: &ContainerStatus{
state: RestartedState,
containerError: errors.New("restart failed"),
},
want: "restart failed",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Error(); got != tt.want {
t.Errorf("ContainerStatus.Error() = %v, want %v", got, tt.want)
}
// Ensure state is still Restarted
if got := tt.u.State(); got != RestartedStateString {
t.Errorf("ContainerStatus.State() = %v, want %v", got, RestartedStateString)
}
})
}
}
func TestContainerStatus_RestartedStateWithMissingData(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
}{
{
name: "restarted state with empty container ID",
u: &ContainerStatus{
containerID: "",
state: RestartedState,
},
},
{
name: "restarted state with empty images",
u: &ContainerStatus{
containerID: "cont1",
oldImage: "",
newImage: "",
state: RestartedState,
},
},
{
name: "restarted state with empty name and image name",
u: &ContainerStatus{
containerID: "cont1",
containerName: "",
imageName: "",
state: RestartedState,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Verify state is Restarted
if got := tt.u.State(); got != RestartedStateString {
t.Errorf("ContainerStatus.State() = %v, want %v", got, RestartedStateString)
}
// Verify methods return empty values appropriately
if tt.u.ID() != tt.u.containerID {
t.Errorf("ID() mismatch")
}
if tt.u.Name() != tt.u.containerName {
t.Errorf("Name() mismatch")
}
if tt.u.CurrentImageID() != tt.u.oldImage {
t.Errorf("CurrentImageID() mismatch")
}
if tt.u.LatestImageID() != tt.u.newImage {
t.Errorf("LatestImageID() mismatch")
}
if tt.u.ImageName() != tt.u.imageName {
t.Errorf("ImageName() mismatch")
}
})
}
}
func TestContainerStatus_SetCooldownInfo(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
age string
delay string
remaining string
eligibleAt time.Time
passed bool
}{
{
name: "cooldown passed with age",
u: &ContainerStatus{containerID: "cont1"},
age: "47 days, 11 hours",
delay: "24 hours",
remaining: "",
eligibleAt: time.Time{},
passed: true,
},
{
name: "cooldown not passed with remaining",
u: &ContainerStatus{containerID: "cont2"},
age: "2 hours",
delay: "24 hours",
remaining: "22 hours",
eligibleAt: time.Date(2026, 5, 26, 0, 45, 0, 0, time.UTC),
passed: false,
},
{
name: "empty values",
u: &ContainerStatus{containerID: "cont3"},
age: "",
delay: "",
remaining: "",
eligibleAt: time.Time{},
passed: false,
},
{
name: "overwrites previous values",
u: &ContainerStatus{containerID: "cont4", cooldownAge: "old age", cooldownDelay: "old delay", cooldownRemaining: "old remaining", cooldownPassed: true},
age: "new age",
delay: "new delay",
remaining: "new remaining",
eligibleAt: time.Date(2026, 5, 27, 12, 0, 0, 0, time.UTC),
passed: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.u.SetCooldownInfo(tt.age, tt.delay, tt.remaining, tt.eligibleAt, tt.passed)
if got := tt.u.CooldownAge(); got != tt.age {
t.Errorf("CooldownAge() = %v, want %v", got, tt.age)
}
if got := tt.u.CooldownDelay(); got != tt.delay {
t.Errorf("CooldownDelay() = %v, want %v", got, tt.delay)
}
if got := tt.u.CooldownRemaining(); got != tt.remaining {
t.Errorf("CooldownRemaining() = %v, want %v", got, tt.remaining)
}
if got := tt.u.CooldownPassed(); got != tt.passed {
t.Errorf("CooldownPassed() = %v, want %v", got, tt.passed)
}
if got := tt.u.CooldownEligibleAt(); got != tt.eligibleAt {
t.Errorf("CooldownEligibleAt() = %v, want %v", got, tt.eligibleAt)
}
})
}
}
func TestContainerStatus_CooldownDefaults(t *testing.T) {
u := &ContainerStatus{containerID: "cont1"}
if got := u.CooldownPassed(); got {
t.Errorf("CooldownPassed() default = %v, want false", got)
}
if got := u.CooldownAge(); got != "" {
t.Errorf("CooldownAge() default = %v, want empty", got)
}
if got := u.CooldownDelay(); got != "" {
t.Errorf("CooldownDelay() default = %v, want empty", got)
}
if got := u.CooldownRemaining(); got != "" {
t.Errorf("CooldownRemaining() default = %v, want empty", got)
}
if !u.CooldownEligibleAt().IsZero() {
t.Errorf("CooldownEligibleAt() default = %v, want zero", u.CooldownEligibleAt())
}
}
func TestContainerStatus_CooldownGettersReturnDirectValues(t *testing.T) {
eligibleAt := time.Date(2026, 5, 26, 0, 45, 0, 0, time.UTC)
u := &ContainerStatus{
containerID: "cont1",
cooldownPassed: true,
cooldownAge: "3 days",
cooldownDelay: "48 hours",
cooldownRemaining: "12 hours",
cooldownEligibleAt: eligibleAt,
}
if got := u.CooldownPassed(); got != true {
t.Errorf("CooldownPassed() = %v, want true", got)
}
if got := u.CooldownAge(); got != "3 days" {
t.Errorf("CooldownAge() = %v, want '3 days'", got)
}
if got := u.CooldownDelay(); got != "48 hours" {
t.Errorf("CooldownDelay() = %v, want '48 hours'", got)
}
if got := u.CooldownRemaining(); got != "12 hours" {
t.Errorf("CooldownRemaining() = %v, want '12 hours'", got)
}
if got := u.CooldownEligibleAt(); got != eligibleAt {
t.Errorf("CooldownEligibleAt() = %v, want %v", got, eligibleAt)
}
}
func TestContainerStatus_RestartedStateIntegration(t *testing.T) {
tests := []struct {
name string
u *ContainerStatus
}{
{
name: "restarted state with monitor only",
u: &ContainerStatus{
containerID: "cont1",
containerName: "my-container",
state: RestartedState,
monitorOnly: true,
},
},
{
name: "restarted state with new container ID",
u: &ContainerStatus{
containerID: "cont1",
newContainerID: "newcont1",
state: RestartedState,
},
},
{
name: "restarted state with all fields and error",
u: &ContainerStatus{
containerID: "cont1",
oldImage: "img123",
newImage: "img456",
containerName: "my-container",
imageName: "nginx:latest",
containerError: errors.New("some error"),
state: RestartedState,
monitorOnly: true,
newContainerID: "newcont1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Verify state is Restarted
if got := tt.u.State(); got != RestartedStateString {
t.Errorf("ContainerStatus.State() = %v, want %v", got, RestartedStateString)
}
// Verify integration of fields
if tt.u.ID() != tt.u.containerID {
t.Errorf("ID integration failed")
}
if tt.u.IsMonitorOnly() != tt.u.monitorOnly {
t.Errorf("IsMonitorOnly integration failed")
}
if tt.u.NewContainerID() != tt.u.newContainerID {
t.Errorf("NewContainerID integration failed")
}
// Test SetNewContainerID
tt.u.SetNewContainerID("updatedID")
if tt.u.NewContainerID() != "updatedID" {
t.Errorf("SetNewContainerID failed")
}
})
}
}