fix: detect storage:exec TTY with isatty, not ModeCharDevice

/dev/null is a character device but not a terminal. Treating it as a TTY
made docker run -it fail under docker exec without -t, which broke the
Dokku-in-Docker storage bats job in CI.
This commit is contained in:
Jose Diaz-Gonzalez
2026-10-14 17:27:22 -04:00
parent f768eb6d07
commit 3dafdcb3f9
3 changed files with 55 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/mattn/go-isatty"
) )
// CommandCreateInput captures the flags accepted by storage:create. // CommandCreateInput captures the flags accepted by storage:create.
@@ -422,14 +423,16 @@ func CommandExec(input CommandExecInput) error {
// stdinModes inspects os.Stdin to decide whether docker / the k8s exec // stdinModes inspects os.Stdin to decide whether docker / the k8s exec
// SDK should request an interactive session and a TTY. // SDK should request an interactive session and a TTY.
//
// TTY detection uses isatty rather than ModeCharDevice: /dev/null is a
// character device but not a terminal, and treating it as a TTY makes
// docker run -it fail with "cannot attach stdin to a TTY-enabled
// container because stdin is not a terminal" (e.g. docker exec without
// -t, as in CI).
func stdinModes() (interactive bool, tty bool) { func stdinModes() (interactive bool, tty bool) {
tty = isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
fi, err := os.Stdin.Stat() fi, err := os.Stdin.Stat()
if err != nil || fi == nil { hasStdinData := err == nil && fi != nil && fi.Mode()&os.ModeNamedPipe != 0
return false, false
}
mode := fi.Mode()
tty = mode&os.ModeCharDevice != 0
hasStdinData := mode&os.ModeNamedPipe != 0
interactive = tty || hasStdinData interactive = tty || hasStdinData
return interactive, tty return interactive, tty
} }

View File

@@ -1,6 +1,7 @@
package storage package storage
import ( import (
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -218,3 +219,47 @@ func TestCommandSetPersistsAndValidates(t *testing.T) {
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("does not exist")) Expect(err.Error()).To(ContainSubstring("does not exist"))
} }
// TestStdinModesDevNullIsNotTTY guards the CI / docker-exec path: stdin is
// often /dev/null, which is a character device but not a terminal. ModeCharDevice
// alone would request docker run -it and fail attach.
func TestStdinModesDevNullIsNotTTY(t *testing.T) {
devNull, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer devNull.Close()
old := os.Stdin
os.Stdin = devNull
t.Cleanup(func() { os.Stdin = old })
interactive, tty := stdinModes()
if tty {
t.Fatalf("expected tty=false for /dev/null, got tty=true interactive=%v", interactive)
}
if interactive {
t.Fatalf("expected interactive=false for /dev/null, got interactive=true")
}
}
func TestStdinModesNamedPipeIsInteractiveWithoutTTY(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer r.Close()
defer w.Close()
old := os.Stdin
os.Stdin = r
t.Cleanup(func() { os.Stdin = old })
interactive, tty := stdinModes()
if tty {
t.Fatalf("expected tty=false for a pipe, got tty=true")
}
if !interactive {
t.Fatalf("expected interactive=true for a named pipe")
}
}

View File

@@ -5,6 +5,7 @@ go 1.26.2
require ( require (
github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000
github.com/dokku/dokku/plugins/docker-options v0.0.0-00010101000000-000000000000 github.com/dokku/dokku/plugins/docker-options v0.0.0-00010101000000-000000000000
github.com/mattn/go-isatty v0.0.24
github.com/onsi/gomega v1.44.0 github.com/onsi/gomega v1.44.0
github.com/spf13/pflag v1.0.10 github.com/spf13/pflag v1.0.10
) )
@@ -17,7 +18,6 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/kr/fs v0.1.0 // indirect github.com/kr/fs v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/melbahja/goph v1.5.2 // indirect github.com/melbahja/goph v1.5.2 // indirect
github.com/otiai10/copy v1.14.1 // indirect github.com/otiai10/copy v1.14.1 // indirect
github.com/otiai10/mint v1.6.3 // indirect github.com/otiai10/mint v1.6.3 // indirect