Fix updating connection when SSH port conflict happens

Signed-off-by: Gabriel Parreiras <gabriel.parreiras@shopify.com>
This commit is contained in:
Gabriel Parreiras 2024-05-03 12:56:17 +01:00
parent 6ec2c0b43b
commit 277312d282
No known key found for this signature in database
GPG Key ID: 15DF7B5581F513AC
3 changed files with 95 additions and 1 deletions

View File

@ -64,7 +64,7 @@ func UpdateConnectionPairPort(name string, port, uid int, remoteUsername string,
URI: con.uri.String(), URI: con.uri.String(),
Identity: identityPath, Identity: identityPath,
} }
cfg.Connection.Connections[name] = dst cfg.Connection.Connections[con.name] = dst
} }
return nil return nil

View File

@ -0,0 +1,23 @@
package e2e_test
type listSystemConnection struct {
/*
--format string Custom Go template for printing connections
*/
format string
}
func (l listSystemConnection) buildCmd(m *machineTestBuilder) []string {
cmd := []string{"system", "connection", "list"}
if len(l.format) > 0 {
cmd = append(cmd, "--format", l.format)
}
return cmd
}
func (l *listSystemConnection) withFormat(format string) *listSystemConnection {
l.format = format
return l
}

View File

@ -1,6 +1,9 @@
package e2e_test package e2e_test
import ( import (
"fmt"
"net"
"net/url"
"sync" "sync"
"time" "time"
@ -88,6 +91,55 @@ var _ = Describe("podman machine start", func() {
Expect(startSession.errorToString()).To(ContainSubstring("VM already running or starting")) Expect(startSession.errorToString()).To(ContainSubstring("VM already running or starting"))
}) })
It("start machine with conflict on SSH port", func() {
i := new(initMachine)
session, err := mb.setCmd(i.withImage(mb.imagePath)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0))
inspect := new(inspectMachine)
inspectSession, err := mb.setCmd(inspect.withFormat("{{.SSHConfig.Port}}")).run()
Expect(err).ToNot(HaveOccurred())
Expect(inspectSession).To(Exit(0))
inspectPort := inspectSession.outputToString()
connections := new(listSystemConnection)
connectionsSession, err := mb.setCmd(connections.withFormat("{{.URI}}")).run()
Expect(err).ToNot(HaveOccurred())
Expect(connectionsSession).To(Exit(0))
connectionURLs := connectionsSession.outputToStringSlice()
connectionPorts, err := mapToPort(connectionURLs)
Expect(err).ToNot(HaveOccurred())
Expect(connectionPorts).To(HaveEach(inspectPort))
// start a listener on the ssh port
listener, err := net.Listen("tcp", "127.0.0.1:"+inspectPort)
Expect(err).ToNot(HaveOccurred())
defer listener.Close()
s := new(startMachine)
startSession, err := mb.setCmd(s).run()
Expect(err).ToNot(HaveOccurred())
Expect(startSession).To(Exit(0))
Expect(startSession.errorToString()).To(ContainSubstring("detected port conflict on machine ssh port"))
inspect2 := new(inspectMachine)
inspectSession2, err := mb.setCmd(inspect2.withFormat("{{.SSHConfig.Port}}")).run()
Expect(err).ToNot(HaveOccurred())
Expect(inspectSession2).To(Exit(0))
inspectPort2 := inspectSession2.outputToString()
Expect(inspectPort2).To(Not(Equal(inspectPort)))
connections2 := new(listSystemConnection)
connectionsSession2, err := mb.setCmd(connections2.withFormat("{{.URI}}")).run()
Expect(err).ToNot(HaveOccurred())
Expect(connectionsSession2).To(Exit(0))
connectionURLs2 := connectionsSession2.outputToStringSlice()
connectionPorts2, err := mapToPort(connectionURLs2)
Expect(err).ToNot(HaveOccurred())
Expect(connectionPorts2).To(HaveEach(inspectPort2))
})
It("start only starts specified machine", func() { It("start only starts specified machine", func() {
i := initMachine{} i := initMachine{}
startme := randomString() startme := randomString()
@ -175,3 +227,22 @@ var _ = Describe("podman machine start", func() {
} }
}) })
}) })
func mapToPort(uris []string) ([]string, error) {
ports := []string{}
for _, uri := range uris {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
port := u.Port()
if port == "" {
return nil, fmt.Errorf("no port in URI: %s", uri)
}
ports = append(ports, port)
}
return ports, nil
}