Skip to content

Commit 961d090

Browse files
committed
Update tests to adopt tooling changes
- Background - Feed, WithFeeder, WithPseudoTTY - use exit code consts where appropriate - some --quiet Signed-off-by: apostasie <[email protected]>
1 parent 598e2ee commit 961d090

13 files changed

+61
-65
lines changed

cmd/nerdctl/builder/builder_build_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ CMD ["echo", "nerdctl-build-test-string"]`, testutil.CommonImage)
102102
Cleanup: func(data test.Data, helpers test.Helpers) {
103103
helpers.Anyhow("rmi", "-f", data.Identifier())
104104
},
105-
Expected: test.Expects(-1, nil, nil),
105+
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil),
106106
},
107107
},
108108
}
@@ -234,7 +234,7 @@ func TestBuildFromStdin(t *testing.T) {
234234
dockerfile := fmt.Sprintf(`FROM %s
235235
CMD ["echo", "nerdctl-build-test-stdin"]`, testutil.CommonImage)
236236
cmd := helpers.Command("build", "-t", data.Identifier(), "-f", "-", ".")
237-
cmd.WithStdin(strings.NewReader(dockerfile))
237+
cmd.Feed(strings.NewReader(dockerfile))
238238
return cmd
239239
},
240240
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {

cmd/nerdctl/builder/builder_builder_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ CMD ["echo", "nerdctl-builder-debug-test-string"]`, testutil.CommonImage)
8383
err := os.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0o600)
8484
assert.NilError(helpers.T(), err)
8585
cmd := helpers.Command("builder", "debug", buildCtx)
86-
cmd.WithStdin(bytes.NewReader([]byte("c\n")))
86+
cmd.Feed(bytes.NewReader([]byte("c\n")))
8787
return cmd
8888
},
8989
Expected: test.Expects(0, nil, nil),

cmd/nerdctl/container/container_attach_linux_test.go

+22-30
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package container
1818

1919
import (
20+
"bytes"
2021
"errors"
21-
"os"
22+
"io"
2223
"strings"
2324
"testing"
2425
"time"
@@ -56,11 +57,9 @@ func TestAttach(t *testing.T) {
5657

5758
testCase.Setup = func(data test.Data, helpers test.Helpers) {
5859
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}))
6463

6564
cmd.Run(&test.Expected{
6665
ExitCode: 0,
@@ -74,15 +73,15 @@ func TestAttach(t *testing.T) {
7473
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
7574
// Run interactively and detach
7675
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 {
7980
// Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the
8081
// container can read stdin before we detach
8182
time.Sleep(time.Second)
8283
// 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})
8685
})
8786

8887
return cmd
@@ -120,10 +119,8 @@ func TestAttachDetachKeys(t *testing.T) {
120119

121120
testCase.Setup = func(data test.Data, helpers test.Helpers) {
122121
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}))
127124

128125
cmd.Run(&test.Expected{
129126
ExitCode: 0,
@@ -137,15 +134,14 @@ func TestAttachDetachKeys(t *testing.T) {
137134
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
138135
// Run interactively and detach
139136
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 {
142140
// Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the
143141
// container can read stdin before we detach
144142
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})
149145
})
150146

151147
return cmd
@@ -179,11 +175,9 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {
179175

180176
testCase.Setup = func(data test.Data, helpers test.Helpers) {
181177
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}))
187181

188182
cmd.Run(&test.Expected{
189183
ExitCode: 0,
@@ -197,10 +191,8 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {
197191
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
198192
// Run interactively and detach
199193
cmd := helpers.Command("attach", data.Identifier())
200-
cmd.WithPseudoTTY(func(f *os.File) error {
201-
_, err := f.WriteString("echo mark${NON}mark\nexit 42\n")
202-
return err
203-
})
194+
cmd.WithPseudoTTY()
195+
cmd.Feed(strings.NewReader("echo mark${NON}mark\nexit 42\n"))
204196

205197
return cmd
206198
}

cmd/nerdctl/container/container_run_linux_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ func TestRunSigProxy(t *testing.T) {
379379
},
380380

381381
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
382+
// FIXME: os.Interrupt will likely not work on Windows
382383
cmd := nerdtest.RunSigProxyContainer(os.Interrupt, true, nil, data, helpers)
383384
err := cmd.Signal(os.Interrupt)
384385
assert.NilError(helpers.T(), err)
@@ -417,7 +418,7 @@ func TestRunSigProxy(t *testing.T) {
417418
return cmd
418419
},
419420

420-
Expected: test.Expects(127, nil, expect.DoesNotContain(nerdtest.SignalCaught)),
421+
Expected: test.Expects(expect.ExitCodeSignaled, nil, expect.DoesNotContain(nerdtest.SignalCaught)),
421422
},
422423
}
423424

