|
| 1 | +// Copyright (c) 2025 Zededa, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package hypervisor |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestPCIReadResources(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + // Create a temporary directory for our fake sysfs |
| 17 | + tempDir, err := os.MkdirTemp("", "test-pci") |
| 18 | + assert.NoError(t, err) |
| 19 | + defer os.RemoveAll(tempDir) // Cleanup after test |
| 20 | + |
| 21 | + // Create a fake PCI device directory |
| 22 | + devicePath := filepath.Join(tempDir, "0000:00:1f.0") |
| 23 | + err = os.MkdirAll(devicePath, 0755) |
| 24 | + assert.NoError(t, err) |
| 25 | + |
| 26 | + // Create mock resource file |
| 27 | + resourceContent := "0x00000000fed40000 0x00000000fed44fff 0x0000000000040200\n" + |
| 28 | + "0x00000000fec00000 0x00000000fec003ff 0x0000000000040200\n" |
| 29 | + |
| 30 | + err = os.WriteFile(filepath.Join(devicePath, "resource"), []byte(resourceContent), 0644) |
| 31 | + assert.NoError(t, err) |
| 32 | + |
| 33 | + deviceAttributes := []string{"resource0", "resource1", "resource1_wc", "resource1_resize", "resource1_not_yet_exist"} |
| 34 | + |
| 35 | + for _, attr := range deviceAttributes { |
| 36 | + err = os.WriteFile(filepath.Join(devicePath, attr), []byte{}, 0644) |
| 37 | + assert.NoError(t, err) |
| 38 | + } |
| 39 | + |
| 40 | + // Instantiate the PCI device and call readResources() |
| 41 | + pciDev := pciDevice{pciLong: "0000:00:1f.0"} |
| 42 | + resources, err := pciDev.readResources(tempDir) |
| 43 | + |
| 44 | + // Assertions |
| 45 | + assert.NoError(t, err) |
| 46 | + assert.Len(t, resources, 2) |
| 47 | + |
| 48 | + assert.Equal(t, uint64(0xfed40000), resources[0].start) |
| 49 | + assert.Equal(t, uint64(0xfed44fff), resources[0].end) |
| 50 | + assert.Equal(t, uint64(0x00040200), resources[0].flags) |
| 51 | + assert.Equal(t, 0, resources[0].index) |
| 52 | + |
| 53 | + assert.Equal(t, uint64(0xfec00000), resources[1].start) |
| 54 | + assert.Equal(t, uint64(0xfec003ff), resources[1].end) |
| 55 | + assert.Equal(t, uint64(0x00040200), resources[1].flags) |
| 56 | + assert.Equal(t, 1, resources[1].index) |
| 57 | +} |
0 commit comments