From 134f55aa8061bc4f956bc72b5e992ae96368c886 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 7 Oct 2020 20:52:15 +1100 Subject: [PATCH] device: Reorganize TestPciPathToSysfs 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 --- device_test.go | 57 ++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/device_test.go b/device_test.go index 7032bb074..0b60e1c0a 100644 --- a/device_test.go +++ b/device_test.go @@ -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) {