|
| 1 | +// Use and distribution licensed under the Apache license version 2. |
| 2 | +// |
| 3 | +// See the COPYING file in the root project directory for full text. |
| 4 | +// |
| 5 | + |
| 6 | +package chassis_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/jaypipes/ghw" |
| 14 | + "github.com/jaypipes/ghw/pkg/chassis" |
| 15 | + "github.com/jaypipes/ghw/pkg/util" |
| 16 | +) |
| 17 | + |
| 18 | +// writeFiles writes name->content files (names may be nested subpaths) rooted at |
| 19 | +// dir, creating parent directories as needed. |
| 20 | +func writeFiles(t *testing.T, dir string, files map[string]string) { |
| 21 | + t.Helper() |
| 22 | + for name, content := range files { |
| 23 | + path := filepath.Join(dir, name) |
| 24 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// deviceTreeChroot builds a chroot whose DeviceTree base holds the given |
| 34 | +// properties and which has no DMI, so the DeviceTree fallback is exercised. |
| 35 | +func deviceTreeChroot(t *testing.T, props map[string]string) string { |
| 36 | + t.Helper() |
| 37 | + root := t.TempDir() |
| 38 | + writeFiles(t, filepath.Join(root, "sys", "firmware", "devicetree", "base"), props) |
| 39 | + return root |
| 40 | +} |
| 41 | + |
| 42 | +func TestChassisDeviceTreeRPi(t *testing.T) { |
| 43 | + root := deviceTreeChroot(t, map[string]string{ |
| 44 | + "model": "Raspberry Pi 4 Model B Rev 1.4\x00", |
| 45 | + "compatible": "raspberrypi,4-model-b\x00brcm,bcm2711\x00", |
| 46 | + "serial-number": "10000000196f8c53\x00", |
| 47 | + }) |
| 48 | + |
| 49 | + info, err := chassis.New(ghw.WithChroot(root)) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + if got, want := info.Vendor, "raspberrypi"; got != want { |
| 54 | + t.Errorf("vendor: got %q, want %q", got, want) |
| 55 | + } |
| 56 | + if got, want := info.Version, "Raspberry Pi 4 Model B Rev 1.4"; got != want { |
| 57 | + t.Errorf("version: got %q, want %q", got, want) |
| 58 | + } |
| 59 | + if got, want := info.SerialNumber, "10000000196f8c53"; got != want { |
| 60 | + t.Errorf("serial: got %q, want %q", got, want) |
| 61 | + } |
| 62 | + if got := info.AssetTag; got != util.UNKNOWN { |
| 63 | + t.Errorf("asset tag: got %q, want %q", got, util.UNKNOWN) |
| 64 | + } |
| 65 | + // No chassis-type on the Pi -> Type/TypeDescription unknown. |
| 66 | + if got := info.Type; got != util.UNKNOWN { |
| 67 | + t.Errorf("type: got %q, want %q", got, util.UNKNOWN) |
| 68 | + } |
| 69 | + if got := info.TypeDescription; got != util.UNKNOWN { |
| 70 | + t.Errorf("type description: got %q, want %q", got, util.UNKNOWN) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestChassisDeviceTreeChassisType(t *testing.T) { |
| 75 | + // radxa NIO 12L: has chassis-type, no serial-number. |
| 76 | + root := deviceTreeChroot(t, map[string]string{ |
| 77 | + "model": "Radxa NIO 12L\x00", |
| 78 | + "compatible": "radxa,nio-12l\x00mediatek,mt8395\x00", |
| 79 | + "chassis-type": "embedded\x00", |
| 80 | + }) |
| 81 | + |
| 82 | + info, err := chassis.New(ghw.WithChroot(root)) |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + if got, want := info.Vendor, "radxa"; got != want { |
| 87 | + t.Errorf("vendor: got %q, want %q", got, want) |
| 88 | + } |
| 89 | + if got, want := info.Type, "34"; got != want { |
| 90 | + t.Errorf("type: got %q, want %q", got, want) |
| 91 | + } |
| 92 | + if got, want := info.TypeDescription, "Embedded PC"; got != want { |
| 93 | + t.Errorf("type description: got %q, want %q", got, want) |
| 94 | + } |
| 95 | + if got := info.SerialNumber; got != util.UNKNOWN { |
| 96 | + t.Errorf("serial: got %q, want %q", got, util.UNKNOWN) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestChassisDMITakesPrecedence(t *testing.T) { |
| 101 | + root := t.TempDir() |
| 102 | + // Both DMI and DeviceTree present: DMI must win. |
| 103 | + writeFiles(t, filepath.Join(root, "sys", "class", "dmi", "id"), map[string]string{ |
| 104 | + "chassis_vendor": "Acme Corp\n", |
| 105 | + "chassis_type": "3\n", |
| 106 | + "chassis_serial": "DMI-SERIAL\n", |
| 107 | + "chassis_version": "1.0\n", |
| 108 | + }) |
| 109 | + writeFiles(t, filepath.Join(root, "sys", "firmware", "devicetree", "base"), map[string]string{ |
| 110 | + "compatible": "raspberrypi,4-model-b\x00", |
| 111 | + "serial-number": "DT-SERIAL\x00", |
| 112 | + }) |
| 113 | + |
| 114 | + info, err := chassis.New(ghw.WithChroot(root)) |
| 115 | + if err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + if got, want := info.Vendor, "Acme Corp"; got != want { |
| 119 | + t.Errorf("vendor: got %q, want %q (DMI should take precedence)", got, want) |
| 120 | + } |
| 121 | + if got, want := info.SerialNumber, "DMI-SERIAL"; got != want { |
| 122 | + t.Errorf("serial: got %q, want %q (DMI should take precedence)", got, want) |
| 123 | + } |
| 124 | + if got, want := info.TypeDescription, "Desktop"; got != want { |
| 125 | + t.Errorf("type description: got %q, want %q", got, want) |
| 126 | + } |
| 127 | +} |
0 commit comments