@@ -17,12 +17,24 @@ limitations under the License.
17
17
package pci
18
18
19
19
import (
20
+ "path/filepath"
21
+ "runtime"
20
22
"testing"
21
23
22
24
"github.com/stretchr/testify/assert"
25
+ nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
26
+ "sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
27
+ "sigs.k8s.io/node-feature-discovery/source"
23
28
)
24
29
25
- func TestPciSource (t * testing.T ) {
30
+ var packagePath string
31
+
32
+ func init () {
33
+ _ , thisFile , _ , _ := runtime .Caller (0 )
34
+ packagePath = filepath .Dir (thisFile )
35
+ }
36
+
37
+ func TestSingletonPciSource (t * testing.T ) {
26
38
assert .Equal (t , src .Name (), Name )
27
39
28
40
// Check that GetLabels works with empty features
@@ -31,5 +43,219 @@ func TestPciSource(t *testing.T) {
31
43
32
44
assert .Nil (t , err , err )
33
45
assert .Empty (t , l )
46
+ }
47
+
48
+ func TestPciSource (t * testing.T ) {
49
+ // Specify expected "raw" features. These are always the same for the same
50
+ // mocked sysfs.
51
+ expectedFeatures := map [string ]* nfdv1alpha1.Features {
52
+ "rootfs-empty" : & nfdv1alpha1.Features {
53
+ Flags : map [string ]nfdv1alpha1.FlagFeatureSet {},
54
+ Attributes : map [string ]nfdv1alpha1.AttributeFeatureSet {},
55
+ Instances : map [string ]nfdv1alpha1.InstanceFeatureSet {},
56
+ },
57
+ "rootfs-1" : & nfdv1alpha1.Features {
58
+ Flags : map [string ]nfdv1alpha1.FlagFeatureSet {},
59
+ Attributes : map [string ]nfdv1alpha1.AttributeFeatureSet {},
60
+ Instances : map [string ]nfdv1alpha1.InstanceFeatureSet {
61
+ "device" : nfdv1alpha1.InstanceFeatureSet {
62
+ Elements : []nfdv1alpha1.InstanceFeature {
63
+ {
64
+ Attributes : map [string ]string {
65
+ "class" : "0880" ,
66
+ "device" : "2021" ,
67
+ "subsystem_device" : "35cf" ,
68
+ "subsystem_vendor" : "8086" ,
69
+ "vendor" : "8086" ,
70
+ },
71
+ },
72
+ {
73
+ Attributes : map [string ]string {
74
+ "class" : "ff00" ,
75
+ "device" : "a1ed" ,
76
+ "subsystem_device" : "35cf" ,
77
+ "subsystem_vendor" : "8086" ,
78
+ "vendor" : "8086" ,
79
+ },
80
+ },
81
+ {
82
+ Attributes : map [string ]string {
83
+ "class" : "0106" ,
84
+ "device" : "a1d2" ,
85
+ "subsystem_device" : "35cf" ,
86
+ "subsystem_vendor" : "8086" ,
87
+ "vendor" : "8086" ,
88
+ },
89
+ },
90
+ {
91
+ Attributes : map [string ]string {
92
+ "class" : "1180" ,
93
+ "device" : "a1b1" ,
94
+ "subsystem_device" : "35cf" ,
95
+ "subsystem_vendor" : "8086" ,
96
+ "vendor" : "8086" ,
97
+ },
98
+ },
99
+ {
100
+ Attributes : map [string ]string {
101
+ "class" : "0780" ,
102
+ "device" : "a1ba" ,
103
+ "subsystem_device" : "35cf" ,
104
+ "subsystem_vendor" : "8086" ,
105
+ "vendor" : "8086" ,
106
+ },
107
+ },
108
+ {
109
+ Attributes : map [string ]string {
110
+ "class" : "0604" ,
111
+ "device" : "a193" ,
112
+ "subsystem_device" : "35cf" ,
113
+ "subsystem_vendor" : "8086" ,
114
+ "vendor" : "8086" ,
115
+ },
116
+ },
117
+ {
118
+ Attributes : map [string ]string {
119
+ "class" : "0c80" ,
120
+ "device" : "a1a4" ,
121
+ "subsystem_device" : "35cf" ,
122
+ "subsystem_vendor" : "8086" ,
123
+ "vendor" : "8086" ,
124
+ },
125
+ },
126
+ {
127
+ Attributes : map [string ]string {
128
+ "class" : "0300" ,
129
+ "device" : "2000" ,
130
+ "subsystem_device" : "2000" ,
131
+ "subsystem_vendor" : "1a03" ,
132
+ "vendor" : "1a03" ,
133
+ },
134
+ },
135
+ {
136
+ Attributes : map [string ]string {
137
+ "class" : "0b40" ,
138
+ "device" : "37c8" ,
139
+ "iommu/intel-iommu/version" : "1:0" ,
140
+ "iommu_group/type" : "identity" ,
141
+ "sriov_totalvfs" : "16" ,
142
+ "subsystem_device" : "35cf" ,
143
+ "subsystem_vendor" : "8086" ,
144
+ "vendor" : "8086" ,
145
+ },
146
+ },
147
+ {
148
+ Attributes : map [string ]string {
149
+ "class" : "0200" ,
150
+ "device" : "37d2" ,
151
+ "sriov_totalvfs" : "32" ,
152
+ "subsystem_device" : "35cf" ,
153
+ "subsystem_vendor" : "8086" ,
154
+ "vendor" : "8086" ,
155
+ },
156
+ },
157
+ },
158
+ },
159
+ },
160
+ },
161
+ }
162
+
163
+ // Specify test cases
164
+ tests := []struct {
165
+ name string
166
+ config * Config
167
+ rootfs string
168
+ expectErr bool
169
+ expectedLabels source.FeatureLabels
170
+ }{
171
+ {
172
+ name : "detect with default config" ,
173
+ rootfs : "rootfs-1" ,
174
+ expectedLabels : source.FeatureLabels {
175
+ "0300_1a03.present" : true ,
176
+ "0b40_8086.present" : true ,
177
+ "0b40_8086.sriov.capable" : true ,
178
+ },
179
+ },
180
+ {
181
+ name : "test config with empty DeviceLabelFields" ,
182
+ rootfs : "rootfs-1" ,
183
+ config : & Config {
184
+ DeviceClassWhitelist : []string {"0c" },
185
+ DeviceLabelFields : []string {},
186
+ },
187
+ expectedLabels : source.FeatureLabels {
188
+ "0c80_8086.present" : true ,
189
+ },
190
+ },
191
+ {
192
+ name : "test config with empty DeviceLabelFields" ,
193
+ rootfs : "rootfs-1" ,
194
+ config : & Config {
195
+ DeviceClassWhitelist : []string {"0c" },
196
+ DeviceLabelFields : []string {},
197
+ },
198
+ expectedLabels : source.FeatureLabels {
199
+ "0c80_8086.present" : true ,
200
+ },
201
+ },
202
+ {
203
+ name : "test config with only invalid DeviceLabelFields" ,
204
+ rootfs : "rootfs-1" ,
205
+ config : & Config {
206
+ DeviceClassWhitelist : []string {"0c" },
207
+ DeviceLabelFields : []string {"foo" , "bar" },
208
+ },
209
+ expectedLabels : source.FeatureLabels {
210
+ "0c80_8086.present" : true ,
211
+ },
212
+ },
213
+ {
214
+ name : "test config with some invalid DeviceLabelFields" ,
215
+ rootfs : "rootfs-1" ,
216
+ config : & Config {
217
+ DeviceClassWhitelist : []string {"0c" },
218
+ DeviceLabelFields : []string {"foo" , "class" },
219
+ },
220
+ expectedLabels : source.FeatureLabels {
221
+ "0c80.present" : true ,
222
+ },
223
+ },
224
+ {
225
+ name : "test empty sysfs" ,
226
+ rootfs : "rootfs-empty" ,
227
+ expectErr : true ,
228
+ expectedLabels : source.FeatureLabels {},
229
+ },
230
+ }
231
+
232
+ // Run test cases
233
+ for _ , tc := range tests {
234
+ t .Run (tc .name , func (t * testing.T ) {
235
+ hostpath .SysfsDir = hostpath .HostDir (filepath .Join (packagePath , "testdata" , tc .rootfs , "sys" ))
236
+
237
+ config := tc .config
238
+ if config == nil {
239
+ config = newDefaultConfig ()
240
+ }
241
+ testSrc := pciSource {config : config }
242
+
243
+ // Discover mock PCI devices
244
+ err := testSrc .Discover ()
245
+ if tc .expectErr {
246
+ assert .NotNil (t , err , err )
247
+ } else {
248
+ assert .Nil (t , err , err )
249
+ }
250
+
251
+ // Check features
252
+ f := testSrc .GetFeatures ()
253
+ assert .Equal (t , expectedFeatures [tc .rootfs ], f )
34
254
255
+ // Check labels
256
+ l , err := testSrc .GetLabels ()
257
+ assert .Nil (t , err , err )
258
+ assert .Equal (t , tc .expectedLabels , l )
259
+ })
260
+ }
35
261
}
0 commit comments