github.com/jaypipes/ghw@v0.21.1/pkg/accelerator/accelerator_linux_test.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  
     6  package accelerator_test
     7  
     8  import (
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/jaypipes/ghw/pkg/accelerator"
    14  	"github.com/jaypipes/ghw/pkg/option"
    15  	"github.com/jaypipes/ghw/pkg/snapshot"
    16  
    17  	"github.com/jaypipes/ghw/testdata"
    18  )
    19  
    20  func testScenario(t *testing.T, filename string, expectedDevs int) {
    21  	testdataPath, err := testdata.SnapshotsDirectory()
    22  	if err != nil {
    23  		t.Fatalf("Expected nil err, but got %v", err)
    24  	}
    25  
    26  	t.Setenv("PCIDB_PATH", testdata.PCIDBChroot())
    27  
    28  	workstationSnapshot := filepath.Join(testdataPath, filename)
    29  
    30  	tmpRoot, err := os.MkdirTemp("", "ghw-accelerator-testing-*")
    31  	if err != nil {
    32  		t.Fatalf("Unable to create temporary directory: %v", err)
    33  	}
    34  
    35  	_, err = snapshot.UnpackInto(workstationSnapshot, tmpRoot, 0)
    36  	if err != nil {
    37  		t.Fatalf("Unable to unpack %q into %q: %v", workstationSnapshot, tmpRoot, err)
    38  	}
    39  
    40  	defer func() {
    41  		_ = snapshot.Cleanup(tmpRoot)
    42  	}()
    43  
    44  	info, err := accelerator.New(option.WithChroot(tmpRoot))
    45  	if err != nil {
    46  		t.Fatalf("Expected nil err, but got %v", err)
    47  	}
    48  	if info == nil {
    49  		t.Fatalf("Expected non-nil AcceleratorInfo, but got nil")
    50  	}
    51  	if len(info.Devices) != expectedDevs {
    52  		t.Fatalf("Expected %d processing accelerator devices, but found %d.", expectedDevs, len(info.Devices))
    53  	}
    54  }
    55  
    56  func TestAcceleratorDefault(t *testing.T) {
    57  	if _, ok := os.LookupEnv("GHW_TESTING_SKIP_ACCELERATOR"); ok {
    58  		t.Skip("Skipping PCI tests.")
    59  	}
    60  
    61  	// In this scenario we have 1 processing accelerator device
    62  	testScenario(t, "linux-amd64-accel.tar.gz", 1)
    63  
    64  }
    65  
    66  func TestAcceleratorNvidia(t *testing.T) {
    67  	if _, ok := os.LookupEnv("GHW_TESTING_SKIP_ACCELERATOR"); ok {
    68  		t.Skip("Skipping PCI tests.")
    69  	}
    70  
    71  	// In this scenario we have 1 Nvidia 3D controller device
    72  	testScenario(t, "linux-amd64-accel-nvidia.tar.gz", 1)
    73  }