github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_attach_linux_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "bytes" 21 "strings" 22 "testing" 23 24 "github.com/containerd/nerdctl/pkg/testutil" 25 "gotest.tools/v3/assert" 26 ) 27 28 // skipAttachForDocker should be called by attach-related tests that assert 'read detach keys' in stdout. 29 func skipAttachForDocker(t *testing.T) { 30 t.Helper() 31 if testutil.GetTarget() == testutil.Docker { 32 t.Skip("When detaching from a container, for a session started with 'docker attach'" + 33 ", it prints 'read escape sequence', but for one started with 'docker (run|start)', it prints nothing." + 34 " However, the flag is called '--detach-keys' in all cases" + 35 ", so nerdctl prints 'read detach keys' for all cases" + 36 ", and that's why this test is skipped for Docker.") 37 } 38 } 39 40 // prepareContainerToAttach spins up a container (entrypoint = shell) with `-it` and detaches from it 41 // so that it can be re-attached to later. 42 func prepareContainerToAttach(base *testutil.Base, containerName string) { 43 opts := []func(*testutil.Cmd){ 44 testutil.WithStdin(testutil.NewDelayOnceReader(bytes.NewReader( 45 []byte{16, 17}, // ctrl+p,ctrl+q, see https://www.physics.udel.edu/~watson/scen103/ascii.html 46 ))), 47 } 48 // unbuffer(1) emulates tty, which is required by `nerdctl run -t`. 49 // unbuffer(1) can be installed with `apt-get install expect`. 50 // 51 // "-p" is needed because we need unbuffer to read from stdin, and from [1]: 52 // "Normally, unbuffer does not read from stdin. This simplifies use of unbuffer in some situations. 53 // To use unbuffer in a pipeline, use the -p flag." 54 // 55 // [1] https://linux.die.net/man/1/unbuffer 56 base.CmdWithHelper([]string{"unbuffer", "-p"}, "run", "-it", "--name", containerName, testutil.CommonImage). 57 CmdOption(opts...).AssertOutContains("read detach keys") 58 container := base.InspectContainer(containerName) 59 assert.Equal(base.T, container.State.Running, true) 60 } 61 62 func TestAttach(t *testing.T) { 63 t.Parallel() 64 65 skipAttachForDocker(t) 66 67 base := testutil.NewBase(t) 68 containerName := testutil.Identifier(t) 69 70 defer base.Cmd("container", "rm", "-f", containerName).AssertOK() 71 prepareContainerToAttach(base, containerName) 72 73 opts := []func(*testutil.Cmd){ 74 testutil.WithStdin(testutil.NewDelayOnceReader(strings.NewReader("expr 1 + 1\nexit\n"))), 75 } 76 // `unbuffer -p` returns 0 even if the underlying nerdctl process returns a non-zero exit code, 77 // so the exit code cannot be easily tested here. 78 base.CmdWithHelper([]string{"unbuffer", "-p"}, "attach", containerName).CmdOption(opts...).AssertOutContains("2") 79 container := base.InspectContainer(containerName) 80 assert.Equal(base.T, container.State.Running, false) 81 } 82 83 func TestAttachDetachKeys(t *testing.T) { 84 t.Parallel() 85 86 skipAttachForDocker(t) 87 88 base := testutil.NewBase(t) 89 containerName := testutil.Identifier(t) 90 91 defer base.Cmd("container", "rm", "-f", containerName).AssertOK() 92 prepareContainerToAttach(base, containerName) 93 94 opts := []func(*testutil.Cmd){ 95 testutil.WithStdin(testutil.NewDelayOnceReader(bytes.NewReader( 96 []byte{1, 2}, // https://www.physics.udel.edu/~watson/scen103/ascii.html 97 ))), 98 } 99 base.CmdWithHelper([]string{"unbuffer", "-p"}, "attach", "--detach-keys=ctrl-a,ctrl-b", containerName). 100 CmdOption(opts...).AssertOutContains("read detach keys") 101 container := base.InspectContainer(containerName) 102 assert.Equal(base.T, container.State.Running, true) 103 }