| 1 | package hostfloor |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestPlan14ParticipationFlagOffFloor(t *testing.T) { |
| 10 | t.Setenv("CARLOS_HOST_CLAIM", "") |
| 11 | for _, mirror := range []string{"absent", "empty", "pending", "symlink"} { |
| 12 | t.Run(mirror, func(t *testing.T) { |
| 13 | d := Disk{Dir: t.TempDir(), Participation: floorParticipationFixture(t)} |
| 14 | if err := d.Participation.State.Remove("census.json"); err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | d.Participation.VerifyLaunches = nil |
| 18 | data := t.TempDir() |
| 19 | dir := filepath.Join(data, ".hostexec") |
| 20 | if mirror == "empty" || mirror == "pending" { |
| 21 | if err := os.Mkdir(dir, 0700); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | } |
| 25 | if mirror == "pending" { |
| 26 | if err := os.WriteFile(filepath.Join(dir, "pending.json"), []byte(`{}`), 0600); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | } |
| 30 | if mirror == "symlink" { |
| 31 | if err := os.Symlink(t.TempDir(), dir); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | } |
| 35 | for _, strict := range []bool{false, true} { |
| 36 | want := mirror == "empty" || (!strict && mirror == "absent") |
| 37 | if err := d.CheckParticipation(t.Context(), data, strict); (err == nil) != want { |
| 38 | t.Fatalf("strict=%t err=%v want admitted=%t", strict, err, want) |
| 39 | } |
| 40 | } |
| 41 | want := mirror == "empty" |
| 42 | if err := d.ControllersIdle(t.Context(), data); (err == nil) != want { |
| 43 | t.Fatalf("idle=%v want admitted=%t", err, want) |
| 44 | } |
| 45 | if err := d.Begin(t.Context(), "legacy-update", data); (err == nil) != want { |
| 46 | t.Fatalf("begin=%v want admitted=%t", err, want) |
| 47 | } |
| 48 | marker := filepath.Join(d.Dir, "update-begun.json") |
| 49 | if _, err := os.Stat(marker); (err == nil) != want { |
| 50 | t.Fatalf("marker=%v", err) |
| 51 | } |
| 52 | if want { |
| 53 | if err := d.EndParticipation(t.Context(), "legacy-update", func() error { return RemoveDurable(marker) }); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if _, err := os.Stat(marker); !os.IsNotExist(err) { |
| 57 | t.Fatalf("completion retained marker: %v", err) |
| 58 | } |
| 59 | } |
| 60 | if _, err := d.Participation.UpdateID(); !os.IsNotExist(err) { |
| 61 | t.Fatalf("legacy floor created root reservation: %v", err) |
| 62 | } |
| 63 | }) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestPlan14ParticipationRetainedCensusFloor(t *testing.T) { |
| 68 | for _, flag := range []string{"", "1"} { |
| 69 | for _, state := range []string{"idle", "pending", "no-launch-proof", "malformed", "missing"} { |
| 70 | t.Run(flag+"/"+state, func(t *testing.T) { |
| 71 | t.Setenv("CARLOS_HOST_CLAIM", flag) |
| 72 | d := Disk{Dir: t.TempDir(), Participation: floorParticipationFixture(t)} |
| 73 | data := t.TempDir() |
| 74 | if err := os.Mkdir(filepath.Join(data, ".hostexec"), 0700); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | switch state { |
| 78 | case "pending": |
| 79 | if err := d.Participation.State.Write("shop.example.json", []byte(`{"Status":"pending"}`)); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | case "no-launch-proof": |
| 83 | d.Participation.VerifyLaunches = nil |
| 84 | case "malformed": |
| 85 | if err := d.Participation.State.Write("census.json", []byte(`{`)); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | case "missing": |
| 89 | if err := d.Participation.State.Remove("census.json"); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | } |
| 93 | want := state == "idle" || (state == "missing" && flag == "") |
| 94 | if err := d.ControllersIdle(t.Context(), data); (err == nil) != want { |
| 95 | t.Fatalf("idle=%v want admitted=%t", err, want) |
| 96 | } |
| 97 | if err := d.Begin(t.Context(), "update", data); (err == nil) != want { |
| 98 | t.Fatalf("begin=%v want admitted=%t", err, want) |
| 99 | } |
| 100 | if state == "idle" { |
| 101 | if err := d.Participation.Acquire(t.Context(), func() error { t.Fatal("acquired across update"); return nil }); err == nil { |
| 102 | t.Fatal("update did not exclude acquisition") |
| 103 | } |
| 104 | if err := d.EndParticipation(t.Context(), "update", nil); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | } |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |