Lorenz Brun | 6294854 | 2023-01-10 13:28:44 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | |
| 9 | "source.monogon.dev/cloud/agent/api" |
| 10 | ) |
| 11 | |
| 12 | func TestParseCpuinfoAMD64(t *testing.T) { |
| 13 | var cases = []struct { |
| 14 | name string |
| 15 | data string |
| 16 | expected *api.CPU |
| 17 | }{ |
| 18 | { |
| 19 | "QEMUSingleCore", |
| 20 | "cpuinfo_qemu_virtual.txt", |
| 21 | &api.CPU{ |
| 22 | Vendor: "GenuineIntel", |
| 23 | Model: "QEMU Virtual CPU version 2.1.0", |
| 24 | Cores: 1, |
| 25 | HardwareThreads: 1, |
| 26 | Architecture: &api.CPU_X86_64_{X86_64: &api.CPU_X86_64{ |
| 27 | Family: 6, |
| 28 | Model: 6, |
| 29 | Stepping: 3, |
| 30 | }}, |
| 31 | }, |
| 32 | }, |
| 33 | { |
| 34 | "AMDEpyc7402P", |
| 35 | "cpuinfo_amd_7402p.txt", |
| 36 | &api.CPU{ |
| 37 | Vendor: "AuthenticAMD", |
| 38 | Model: "AMD EPYC 7402P 24-Core Processor", |
| 39 | Cores: 24, |
| 40 | HardwareThreads: 48, |
| 41 | Architecture: &api.CPU_X86_64_{X86_64: &api.CPU_X86_64{ |
| 42 | Family: 23, |
| 43 | Model: 49, |
| 44 | Stepping: 0, |
| 45 | }}, |
| 46 | }, |
| 47 | }, |
| 48 | { |
| 49 | "Intel12900K", |
| 50 | "cpuinfo_intel_12900k.txt", |
| 51 | &api.CPU{ |
| 52 | Vendor: "GenuineIntel", |
| 53 | Model: "12th Gen Intel(R) Core(TM) i9-12900K", |
| 54 | Cores: 16, |
| 55 | HardwareThreads: 24, |
| 56 | Architecture: &api.CPU_X86_64_{X86_64: &api.CPU_X86_64{ |
| 57 | Family: 6, |
| 58 | Model: 151, |
| 59 | Stepping: 2, |
| 60 | }}, |
| 61 | }, |
| 62 | }, |
| 63 | } |
| 64 | for _, c := range cases { |
| 65 | t.Run(c.name, func(t *testing.T) { |
| 66 | rawData, err := os.ReadFile("testdata/" + c.data) |
| 67 | if err != nil { |
| 68 | t.Fatalf("unable to read testdata file: %v", err) |
| 69 | } |
| 70 | res, errs := parseCpuinfoAMD64(rawData) |
| 71 | if len(errs) > 0 { |
| 72 | t.Fatal(errs[0]) |
| 73 | } |
| 74 | assert.Equal(t, c.expected.Vendor, res.Vendor, "vendor mismatch") |
| 75 | assert.Equal(t, c.expected.Model, res.Model, "model mismatch") |
| 76 | assert.Equal(t, c.expected.Cores, res.Cores, "cores mismatch") |
| 77 | assert.Equal(t, c.expected.HardwareThreads, res.HardwareThreads, "hardware threads mismatch") |
| 78 | x86_64, ok := res.Architecture.(*api.CPU_X86_64_) |
| 79 | if !ok { |
| 80 | t.Fatal("CPU architecture not X86_64") |
| 81 | } |
| 82 | expectedX86_64 := c.expected.Architecture.(*api.CPU_X86_64_) |
| 83 | assert.Equal(t, expectedX86_64.X86_64.Family, x86_64.X86_64.Family, "x86_64 family mismatch") |
| 84 | assert.Equal(t, expectedX86_64.X86_64.Model, x86_64.X86_64.Model, "x86_64 model mismatch") |
| 85 | assert.Equal(t, expectedX86_64.X86_64.Stepping, x86_64.X86_64.Stepping, "x86_64 stepping mismatch") |
| 86 | }) |
| 87 | } |
| 88 | } |