Skip to content

Commit ee32a21

Browse files
committed
chore: cleanup tests
Signed-off-by: Alano Terblanche <[email protected]>
1 parent 4062944 commit ee32a21

File tree

13 files changed

+110
-87
lines changed

13 files changed

+110
-87
lines changed

cli/command/builder/prune_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"testing"
7-
"time"
87

98
"github.com/docker/cli/internal/test"
109
"github.com/docker/docker/api/types"
@@ -19,8 +18,6 @@ func TestBuilderPromptTermination(t *testing.T) {
1918
return nil, errors.New("fakeClient builderPruneFunc should not be called")
2019
},
2120
})
22-
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
23-
cli.SetIn(test.NewFakeStreamIn(ctx, time.Second*2))
2421
cmd := NewPruneCommand(cli)
2522
test.TerminatePrompt(ctx, t, cmd, cli, nil)
2623
}

cli/command/container/prune_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package container
33
import (
44
"context"
55
"testing"
6-
"time"
76

87
"github.com/docker/cli/internal/test"
98
"github.com/docker/docker/api/types"
@@ -20,9 +19,6 @@ func TestContainerPrunePromptTermination(t *testing.T) {
2019
return types.ContainersPruneReport{}, errors.New("fakeClient containerPruneFunc should not be called")
2120
},
2221
})
23-
24-
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
25-
cli.SetIn(test.NewFakeStreamIn(ctx, time.Second*2))
2622
cmd := NewPruneCommand(cli)
2723
test.TerminatePrompt(ctx, t, cmd, cli, nil)
2824
}

cli/command/image/prune_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"testing"
8-
"time"
98

109
"github.com/docker/cli/internal/test"
1110
"github.com/docker/docker/api/types"
@@ -113,8 +112,6 @@ func TestPrunePromptTermination(t *testing.T) {
113112
return types.ImagesPruneReport{}, errors.New("fakeClient imagesPruneFunc should not be called")
114113
},
115114
})
116-
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
117-
cli.SetIn(test.NewFakeStreamIn(ctx, time.Second*2))
118115
cmd := NewPruneCommand(cli)
119116
test.TerminatePrompt(ctx, t, cmd, cli, nil)
120117
}

cli/command/network/prune_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package network
33
import (
44
"context"
55
"testing"
6-
"time"
76

87
"github.com/docker/cli/internal/test"
98
"github.com/docker/docker/api/types"
@@ -20,8 +19,6 @@ func TestNetworkPrunePromptTermination(t *testing.T) {
2019
return types.NetworksPruneReport{}, errors.New("fakeClient networkPruneFunc should not be called")
2120
},
2221
})
23-
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
24-
cli.SetIn(test.NewFakeStreamIn(ctx, time.Second*2))
2522
cmd := NewPruneCommand(cli)
2623
test.TerminatePrompt(ctx, t, cmd, cli, nil)
2724
}

cli/command/network/remove_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"io"
66
"testing"
7-
"time"
87

98
"github.com/docker/cli/internal/test"
109
"github.com/docker/docker/api/types"
@@ -113,8 +112,6 @@ func TestNetworkRemovePromptTermination(t *testing.T) {
113112
}, nil, nil
114113
},
115114
})
116-
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
117-
cli.SetIn(test.NewFakeStreamIn(ctx, time.Second*2))
118115
cmd := newRemoveCommand(cli)
119116
cmd.SetArgs([]string{"existing-network"})
120117
test.TerminatePrompt(ctx, t, cmd, cli, nil)

cli/command/plugin/upgrade_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"io"
66
"testing"
7-
"time"
87

98
"github.com/docker/cli/internal/test"
109
"github.com/docker/docker/api/types"
@@ -14,9 +13,6 @@ import (
1413
)
1514

