github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_start_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 func TestStartDetachKeys(t *testing.T) { 29 t.Parallel() 30 31 skipAttachForDocker(t) 32 33 base := testutil.NewBase(t) 34 containerName := testutil.Identifier(t) 35 36 defer base.Cmd("container", "rm", "-f", containerName).AssertOK() 37 opts := []func(*testutil.Cmd){ 38 // If NewDelayOnceReader is not used, 39 // the container state will be Created instead of Exited. 40 // Maybe `unbuffer` exits too early in that case? 41 testutil.WithStdin(testutil.NewDelayOnceReader(strings.NewReader("exit\n"))), 42 } 43 // unbuffer(1) emulates tty, which is required by `nerdctl run -t`. 44 // unbuffer(1) can be installed with `apt-get install expect`. 45 // 46 // "-p" is needed because we need unbuffer to read from stdin, and from [1]: 47 // "Normally, unbuffer does not read from stdin. This simplifies use of unbuffer in some situations. 48 // To use unbuffer in a pipeline, use the -p flag." 49 // 50 // [1] https://linux.die.net/man/1/unbuffer 51 base.CmdWithHelper([]string{"unbuffer", "-p"}, "run", "-it", "--name", containerName, testutil.CommonImage). 52 CmdOption(opts...).AssertOK() 53 container := base.InspectContainer(containerName) 54 assert.Equal(base.T, container.State.Running, false) 55 56 opts = []func(*testutil.Cmd){ 57 testutil.WithStdin(testutil.NewDelayOnceReader(bytes.NewReader([]byte{1, 2}))), // https://www.physics.udel.edu/~watson/scen103/ascii.html 58 } 59 base.CmdWithHelper([]string{"unbuffer", "-p"}, "start", "-a", "--detach-keys=ctrl-a,ctrl-b", containerName). 60 CmdOption(opts...).AssertOutContains("read detach keys") 61 container = base.InspectContainer(containerName) 62 assert.Equal(base.T, container.State.Running, true) 63 }