Skip to content

Commit

Permalink
Merge pull request #22587 from gmpify/main
Browse files Browse the repository at this point in the history
Fix updating connection when SSH port conflict happens
  • Loading branch information
openshift-merge-bot[bot] authored May 14, 2024
2 parents ef66190 + 277312d commit 87bd775
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/machine/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func UpdateConnectionPairPort(name string, port, uid int, remoteUsername string,
URI: con.uri.String(),
Identity: identityPath,
}
cfg.Connection.Connections[name] = dst
cfg.Connection.Connections[con.name] = dst
}

return nil
Expand Down
23 changes: 23 additions & 0 deletions pkg/machine/e2e/config_system_connection_list_test.go
Original file line number Diff line number Diff line change
@@ -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
}
71 changes: 71 additions & 0 deletions pkg/machine/e2e/start_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package e2e_test

import (
"fmt"
"net"
"net/url"
"sync"
"time"

Expand Down Expand Up @@ -88,6 +91,55 @@ var _ = Describe("podman machine start", func() {
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() {
i := initMachine{}
startme := randomString()
Expand Down Expand Up @@ -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
}

1 comment on commit 87bd775

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

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

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.