Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

source/pci: add unit test for the pci source #1589

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 227 additions & 1 deletion source/pci/pci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@ limitations under the License.
package pci

import (
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
"sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
"sigs.k8s.io/node-feature-discovery/source"
)

func TestPciSource(t *testing.T) {
var packagePath string

func init() {
_, thisFile, _, _ := runtime.Caller(0)
packagePath = filepath.Dir(thisFile)
}

func TestSingletonPciSource(t *testing.T) {
assert.Equal(t, src.Name(), Name)

// Check that GetLabels works with empty features
Expand All @@ -31,5 +43,219 @@ func TestPciSource(t *testing.T) {

assert.Nil(t, err, err)
assert.Empty(t, l)
}

func TestPciSource(t *testing.T) {
// Specify expected "raw" features. These are always the same for the same
// mocked sysfs.
expectedFeatures := map[string]*nfdv1alpha1.Features{
"rootfs-empty": &nfdv1alpha1.Features{
Flags: map[string]nfdv1alpha1.FlagFeatureSet{},
Attributes: map[string]nfdv1alpha1.AttributeFeatureSet{},
Instances: map[string]nfdv1alpha1.InstanceFeatureSet{},
},
"rootfs-1": &nfdv1alpha1.Features{
Flags: map[string]nfdv1alpha1.FlagFeatureSet{},
Attributes: map[string]nfdv1alpha1.AttributeFeatureSet{},
Instances: map[string]nfdv1alpha1.InstanceFeatureSet{
"device": nfdv1alpha1.InstanceFeatureSet{
Elements: []nfdv1alpha1.InstanceFeature{
{
Attributes: map[string]string{
"class": "0880",
"device": "2021",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
{
Attributes: map[string]string{
"class": "ff00",
"device": "a1ed",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
{
Attributes: map[string]string{
"class": "0106",
"device": "a1d2",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
{
Attributes: map[string]string{
"class": "1180",
"device": "a1b1",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
{
Attributes: map[string]string{
"class": "0780",
"device": "a1ba",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
{
Attributes: map[string]string{
"class": "0604",
"device": "a193",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
{
Attributes: map[string]string{
"class": "0c80",
"device": "a1a4",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
{
Attributes: map[string]string{
"class": "0300",
"device": "2000",
"subsystem_device": "2000",
"subsystem_vendor": "1a03",
"vendor": "1a03",
},
},
{
Attributes: map[string]string{
"class": "0b40",
"device": "37c8",
"iommu/intel-iommu/version": "1:0",
"iommu_group/type": "identity",
"sriov_totalvfs": "16",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
{
Attributes: map[string]string{
"class": "0200",
"device": "37d2",
"sriov_totalvfs": "32",
"subsystem_device": "35cf",
"subsystem_vendor": "8086",
"vendor": "8086",
},
},
},
},
},
},
}

// Specify test cases
tests := []struct {
name string
config *Config
rootfs string
expectErr bool
expectedLabels source.FeatureLabels
}{
{
name: "detect with default config",
rootfs: "rootfs-1",
expectedLabels: source.FeatureLabels{
"0300_1a03.present": true,
"0b40_8086.present": true,
"0b40_8086.sriov.capable": true,
},
},
{
name: "test config with empty DeviceLabelFields",
rootfs: "rootfs-1",
config: &Config{
DeviceClassWhitelist: []string{"0c"},
DeviceLabelFields: []string{},
},
expectedLabels: source.FeatureLabels{
"0c80_8086.present": true,
},
},
{
name: "test config with empty DeviceLabelFields",
rootfs: "rootfs-1",
config: &Config{
DeviceClassWhitelist: []string{"0c"},
DeviceLabelFields: []string{},
},
expectedLabels: source.FeatureLabels{
"0c80_8086.present": true,
},
},
{
name: "test config with only invalid DeviceLabelFields",
rootfs: "rootfs-1",
config: &Config{
DeviceClassWhitelist: []string{"0c"},
DeviceLabelFields: []string{"foo", "bar"},
},
expectedLabels: source.FeatureLabels{
"0c80_8086.present": true,
},
},
{
name: "test config with some invalid DeviceLabelFields",
rootfs: "rootfs-1",
config: &Config{
DeviceClassWhitelist: []string{"0c"},
DeviceLabelFields: []string{"foo", "class"},
},
expectedLabels: source.FeatureLabels{
"0c80.present": true,
},
},
{
name: "test empty sysfs",
rootfs: "rootfs-empty",
expectErr: true,
expectedLabels: source.FeatureLabels{},
},
}

// Run test cases
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
hostpath.SysfsDir = hostpath.HostDir(filepath.Join(packagePath, "testdata", tc.rootfs, "sys"))

config := tc.config
if config == nil {
config = newDefaultConfig()
}
testSrc := pciSource{config: config}

// Discover mock PCI devices
err := testSrc.Discover()
if tc.expectErr {
assert.NotNil(t, err, err)
} else {
assert.Nil(t, err, err)
}

// Check features
f := testSrc.GetFeatures()
assert.Equal(t, expectedFeatures[tc.rootfs], f)

// Check labels
l, err := testSrc.GetLabels()
assert.Nil(t, err, err)
assert.Equal(t, tc.expectedLabels, l)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x088000
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x2021
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(null)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
418
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0-2,5-6,10-12,15-16,40-42,45-46,50-52,55-56
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000,019c6700,00019c67
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
255
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pci:v00008086d00002021sv00008086sd000035CFbc08sc80i00
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
poolinfo - 0.1
completion_pool 0 64 64 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
D0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x04
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x35cf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x8086
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x8086
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xff0000
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
32
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xa1ed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
32
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(null)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0-2,5-6,10-12,15-16,40-42,45-46,50-52,55-56
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000,019c6700,00019c67
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pci:v00008086d0000A1EDsv00008086sd000035CFbcFFsc00i00
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x04
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x35cf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x8086
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x8086
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x010601
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xa1d2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(null)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
41
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PCH Integrated sSATA Controller
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0-2,5-6,10-12,15-16,40-42,45-46,50-52,55-56
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000,019c6700,00019c67
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pci:v00008086d0000A1D2sv00008086sd000035CFbc01sc06i01
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
D0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x04
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x35cf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x8086
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x8086
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x118000
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
32
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xa1b1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
32
Loading