Skip to content

Commit 3d566d8

Browse files
committed
Ignore prompt if stdin not a tty on machine start
When starting a machine and the user has not explicitly passed -u=true|false AND stdin is a not a tty, we should not prompt to update connections. Fixes: #27556 Signed-off-by: Brent Baude <[email protected]>
1 parent 5c48d02 commit 3d566d8

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

docs/source/markdown/options/update-connection.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
When used in conjunction with `podman machine init --now` or `podman machine start`, this option sets the
88
associated machine system connection as the default. When using this option, a `-u` or `-update-connection` will
99
set the value to true. To set this value to false, meaning no change and no prompting,
10-
use `--update-connection=false`.
10+
use `--update-connection=false`. If the value is unset and stdin is not a tty, no prompt or update
11+
shall occur.
1112

1213
If the value is set to true, the machine connection will be set as the system default.
1314
If the value is set to false, the system default will be unchanged.

pkg/machine/e2e/start_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package e2e_test
22

33
import (
4+
"bytes"
45
"fmt"
56
"net"
67
"net/url"
@@ -135,22 +136,28 @@ var _ = Describe("podman machine start", func() {
135136
})
136137

137138
It("start only starts specified machine", func() {
138-
i := initMachine{}
139-
startme := randomString()
140-
session, err := mb.setName(startme).setCmd(i.withImage(mb.imagePath)).run()
141-
Expect(err).ToNot(HaveOccurred())
142-
Expect(session).To(Exit(0))
143-
144139
j := initMachine{}
145140
dontstartme := randomString()
146141
session2, err := mb.setName(dontstartme).setCmd(j.withFakeImage(mb)).run()
147142
Expect(err).ToNot(HaveOccurred())
148143
Expect(session2).To(Exit(0))
149144

145+
i := initMachine{}
146+
startme := randomString()
147+
session, err := mb.setName(startme).setCmd(i.withImage(mb.imagePath)).run()
148+
Expect(err).ToNot(HaveOccurred())
149+
Expect(session).To(Exit(0))
150+
150151
s := &startMachine{}
151-
session3, err := mb.setName(startme).setCmd(s).setTimeout(time.Minute * 10).run()
152+
// Provide a buffer as stdin to simulate non-tty input (e.g., piped or redirected stdin)
153+
// When stdin is not a tty, the command should not prompt for connection updates
154+
stdinBuf := bytes.NewBufferString("n\n")
155+
session3, err := mb.setName(startme).setCmd(s).setTimeout(time.Minute * 10).setStdin(stdinBuf).run()
152156
Expect(err).ToNot(HaveOccurred())
153157
Expect(session3).Should(Exit(0))
158+
// Verify that the prompt message did not appear (no prompting when stdin is not a tty)
159+
combinedOutput := session3.outputToString() + session3.errorToString()
160+
Expect(combinedOutput).ToNot(ContainSubstring("Set the default Podman connection to this machine"), "should not prompt when stdin is not a tty")
154161

155162
inspect := new(inspectMachine)
156163
inspect = inspect.withFormat("{{.State}}")

pkg/machine/shim/host.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/hashicorp/go-multierror"
3030
"github.com/sirupsen/logrus"
3131
"go.podman.io/common/pkg/config"
32+
"golang.org/x/term"
3233
)
3334

3435
// List is done at the host level to allow for a *possible* future where
@@ -501,8 +502,11 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, opts machine.St
501502
// Do not do anything with the system connection if its already
502503
// the default system connection.
503504
if !conn.Default {
504-
// Prompt for system connection update
505-
if updateSystemConn == nil {
505+
if updateSystemConn != nil {
506+
updateDefaultConnection = *updateSystemConn
507+
} else if term.IsTerminal(int(os.Stdin.Fd())) {
508+
// Prompt for system connection update if there is a terminal
509+
// on stdin
506510
response, err := promptUpdateSystemConn()
507511
if err != nil {
508512
return err
@@ -517,8 +521,6 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, opts machine.St
517521
fmt.Println("Default system connection will remain unchanged")
518522
}
519523
updateDefaultConnection = response
520-
} else {
521-
updateDefaultConnection = *updateSystemConn
522524
}
523525
}
524526

0 commit comments

Comments
 (0)