diff --git a/pkg/machine/config_test.go b/pkg/machine/config_test.go index 869804ddcb..3450a111f1 100644 --- a/pkg/machine/config_test.go +++ b/pkg/machine/config_test.go @@ -3,79 +3,12 @@ package machine import ( - "net" - "net/url" "path/filepath" - "reflect" "testing" - "github.com/containers/podman/v5/pkg/machine/connection" "github.com/stretchr/testify/assert" ) -func TestRemoteConnectionType_MakeSSHURL(t *testing.T) { - var ( - host = "foobar" - path = "/path/to/socket" - rc = "ssh" - username = "core" - ) - type args struct { - host string - path string - port string - userName string - } - tests := []struct { - name string - rc connection.RemoteConnectionType - args args - want url.URL - }{ - { - name: "Good no port", - rc: "ssh", - args: args{ - host: host, - path: path, - port: "", - userName: username, - }, - want: url.URL{ - Scheme: rc, - User: url.User(username), - Host: host, - Path: path, - ForceQuery: false, - }, - }, - { - name: "Good with port", - rc: "ssh", - args: args{ - host: host, - path: path, - port: "222", - userName: username, - }, - want: url.URL{ - Scheme: rc, - User: url.User(username), - Host: net.JoinHostPort(host, "222"), - Path: path, - ForceQuery: false, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := tt.rc.MakeSSHURL(tt.args.host, tt.args.path, tt.args.port, tt.args.userName); !reflect.DeepEqual(got, tt.want) { //nolint: scopelint - t.Errorf("MakeSSHURL() = %v, want %v", got, tt.want) //nolint: scopelint - } - }) - } -} - func TestGetSSHIdentityPath(t *testing.T) { name := "p-test" datadir, err := GetGlobalDataDir() diff --git a/pkg/machine/connection/add.go b/pkg/machine/connection/add.go index 5a5e150e7a..fe0ed7f3fa 100644 --- a/pkg/machine/connection/add.go +++ b/pkg/machine/connection/add.go @@ -15,10 +15,10 @@ func AddSSHConnectionsToPodmanSocket(uid, port int, identityPath, name, remoteUs fmt.Println("An ignition path was provided. No SSH connection was added to Podman") return nil } - uri := SSHRemoteConnection.MakeSSHURL(LocalhostIP, fmt.Sprintf("/run/user/%d/podman/podman.sock", uid), strconv.Itoa(port), remoteUsername) - uriRoot := SSHRemoteConnection.MakeSSHURL(LocalhostIP, "/run/podman/podman.sock", strconv.Itoa(port), "root") + uri := makeSSHURL(LocalhostIP, fmt.Sprintf("/run/user/%d/podman/podman.sock", uid), strconv.Itoa(port), remoteUsername) + uriRoot := makeSSHURL(LocalhostIP, "/run/podman/podman.sock", strconv.Itoa(port), "root") - uris := []url.URL{uri, uriRoot} + uris := []*url.URL{uri, uriRoot} names := []string{name, name + "-root"} // The first connection defined when connections is empty will become the default @@ -28,7 +28,7 @@ func AddSSHConnectionsToPodmanSocket(uid, port int, identityPath, name, remoteUs } for i := 0; i < 2; i++ { - if err := AddConnection(&uris[i], names[i], identityPath, opts.IsDefault && i == 0); err != nil { + if err := AddConnection(uris[i], names[i], identityPath, opts.IsDefault && i == 0); err != nil { return err } } diff --git a/pkg/machine/connection/connection.go b/pkg/machine/connection/connection.go index 11f2ba7b18..94dc26569c 100644 --- a/pkg/machine/connection/connection.go +++ b/pkg/machine/connection/connection.go @@ -15,7 +15,7 @@ import ( const LocalhostIP = "127.0.0.1" -func AddConnection(uri fmt.Stringer, name, identity string, isDefault bool) error { +func AddConnection(uri *url.URL, name, identity string, isDefault bool) error { if len(identity) < 1 { return errors.New("identity must be defined") } @@ -108,27 +108,19 @@ func RemoveFilesAndConnections(files []string, names ...string) { } } -type RemoteConnectionType string - -var SSHRemoteConnection RemoteConnectionType = "ssh" - -// MakeSSHURL -func (rc RemoteConnectionType) MakeSSHURL(host, path, port, userName string) url.URL { - // TODO Should this function have input verification? - userInfo := url.User(userName) - uri := url.URL{ - Scheme: "ssh", - Opaque: "", - User: userInfo, - Host: host, - Path: path, - RawPath: "", - ForceQuery: false, - RawQuery: "", - Fragment: "", - } +// makeSSHURL creates a URL from the given input +func makeSSHURL(host, path, port, userName string) *url.URL { + var hostname string if len(port) > 0 { - uri.Host = net.JoinHostPort(uri.Hostname(), port) + hostname = net.JoinHostPort(host, port) + } else { + hostname = host + } + userInfo := url.User(userName) + return &url.URL{ + Scheme: "ssh", + User: userInfo, + Host: hostname, + Path: path, } - return uri } diff --git a/pkg/machine/connection/connection_test.go b/pkg/machine/connection/connection_test.go new file mode 100644 index 0000000000..3cdabf485f --- /dev/null +++ b/pkg/machine/connection/connection_test.go @@ -0,0 +1,68 @@ +//go:build amd64 || arm64 + +package connection + +import ( + "net" + "net/url" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_makeSSHURL(t *testing.T) { + var ( + host = "foobar" + path = "/path/to/socket" + rc = "ssh" + username = "core" + ) + type args struct { + host string + path string + port string + userName string + } + tests := []struct { + name string + args args + want *url.URL + }{ + { + name: "Good no port", + args: args{ + host: host, + path: path, + port: "", + userName: username, + }, + want: &url.URL{ + Scheme: rc, + User: url.User(username), + Host: host, + Path: path, + }, + }, + { + name: "Good with port", + args: args{ + host: host, + path: path, + port: "222", + userName: username, + }, + want: &url.URL{ + Scheme: rc, + User: url.User(username), + Host: net.JoinHostPort(host, "222"), + Path: path, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := makeSSHURL(tt.args.host, tt.args.path, tt.args.port, tt.args.userName) + assert.Equal(t, tt.want, got, "URL matches") + }) + } +}