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
Signed-off-by: Kishen V <[email protected]>
  • Loading branch information
kishen-v committed Mar 14, 2024
1 parent 151c79a commit d2d4d98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
package cpu_test

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

"github.com/jaypipes/ghw/pkg/cpu"
Expand Down Expand Up @@ -71,3 +74,44 @@ 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)
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")
}
}
Binary file not shown.

0 comments on commit d2d4d98

Please sign in to comment.