github.com/jaypipes/ghw@v0.21.1/pkg/gpu/gpu_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  
     7  package gpu_test
     8  
     9  import (
    10  	"errors"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/jaypipes/ghw/pkg/gpu"
    16  	"github.com/jaypipes/ghw/pkg/option"
    17  	"github.com/jaypipes/ghw/pkg/snapshot"
    18  
    19  	"github.com/jaypipes/ghw/testdata"
    20  )
    21  
    22  // testcase for https://github.com/jaypipes/ghw/issues/234
    23  // if nothing else: demonstrate how to consume snapshots from tests;
    24  // test a boundary condition actually happened in the wild, even though on a VM environment.
    25  func TestGPUWithoutNUMANodeInfo(t *testing.T) {
    26  	if _, ok := os.LookupEnv("GHW_TESTING_SKIP_GPU"); ok {
    27  		t.Skip("Skipping PCI tests.")
    28  	}
    29  
    30  	testdataPath, err := testdata.SnapshotsDirectory()
    31  	if err != nil {
    32  		t.Fatalf("Expected nil err, but got %v", err)
    33  	}
    34  
    35  	t.Setenv("PCIDB_PATH", testdata.PCIDBChroot())
    36  
    37  	workstationSnapshot := filepath.Join(testdataPath, "linux-amd64-amd-ryzen-1600.tar.gz")
    38  	// from now on we use constants reflecting the content of the snapshot we requested,
    39  	// which we reviewed beforehand. IOW, you need to know the content of the
    40  	// snapshot to fully understand this test. Inspect it using
    41  	// GHW_SNAPSHOT_PATH="/path/to/linux-amd64-amd-ryzen-1600.tar.gz" ghwc gpu
    42  
    43  	tmpRoot, err := os.MkdirTemp("", "ghw-gpu-testing-*")
    44  	if err != nil {
    45  		t.Fatalf("Unable to create temporary directory: %v", err)
    46  	}
    47  
    48  	_, err = snapshot.UnpackInto(workstationSnapshot, tmpRoot, 0)
    49  	if err != nil {
    50  		t.Fatalf("Unable to unpack %q into %q: %v", workstationSnapshot, tmpRoot, err)
    51  	}
    52  	defer func() {
    53  		_ = snapshot.Cleanup(tmpRoot)
    54  	}()
    55  
    56  	err = os.Remove(filepath.Join(tmpRoot, "/sys/class/drm/card0/device/numa_node"))
    57  	if err != nil && !errors.Is(err, os.ErrNotExist) {
    58  		t.Fatalf("Cannot remove the NUMA node info: %v", err)
    59  	}
    60  
    61  	info, err := gpu.New(option.WithChroot(tmpRoot))
    62  	if err != nil {
    63  		t.Fatalf("Expected nil err, but got %v", err)
    64  	}
    65  	if info == nil {
    66  		t.Fatalf("Expected non-nil GPUInfo, but got nil")
    67  	}
    68  	if len(info.GraphicsCards) == 0 {
    69  		t.Fatalf("Expected >0 GPU cards, but found 0.")
    70  	}
    71  }