package rollout import ( "fmt" "strings" "testing" "time" "github.com/carlosframework/platform/internal/activator" "github.com/carlosframework/platform/internal/rolloutwords" "github.com/carlosframework/platform/internal/status" ) // textHead is the event text up to its first argument the caller does not // know: sentinel is that argument, as the text function was given it. It // keeps the other tests' pins on the wording itself (rolloutwords), so a // copy change re-keys once. func textHead(text, sentinel string) string { head, _, _ := strings.Cut(text, sentinel) return head } // The text functions take reason WORDS (status.Reason*), as the emit sites // give them after reasonWord; each is worded through rolloutwords.Reason and // is not mapped again (reasonWord("exit") would read as a health failure). func TestEventTextsAreTheApprovedOnes(t *testing.T) { cases := map[string][2]string{ "swapping": {textSwapping("v2", activator.CauseRelease), fmt.Sprintf(rolloutwords.EventSwapping, "v2", rolloutwords.Cause("release"))}, "swapped": {textSwapped("v2", 1234*time.Millisecond), fmt.Sprintf(rolloutwords.EventSwapped, "v2", "1.2s")}, "swap failed": {textSwapFailed("v2", "v1", status.ReasonHealth), fmt.Sprintf(rolloutwords.EventSwapFailed, "v2", rolloutwords.Reason(status.ReasonHealth), "v1")}, "down": {textSwapDown("v2"), fmt.Sprintf(rolloutwords.EventHoldDown, "v2")}, "halted": {textCohortHalted("v2", "box-7", status.ReasonSettle), fmt.Sprintf(rolloutwords.EventCohortHalted, "v2", "box-7", rolloutwords.Reason(status.ReasonSettle))}, "fallback": {textFallback("v2", "v1"), fmt.Sprintf(rolloutwords.EventFallbackServed, "v2", "v1")}, "config pending": {textConfigPending(status.ReasonExit, 2*time.Minute), fmt.Sprintf(rolloutwords.EventConfigPending, rolloutwords.Reason(status.ReasonExit), (2 * time.Minute).String())}, "staging refused": {textStagingRefused("v2"), fmt.Sprintf(rolloutwords.EventStagingRefused, "v2")}, } for name, c := range cases { if c[0] != c[1] { t.Errorf("%s: %q, want the approved %q", name, c[0], c[1]) } } } // Every closed reason word is worded as itself: none is folded into another // by a second mapping. func TestEventTextsKeepEachReasonWord(t *testing.T) { for _, w := range []string{status.ReasonHealth, status.ReasonSettle, status.ReasonExit, status.ReasonStaging, status.ReasonReused} { want := rolloutwords.Reason(w) for name, s := range map[string]string{ "swap failed": textSwapFailed("v2", "v1", w), "halted": textCohortHalted("v2", "box-7", w), "config pending": textConfigPending(w, time.Minute), } { if !strings.Contains(s, want) { t.Errorf("%s with reason word %q: %q does not say %q", name, w, s, want) } } } }