Debian Codegraph @

imageprogress/progress_test.go

raw
1 package imageprogress
2
3 import (
4 "encoding/json"
5 "io"
6 "reflect"
7 "strconv"
8 "testing"
9 )
10
11 func TestReports(t *testing.T) {
12 tests := []struct {
13 name string
14 gen func(*progressGenerator)
15 errExpected bool
16 expected report
17 }{
18 {
19 name: "simple report",
20 gen: func(p *progressGenerator) {
21 p.status("1", "Extracting")
22 p.status("2", "Downloading")
23 p.status("1", "Downloading")
24 p.status("2", "Pull complete")
25 },
26 expected: report{
27 statusDownloading: &layerDetail{Count: 1},
28 statusComplete: &layerDetail{Count: 1},
29 },
30 },
31 {
32 name: "ignore invalid layer id",
33 gen: func(p *progressGenerator) {
34 p.status("1", "Downloading")
35 p.status("hello", "testing")
36 p.status("1", "Downloading")
37 },
38 expected: report{
39 statusDownloading: &layerDetail{Count: 1},
40 },
41 },
42 {
43 name: "ignore retrying status",
44 gen: func(p *progressGenerator) {
45 p.status("1", "Downloading")
46 p.status("2", "Pull complete")
47 p.status("1", "Downloading")
48 p.status("3", "Retrying")
49 },
50 expected: report{
51 statusDownloading: &layerDetail{Count: 1},
52 statusComplete: &layerDetail{Count: 1},
53 },
54 },
55 {
56 name: "detect error",
57 gen: func(p *progressGenerator) {
58 p.status("1", "Downloading")
59 p.err("an error")
60 },
61 errExpected: true,
62 },
63 }
64
65 for _, test := range tests {
66 t.Run(test.name, func(t *testing.T) {
67 pipeIn, pipeOut := io.Pipe()
68 go func() {
69 p := newProgressGenerator(pipeOut)
70 test.gen(p)
71 pipeOut.Close()
72 }()
73 var lastReport report
74 w := newWriter(
75 func(r report) {
76 lastReport = r
77 },
78 func(a report, b report) bool {
79 return true
80 },
81 )
82 w.(*imageProgressWriter).stableThreshhold = 0
83 _, err := io.Copy(w, pipeIn)
84 if err == nil {
85 err = w.Close()
86 }
87 if err != nil {
88 if !test.errExpected {
89 t.Fatalf("%s: unexpected: %v", test.name, err)
90 }
91 return
92 }
93 if test.errExpected {
94 t.Fatalf("%s: did not get expected error", test.name)
95 }
96 // w.Close() waits until the goroutine inside of the progress generator finishes
97 if !compareReport(lastReport, test.expected) {
98 t.Errorf("%s: unexpected report, got: %v, expected: %v", test.name, lastReport, test.expected)
99 }
100 })
101 }
102 }
103
104 func TestErrorOnCopy(t *testing.T) {
105 // Producer pipe
106 genIn, genOut := io.Pipe()
107 p := newProgressGenerator(genOut)
108
109 // generate some data
110 go func() {
111 for i := 0; i < 100; i++ {
112 p.status("1", "Downloading")
113 p.status("2", "Downloading")
114 p.status("3", "Downloading")
115 }
116 p.err("data error")
117 genOut.Close()
118 }()
119
120 w := newWriter(func(r report) {}, func(a, b report) bool { return true })
121
122 // Ensure that the error is propagated to the copy
123 _, err := io.Copy(w, genIn)
124 if err == nil {
125 err = w.Close()
126 }
127 if err == nil {
128 t.Errorf("Did not get an error when copying to writer")
129 }
130 if err.Error() != "data error" {
131 t.Errorf("Did not get expected error: %v", err)
132 }
133 }
134
135 func TestStableLayerCount(t *testing.T) {
136
137 tests := []struct {
138 name string
139 lastLayerCount int
140 layerStatusCount int
141 stableThreshhold int
142 callCount int
143 expectStable bool
144 }{
145 {
146 name: "increasing layer count",
147 lastLayerCount: 3,
148 layerStatusCount: 4,
149 callCount: 1,
150 expectStable: false,
151 },
152 {
153 name: "has not met stable threshhold",
154 lastLayerCount: 3,
155 layerStatusCount: 3,
156 callCount: 2,
157 stableThreshhold: 3,
158 expectStable: false,
159 },
160 {
161 name: "met stable threshhold",
162 lastLayerCount: 3,
163 layerStatusCount: 3,
164 callCount: 4,
165 stableThreshhold: 3,
166 expectStable: true,
167 },
168 }
169 for _, test := range tests {
170 t.Run(test.name, func(t *testing.T) {
171 w := newWriter(func(report) {}, func(a, b report) bool { return true }).(*imageProgressWriter)
172 w.lastLayerCount = test.lastLayerCount
173 w.layerStatus = map[string]*progressLine{}
174 w.stableThreshhold = test.stableThreshhold
175 for i := 0; i < test.layerStatusCount; i++ {
176 w.layerStatus[strconv.Itoa(i)] = &progressLine{}
177 }
178 var result bool
179 for i := 0; i < test.callCount; i++ {
180 result = w.isStableLayerCount()
181 }
182 if result != test.expectStable {
183 t.Errorf("%s: expected %v, got %v", test.name, test.expectStable, result)
184 }
185 })
186 }
187 }
188
189 func compareReport(a, b report) bool {
190 if len(a) != len(b) {
191 return false
192 }
193 for k := range a {
194 if _, ok := b[k]; !ok {
195 return false
196 }
197 if !reflect.DeepEqual(*a[k], *b[k]) {
198 return false
199 }
200 }
201 return true
202 }
203
204 type progressGenerator json.Encoder
205
206 func newProgressGenerator(w io.Writer) *progressGenerator {
207 return (*progressGenerator)(json.NewEncoder(w))
208 }
209
210 func (p *progressGenerator) status(id, status string) {
211 (*json.Encoder)(p).Encode(&progressLine{ID: id, Status: status})
212 }
213
214 func (p *progressGenerator) err(msg string) {
215 (*json.Encoder)(p).Encode(&progressLine{Error: msg})
216 }