mirror of
https://github.com/containers/podman.git
synced 2025-11-01 22:32:50 +08:00
pkg/machine: cleanup MakeSSHURL
Remove unnecessary type redirection and just make it a normal function. Also unexport it and move the test as it does not need to be public and remove the default value assignments from the struct. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
@ -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()
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
68
pkg/machine/connection/connection_test.go
Normal file
68
pkg/machine/connection/connection_test.go
Normal file
@ -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")
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user