Skip to content

Commit aeef5be

Browse files
committed
Fix Windows path check in 'LogURIGenerator'
Signed-off-by: Christine Murimi <[email protected]>
1 parent 7542c87 commit aeef5be

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

cmd/nerdctl/container/container_run_linux_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,24 @@ func TestRunWithDetachKeys(t *testing.T) {
498498
container := base.InspectContainer(containerName)
499499
assert.Equal(base.T, container.State.Running, true)
500500
}
501+
502+
func TestRunWithTtyAndDetached(t *testing.T) {
503+
base := testutil.NewBase(t)
504+
imageName := testutil.CommonImage
505+
withoutTtyContainerName := "without-terminal-" + testutil.Identifier(t)
506+
withTtyContainerName := "with-terminal-" + testutil.Identifier(t)
507+
508+
// without -t, fail
509+
base.Cmd("run", "-d", "--name", withoutTtyContainerName, imageName, "stty").AssertOK()
510+
defer base.Cmd("container", "rm", "-f", withoutTtyContainerName).AssertOK()
511+
base.Cmd("logs", withoutTtyContainerName).AssertCombinedOutContains("stty: standard input: Not a tty")
512+
withoutTtyContainer := base.InspectContainer(withoutTtyContainerName)
513+
assert.Equal(base.T, 1, withoutTtyContainer.State.ExitCode)
514+
515+
// with -t, success
516+
base.Cmd("run", "-d", "-t", "--name", withTtyContainerName, imageName, "stty").AssertOK()
517+
defer base.Cmd("container", "rm", "-f", withTtyContainerName).AssertOK()
518+
base.Cmd("logs", withTtyContainerName).AssertCombinedOutContains("speed 38400 baud; line = 0;")
519+
withTtyContainer := base.InspectContainer(withTtyContainerName)
520+
assert.Equal(base.T, 0, withTtyContainer.State.ExitCode)
521+
}

cmd/nerdctl/container/container_run_test.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -459,30 +459,6 @@ COPY --from=builder /go/src/logger/logger /
459459
assert.Check(t, strings.Contains(log, "bar"))
460460
}
461461

462-
func TestRunWithTtyAndDetached(t *testing.T) {
463-
if runtime.GOOS == "windows" {
464-
t.Skip("json-file log driver is not yet implemented on Windows")
465-
}
466-
base := testutil.NewBase(t)
467-
imageName := testutil.CommonImage
468-
withoutTtyContainerName := "without-terminal-" + testutil.Identifier(t)
469-
withTtyContainerName := "with-terminal-" + testutil.Identifier(t)
470-
471-
// without -t, fail
472-
base.Cmd("run", "-d", "--name", withoutTtyContainerName, imageName, "stty").AssertOK()
473-
defer base.Cmd("container", "rm", "-f", withoutTtyContainerName).AssertOK()
474-
base.Cmd("logs", withoutTtyContainerName).AssertCombinedOutContains("stty: standard input: Not a tty")
475-
withoutTtyContainer := base.InspectContainer(withoutTtyContainerName)
476-
assert.Equal(base.T, 1, withoutTtyContainer.State.ExitCode)
477-
478-
// with -t, success
479-
base.Cmd("run", "-d", "-t", "--name", withTtyContainerName, imageName, "stty").AssertOK()
480-
defer base.Cmd("container", "rm", "-f", withTtyContainerName).AssertOK()
481-
base.Cmd("logs", withTtyContainerName).AssertCombinedOutContains("speed 38400 baud; line = 0;")
482-
withTtyContainer := base.InspectContainer(withTtyContainerName)
483-
assert.Equal(base.T, 0, withTtyContainer.State.ExitCode)
484-
}
485-
486462
// history: There was a bug that the --add-host items disappear when the another container created.
487463
// This case ensures that it's doesn't happen.
488464
// (https://github.com/containerd/nerdctl/issues/2560)

cmd/nerdctl/container/container_run_windows_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,18 @@ func TestRunProcessContainerWithDevice(t *testing.T) {
117117
"cmd", "/S", "/C", "dir C:\\Windows\\System32\\HostDriverStore",
118118
).AssertOutContains("FileRepository")
119119
}
120+
121+
func TestRunWithTtyAndDetached(t *testing.T) {
122+
base := testutil.NewBase(t)
123+
imageName := testutil.CommonImage
124+
withTtyContainerName := "with-terminal-" + testutil.Identifier(t)
125+
126+
// with -t, success, the container should run with tty support.
127+
base.Cmd("run", "-d", "-t", "--name", withTtyContainerName, imageName, "cmd", "/c", "echo", "Hello, World with TTY!").AssertOK()
128+
defer base.Cmd("container", "rm", "-f", withTtyContainerName).AssertOK()
129+
130+
// Check logs for successful command execution (with TTY specific behavior if any)
131+
base.Cmd("logs", withTtyContainerName).AssertOutContains("Hello, World with TTY!")
132+
withTtyContainer := base.InspectContainer(withTtyContainerName)
133+
assert.Equal(base.T, 0, withTtyContainer.State.ExitCode)
134+
}

pkg/taskutil/taskutil.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
9696
if len(args) != 2 {
9797
return nil, errors.New("parse logging path error")
9898
}
99-
ioCreator = cio.TerminalBinaryIO(u.Path, map[string]string{
99+
parsedPath := u.Path
100+
// For Windows, remove the leading slash
101+
if (runtime.GOOS == "windows") && (strings.HasPrefix(parsedPath, "/")) {
102+
parsedPath = strings.TrimLeft(parsedPath, "/")
103+
}
104+
ioCreator = cio.TerminalBinaryIO(parsedPath, map[string]string{
100105
args[0]: args[1],
101106
})
102107
} else if flagT && !flagD {
103-
104108
if con == nil {
105109
return nil, errors.New("got nil con with flagT=true")
106110
}

0 commit comments

Comments
 (0)