1615
func TestUpgradePromptTermination(t *testing.T) {
17-
t.Cleanup(func() {
18-
})
19-
2016
ctx, cancel := context.WithCancel(context.Background())
2117
t.Cleanup(cancel)
2218

@@ -33,7 +29,6 @@ func TestUpgradePromptTermination(t *testing.T) {
3329
}, []byte{}, nil
3430
},
3531
})
36-
cli.SetIn(test.NewFakeStreamIn(ctx, time.Second*2))
3732
cmd := newUpgradeCommand(cli)
3833
// need to set a remote address that does not match the plugin
3934
// reference sent by the `pluginInspectFunc`

cli/command/system/prune_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package system
33
import (
44
"context"
55
"testing"
6-
"time"
76

87
"github.com/docker/cli/cli/config/configfile"
98
"github.com/docker/cli/internal/test"
@@ -68,8 +67,6 @@ func TestSystemPrunePromptTermination(t *testing.T) {
6867
},
6968
})
7069

71-
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
72-
cli.SetIn(test.NewFakeStreamIn(ctx, time.Second*2))
7370
cmd := newPruneCommand(cli)
7471
test.TerminatePrompt(ctx, t, cmd, cli, nil)
7572
}

cli/command/trust/revoke_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"io"
66
"testing"
7-
"time"
87

