mirror of
https://github.com/containers/podman.git
synced 2025-06-02 02:26:52 +08:00

the way machine was written was very adjunct and as such is in dire need of refactoring to better structures and structure methods where appropriate. the weekest part is specifically around all the files that machine requires and how some are just dynamically built on the fly. this pr defines a new machinefile type which allows us to work with the file and also takes into account the use of symlinks which are going to be needed on macos due to its relatively short file length restriction. also, added unit tests for new methods as well as anywhere else I saw a need. Signed-off-by: Brent Baude <bbaude@redhat.com>
104 lines
2.0 KiB
Go
104 lines
2.0 KiB
Go
package qemu
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestMachineFile_GetPath(t *testing.T) {
|
|
path := "/var/tmp/podman/my.sock"
|
|
sym := "/tmp/podman/my.sock"
|
|
type fields struct {
|
|
Path string
|
|
Symlink *string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
want string
|
|
}{
|
|
{
|
|
name: "Original path",
|
|
fields: fields{path, nil},
|
|
want: path,
|
|
},
|
|
{
|
|
name: "Symlink over path",
|
|
fields: fields{
|
|
Path: path,
|
|
Symlink: &sym,
|
|
},
|
|
want: sym,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
m := &MachineFile{
|
|
Path: tt.fields.Path, //nolint: scopelint
|
|
Symlink: tt.fields.Symlink, //nolint: scopelint
|
|
}
|
|
if got := m.GetPath(); got != tt.want { //nolint: scopelint
|
|
t.Errorf("GetPath() = %v, want %v", got, tt.want) //nolint: scopelint
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewMachineFile(t *testing.T) {
|
|
p := "/var/tmp/podman/my.sock"
|
|
sym := "/tmp/podman/my.sock"
|
|
empty := ""
|
|
|
|
m := MachineFile{
|
|
Path: p,
|
|
Symlink: nil,
|
|
}
|
|
type args struct {
|
|
path string
|
|
symlink *string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want *MachineFile
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Good",
|
|
args: args{path: p},
|
|
want: &m,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Good with Symlink",
|
|
args: args{p, &sym},
|
|
want: &MachineFile{p, &sym},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Bad path name",
|
|
args: args{empty, nil},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Bad symlink name",
|
|
args: args{p, &empty},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := NewMachineFile(tt.args.path, tt.args.symlink) //nolint: scopelint
|
|
if (err != nil) != tt.wantErr { //nolint: scopelint
|
|
t.Errorf("NewMachineFile() error = %v, wantErr %v", err, tt.wantErr) //nolint: scopelint
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) { //nolint: scopelint
|
|
t.Errorf("NewMachineFile() got = %v, want %v", got, tt.want) //nolint: scopelint
|
|
}
|
|
})
|
|
}
|
|
}
|