package activator import ( "errors" "net/http" "os" "path/filepath" "strings" "testing" "github.com/carlosframework/platform/internal/instancekey" "github.com/carlosframework/platform/internal/registry" ) // Ruling 22 (R-C): 204 means the record is durable on disk. A nested // record whose directory sync fails answers 500 and publishes nothing; the // next report writes it and publishes it. func TestInstanceKeyAdoptionIsDurableBeforeItAnswers(t *testing.T) { tok := strings.Repeat("f", 64) key := strings.Repeat("K", 43) + "=" rt := registry.Route{Host: "shop.example.com", Kind: "instance"} f := newAdoptionFixture(t, rt, tok, key, instancekey.SourcePlatform) f.launch.Beneath = true adminTokenMu.Lock() old := adoptionRecordSync adoptionRecordSync = func(string) error { return errors.New("injected: EIO") } adminTokenMu.Unlock() t.Cleanup(func() { // whatever fails below, the next test gets the real sync adminTokenMu.Lock() adoptionRecordSync = old adminTokenMu.Unlock() }) if got := f.put(tok, adoptionBody(key, "platform")); got != http.StatusInternalServerError { t.Fatalf("a failing directory sync: %d, want 500", got) } if len(f.published) != 0 { t.Fatal("a record that is not durable was published") } adminTokenMu.Lock() adoptionRecordSync = old adminTokenMu.Unlock() if got := f.put(tok, adoptionBody(key, "platform")); got != http.StatusNoContent { t.Fatalf("retry: %d, want 204", got) } rec, err := os.ReadFile(filepath.Join(f.launch.Dir, rt.Host+instancekey.AdoptionSuffix)) if err != nil || len(f.published) != 1 || string(rec) != string(f.published[0]) { t.Fatalf("after the retry: record %d bytes (%v), %d published", len(rec), err, len(f.published)) } }