-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpciexpress_test.go
137 lines (119 loc) · 4 KB
/
pciexpress_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package qcli
import (
"fmt"
"reflect"
"strings"
"testing"
)
var (
devicePCIeRootPortSimpleString = "-device pcie-root-port,id=rp1,bus=pcie.0,chassis=0x00,slot=0x00,addr=0x00,multifunction=off"
devicePCIeRootPortFullString = "-device pcie-root-port,id=rp2,bus=pcie.0,chassis=0x0,slot=0x1,addr=0x2,multifunction=on,bus-reserve=0x3,pref64-reserve=16G,mem-reserve=1G,io-reserve=512M,romfile=efi-virtio.rom"
)
func TestDevicePCIeRootPortValid(t *testing.T) {
pcieRootPortDevice := PCIeRootPortDevice{}
if err := pcieRootPortDevice.Valid(); err == nil {
t.Fatalf("PCIeRootPort should NOT be valid when ID is empty")
}
pcieRootPortDevice.ID = "rp0"
if err := pcieRootPortDevice.Valid(); err != nil {
t.Fatalf("PCIeRootPort should be valid")
}
pcieRootPortDevice.Pref32Reserve = "256M"
pcieRootPortDevice.Pref64Reserve = "16G"
if err := pcieRootPortDevice.Valid(); err == nil {
t.Fatalf("PCIeRootPort should NOT be valid, Pref32Reserve and Pref64Reserve are mutually exclusive")
}
}
func TestAppendDevicePCIeRootPortSimple(t *testing.T) {
pcieRootPortDevice := PCIeRootPortDevice{
ID: "rp1",
}
testAppend(pcieRootPortDevice, devicePCIeRootPortSimpleString, t)
}
func TestAppendDevicePCIeRootPortFull(t *testing.T) {
pcieRootPortDevice := PCIeRootPortDevice{
ID: "rp2",
Multifunction: true,
Bus: "pcie.0",
Chassis: "0x0",
Slot: "0x1",
Addr: "0x2",
Pref64Reserve: "16G",
IOReserve: "512M",
MemReserve: "1G",
BusReserve: "0x3",
ROMFile: romfile,
}
testAppend(pcieRootPortDevice, devicePCIeRootPortFullString, t)
}
func TestAppendDevicePCIeRootPortMultiFuncPair(t *testing.T) {
c := &Config{
PCIeRootPortDevices: []PCIeRootPortDevice{
PCIeRootPortDevice{
ID: "root-port.4.0",
Bus: "pcie.0",
Chassis: "0x0",
Slot: "0x00",
Port: "0x0",
Addr: "0x4.0x0",
Multifunction: true,
},
PCIeRootPortDevice{
ID: "root-port.4.1",
Bus: "pcie.0",
Chassis: "0x1",
Slot: "0x00",
Port: "0x1",
Addr: "0x4.0x1",
Multifunction: false,
},
},
}
expected := "-device pcie-root-port,id=root-port.4.0,bus=pcie.0,chassis=0x0,slot=0x00,port=0x0,addr=0x4.0x0,multifunction=on -device pcie-root-port,id=root-port.4.1,bus=pcie.0,chassis=0x1,slot=0x00,port=0x1,addr=0x4.0x1"
testConfig(c, expected, t)
}
func TestAppendDevicePCIeRootMultifunctionPortRange(t *testing.T) {
portPrefix := "root-port"
bus := "pcie.0"
baseAddr := "4"
numPorts := 8
devices := []Device{}
expectedParams := []string{}
// hand generate devices and expected results
for p := 0; p < numPorts; p++ {
rootPortID := fmt.Sprintf("%s.%s.%d", portPrefix, baseAddr, p)
port := fmt.Sprintf("0x%x", p)
chassis := fmt.Sprintf("0x%x", p)
addr := fmt.Sprintf("%s.0x%x", baseAddr, p)
expected := fmt.Sprintf("-device pcie-root-port,id=%s,bus=%s,chassis=%s,slot=0x00,port=%s,addr=%s", rootPortID, bus, chassis, port, addr)
pcieRootPort := PCIeRootPortDevice{
ID: rootPortID,
Port: port,
Chassis: chassis,
Addr: addr,
Bus: bus,
}
if p == 0 {
pcieRootPort.Multifunction = true
expected = expected + ",multifunction=on"
}
// verify we got the string we wanted
var config Config
testConfigAppend(&config, pcieRootPort, expected, t)
// Add this to the list of devices
expectedParams = append(expectedParams, expected)
devices = append(devices, pcieRootPort)
}
// test them all together
config := &Config{devices: devices}
testConfig(config, strings.Join(expectedParams, " "), t)
// Use NewPCIeRootPortRange() and compare to current devices
newDevices, err := NewPCIeRootMultifunctionPortRange(portPrefix, bus, baseAddr, numPorts)
if err != nil {
t.Errorf("NewPCIeRootMultifunctionPortRoage returned error: %s", err.Error())
}
ok := reflect.DeepEqual(devices, newDevices)
if !ok {
t.Errorf("PCIeRootMultifunctionPortRage mismatch, expected %+v, found %+v", devices, newDevices)
}
}