diff --git a/pkg/pci/pci.go b/pkg/pci/pci.go index a70f9a38..6c8ae109 100644 --- a/pkg/pci/pci.go +++ b/pkg/pci/pci.go @@ -144,6 +144,11 @@ type Address struct { Function string } +// String() returns the canonical [D]BSF representation of this Address +func (addr *Address) String() string { + return addr.Domain + ":" + addr.Bus + ":" + addr.Slot + "." + addr.Function +} + // Given a string address, returns a complete Address struct, filled in with // domain, bus, slot and function components. The address string may either // be in $BUS:$SLOT.$FUNCTION (BSF) format or it can be a full PCI address diff --git a/pkg/pci/pci_linux.go b/pkg/pci/pci_linux.go index 0f270dab..8a9c75f2 100644 --- a/pkg/pci/pci_linux.go +++ b/pkg/pci/pci_linux.go @@ -52,7 +52,7 @@ func getDeviceRevision(ctx *context.Context, address string) string { } revisionPath := filepath.Join( paths.SysBusPciDevices, - pciAddr.Domain+":"+pciAddr.Bus+":"+pciAddr.Slot+"."+pciAddr.Function, + pciAddr.String(), "revision", ) diff --git a/pkg/pci/pci_test.go b/pkg/pci/pci_test.go index 6ddc1c2d..50d03843 100644 --- a/pkg/pci/pci_test.go +++ b/pkg/pci/pci_test.go @@ -9,6 +9,7 @@ package pci_test import ( "os" "reflect" + "strings" "testing" "github.com/jaypipes/ghw/pkg/context" @@ -21,6 +22,11 @@ func TestPCIAddressFromString(t *testing.T) { tests := []struct { addrStr string expected *pci.Address + // AddressFromString is more flexible than String() and wants + // to accept addresses not in full canonical form, as long as + // it can do the right thing - e.g. a sane default Domain exists. + // Thus we need to sometimes skip the Address -> string check. + skipStringTest bool }{ { addrStr: "00:00.0", @@ -30,6 +36,7 @@ func TestPCIAddressFromString(t *testing.T) { Slot: "00", Function: "0", }, + skipStringTest: true, }, { addrStr: "0000:00:00.0", @@ -64,6 +71,16 @@ func TestPCIAddressFromString(t *testing.T) { if !reflect.DeepEqual(got, test.expected) { t.Fatalf("Test #%d failed. Expected %v but got %v", x, test.expected, got) } + + if test.skipStringTest { + continue + } + + addrStr := got.String() + // addresses are case insensitive + if !strings.EqualFold(addrStr, test.addrStr) { + t.Fatalf("Test #%d failed. Expected %q but got %q (case insensitive match)", x, test.addrStr, addrStr) + } } }