-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support '--detach-keys' for 'nerdctl (run|start)'
Control flow: 1. Detect if the detach keys are present in stdin. 2. After detecting the detach keys, cancel the current I/O operations by calling IO.Cancel(). This means that the underlying I/O FIFOs should be left intact, and containerd should keep writing to/reading from those FIFOs, but nerdctl should stop writing to/reading from them. 3. After starting the task, we need to call IO.Wait(), and after it returns, we need to see if the container exits or it's just the user detaching from the container by checking the state of the container. Signed-off-by: Hsing-Yu (David) Chen <davidhsingyuchen@gmail.com>
- Loading branch information
1 parent
c9174fe
commit ec49140
Showing
15 changed files
with
322 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/containerd/nerdctl/pkg/testutil" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestStartDetachKeys(t *testing.T) { | ||
t.Parallel() | ||
|
||
if testutil.GetTarget() == testutil.Docker { | ||
t.Skip("When detaching from a container, for a session started with 'docker attach'" + | ||
", it prints 'read escape sequence', but for one started with 'docker (run|start)', it prints nothing." + | ||
" However, the flag is called '--detach-keys' in all cases" + | ||
", so nerdctl prints 'read detach keys' for all cases" + | ||
", and that's why this test is skipped for Docker.") | ||
} | ||
|
||
base := testutil.NewBase(t) | ||
containerName := testutil.Identifier(t) | ||
|
||
defer base.Cmd("container", "rm", "-f", containerName).AssertOK() | ||
opts := []func(*testutil.Cmd){ | ||
// If NewDelayOnceReader is not used, | ||
// the container state will be Created instead of Exited. | ||
// Maybe `unbuffer` exits too early in that case? | ||
testutil.WithStdin(testutil.NewDelayOnceReader(strings.NewReader("exit\n"))), | ||
} | ||
// unbuffer(1) emulates tty, which is required by `nerdctl run -t`. | ||
// unbuffer(1) can be installed with `apt-get install expect`. | ||
// | ||
// "-p" is needed because we need unbuffer to read from stdin, and from [1]: | ||
// "Normally, unbuffer does not read from stdin. This simplifies use of unbuffer in some situations. | ||
// To use unbuffer in a pipeline, use the -p flag." | ||
// | ||
// [1] https://linux.die.net/man/1/unbuffer | ||
base.CmdWithHelper([]string{"unbuffer", "-p"}, "run", "-it", "--name", containerName, testutil.CommonImage). | ||
CmdOption(opts...).AssertOK() | ||
container := base.InspectContainer(containerName) | ||
assert.Equal(base.T, container.State.Running, false) | ||
|
||
opts = []func(*testutil.Cmd){ | ||
testutil.WithStdin(testutil.NewDelayOnceReader(bytes.NewReader([]byte{1, 2}))), // https://www.physics.udel.edu/~watson/scen103/ascii.html | ||
} | ||
base.CmdWithHelper([]string{"unbuffer", "-p"}, "start", "-a", "--detach-keys=ctrl-a,ctrl-b", containerName). | ||
CmdOption(opts...).AssertOutContains("read detach keys") | ||
container = base.InspectContainer(containerName) | ||
assert.Equal(base.T, container.State.Running, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.