98
"github.com/docker/cli/cli/trust"
109
"github.com/docker/cli/internal/test"
@@ -157,10 +156,8 @@ func TestRevokeTrustPromptTermination(t *testing.T) {
157156
t.Cleanup(cancel)
158157

159158
cli := test.NewFakeCli(&fakeClient{})
160-
cli.SetIn(test.NewFakeStreamIn(ctx, 2*time.Second))
161159
cmd := newRevokeCommand(cli)
162160
cmd.SetArgs([]string{"example/trust-demo"})
163-
164161
test.TerminatePrompt(ctx, t, cmd, cli, nil)
165162
assert.Equal(t, cli.ErrBuffer().String(), "")
166163
golden.Assert(t, cli.OutBuffer().String(), "trust-revoke-prompt-termination.golden")

cli/command/utils_test.go

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/docker/cli/cli/command"
15+
"github.com/docker/cli/cli/streams"
1516
"github.com/docker/cli/internal/test"
1617
"github.com/pkg/errors"
1718
"gotest.tools/v3/assert"
@@ -81,46 +82,98 @@ func TestPromptForConfirmation(t *testing.T) {
8182
ctx, cancel := context.WithCancel(context.Background())
8283
t.Cleanup(cancel)
8384

85+
t.Run("case=prompt should wait for user input", func(t *testing.T) {
86+
buf := new(bytes.Buffer)
87+
wroteHook := make(chan struct{}, 1)
88+
bufioWriter := bufio.NewWriter(buf)
89+
w := test.NewWriterWithHook(bufioWriter, func(p []byte) {
90+
wroteHook <- struct{}{}
91+
})
92+
93+
r, _, err := os.Pipe()
94+
assert.NilError(t, err)
95+
in := streams.NewIn(r)
96+
97+
result := make(chan bool, 1)
98+
go func() {
99+
result <- command.PromptForConfirmation(ctx, in, w, "")
100+
}()
101+
102+
// wait for the prompt to write to the buffer
103+
pollForPromptOutput(ctx, t, wroteHook)
104+
105+
assert.NilError(t, bufioWriter.Flush())
106+
assert.Check(t, strings.Contains(buf.String(), "Are you sure you want to proceed? [y/N]"))
107+
108+
writeCtx, writeCancel := context.WithTimeout(ctx, 100*time.Millisecond)
109+
defer writeCancel()
110+
111+
writeInputChan := make(chan struct{})
112+
113+
go func() {
114+
_, err := w.Write([]byte("n"))
115+
assert.NilError(t, err)
116+
writeInputChan <- struct{}{}
117+
}()
118+
119+
select {
120+
case <-writeCtx.Done():
121+
t.Fatal("PromptForConfirmation did not receive user input")
122+
case <-result:
123+
t.Fatal("PromptForConfirmation returned before user input")
124+
case <-writeInputChan:
125+
}
126+
})
127+
84128
t.Run("case=terminate the prompt with SIGINT", func(t *testing.T) {
85129
wroteHook := make(chan struct{}, 1)
86130
buf := new(bytes.Buffer)
87131
bufioWriter := bufio.NewWriter(buf)
88-
w := test.NewWriterWithHook(bufioWriter, func() {
132+
w := test.NewWriterWithHook(bufioWriter, func(p []byte) {
89133
wroteHook <- struct{}{}
90134
})
91135

92-
in := test.NewFakeStreamIn(ctx, time.Second*2)
93-
t.Cleanup(func() {
94-
assert.NilError(t, in.Close())
95-
})
136+
r, _, err := os.Pipe()
137+
assert.NilError(t, err)
138+
in := streams.NewIn(r)
139+
96140
result := make(chan bool, 1)
141+
defer close(result)
142+
97143
go func() {
98-
defer close(result)
99144
result <- command.PromptForConfirmation(ctx, in, w, "")
100145
}()
101146

102147
// wait for the Prompt to write to the buffer
103148
pollForPromptOutput(ctx, t, wroteHook)
149+
104150
assert.NilError(t, bufioWriter.Flush())
105151
assert.Equal(t, strings.TrimSpace(buf.String()), "Are you sure you want to proceed? [y/N]")
106152

153+
resultCtx, resultCancel := context.WithTimeout(ctx, 100*time.Millisecond)
154+
defer resultCancel()
155+
107156
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
157+
108158
select {
159+
case <-resultCtx.Done():
160+
t.Fatal("PromptForConfirmation did not return after SIGINT")
109161
case r := <-result:
110162
assert.Check(t, !r)
111-
case <-time.After(100 * time.Millisecond):
112-
t.Fatal("PromptForConfirmation did not return after SIGINT")
113163
}
114164
})
115165

116166
t.Run("case=prompt should return on io.EOF", func(t *testing.T) {
117167
buf := new(bytes.Buffer)
118168
wroteHook := make(chan struct{}, 1)
119169
bufioWriter := bufio.NewWriter(buf)
120-
w := test.NewWriterWithHook(bufioWriter, func() {
170+
w := test.NewWriterWithHook(bufioWriter, func(p []byte) {
121171
wroteHook <- struct{}{}
122172
})
123-
in := test.NewFakeStreamIn(ctx, 0)
173+
174+
r, _, err := os.Pipe()
175+
assert.NilError(t, err)
176+
in := streams.NewIn(r)
124177

125178
result := make(chan bool, 1)
126179
go func() {
@@ -132,13 +185,17 @@ func TestPromptForConfirmation(t *testing.T) {
132185

133186
assert.NilError(t, bufioWriter.Flush())
134187
assert.Check(t, strings.Contains(buf.String(), "Are you sure you want to proceed? [y/N]"))
188+
135189
assert.NilError(t, in.Close())
136190

191+
resultCtx, resultCancel := context.WithTimeout(ctx, 100*time.Millisecond)
192+
defer resultCancel()
193+
137194
select {
195+
case <-resultCtx.Done():
196+
t.Fatal("PromptForConfirmation did not return after io.EOF")
138197
case r := <-result:
139198
assert.Check(t, !r)
140-
case <-time.After(100 * time.Millisecond):
141-
t.Fatal("PromptForConfirmation did not return after io.EOF")
142199
}
143200
})
144201
}

cli/command/volume/prune_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"runtime"
88
"strings"
99
"testing"
10-
"time"
1110

1211
"github.com/docker/cli/cli/streams"
1312
"github.com/docker/cli/internal/test"
@@ -196,8 +195,6 @@ func TestVolumePrunePromptTerminate(t *testing.T) {
196195
},
197196
})
198197

199-
// set a fake reader so that our kill signal reaches the prompt before the prompt reads from stdin
200-
cli.SetIn(test.NewFakeStreamIn(ctx, time.Second*2))
201198
cmd := NewPruneCommand(cli)
202199
test.TerminatePrompt(ctx, t, cmd, cli, nil)
203200

0 commit comments

Comments
 (0)