|
7 | 7 | package cpu_test
|
8 | 8 |
|
9 | 9 | import (
|
| 10 | + "bytes" |
| 11 | + "io" |
10 | 12 | "os"
|
11 | 13 | "path/filepath"
|
| 14 | + "strings" |
12 | 15 | "testing"
|
13 | 16 |
|
14 | 17 | "github.com/jaypipes/ghw/pkg/cpu"
|
15 | 18 | "github.com/jaypipes/ghw/pkg/option"
|
| 19 | + "github.com/jaypipes/ghw/pkg/topology" |
16 | 20 | "github.com/jaypipes/ghw/testdata"
|
17 | 21 | )
|
18 | 22 |
|
@@ -71,3 +75,84 @@ func TestArmCPU(t *testing.T) {
|
71 | 75 | }
|
72 | 76 | }
|
73 | 77 | }
|
| 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 | +} |
0 commit comments