-
Notifications
You must be signed in to change notification settings - Fork 6
/
cachemetrics_test.go
120 lines (116 loc) · 2.61 KB
/
cachemetrics_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
package docker
import (
"reflect"
"testing"
)
func TestParseCacheMetrics(t *testing.T) {
tests := []struct {
name string
input []string
expected CacheMetrics
expectErr bool
}{
{
name: "empty channel",
input: []string{},
expected: CacheMetrics{Layers: make(map[int]LayerStatus)},
},
{
name: "valid metrics with multiple on same line",
input: []string{
"#1 DONE 0.5s #1 DONE 0.6s #2 CACHED #3 ERROR #4 CANCELED",
"#5 DONE 1.0s #6 CACHED",
},
expected: CacheMetrics{
Layers: map[int]LayerStatus{
1: {Status: "DONE", Time: 0.6},
2: {Status: "CACHED"},
3: {Status: "ERROR"},
4: {Status: "CANCELED"},
5: {Status: "DONE", Time: 1.0},
6: {Status: "CACHED"},
},
Done: 2,
Cached: 2,
Error: 1,
Canceled: 1,
TotalLayers: 6,
},
},
{
name: "mixed valid and invalid lines with multiple on same line",
input: []string{
"#1 DONE 0.5s #2 INVALID #3 CACHED",
"#4 DONE #invalid_line",
},
expected: CacheMetrics{
Layers: map[int]LayerStatus{
1: {Status: "DONE", Time: 0.5},
3: {Status: "CACHED"},
4: {Status: "DONE"},
},
Done: 2,
Cached: 1,
TotalLayers: 3,
},
},
{
name: "various statuses with multiple on same line",
input: []string{
"#1 DONE 1.2s #2 DONE 0.8s",
"#3 CACHED #4 ERRORED #5 CANCELED",
"#6 DONE",
},
expected: CacheMetrics{
Layers: map[int]LayerStatus{
1: {Status: "DONE", Time: 1.2},
2: {Status: "DONE", Time: 0.8},
3: {Status: "CACHED"},
4: {Status: "ERROR"},
5: {Status: "CANCELED"},
6: {Status: "DONE"},
},
Done: 3,
Cached: 1,
Error: 1,
Canceled: 1,
TotalLayers: 6,
},
},
{
name: "invalid formats with multiple on same line",
input: []string{
"#1 DONE 0.5s #2 INVALID",
"#3 CACHED #invalid_line",
},
expected: CacheMetrics{
Layers: map[int]LayerStatus{
1: {Status: "DONE", Time: 0.5},
3: {Status: "CACHED"},
},
Done: 1,
Cached: 1,
TotalLayers: 2,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ch := make(chan string, len(tt.input))
go func() {
for _, line := range tt.input {
ch <- line
}
close(ch)
}()
actual, err := parseCacheMetrics(ch)
if (err != nil) != tt.expectErr {
t.Errorf("parseCacheMetrics() error = %v, expectErr %v", err, tt.expectErr)
return
}
if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("parseCacheMetrics() = %v, expected %v", actual, tt.expected)
}
})
}
}