17
17
package container
18
18
19
19
import (
20
+ "bytes"
20
21
"errors"
21
- "os "
22
+ "io "
22
23
"strings"
23
24
"testing"
24
25
"time"
@@ -56,11 +57,9 @@ func TestAttach(t *testing.T) {
56
57
57
58
testCase .Setup = func (data test.Data , helpers test.Helpers ) {
58
59
cmd := helpers .Command ("run" , "--rm" , "-it" , "--name" , data .Identifier (), testutil .CommonImage )
59
- cmd .WithPseudoTTY (func (f * os.File ) error {
60
- // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
61
- _ , err := f .Write ([]byte {16 , 17 })
62
- return err
63
- })
60
+ cmd .WithPseudoTTY ()
61
+ // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
62
+ cmd .Feed (bytes .NewReader ([]byte {16 , 17 }))
64
63
65
64
cmd .Run (& test.Expected {
66
65
ExitCode : 0 ,
@@ -74,15 +73,15 @@ func TestAttach(t *testing.T) {
74
73
testCase .Command = func (data test.Data , helpers test.Helpers ) test.TestableCommand {
75
74
// Run interactively and detach
76
75
cmd := helpers .Command ("attach" , data .Identifier ())
77
- cmd .WithPseudoTTY (func (f * os.File ) error {
78
- _ , _ = f .WriteString ("echo mark${NON}mark\n " )
76
+
77
+ cmd .WithPseudoTTY ()
78
+ cmd .Feed (strings .NewReader ("echo mark${NON}mark\n " ))
79
+ cmd .WithFeeder (func () io.Reader {
79
80
// Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the
80
81
// container can read stdin before we detach
81
82
time .Sleep (time .Second )
82
83
// ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
83
- _ , err := f .Write ([]byte {16 , 17 })
84
-
85
- return err
84
+ return bytes .NewReader ([]byte {16 , 17 })
86
85
})
87
86
88
87
return cmd
@@ -120,10 +119,8 @@ func TestAttachDetachKeys(t *testing.T) {
120
119
121
120
testCase .Setup = func (data test.Data , helpers test.Helpers ) {
122
121
cmd := helpers .Command ("run" , "--rm" , "-it" , "--detach-keys=ctrl-q" , "--name" , data .Identifier (), testutil .CommonImage )
123
- cmd .WithPseudoTTY (func (f * os.File ) error {
124
- _ , err := f .Write ([]byte {17 })
125
- return err
126
- })
122
+ cmd .WithPseudoTTY ()
123
+ cmd .Feed (bytes .NewReader ([]byte {17 }))
127
124
128
125
cmd .Run (& test.Expected {
129
126
ExitCode : 0 ,
@@ -137,15 +134,14 @@ func TestAttachDetachKeys(t *testing.T) {
137
134
testCase .Command = func (data test.Data , helpers test.Helpers ) test.TestableCommand {
138
135
// Run interactively and detach
139
136
cmd := helpers .Command ("attach" , "--detach-keys=ctrl-a,ctrl-b" , data .Identifier ())
140
- cmd .WithPseudoTTY (func (f * os.File ) error {
141
- _ , _ = f .WriteString ("echo mark${NON}mark\n " )
137
+ cmd .WithPseudoTTY ()
138
+ cmd .Feed (strings .NewReader ("echo mark${NON}mark\n " ))
139
+ cmd .WithFeeder (func () io.Reader {
142
140
// Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the
143
141
// container can read stdin before we detach
144
142
time .Sleep (time .Second )
145
- // ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
146
- _ , err := f .Write ([]byte {1 , 2 })
147
-
148
- return err
143
+ // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
144
+ return bytes .NewReader ([]byte {1 , 2 })
149
145
})
150
146
151
147
return cmd
@@ -179,11 +175,9 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {
179
175
180
176
testCase .Setup = func (data test.Data , helpers test.Helpers ) {
181
177
cmd := helpers .Command ("run" , "--rm" , "-it" , "--detach-keys=ctrl-a,ctrl-b" , "--name" , data .Identifier (), testutil .CommonImage )
182
- cmd .WithPseudoTTY (func (f * os.File ) error {
183
- // ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
184
- _ , err := f .Write ([]byte {1 , 2 })
185
- return err
186
- })
178
+ cmd .WithPseudoTTY ()
179
+ // ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
180
+ cmd .Feed (bytes .NewReader ([]byte {1 , 2 }))
187
181
188
182
cmd .Run (& test.Expected {
189
183
ExitCode : 0 ,
@@ -197,10 +191,8 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {
197
191
testCase .Command = func (data test.Data , helpers test.Helpers ) test.TestableCommand {
198
192
// Run interactively and detach
199
193
cmd := helpers .Command ("attach" , data .Identifier ())
200
- cmd .WithPseudoTTY (func (f * os.File ) error {
201
- _ , err := f .WriteString ("echo mark${NON}mark\n exit 42\n " )
202
- return err
203
- })
194
+ cmd .WithPseudoTTY ()
195
+ cmd .Feed (strings .NewReader ("echo mark${NON}mark\n exit 42\n " ))
204
196
205
197
return cmd
206
198
}
0 commit comments