MCPcopy Create free account
hub / github.com/containerd/nerdctl / TestAttach

Function TestAttach

cmd/nerdctl/container/container_attach_linux_test.go:45–105  ·  view source on GitHub ↗

* Important notes: - for both docker and nerdctl, you can run+detach of a container and exit 0, while the container would actually fail starting - nerdctl (not docker): on run, detach will race anything on stdin before the detach sequence from reaching the container - nerdctl AND docker: on attach ^

(t *testing.T)

Source from the content-addressed store, hash-verified

43*/
44
45func TestAttach(t *testing.T) {
46 // In nerdctl the detach return code from the container after attach is 0, but in docker the return code is 1.
47 // This behaviour is reported in https://github.com/containerd/nerdctl/issues/3571
48 ex := 0
49 if nerdtest.IsDocker() {
50 ex = 1
51 }
52
53 testCase := nerdtest.Setup()
54
55 testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
56 helpers.Anyhow("rm", "-f", data.Identifier())
57 }
58
59 testCase.Setup = func(data test.Data, helpers test.Helpers) {
60 cmd := helpers.Command("run", "--rm", "-it", "--name", data.Identifier(), testutil.CommonImage)
61 cmd.WithPseudoTTY()
62 // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
63 cmd.Feed(bytes.NewReader([]byte{16, 17}))
64
65 cmd.Run(&test.Expected{
66 ExitCode: 0,
67 Errors: []error{errors.New("read detach keys")},
68 Output: func(stdout string, t tig.T) {
69 assert.Assert(t, strings.Contains(helpers.Capture("inspect", "--format", "json", data.Identifier()), "\"Running\":true"))
70 },
71 })
72 }
73
74 testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
75 // Run interactively and detach
76 cmd := helpers.Command("attach", data.Identifier())
77
78 cmd.WithPseudoTTY()
79 cmd.Feed(strings.NewReader("echo mark${NON}mark\n"))
80 cmd.WithFeeder(func() io.Reader {
81 // Interestingly, and unlike with run, on attach, docker (like nerdctl) ALSO needs a pause so that the
82 // container can read stdin before we detach
83 time.Sleep(time.Second)
84 // ctrl+p and ctrl+q (see https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
85 return bytes.NewReader([]byte{16, 17})
86 })
87
88 return cmd
89 }
90
91 testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected {
92 return &test.Expected{
93 ExitCode: ex,
94 Errors: []error{errors.New("read detach keys")},
95 Output: expect.All(
96 expect.Contains("markmark"),
97 func(stdout string, t tig.T) {
98 assert.Assert(t, strings.Contains(helpers.Capture("inspect", "--format", "json", data.Identifier()), "\"Running\":true"))
99 },
100 ),
101 }
102 }

Callers

nothing calls this directly

Calls 11

IsDockerFunction · 0.92
SetupFunction · 0.92
AssertMethod · 0.80
AnyhowMethod · 0.65
IdentifierMethod · 0.65
CommandMethod · 0.65
WithPseudoTTYMethod · 0.65
FeedMethod · 0.65
RunMethod · 0.65
CaptureMethod · 0.65
WithFeederMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…