Files
podman/libpod/util_test.go
Miloslav Trmač 4c3027c149 Make most of libpod, and everything that relies on it, non-darwin
Require (linux || freebsd), because the code already does that, in practice.
This just means macOS users of IDEs aren't hit with thousands of compilation
errors (and then the IDE can open an Linux-specific file and then process it
under the Linux assumption, which works much better).

This commit ONLY replaces
	//go:build !remote
with
	//go:build !remote && (linux || freebsd)

and is split from the rest to allow mechanically verifying that fact,
and focusing a review on the other kinds of changes.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2026-02-27 22:18:36 +01:00

215 lines
4.1 KiB
Go

//go:build !remote && (linux || freebsd)
package libpod
import (
"testing"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
)
func Test_sortMounts(t *testing.T) {
tests := []struct {
name string
args []spec.Mount
want []spec.Mount
}{
{
name: "simple nested mounts",
args: []spec.Mount{
{
Destination: "/abc/123",
},
{
Destination: "/abc",
},
},
want: []spec.Mount{
{
Destination: "/abc",
},
{
Destination: "/abc/123",
},
},
},
{
name: "root mount",
args: []spec.Mount{
{
Destination: "/abc",
},
{
Destination: "/",
},
{
Destination: "/def",
},
},
want: []spec.Mount{
{
Destination: "/",
},
{
Destination: "/abc",
},
{
Destination: "/def",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sortMounts(tt.args)
assert.Equal(t, tt.want, got)
})
}
}
type mockVendorLister struct {
vendors []string
}
func (m *mockVendorLister) ListVendors() []string {
return m.vendors
}
func Test_gpusToCDIDevices(t *testing.T) {
tests := []struct {
name string
gpus []string
vendors []string
expectError bool
expectDevices []string
}{
{
name: "No GPUs",
gpus: []string{},
vendors: []string{"amd.com"},
},
{
name: "Nil GPUs",
gpus: nil,
vendors: []string{"amd.com"},
},
{
name: "Nil vendors",
gpus: []string{"0"},
vendors: nil,
expectError: true,
},
{
name: "Single GPU with AMD",
gpus: []string{"0"},
vendors: []string{"amd.com"},
expectDevices: []string{"amd.com/gpu=0"},
},
{
name: "Multiple GPUs with AMD",
gpus: []string{"0", "1"},
vendors: []string{"amd.com"},
expectDevices: []string{"amd.com/gpu=0", "amd.com/gpu=1"},
},
{
name: "Single GPU with NVIDIA",
gpus: []string{"0"},
vendors: []string{"nvidia.com"},
expectDevices: []string{"nvidia.com/gpu=0"},
},
{
name: "Multiple GPUs with NVIDIA",
gpus: []string{"0", "1"},
vendors: []string{"nvidia.com"},
expectDevices: []string{"nvidia.com/gpu=0", "nvidia.com/gpu=1"},
},
{
name: "No vendors",
gpus: []string{"0"},
vendors: []string{},
expectError: true,
},
{
name: "Unknown vendor",
gpus: []string{"0"},
vendors: []string{"unknown.com"},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var lister vendorLister
if tt.vendors != nil {
lister = &mockVendorLister{vendors: tt.vendors}
}
cdiDevices, err := gpusToCDIDevices(tt.gpus, lister)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectDevices, cdiDevices)
}
})
}
}
func Test_discoverGPUVendorFromCDI(t *testing.T) {
tests := []struct {
name string
vendors []string
expectVendor string
expectError bool
}{
{
name: "Nil vendors",
vendors: nil,
expectError: true,
},
{
name: "NVIDIA vendor",
vendors: []string{"nvidia.com"},
expectVendor: "nvidia.com",
},
{
name: "AMD vendor",
vendors: []string{"amd.com"},
expectVendor: "amd.com",
},
{
name: "No vendors",
vendors: []string{},
expectError: true,
},
{
name: "Unknown vendor",
vendors: []string{"unknown.com"},
expectError: true,
},
{
name: "Mixed vendor",
vendors: []string{"amd.com", "nvidia.com"},
expectVendor: "nvidia.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var lister vendorLister
if tt.vendors != nil {
lister = &mockVendorLister{vendors: tt.vendors}
}
vendor, err := discoverGPUVendorFromCDI(lister)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectVendor, vendor)
}
})
}
}