| 1 | package activator |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/instancekey" |
| 12 | "github.com/carlosframework/platform/internal/registry" |
| 13 | ) |
| 14 | |
| 15 | // Ruling 22 (R-C): 204 means the record is durable on disk. A nested |
| 16 | // record whose directory sync fails answers 500 and publishes nothing; the |
| 17 | // next report writes it and publishes it. |
| 18 | func TestInstanceKeyAdoptionIsDurableBeforeItAnswers(t *testing.T) { |
| 19 | tok := strings.Repeat("f", 64) |
| 20 | key := strings.Repeat("K", 43) + "=" |
| 21 | rt := registry.Route{Host: "shop.example.com", Kind: "instance"} |
| 22 | f := newAdoptionFixture(t, rt, tok, key, instancekey.SourcePlatform) |
| 23 | f.launch.Beneath = true |
| 24 | adminTokenMu.Lock() |
| 25 | old := adoptionRecordSync |
| 26 | adoptionRecordSync = func(string) error { return errors.New("injected: EIO") } |
| 27 | adminTokenMu.Unlock() |
| 28 | t.Cleanup(func() { // whatever fails below, the next test gets the real sync |
| 29 | adminTokenMu.Lock() |
| 30 | adoptionRecordSync = old |
| 31 | adminTokenMu.Unlock() |
| 32 | }) |
| 33 | if got := f.put(tok, adoptionBody(key, "platform")); got != http.StatusInternalServerError { |
| 34 | t.Fatalf("a failing directory sync: %d, want 500", got) |
| 35 | } |
| 36 | if len(f.published) != 0 { |
| 37 | t.Fatal("a record that is not durable was published") |
| 38 | } |
| 39 | adminTokenMu.Lock() |
| 40 | adoptionRecordSync = old |
| 41 | adminTokenMu.Unlock() |
| 42 | if got := f.put(tok, adoptionBody(key, "platform")); got != http.StatusNoContent { |
| 43 | t.Fatalf("retry: %d, want 204", got) |
| 44 | } |
| 45 | rec, err := os.ReadFile(filepath.Join(f.launch.Dir, rt.Host+instancekey.AdoptionSuffix)) |
| 46 | if err != nil || len(f.published) != 1 || string(rec) != string(f.published[0]) { |
| 47 | t.Fatalf("after the retry: record %d bytes (%v), %d published", len(rec), err, len(f.published)) |
| 48 | } |
| 49 | } |
| 50 | |