Skip to content

Commit

Permalink
Add tests for files in topology directory for offline CPUs
Browse files Browse the repository at this point in the history
Tests for missing file warnings for offline CPUs

Signed-off-by: Kishen V <[email protected]>
  • Loading branch information
kishen-v committed Mar 21, 2024
1 parent 92e3466 commit 1b877ca
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions pkg/cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
package cpu_test

import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/jaypipes/ghw/pkg/cpu"
"github.com/jaypipes/ghw/pkg/option"
"github.com/jaypipes/ghw/pkg/topology"
"github.com/jaypipes/ghw/testdata"
)

Expand Down Expand Up @@ -71,3 +75,84 @@ func TestArmCPU(t *testing.T) {
}
}
}

func TestCheckCPUTopologyFilesForOfflineCPU(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_CPU"); ok {
t.Skip("Skipping CPU tests.")
}

testdataPath, err := testdata.SnapshotsDirectory()
if err != nil {
t.Fatalf("Expected nil err, but got %v", err)
}

offlineCPUSnapshot := filepath.Join(testdataPath, "linux-amd64-offlineCPUs.tar.gz")

// Capture stderr
rErr, wErr, err := os.Pipe()
if err != nil {
t.Fatalf("Cannot pipe StdErr. %v", err)
}
os.Stderr = wErr

info, err := cpu.New(option.WithSnapshot(option.SnapshotOptions{
Path: offlineCPUSnapshot,
}))
if err != nil {
t.Fatalf("Expected nil err, but got %v", err)
}
if info == nil {
t.Fatalf("Expected non-nil CPUInfo, but got nil")
}

if len(info.Processors) == 0 {
t.Fatalf("Expected >0 processors but got 0.")
}
wErr.Close()
var bufErr bytes.Buffer
io.Copy(&bufErr, rErr)

Check failure on line 113 in pkg/cpu/cpu_linux_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `io.Copy` is not checked (errcheck)
errorOutput := bufErr.String()
if strings.Contains(errorOutput, "WARNING: failed to read int from file:") {
t.Fatalf("Unexpected warning related to missing files under topology directory was reported")
}
}
func TestNumCoresAmongOfflineCPUs(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_CPU"); ok {
t.Skip("Skipping CPU tests.")
}

testdataPath, err := testdata.SnapshotsDirectory()
if err != nil {
t.Fatalf("Expected nil err, but got %v", err)
}

offlineCPUSnapshot := filepath.Join(testdataPath, "linux-amd64-offlineCPUs.tar.gz")

// Capture stderr
rErr, wErr, err := os.Pipe()
if err != nil {
t.Fatalf("Cannot pipe the StdErr. %v", err)
}
info, err := topology.New(option.WithSnapshot(option.SnapshotOptions{
Path: offlineCPUSnapshot,
}))
if err != nil {
t.Fatalf("Error determining node topology. %v", err)
}

if len(info.Nodes) < 1 {
t.Fatal("No nodes found. Must contain one or more nodes")
}
for _, node := range info.Nodes {
if len(node.Cores) < 1 {
t.Fatal("No cores found. Must contain one or more cores")
}
}
wErr.Close()
var bufErr bytes.Buffer
io.Copy(&bufErr, rErr)

Check failure on line 153 in pkg/cpu/cpu_linux_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `io.Copy` is not checked (errcheck)
errorOutput := bufErr.String()
if strings.Contains(errorOutput, "WARNING: failed to read int from file:") {
t.Fatalf("Unexpected warnings related to missing files under topology directory was raised")
}
}
Binary file added testdata/snapshots/linux-amd64-offlineCPUs.tar.gz
Binary file not shown.

0 comments on commit 1b877ca

Please sign in to comment.