Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
device: Reorganize TestPciPathToSysfs
Browse files Browse the repository at this point in the history
This does some general reorganization of TestPciPathToSysfs.  It tests the
same things (plus a few trivial extras), but is set out in a way that will
be easier to extend when we broaden the allowed pciPath values we accept.

It also removes some intermediate variables that seemed to make things
harder rather than easier to follow.

Signed-off-by: David Gibson <[email protected]>
  • Loading branch information
dgibson committed Oct 8, 2020
1 parent da4bc1d commit 134f55a
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,40 +99,47 @@ func TestPciPathToSysfs(t *testing.T) {
}
defer os.RemoveAll(testDir)

pciPath := PciPath{"02"}
_, err = pciPathToSysfs(pciPath)
assert.NotNil(t, err)
// Set sysBusPrefix to test directory for unit tests.
sysBusPrefix = testDir

pciPath = PciPath{"02/03/04"}
_, err = pciPathToSysfs(pciPath)
assert.NotNil(t, err)
_, err = pciPathToSysfs(PciPath{"02"})
assert.Error(t, err)

bridgeID := "02"
deviceID := "03"
pciBus := "0000:01"
expectedRelPath := "0000:00:02.0/0000:01:03.0"
pciPath = PciPath{fmt.Sprintf("%s/%s", bridgeID, deviceID)}
_, err = pciPathToSysfs(PciPath{"02/03"})
assert.Error(t, err)

// Set sysBusPrefix to test directory for unit tests.
sysBusPrefix = testDir
bridgeBusPath := fmt.Sprintf(pciBusPathFormat, sysBusPrefix, "0000:00:02.0")
_, err = pciPathToSysfs(PciPath{"02/03/04"})
assert.Error(t, err)

// Create mock sysfs files for the device at 0000:00:02.0
bridge2Path := fmt.Sprintf(pciBusPathFormat, sysBusPrefix, "0000:00:02.0")

err = os.MkdirAll(bridge2Path, mountPerm)
assert.NoError(t, err)

_, err = pciPathToSysfs(pciPath)
assert.NotNil(t, err)
_, err = pciPathToSysfs(PciPath{"02"})
assert.Error(t, err)

err = os.MkdirAll(bridgeBusPath, mountPerm)
assert.Nil(t, err)
_, err = pciPathToSysfs(PciPath{"02/03"})
assert.Error(t, err)

_, err = pciPathToSysfs(pciPath)
assert.NotNil(t, err)
_, err = pciPathToSysfs(PciPath{"02/03/04"})
assert.Error(t, err)

err = os.MkdirAll(filepath.Join(bridgeBusPath, pciBus), mountPerm)
assert.Nil(t, err)
// Create mock sysfs files to indicate that 0000:00:02.0 is a bridge to bus 01
bridge2Bus := "0000:01"
err = os.MkdirAll(filepath.Join(bridge2Path, bridge2Bus), mountPerm)
assert.NoError(t, err)

addr, err := pciPathToSysfs(pciPath)
assert.Nil(t, err)
_, err = pciPathToSysfs(PciPath{"02"})
assert.Error(t, err)

sysRelPath, err := pciPathToSysfs(PciPath{"02/03"})
assert.NoError(t, err)
assert.Equal(t, sysRelPath, "0000:00:02.0/0000:01:03.0")

assert.Equal(t, addr, expectedRelPath)
_, err = pciPathToSysfs(PciPath{"02/03/04"})
assert.Error(t, err)
}

func TestScanSCSIBus(t *testing.T) {
Expand Down

0 comments on commit 134f55a

Please sign in to comment.