Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/source/markdown/options/update-connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
When used in conjunction with `podman machine init --now` or `podman machine start`, this option sets the
associated machine system connection as the default. When using this option, a `-u` or `-update-connection` will
set the value to true. To set this value to false, meaning no change and no prompting,
use `--update-connection=false`.
use `--update-connection=false`. If the value is unset and stdin is not a tty, no prompt or update
shall occur.

If the value is set to true, the machine connection will be set as the system default.
If the value is set to false, the system default will be unchanged.
Expand Down
21 changes: 14 additions & 7 deletions pkg/machine/e2e/start_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2e_test

import (
"bytes"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -135,22 +136,28 @@ var _ = Describe("podman machine start", func() {
})

It("start only starts specified machine", func() {
i := initMachine{}
startme := randomString()
session, err := mb.setName(startme).setCmd(i.withImage(mb.imagePath)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0))

j := initMachine{}
dontstartme := randomString()
session2, err := mb.setName(dontstartme).setCmd(j.withFakeImage(mb)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session2).To(Exit(0))

i := initMachine{}
startme := randomString()
session, err := mb.setName(startme).setCmd(i.withImage(mb.imagePath)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0))

s := &startMachine{}
session3, err := mb.setName(startme).setCmd(s).setTimeout(time.Minute * 10).run()
// Provide a buffer as stdin to simulate non-tty input (e.g., piped or redirected stdin)
// When stdin is not a tty, the command should not prompt for connection updates
stdinBuf := bytes.NewBufferString("n\n")
session3, err := mb.setName(startme).setCmd(s).setTimeout(time.Minute * 10).setStdin(stdinBuf).run()
Expect(err).ToNot(HaveOccurred())
Expect(session3).Should(Exit(0))
// Verify that the prompt message did not appear (no prompting when stdin is not a tty)
combinedOutput := session3.outputToString() + session3.errorToString()
Expect(combinedOutput).ToNot(ContainSubstring("Set the default Podman connection to this machine"), "should not prompt when stdin is not a tty")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with negative matches like this is this quickly turns into a NOP if someone tweaks the message in the code. But yeah I guess you are right we cannot check the buffer because the go std code creates a pipe and writes the buffer into it since the writer never can know if it is read or not.

To fix this we would need to manage the pipe ourselves so we can drain the reader side and see if the input is still in there. Anyhow this getting more complicated and for now this test seems totally fine.


inspect := new(inspectMachine)
inspect = inspect.withFormat("{{.State}}")
Expand Down
10 changes: 6 additions & 4 deletions pkg/machine/shim/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/sirupsen/logrus"
"go.podman.io/common/pkg/config"
"golang.org/x/term"
)

// List is done at the host level to allow for a *possible* future where
Expand Down Expand Up @@ -501,8 +502,11 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, opts machine.St
// Do not do anything with the system connection if its already
// the default system connection.
if !conn.Default {
// Prompt for system connection update
if updateSystemConn == nil {
if updateSystemConn != nil {
updateDefaultConnection = *updateSystemConn
} else if term.IsTerminal(int(os.Stdin.Fd())) {
// Prompt for system connection update if there is a terminal
// on stdin
response, err := promptUpdateSystemConn()
if err != nil {
return err
Expand All @@ -517,8 +521,6 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, opts machine.St
fmt.Println("Default system connection will remain unchanged")
}
updateDefaultConnection = response
} else {
updateDefaultConnection = *updateSystemConn
}
}

Expand Down