package libraryfreeze import ( "context" "os" "strings" "testing" ) type fixtureControl struct { services []Service calls []string } func (c *fixtureControl) Inventory(context.Context) ([]Service, error) { return append([]Service(nil), c.services...), nil } func (c *fixtureControl) Stop(_ context.Context, id string) error { c.calls = append(c.calls, "stop:"+id) for i := range c.services { if c.services[i].ID == id { c.services[i].Running = false c.services[i].Healthy = false } } return nil } func (c *fixtureControl) Start(_ context.Context, id string) error { c.calls = append(c.calls, "start:"+id) for i := range c.services { if c.services[i].ID == id { c.services[i].Running = true c.services[i].Healthy = true } } return nil } func freezeFixture(t *testing.T) (string, *fixtureControl, *Coordinator) { t.Helper() root := t.TempDir() if err := os.Chmod(root, 0700); err != nil { t.Fatal(err) } c := &fixtureControl{services: []Service{{ID: "app-a", Running: true, Healthy: true}, {ID: "app-b"}}} engine, err := Open(root, "test-source", c) if err != nil { t.Fatal(err) } t.Cleanup(func() { engine.Close() }) return root, c, engine } func TestFreezeRestoresOnlyServicesOriginallyRunning(t *testing.T) { _, c, e := freezeFixture(t) captures := 0 result, err := e.Run(context.Background(), "job-a", func(ctx context.Context) (string, error) { captures++ for _, s := range c.services { if s.Running { t.Fatal("capture started while writer running") } } return strings.Repeat("a", 64), nil }) if err != nil { t.Fatal(err) } if result.State != "succeeded" || result.SnapshotID != strings.Repeat("a", 64) || captures != 1 { t.Fatalf("result=%+v captures=%d", result, captures) } if strings.Join(c.calls, ",") != "stop:app-a,start:app-a" { t.Fatalf("calls=%v", c.calls) } replay, err := e.Run(context.Background(), "job-a", func(context.Context) (string, error) { t.Fatal("replay captured again"); return "", nil }) if err != nil || replay.SnapshotID != result.SnapshotID { t.Fatalf("replay=%+v err=%v", replay, err) } }