Fix regression in podman machine ssh

While doing the provider obfuscation, I injected a regression where
podman ssh machine failed.  The regression was added in
0f22c1c772.  I have fixed the regression
and added a test to prevent future occurance.

Fixes: #27491

Signed-off-by: Brent Baude <bbaude@redhat.com>
This commit is contained in:
Brent Baude
2025-11-10 14:47:54 -06:00
parent 1afe2ce6d3
commit 57052a8cc7
3 changed files with 32 additions and 18 deletions

View File

@@ -190,7 +190,7 @@ func toHumanFormat(vms []*machine.ListResponse, defaultCon *config.Connection) [
isDefault := false
// check port, in case we somehow have machines with the same name in different providers
if defaultCon != nil {
isDefault = vm.Name == defaultCon.Name && strings.Contains(defaultCon.URI, strconv.Itoa((vm.Port)))
isDefault = vm.Name == defaultCon.Name && strings.Contains(defaultCon.URI, strconv.Itoa(vm.Port))
}
if isDefault {
response.Name = vm.Name + "*"

View File

@@ -60,15 +60,16 @@ func ssh(_ *cobra.Command, args []string) error {
// it implies podman cannot read its machine files, which is bad
mc, vmProvider, err = shim.VMExists(args[0])
if err != nil {
var vmNotExistsErr *define.ErrVMDoesNotExist
if !errors.As(err, &vmNotExistsErr) {
return err
}
if errors.Is(err, &define.ErrVMDoesNotExist{}) {
vmName = args[0]
} else {
sshOpts.Args = append(sshOpts.Args, args[0])
}
} else {
vmName = args[0]
exists = true
}
}
// If len is greater than 1, it means we might have been
// given a vmname and args or just args

View File

@@ -33,14 +33,27 @@ var _ = Describe("podman machine ssh", func() {
It("ssh to running machine and check os-type", func() {
wsl := testProvider.VMType() == define.WSLVirt
name := randomString()
// setting this name instead of randomized because we want to test when the
// machine name is and is not provided. That's what the loop below is for
name := "podman-machine-default"
i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath).withNow()).run()
session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath).withNow().withUpdateConnection(ptrBool(true))).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0))
// pass 1
ssh := &sshMachine{}
sshSession, err := mb.setName(name).setCmd(ssh.withSSHCommand([]string{"cat", "/etc/os-release"})).run()
// pass 2
bm := basicMachine{}
var mcs []machineCommand
// check with the machine name
mcs = append(mcs, ssh.withSSHCommand([]string{"cat", "/etc/os-release"}))
// check without the machine name
mcs = append(mcs, bm.withPodmanCommand([]string{"machine", "ssh", "cat", "/etc/os-release"}))
for _, mc := range mcs {
sshSession, err := mb.setCmd(mc).run()
Expect(err).ToNot(HaveOccurred())
Expect(sshSession).To(Exit(0))
@@ -49,9 +62,9 @@ var _ = Describe("podman machine ssh", func() {
} else {
Expect(sshSession.outputToString()).To(ContainSubstring("Fedora CoreOS"))
}
}
// keep exit code
sshSession, err = mb.setName(name).setCmd(ssh.withSSHCommand([]string{"false"})).run()
sshSession, err := mb.setName(name).setCmd(ssh.withSSHCommand([]string{"false"})).run()
Expect(err).ToNot(HaveOccurred())
Expect(sshSession).To(Exit(1))
Expect(sshSession.outputToString()).To(Equal(""))