| index 9cd5bdcd4..db7f33bcb 100644 |
| --- a/internal/console/alertengine_pass_test.go |
| +++ b/internal/console/alertengine_pass_test.go |
| @@ -3,6 +3,7 @@ package console |
| import ( |
| "context" |
| "encoding/json" |
| + "errors" |
| "fmt" |
| "os" |
| "path/filepath" |
| @@ -12,6 +13,7 @@ import ( |
| "time" |
| |
| "github.com/carlosframework/platform/internal/metrics" |
| + "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/status" |
| ) |
| |
| @@ -178,16 +180,78 @@ func TestEnginePassFiveCycleRequests(t *testing.T) { |
| ef.fleetServices(t, map[string]bool{"keymail": true}) |
| |
| ef.passAt(t, 0) // warm-up: the first reads, and the first heartbeat pass |
| - rec := recordS3(ef.lat, nil) |
| - ef.passesTo(t, time.Minute, 5*time.Minute) |
| + want := map[string]int{"LIST alerts/boxes/": 5, "LIST status/boxes/": 1, "GET status/fleet.json": 1} |
| + for i := 1; i <= alerts; i++ { |
| + want[fmt.Sprintf("GET alerts/boxes/box-%d.json", i)] = 5 |
| + } |
| + for i := 1; i <= heartbeats; i++ { |
| + want[fmt.Sprintf("GET status/boxes/box-%d.json", i)] = 1 |
| + } |
| + ef.fiveCycleRequests(t, "5 idle passes", time.Minute, nil, want) |
| + |
| + // Ruling R-41: a failed conditional read is not read again plainly, and |
| + // a failed listing with times is not listed again plainly. An alert |
| + // object and the status/boxes/ listing fail for five passes: the same |
| + // requests, the failed ones once each. |
| + ef.fiveCycleRequests(t, "5 passes with an alert object and the heartbeat listing failing", 6*time.Minute, |
| + func(q s3Req) bool { |
| + return q.Key == "alerts/boxes/box-1.json" || (q.Key == "" && q.Prefix == alertStatusBoxesPrefix) |
| + }, |
| + map[string]int{"LIST alerts/boxes/": 5, "LIST status/boxes/": 1, "GET status/fleet.json": 1, |
| + "GET alerts/boxes/box-1.json": 5, "GET alerts/boxes/box-2.json": 5, "GET alerts/boxes/box-3.json": 5}) |
| + |
| + // A heartbeat object failing costs its one read a heartbeat pass. |
| + want["GET alerts/boxes/box-1.json"] = 5 |
| + ef.fiveCycleRequests(t, "5 passes with a heartbeat object failing", 11*time.Minute, |
| + func(q s3Req) bool { return q.Key == "status/boxes/box-1.json" }, want) |
| +} |
| + |
| +// unsupportedReads is a store whose conditional read and timed listing |
| +// answer errors.ErrUnsupported, as a wrapper over a store without them |
| +// does: a missing capability, not a failure. |
| +type unsupportedReads struct{ *release.DirStore } |
| + |
| +func (unsupportedReads) GetIfChanged(context.Context, string, string) ([]byte, string, bool, error) { |
| + return nil, "", false, errors.ErrUnsupported |
| +} |
| + |
| +func (unsupportedReads) ListMod(context.Context, string) ([]release.KeyMod, error) { |
| + return nil, errors.ErrUnsupported |
| +} |
| |
| +// Ruling R-41: a store without the conditional read or the timed listing |
| +// is still read, plainly; only a failure is not retried. |
| +func TestEngineReadsUnsupportedFallsBackToPlain(t *testing.T) { |
| + ctx := context.Background() |
| + store := unsupportedReads{release.NewDirStore(t.TempDir())} |
| + if _, err := store.Put(ctx, "status/boxes/box-1.json", []byte(`{}`), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + var r engineReads |
| + body, changed, err := r.get(ctx, store, "status/boxes/box-1.json") |
| + if err != nil || !changed || string(body) != `{}` { |
| + t.Fatalf("get = %q, %v, %v; want the object read plainly", body, changed, err) |
| + } |
| + keys, err := engineBoxKeyList(ctx, store, alertT0) |
| + if err != nil || len(keys) != 1 { |
| + t.Fatalf("listing = %v, %v; want the object listed plainly", keys, err) |
| + } |
| +} |
| + |
| +// fiveCycleRequests runs passes a minute apart from +from to +from+4m with |
| +// requests matching fail refused, and checks every bucket request they made |
| +// against want: each GET conditional, LISTs by prefix. |
| +func (ef *engineFixture) fiveCycleRequests(t *testing.T, what string, from time.Duration, fail func(s3Req) bool, want map[string]int) { |
| + t.Helper() |
| + rec := recordS3(ef.lat, fail) |
| + ef.passesTo(t, from, from+4*time.Minute) |
| got := map[string]int{} |
| for _, q := range rec.all() { |
| switch { |
| case q.Key == "" && q.Method == "GET": |
| got["LIST "+q.Prefix]++ |
| case q.Method == "GET" && q.IfNoneMatch == "": |
| - t.Errorf("unconditional GET %s", q.Key) |
| + t.Errorf("%s: unconditional GET %s", what, q.Key) |
| got["GET "+q.Key]++ |
| case q.Method == "GET": |
| got["GET "+q.Key]++ |
| @@ -195,15 +259,8 @@ func TestEnginePassFiveCycleRequests(t *testing.T) { |
| got[q.Method+" "+q.Key]++ |
| } |
| } |
| - want := map[string]int{"LIST alerts/boxes/": 5, "LIST status/boxes/": 1, "GET status/fleet.json": 1} |
| - for i := 1; i <= alerts; i++ { |
| - want[fmt.Sprintf("GET alerts/boxes/box-%d.json", i)] = 5 |
| - } |
| - for i := 1; i <= heartbeats; i++ { |
| - want[fmt.Sprintf("GET status/boxes/box-%d.json", i)] = 1 |
| - } |
| if fmt.Sprint(got) != fmt.Sprint(want) { |
| - t.Fatalf("requests over 5 idle passes:\n got %v\nwant %v", got, want) |
| + t.Fatalf("requests over %s:\n got %v\nwant %v", what, got, want) |
| } |
| } |
| |