package host import ( "context" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" "time" "github.com/carlosframework/platform/internal/activator" "github.com/carlosframework/platform/internal/hostfiles" "github.com/carlosframework/platform/internal/instancekey" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/release" ) // Ruling 22 (R-C): an adoption is published by the running session's // publisher, with no admission or release after it. The box is then lost: // every process and every local file is gone. The data bucket's manifest // for the session's generation still holds the record, bound to the // session's writer, which is what O2' pins. func TestAdoptionSurvivesBoxLossWithoutAnotherAdmissionOrRelease(t *testing.T) { t.Parallel() ctx, cancel := context.WithCancel(context.Background()) defer cancel() data := release.NewDirStore(t.TempDir()) writer := instancekey.Binding{Host: "shop.example.com", Incarnation: "inc-1", Session: 3, Generation: "g3"} prefix := "instances/" + writer.Host + "/" + writer.Incarnation + "/files/" + writer.Generation + "/" pub, err := hostfiles.Open(ctx, data, prefix) if err != nil { t.Fatal(err) } box := t.TempDir() filesDir, envDir := filepath.Join(box, "files"), filepath.Join(box, "env") for _, d := range []string{filesDir, envDir} { if err := os.MkdirAll(d, 0o700); err != nil { t.Fatal(err) } } // The session's continuous publisher and the adoption relay. running := make(chan error, 2) go func() { running <- hostfiles.Run(ctx, pub, filesDir, hostfiles.Options{Deadline: 40 * time.Millisecond, Scan: time.Minute, Poll: 40 * time.Millisecond}) }() relay := &hostfiles.PlatformRelay{Name: instancekey.AdoptionItem, Stage: pub, Retry: 10 * time.Millisecond} go func() { running <- relay.Run(ctx) }() key := strings.Repeat("K", 43) + "=" tok := strings.Repeat("f", 64) launch := activator.AdoptionLaunch{Writer: writer, KeySHA256: instancekey.Digest(key), Source: instancekey.SourcePlatform, Dir: envDir, Publish: relay.Publish} mux := http.NewServeMux() (&activator.ExecBackend{}).MountInstanceKeyAdoption(mux, registry.Route{Host: writer.Host, Kind: "instance"}, func() (string, error) { return tok, nil }, func() (activator.AdoptionLaunch, bool) { return launch, true }) req := httptest.NewRequest(http.MethodPut, "/instance-key/adoption", strings.NewReader(`{"sha256":"`+instancekey.Digest(key)+`","source":"platform"}`)) req.Header.Set("Authorization", "Bearer "+tok) w := httptest.NewRecorder() mux.ServeHTTP(w, req) if w.Code != http.StatusNoContent { t.Fatalf("adoption = %d, want 204", w.Code) } // Within the publisher's continuous bound the record is committed. readRecord := func() ([]byte, error) { raw, _, err := data.Get(context.Background(), hostfiles.ManifestKey(prefix)) if err != nil { return nil, err } var m hostfiles.Manifest if err := json.Unmarshal(raw, &m); err != nil { return nil, err } return hostfiles.ReadPlatform(context.Background(), data, prefix, m, instancekey.AdoptionItem, nil) } deadline := time.Now().Add(2 * time.Second) for { if _, err := readRecord(); err == nil { break } if time.Now().After(deadline) { t.Fatal("the adoption was not published within the continuous bound") } time.Sleep(10 * time.Millisecond) } // Box loss: the processes stop and the disk is gone. cancel() <-running <-running if err := os.RemoveAll(box); err != nil { t.Fatal(err) } rec, err := readRecord() if err != nil { t.Fatalf("after box loss: %v", err) } a, err := instancekey.ParseAdoption(rec) if err != nil || !a.Binds(writer) || a.SHA256 != instancekey.Digest(key) || a.Source != instancekey.SourcePlatform { t.Fatalf("after box loss the bucket holds %+v (%v), want the session's record", a, err) } }