Files
podman/pkg/domain/utils/utils_test.go
Zachary Hanham feb46513f0 scp: add option types
Prior to this commit, many scp functions existed without option structs, which would make extending functionality (adding new options) impossible without breaking changes, or without adding redundant wrapper functions.

This commit adds in new option types for various scp related functions, and changes those functions' signatures to use the new options.

This commit also modifies the `ImageEngine.Scp()` function's interface to use the new opts.

The commit also renames the existing `ImageScpOptions` entity type to `ScpTransferImageOptions`. This is because the previous `ImageScpOptions` was inaccurate, as it is not the actual options for `ImageEngine.Scp()`. `ImageEngine.Scp()` should instead receive `ImageScpOptions`.

This commit should not change any behavior, however it will break the existing functions' signatures.

Signed-off-by: Zachary Hanham <z.hanham00@gmail.com>
2024-10-14 21:46:55 -04:00

116 lines
2.7 KiB
Go

package utils
import (
"net/url"
"sort"
"testing"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/stretchr/testify/assert"
)
func TestToLibpodFilters(t *testing.T) {
good := url.Values{}
good.Set("apple", "red")
good.Set("banana", "yellow")
good.Set("pear", "")
goodResult := []string{"apple=red", "banana=yellow", "pear="}
sort.Strings(goodResult)
empty := url.Values{}
type args struct {
f url.Values
}
tests := []struct {
name string
args args
wantFilters []string
}{
{
name: "GoodURLValue",
args: args{
f: good,
},
wantFilters: goodResult,
},
{
name: "Empty",
args: args{
f: empty,
},
wantFilters: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.ElementsMatchf(t, ToLibpodFilters(tt.args.f), tt.wantFilters, "ToLibpodFilters() = %v, want %v", ToLibpodFilters(tt.args.f), tt.wantFilters)
})
}
}
func TestToURLValues(t *testing.T) {
good := url.Values{}
good.Set("apple", "red")
good.Set("banana", "yellow")
good.Set("pear", "")
goodResult := []string{"apple=red", "banana=yellow", "pear="}
type args struct {
f []string
}
tests := []struct {
name string
args args
wantFilters url.Values
}{
{
name: "Good",
args: args{goodResult},
wantFilters: good,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualValuesf(t, ToURLValues(tt.args.f), tt.wantFilters, "ToURLValues() = %v, want %v", ToURLValues(tt.args.f), tt.wantFilters)
})
}
}
func TestParseSCPArgs(t *testing.T) {
args := []string{"alpine", "root@localhost::"}
var source *entities.ScpTransferImageOptions
var dest *entities.ScpTransferImageOptions
var err error
source, _, err = ParseImageSCPArg(args[0])
assert.Nil(t, err)
assert.Equal(t, source.Image, "alpine")
dest, _, err = ParseImageSCPArg(args[1])
assert.Nil(t, err)
assert.Equal(t, dest.Image, "")
assert.Equal(t, dest.User, "root")
args = []string{"root@localhost::alpine"}
source, _, err = ParseImageSCPArg(args[0])
assert.Nil(t, err)
assert.Equal(t, source.User, "root")
assert.Equal(t, source.Image, "alpine")
args = []string{"charliedoern@192.168.68.126::alpine", "foobar@192.168.68.126::"}
source, _, err = ParseImageSCPArg(args[0])
assert.Nil(t, err)
assert.True(t, source.Remote)
assert.Equal(t, source.Image, "alpine")
dest, _, err = ParseImageSCPArg(args[1])
assert.Nil(t, err)
assert.True(t, dest.Remote)
assert.Equal(t, dest.Image, "")
args = []string{"charliedoern@192.168.68.126::alpine"}
source, _, err = ParseImageSCPArg(args[0])
assert.Nil(t, err)
assert.True(t, source.Remote)
assert.Equal(t, source.Image, "alpine")
}