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

Commit 134f55a

Browse files
committed
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 <[email protected]>
1 parent da4bc1d commit 134f55a

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

device_test.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,40 +99,47 @@ func TestPciPathToSysfs(t *testing.T) {
9999
}
100100
defer os.RemoveAll(testDir)
101101

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

106-
pciPath = PciPath{"02/03/04"}
107-
_, err = pciPathToSysfs(pciPath)
108-
assert.NotNil(t, err)
105+
_, err = pciPathToSysfs(PciPath{"02"})
106+
assert.Error(t, err)
109107

110-
bridgeID := "02"
111-
deviceID := "03"
112-
pciBus := "0000:01"
113-
expectedRelPath := "0000:00:02.0/0000:01:03.0"
114-
pciPath = PciPath{fmt.Sprintf("%s/%s", bridgeID, deviceID)}
108+
_, err = pciPathToSysfs(PciPath{"02/03"})
109+
assert.Error(t, err)
115110

116-
// Set sysBusPrefix to test directory for unit tests.
117-
sysBusPrefix = testDir
118-
bridgeBusPath := fmt.Sprintf(pciBusPathFormat, sysBusPrefix, "0000:00:02.0")
111+
_, err = pciPathToSysfs(PciPath{"02/03/04"})
112+
assert.Error(t, err)
113+
114+
// Create mock sysfs files for the device at 0000:00:02.0
115+
bridge2Path := fmt.Sprintf(pciBusPathFormat, sysBusPrefix, "0000:00:02.0")
116+
117+
err = os.MkdirAll(bridge2Path, mountPerm)
118+
assert.NoError(t, err)
119119

120-
_, err = pciPathToSysfs(pciPath)
121-
assert.NotNil(t, err)
120+
_, err = pciPathToSysfs(PciPath{"02"})
121+
assert.Error(t, err)
122122

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

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

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

132-
addr, err := pciPathToSysfs(pciPath)
133-
assert.Nil(t, err)
134+
_, err = pciPathToSysfs(PciPath{"02"})
135+
assert.Error(t, err)
136+
137+
sysRelPath, err := pciPathToSysfs(PciPath{"02/03"})
138+
assert.NoError(t, err)
139+
assert.Equal(t, sysRelPath, "0000:00:02.0/0000:01:03.0")
134140

135-
assert.Equal(t, addr, expectedRelPath)
141+
_, err = pciPathToSysfs(PciPath{"02/03/04"})
142+
assert.Error(t, err)
136143
}
137144

138145
func TestScanSCSIBus(t *testing.T) {

0 commit comments

Comments
 (0)