Skip to content

Commit

Permalink
Merge pull request #223 from fromanirh/pci-string-fn
Browse files Browse the repository at this point in the history
pci: add String() method to pci.Device
  • Loading branch information
jaypipes authored Feb 11, 2021
2 parents 55f240f + 6a73eaa commit 3c8d1f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/pci/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/pci/pci_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
17 changes: 17 additions & 0 deletions pkg/pci/pci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package pci_test
import (
"os"
"reflect"
"strings"
"testing"

"github.com/jaypipes/ghw/pkg/context"
Expand All @@ -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",
Expand All @@ -30,6 +36,7 @@ func TestPCIAddressFromString(t *testing.T) {
Slot: "00",
Function: "0",
},
skipStringTest: true,
},
{
addrStr: "0000:00:00.0",
Expand Down Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit 3c8d1f9

Please sign in to comment.