File: mux_test.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (321 lines) | stat: -rw-r--r-- 7,245 bytes parent folder | download
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
package ioset

import (
	"bytes"
	"fmt"
	"io"
	"regexp"
	"strings"
	"testing"

	"golang.org/x/sync/errgroup"
)

// TestMuxIO tests MuxIO
func TestMuxIO(t *testing.T) {
	tests := []struct {
		name       string
		inputs     []instruction
		initIdx    int
		outputsNum int
		wants      []string

		// Everytime string is written to the mux stdin, the output end
		// that received the string write backs to the string that is masked with
		// its index number. This is useful to check if writeback is written from the
		// expected output destination.
		wantsMaskedOutput string
	}{
		{
			name: "single output",
			inputs: []instruction{
				input("foo\nbar\n"),
				toggle(),
				input("1234"),
				toggle(),
				input("456"),
			},
			initIdx:           0,
			outputsNum:        1,
			wants:             []string{"foo\nbar\n1234456"},
			wantsMaskedOutput: `^0+$`,
		},
		{
			name: "multi output",
			inputs: []instruction{
				input("foo\nbar\n"),
				toggle(),
				input("12" + string([]rune{rune(1)}) + "34abc"),
				toggle(),
				input("456"),
			},
			initIdx:           0,
			outputsNum:        3,
			wants:             []string{"foo\nbar\n", "1234abc", "456"},
			wantsMaskedOutput: `^0+1+2+$`,
		},
		{
			name: "multi output with nonzero index",
			inputs: []instruction{
				input("foo\nbar\n"),
				toggle(),
				input("1234"),
				toggle(),
				input("456"),
			},
			initIdx:           1,
			outputsNum:        3,
			wants:             []string{"456", "foo\nbar\n", "1234"},
			wantsMaskedOutput: `^1+2+0+$`,
		},
		{
			name: "multi output many toggles",
			inputs: []instruction{
				input("foo\nbar\n"),
				toggle(),
				input("1234"),
				toggle(),
				toggle(),
				input("456"),
				toggle(),
				input("%%%%"),
				toggle(),
				toggle(),
				toggle(),
				input("aaaa"),
			},
			initIdx:           0,
			outputsNum:        3,
			wants:             []string{"foo\nbar\n456", "1234%%%%aaaa", ""},
			wantsMaskedOutput: `^0+1+0+1+$`,
		},
		{
			name: "enable disable",
			inputs: []instruction{
				input("foo\nbar\n"),
				toggle(),
				input("1234"),
				toggle(),
				input("456"),
				disable(2),
				input("%%%%"),
				enable(2),
				toggle(),
				toggle(),
				input("aaa"),
				disable(2),
				disable(1),
				input("1111"),
				toggle(),
				input("2222"),
				toggle(),
				input("3333"),
			},
			initIdx:           0,
			outputsNum:        3,
			wants:             []string{"foo\nbar\n%%%%111122223333", "1234", "456aaa"},
			wantsMaskedOutput: `^0+1+2+0+2+0+$`,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			inBuf, end, in := newTestIn(t)
			var outBufs []*outBuf
			var outs []MuxOut
			if tt.outputsNum != len(tt.wants) {
				t.Fatalf("wants != outputsNum")
			}
			for i := 0; i < tt.outputsNum; i++ {
				outBuf, out := newTestOut(t, i)
				outBufs = append(outBufs, outBuf)
				outs = append(outs, MuxOut{out, nil, nil})
			}
			mio := NewMuxIO(in, outs, tt.initIdx, func(prev int, res int) string { return "" })
			for _, i := range tt.inputs {
				// Add input to MuxIO
				istr, writeback := i(mio)
				if _, err := end.Stdin.Write([]byte(istr)); err != nil {
					t.Fatalf("failed to write data to stdin: %v", err)
				}

				// Wait for writeback of this input
				var eg errgroup.Group
				eg.Go(func() error {
					outbuf := make([]byte, len(writeback))
					if _, err := io.ReadAtLeast(end.Stdout, outbuf, len(outbuf)); err != nil {
						return err
					}
					return nil
				})
				eg.Go(func() error {
					errbuf := make([]byte, len(writeback))
					if _, err := io.ReadAtLeast(end.Stderr, errbuf, len(errbuf)); err != nil {
						return err
					}
					return nil
				})
				if err := eg.Wait(); err != nil {
					t.Fatalf("failed to wait for output: %v", err)
				}
			}

			// Close stdin on this MuxIO
			end.Stdin.Close()

			// Wait for all output ends reach EOF
			mio.waitClosed()

			// Close stdout/stderr as well
			in.Close()

			// Check if each output end received expected string
			<-inBuf.doneCh
			for i, o := range outBufs {
				<-o.doneCh
				if o.stdin != tt.wants[i] {
					t.Fatalf("output[%d]: got %q; wanted %q", i, o.stdin, tt.wants[i])
				}
			}

			// Check if expected string is returned from expected outputs
			if !regexp.MustCompile(tt.wantsMaskedOutput).MatchString(inBuf.stdout) {
				t.Fatalf("stdout: got %q; wanted %q", inBuf.stdout, tt.wantsMaskedOutput)
			}
			if !regexp.MustCompile(tt.wantsMaskedOutput).MatchString(inBuf.stderr) {
				t.Fatalf("stderr: got %q; wanted %q", inBuf.stderr, tt.wantsMaskedOutput)
			}
		})
	}
}

