package manager_test import ( "bytes" "context" "errors" "testing" "slackerbot/internal/manager" "slackerbot/internal/model" "slackerbot/internal/node" "slackerbot/internal/testutil" "slackerbot/internal/testutil/clustertest" ) func TestMoveLiveContainer(t *testing.T) { c := clustertest.New(t) c.AddOwner(t, testutil.HomeData(), true) var phases []manager.MovePhase res, err := c.Mgr.Migrate(c.Ctx, "C1", manager.MoveOptions{ Target: "slave-1", WaitIdle: func(context.Context) { c.Master.Mu.Lock() c.Master.Homes["/d/c-c1"] = append(c.Master.Homes["/d/c-c1"], []byte("written during the bulk copy")...) c.Master.Mu.Unlock() }, Progress: func(p manager.MoveProgress) { if len(phases) == 0 || phases[len(phases)-1] != p.Phase { phases = append(phases, p.Phase) } }, }) if err != nil { t.Fatal(err) } want := append(testutil.HomeData(), []byte("written during the bulk copy")...) if !bytes.Equal(c.Slave.Homes["/d/c-c1"], want) { t.Fatal("the late write didn't make it to the new node") } if c.Slave.Passes != 2 { t.Fatalf("a live move should copy twice (bulk + final sync), got %d", c.Slave.Passes) } if _, still := c.Master.Homes["/d/c-c1"]; still { t.Fatal("old home should be deleted after a good move") } if !c.Slave.IsRunning("c-c1") || c.Master.IsRunning("c-c1") || !res.Started { t.Fatal("container should be running on the new node only") } if u := c.Store.Owner("C1"); u.Node != "slave-1" || u.Drive != "/d" { t.Fatalf("placement not switched: %+v", u) } if len(c.Master.Exports) != 0 { t.Fatal("snapshots on the old node should be cleaned up") } wantPhases := []manager.MovePhase{manager.PhaseCopy, manager.PhaseFinal, manager.PhaseCleanup, manager.PhaseStart} if len(phases) != len(wantPhases) { t.Fatalf("phases %v, want %v", phases, wantPhases) } } func TestMoveAutoStoppedContainer(t *testing.T) { c := clustertest.New(t) c.AddOwner(t, testutil.HomeData(), false) res, err := c.Mgr.Migrate(c.Ctx, "C1", manager.MoveOptions{}) if err != nil { t.Fatal(err) } if res.To != "slave-1" || res.Started || c.Slave.IsRunning("c-c1") { t.Fatalf("a stopped container should move without starting: %+v", res) } if c.Slave.Passes != 1 { t.Fatalf("a stopped container needs one copy, got %d", c.Slave.Passes) } } func TestMoveFullNodeStartsElsewhere(t *testing.T) { c := clustertest.New(t) c.AddOwner(t, testutil.HomeData(), false) c.Master.Mu.Lock() c.Master.Usable = model.Size{MemoryMB: 512, CPUs: 0.5} c.Master.Mu.Unlock() _, err := c.Mgr.Ensure(c.Ctx, "C1", model.OwnerChannel) var full *node.NodeFullError if !errors.As(err, &full) || full.Node != "master" { t.Fatalf("want node full on master, got %v", err) } res, err := c.Mgr.Migrate(c.Ctx, "C1", manager.MoveOptions{StartAfter: true}) if err != nil || !res.Started || !c.Slave.IsRunning("c-c1") { t.Fatalf("auto-move should start it on the new node: %+v %v", res, err) } } func TestMoveFailureRollsBack(t *testing.T) { c := clustertest.New(t) c.AddOwner(t, testutil.HomeData(), true) _, err := c.Mgr.Migrate(c.Ctx, "C1", manager.MoveOptions{ Target: "slave-1", WaitIdle: func(context.Context) { c.Slave.Mu.Lock() c.Slave.FailWrites = true c.Slave.Mu.Unlock() }, }) if err == nil { t.Fatal("expected the final sync to fail") } if u := c.Store.Owner("C1"); u.Node != "master" { t.Fatalf("container should stay on its old node, got %s", u.Node) } if !c.Master.IsRunning("c-c1") { t.Fatal("container should be restarted on its old node") } if _, ok := c.Master.Homes["/d/c-c1"]; !ok { t.Fatal("old home must survive a failed move") } if _, ok := c.Slave.Homes["/d/c-c1"]; ok || len(c.Slave.Imports) != 0 { t.Fatal("half-copied home on the target should be cleaned up") } } func TestMoveRefusals(t *testing.T) { c := clustertest.New(t) c.AddOwner(t, testutil.HomeData(), false) if _, err := c.Mgr.Migrate(c.Ctx, "C1", manager.MoveOptions{Target: "master"}); !errors.Is(err, manager.ErrSameNode) { t.Fatalf("same node: %v", err) } var unknown *manager.UnknownNodeError if _, err := c.Mgr.Migrate(c.Ctx, "C1", manager.MoveOptions{Target: "nope"}); !errors.As(err, &unknown) { t.Fatalf("unknown node: %v", err) } if _, err := c.Mgr.Migrate(c.Ctx, "nobody", manager.MoveOptions{}); !errors.Is(err, manager.ErrNoContainer) { t.Fatalf("no container: %v", err) } c.Slave.Mu.Lock() c.Slave.Usable = model.Size{MemoryMB: 512, CPUs: 0.5} c.Slave.Mu.Unlock() c.Master.Mu.Lock() c.Master.Runs["c-c1"] = true c.Master.Mu.Unlock() testutil.WaitFor(t, "slave report", func() bool { views, _ := c.Mgr.Views(c.Ctx) for _, v := range views { if v.Name == "slave-1" { return v.Usable.MemoryMB == 512 } } return false }) var full *node.NodeFullError if _, err := c.Mgr.Migrate(c.Ctx, "C1", manager.MoveOptions{Target: "slave-1"}); !errors.As(err, &full) { t.Fatalf("full target: %v", err) } }