@@ -503,8 +504,9 @@ func TestRunWithDetachKeys(t *testing.T) {
503504
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
504505
// Run interactively and detach
505506
cmd := helpers.Command("run", "-it", "--detach-keys=ctrl-a,ctrl-b", "--name", data.Identifier(), testutil.CommonImage)
506-
cmd.WithPseudoTTY(func(f *os.File) error {
507-
_, _ = f.WriteString("echo mark${NON}mark\n")
507+
cmd.WithPseudoTTY()
508+
cmd.Feed(strings.NewReader("echo mark${NON}mark\n"))
509+
cmd.WithFeeder(func() io.Reader {
508510
// Because of the way we proxy stdin, we have to wait here, otherwise we detach before
509511
// the rest of the input ever reaches the container
510512
// Note that this only concerns nerdctl, as docker seems to behave ok LOCALLY.
@@ -514,8 +516,7 @@ func TestRunWithDetachKeys(t *testing.T) {
514516
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
515517
// }
516518
// ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
517-
_, err := f.Write([]byte{1, 2})
518-
return err
519+
return bytes.NewReader([]byte{1, 2})
519520
})
520521

521522
return cmd
@@ -571,8 +572,9 @@ func TestIssue3568(t *testing.T) {
571572
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
572573
// Run interactively and detach
573574
cmd := helpers.Command("run", "--rm", "-it", "--detach-keys=ctrl-a,ctrl-b", "--name", data.Identifier(), testutil.CommonImage)
574-
cmd.WithPseudoTTY(func(f *os.File) error {
575-
_, _ = f.WriteString("echo mark${NON}mark\n")
575+
cmd.WithPseudoTTY()
576+
cmd.Feed(strings.NewReader("echo mark${NON}mark\n"))
577+
cmd.WithFeeder(func() io.Reader {
576578
// Because of the way we proxy stdin, we have to wait here, otherwise we detach before
577579
// the rest of the input ever reaches the container
578580
// Note that this only concerns nerdctl, as docker seems to behave ok LOCALLY.
@@ -582,8 +584,7 @@ func TestIssue3568(t *testing.T) {
582584
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
583585
// }
584586
// ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
585-
_, err := f.Write([]byte{1, 2})
586-
return err
587+
return bytes.NewReader([]byte{1, 2})
587588
})
588589

589590
return cmd

cmd/nerdctl/container/container_start_linux_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package container
1818

1919
import (
20+
"bytes"
2021
"errors"
21-
"os"
22+
"io"
2223
"strings"
2324
"testing"
2425

@@ -40,10 +41,8 @@ func TestStartDetachKeys(t *testing.T) {
4041

4142
testCase.Setup = func(data test.Data, helpers test.Helpers) {
4243
cmd := helpers.Command("run", "-it", "--name", data.Identifier(), testutil.CommonImage)
43-
cmd.WithPseudoTTY(func(f *os.File) error {
44-
_, err := f.WriteString("exit\n")
45-
return err
46-
})
44+
cmd.WithPseudoTTY()
45+
cmd.Feed(strings.NewReader("exit\n"))
4746
cmd.Run(&test.Expected{
4847
ExitCode: 0,
4948
})
@@ -60,10 +59,10 @@ func TestStartDetachKeys(t *testing.T) {
6059
flags += "i"
6160
}
6261
cmd := helpers.Command("start", flags, "--detach-keys=ctrl-a,ctrl-b", data.Identifier())
63-
cmd.WithPseudoTTY(func(f *os.File) error {
62+
cmd.WithPseudoTTY()
63+
cmd.WithFeeder(func() io.Reader {
6464
// ctrl+a and ctrl+b (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
65-
_, err := f.Write([]byte{1, 2})
66-
return err
65+
return bytes.NewReader([]byte{1, 2})
6766
})
6867

6968
return cmd

cmd/nerdctl/image/image_list_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,13 @@ RUN echo "actually creating a layer so that docker sets the createdAt time"
270270
Description: "since=non-exists-image",
271271
Require: nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/3511"),
272272
Command: test.Command("images", "--filter", "since=non-exists-image"),
273-
Expected: test.Expects(-1, []error{errors.New("No such image: ")}, nil),
273+
Expected: test.Expects(expect.ExitCodeGenericFail, []error{errors.New("No such image: ")}, nil),
274274
},
275275
{
276276
Description: "before=non-exists-image",
277277
Require: nerdtest.NerdctlNeedsFixing("https://github.com/containerd/nerdctl/issues/3511"),
278278
Command: test.Command("images", "--filter", "before=non-exists-image"),
279-
Expected: test.Expects(-1, []error{errors.New("No such image: ")}, nil),
279+
Expected: test.Expects(expect.ExitCodeGenericFail, []error{errors.New("No such image: ")}, nil),
280280
},
281281
},
282282
}

cmd/nerdctl/image/image_load_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestLoadStdinFromPipe(t *testing.T) {
5353
cmd := helpers.Command("load")
5454
reader, err := os.Open(filepath.Join(data.TempDir(), "common.tar"))
5555
assert.NilError(t, err, "failed to open common.tar")
56-
cmd.WithStdin(reader)
56+
cmd.Feed(reader)
5757
return cmd
5858
},
5959
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {

cmd/nerdctl/ipfs/ipfs_compose_linux_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ipfs
1919
import (
2020
"fmt"
2121
"io"
22+
"os"
2223
"strconv"
2324
"strings"
2425
"testing"
@@ -211,8 +212,9 @@ func TestIPFSCompBuild(t *testing.T) {
211212
// Start a local ipfs backed registry
212213
// FIXME: this is bad and likely to collide with other tests
213214
ipfsServer = helpers.Command("ipfs", "registry", "serve", "--listen-registry", listenAddr)
214-
// Once foregrounded, do not wait for it more than a second
215-
ipfsServer.Background(1 * time.Second)
215+
// This should not take longer than that
216+
ipfsServer.WithTimeout(30 * time.Second)
217+
ipfsServer.Background()
216218
// Apparently necessary to let it start...
217219
time.Sleep(time.Second)
218220

@@ -237,9 +239,8 @@ COPY index.html /usr/share/nginx/html/index.html
237239

238240
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
239241
if ipfsServer != nil {
240-
// Close the server once done
241242
helpers.Anyhow("rmi", "-f", data.Get(mainImageCIDKey))
242-
ipfsServer.Run(nil)
243+
ipfsServer.Signal(os.Kill)
243244
}
244245
if comp != nil {
245246
helpers.Anyhow("compose", "-f", comp.YAMLFullPath(), "down", "-v")

cmd/nerdctl/ipfs/ipfs_registry_linux_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,17 @@ func TestIPFSNerdctlRegistry(t *testing.T) {
7070

7171
// Start a local ipfs backed registry
7272
ipfsServer = helpers.Command("ipfs", "registry", "serve", "--listen-registry", listenAddr)
73-
// Once foregrounded, do not wait for it more than a second
74-
ipfsServer.Background(1 * time.Second)
73+
// This should not take longer than that
74+
ipfsServer.WithTimeout(30 * time.Second)
75+
ipfsServer.Background()
7576
// Apparently necessary to let it start...
7677
time.Sleep(time.Second)
7778
}
7879

7980
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
8081
if ipfsServer != nil {
8182
// Close the server once done
82-
ipfsServer.Run(nil)
83+
ipfsServer.Signal(os.Kill)
8384
}
8485
}
8586

cmd/nerdctl/issues/main_linux_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestIssue108(t *testing.T) {
3939
{
4040
Description: "-it --net=host",
4141
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
42-
cmd := helpers.Command("run", "-it", "--rm", "--net=host", testutil.CommonImage, "echo", "this was always working")
42+
cmd := helpers.Command("run", "--quiet", "-it", "--rm", "--net=host", testutil.CommonImage, "echo", "this was always working")
4343
cmd.WithPseudoTTY()
4444
return cmd
4545
},
@@ -48,7 +48,7 @@ func TestIssue108(t *testing.T) {
4848
{
4949
Description: "--net=host -it",
5050
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
51-
cmd := helpers.Command("run", "--rm", "--net=host", "-it", testutil.CommonImage, "echo", "this was not working due to issue #108")
51+
cmd := helpers.Command("run", "--quiet", "--rm", "--net=host", "-it", testutil.CommonImage, "echo", "this was not working due to issue #108")
5252
cmd.WithPseudoTTY()
5353
return cmd
5454
},

cmd/nerdctl/main_test_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestTest(t *testing.T) {
6161
{
6262
Description: "failure with multiple error testing",
6363
Command: test.Command("-fail"),
64-
Expected: test.Expects(-1, []error{errors.New("unknown"), errors.New("shorthand")}, nil),
64+
Expected: test.Expects(expect.ExitCodeGenericFail, []error{errors.New("unknown"), errors.New("shorthand")}, nil),
6565
},
6666
{
6767
Description: "success with exact output testing",

cmd/nerdctl/system/system_events_linux_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import (
3030

3131
func testEventFilterExecutor(data test.Data, helpers test.Helpers) test.TestableCommand {
3232
cmd := helpers.Command("events", "--filter", data.Get("filter"), "--format", "json")
33-
cmd.Background(1 * time.Second)
33+
// 3 seconds is too short on slow rig (EL8)
34+
cmd.WithTimeout(10 * time.Second)
35+
cmd.Background()
3436
helpers.Ensure("run", "--rm", testutil.CommonImage)
3537
return cmd
3638
}

cmd/nerdctl/volume/volume_create_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestVolumeCreate(t *testing.T) {
8585
helpers.Anyhow("volume", "rm", "-f", data.Identifier())
8686
},
8787
// NOTE: docker returns 125 on this
88-
Expected: test.Expects(-1, []error{errdefs.ErrInvalidArgument}, nil),
88+
Expected: test.Expects(expect.ExitCodeGenericFail, []error{errdefs.ErrInvalidArgument}, nil),
8989
},
9090
{
9191
Description: "creating already existing volume should succeed",

0 commit comments

Comments
 (0)