type instruction func(m *MuxIO) (intput string, writeBackView string)

func input(s string) instruction {
	return func(m *MuxIO) (string, string) {
		return s, strings.ReplaceAll(s, string([]rune{rune(1)}), "")
	}
}

func toggle() instruction {
	return func(m *MuxIO) (string, string) {
		return string([]rune{rune(1)}) + "c", ""
	}
}

func enable(i int) instruction {
	return func(m *MuxIO) (string, string) {
		m.Enable(i)
		return "", ""
	}
}

func disable(i int) instruction {
	return func(m *MuxIO) (string, string) {
		m.Disable(i)
		return "", ""
	}
}

type inBuf struct {
	stdout string
	stderr string
	doneCh chan struct{}
}

func newTestIn(t *testing.T) (*inBuf, Out, In) {
	ti := &inBuf{
		doneCh: make(chan struct{}),
	}
	gotOutR, gotOutW := io.Pipe()
	gotErrR, gotErrW := io.Pipe()
	outR, outW := io.Pipe()
	var eg errgroup.Group
	eg.Go(func() error {
		buf := new(bytes.Buffer)
		if _, err := io.Copy(io.MultiWriter(gotOutW, buf), outR); err != nil {
			return err
		}
		ti.stdout = buf.String()
		return nil
	})
	errR, errW := io.Pipe()
	eg.Go(func() error {
		buf := new(bytes.Buffer)
		if _, err := io.Copy(io.MultiWriter(gotErrW, buf), errR); err != nil {
			return err
		}
		ti.stderr = buf.String()
		return nil
	})
	go func() {
		eg.Wait()
		close(ti.doneCh)
	}()
	inR, inW := io.Pipe()
	return ti, Out{Stdin: inW, Stdout: gotOutR, Stderr: gotErrR}, In{Stdin: inR, Stdout: outW, Stderr: errW}
}

type outBuf struct {
	idx    int
	stdin  string
	doneCh chan struct{}
}

func newTestOut(t *testing.T, idx int) (*outBuf, Out) {
	to := &outBuf{
		idx:    idx,
		doneCh: make(chan struct{}),
	}
	inR, inW := io.Pipe()
	outR, outW := io.Pipe()
	errR, errW := io.Pipe()
	go func() {
		defer inR.Close()
		defer outW.Close()
		defer errW.Close()
		buf := new(bytes.Buffer)
		mw := io.MultiWriter(buf,
			writeMasked(outW, fmt.Sprintf("%d", to.idx)),
			writeMasked(errW, fmt.Sprintf("%d", to.idx)),
		)
		if _, err := io.Copy(mw, inR); err != nil {
			inR.CloseWithError(err)
			outW.CloseWithError(err)
			errW.CloseWithError(err)
			return
		}
		to.stdin = string(buf.Bytes())
		outW.Close()
		errW.Close()
		close(to.doneCh)
	}()
	return to, Out{Stdin: inW, Stdout: outR, Stderr: errR}
}

func writeMasked(w io.Writer, s string) io.Writer {
	buf := make([]byte, 4096)
	pr, pw := io.Pipe()
	go func() {
		for {
			n, readErr := pr.Read(buf)
			if readErr != nil && readErr != io.EOF {
				pr.CloseWithError(readErr)
				return
			}
			var masked string
			for i := 0; i < n; i++ {
				masked += s
			}
			if _, err := w.Write([]byte(masked)); err != nil {
				pr.CloseWithError(err)
				return
			}
			if readErr == io.EOF {
				pr.Close()
				return
			}
		}
	}()
	return pw
}