blob: 7f7f6decd07a5895e10a8f563f14c900a7db901a [file] [log] [blame]
Lorenz Brunee17d832022-10-18 12:02:45 +00001package gpt
2
3import (
4 "os"
5 "testing"
6
7 "github.com/google/uuid"
Lorenz Brunad131882023-06-28 16:42:20 +02008
9 "source.monogon.dev/metropolis/pkg/blockdev"
Lorenz Brunee17d832022-10-18 12:02:45 +000010)
11
12var testUUID = uuid.MustParse("85c0b60f-caf9-40dd-86fa-f8797e26238d")
13
14func TestKernelInterop(t *testing.T) {
15 if os.Getenv("IN_KTEST") != "true" {
16 t.Skip("Not in ktest")
17 }
Lorenz Brunad131882023-06-28 16:42:20 +020018 ram0, err := blockdev.Open("/dev/ram0")
Lorenz Brunee17d832022-10-18 12:02:45 +000019 if err != nil {
20 panic(err)
21 }
22 g := Table{
Lorenz Brunad131882023-06-28 16:42:20 +020023 ID: uuid.NewSHA1(testUUID, []byte("test")),
24 BootCode: []byte("just some test code"),
Lorenz Brunee17d832022-10-18 12:02:45 +000025 Partitions: []*Partition{
26 nil,
27 // This emoji is very complex and exercises UTF16 surrogate encoding
28 // and composing.
29 {Name: "Test 🏃‍♂️", FirstBlock: 10, LastBlock: 19, Type: PartitionTypeEFISystem, ID: uuid.NewSHA1(testUUID, []byte("test1")), Attributes: AttrNoBlockIOProto},
30 nil,
31 {Name: "Test2", FirstBlock: 20, LastBlock: 49, Type: PartitionTypeEFISystem, ID: uuid.NewSHA1(testUUID, []byte("test2")), Attributes: 0},
32 },
Lorenz Brunad131882023-06-28 16:42:20 +020033 b: ram0,
Lorenz Brunee17d832022-10-18 12:02:45 +000034 }
Lorenz Brunad131882023-06-28 16:42:20 +020035 if err := g.Write(); err != nil {
Lorenz Brunee17d832022-10-18 12:02:45 +000036 t.Fatalf("Failed to write GPT: %v", err)
37 }
Lorenz Brunad131882023-06-28 16:42:20 +020038 if err := ram0.RefreshPartitionTable(); err != nil {
39 t.Fatalf("Failed to refresh partition table: %v", err)
Lorenz Brunee17d832022-10-18 12:02:45 +000040 }
41 if _, err := os.Stat("/sys/block/ram0/ram0p2"); err != nil {
42 t.Errorf("Expected ram0p2 to exist, got %v", err)
43 }
44 if _, err := os.Stat("/sys/block/ram0/ram0p4"); err != nil {
45 t.Errorf("Expected ram0p4 to exist, got %v", err)
46 }
47 if _, err := os.Stat("/sys/block/ram0/ram0p1"); err == nil {
48 t.Error("Expected ram0p1 not to exist, but it exists")
49 }
50}