Skip to content

Commit 524a3b3

Browse files
committed
Add tests for files in topology directory for offline CPUs
Tests for missing file warnings for offline CPUs Signed-off-by: Kishen V <kishen.viswanathan@ibm.com>
1 parent 151c79a commit 524a3b3

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

pkg/cpu/cpu_linux_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
package cpu_test
88

99
import (
10+
"bytes"
11+
"io"
1012
"os"
1113
"path/filepath"
14+
"strings"
1215
"testing"
1316

1417
"github.com/jaypipes/ghw/pkg/cpu"
1518
"github.com/jaypipes/ghw/pkg/option"
19+
"github.com/jaypipes/ghw/pkg/topology"
1620
"github.com/jaypipes/ghw/testdata"
1721
)
1822

@@ -71,3 +75,84 @@ func TestArmCPU(t *testing.T) {
7175
}
7276
}
7377
}
78+
79+
func TestCheckCPUTopologyFilesForOfflineCPU(t *testing.T) {
80+
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_CPU"); ok {
81+
t.Skip("Skipping CPU tests.")
82+
}
83+
84+
testdataPath, err := testdata.SnapshotsDirectory()
85+
if err != nil {
86+
t.Fatalf("Expected nil err, but got %v", err)
87+
}
88+
89+
offlineCPUSnapshot := filepath.Join(testdataPath, "linux-amd64-offlineCPUs.tar.gz")
90+
91+
// Capture stderr
92+
rErr, wErr, err := os.Pipe()
93+
if err != nil {
94+
t.Fatalf("Cannot pipe StdErr. %v", err)
95+
}
96+
os.Stderr = wErr
97+
98+
info, err := cpu.New(option.WithSnapshot(option.SnapshotOptions{
99+
Path: offlineCPUSnapshot,
100+
}))
101+
if err != nil {
102+
t.Fatalf("Expected nil err, but got %v", err)
103+
}
104+
if info == nil {
105+
t.Fatalf("Expected non-nil CPUInfo, but got nil")
106+
}
107+
108+
if len(info.Processors) == 0 {
109+
t.Fatalf("Expected >0 processors but got 0.")
110+
}
111+
wErr.Close()
112+
var bufErr bytes.Buffer
113+
io.Copy(&bufErr, rErr)
114+
errorOutput := bufErr.String()
115+
if strings.Contains(errorOutput, "WARNING: failed to read int from file:") {
116+
t.Fatalf("Unexpected warning related to missing files under topology directory was reported")
117+
}
118+
}
119+
func TestNumCoresAmongOfflineCPUs(t *testing.T) {
120+
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_CPU"); ok {
121+
t.Skip("Skipping CPU tests.")
122+
}
123+
124+
testdataPath, err := testdata.SnapshotsDirectory()
125+
if err != nil {
126+
t.Fatalf("Expected nil err, but got %v", err)
127+
}
128+
129+
offlineCPUSnapshot := filepath.Join(testdataPath, "linux-amd64-offlineCPUs.tar.gz")
130+
131+
// Capture stderr
132+
rErr, wErr, err := os.Pipe()
133+
if err != nil {
134+
t.Fatalf("Cannot pipe the StdErr. %v", err)
135+
}
136+
info, err := topology.New(option.WithSnapshot(option.SnapshotOptions{
137+
Path: offlineCPUSnapshot,
138+
}))
139+
if err != nil {
140+
t.Fatalf("Error determining node topology. %v", err)
141+
}
142+
143+
if len(info.Nodes) < 1 {
144+
t.Fatal("No nodes found. Must contain one or more nodes")
145+
}
146+
for _, node := range info.Nodes {
147+
if len(node.Cores) < 1 {
148+
t.Fatal("No cores found. Must contain one or more cores")
149+
}
150+
}
151+
wErr.Close()
152+
var bufErr bytes.Buffer
153+
io.Copy(&bufErr, rErr)
154+
errorOutput := bufErr.String()
155+
if strings.Contains(errorOutput, "WARNING: failed to read int from file:") {
156+
t.Fatalf("Unexpected warnings related to missing files under topology directory was raised")
157+
}
158+
}
Binary file not shown.

0 commit comments

Comments
 (0)