(t *testing.T)
| 653 | } |
| 654 | |
| 655 | func TestRuler_RulerGroupLimits(t *testing.T) { |
| 656 | store := newMockRuleStore(make(map[string]rulespb.RuleGroupList), nil) |
| 657 | cfg := defaultRulerConfig(t) |
| 658 | |
| 659 | r := newTestRuler(t, cfg, store, nil) |
| 660 | defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck |
| 661 | |
| 662 | r.limits = &ruleLimits{maxRuleGroups: 1, maxRulesPerRuleGroup: 1} |
| 663 | |
| 664 | a := NewAPI(r, r.store, log.NewNopLogger()) |
| 665 | |
| 666 | tc := []struct { |
| 667 | name string |
| 668 | input string |
| 669 | output string |
| 670 | err error |
| 671 | status int |
| 672 | }{ |
| 673 | { |
| 674 | name: "when pushing the first group within bounds of the limit", |
| 675 | status: 202, |
| 676 | input: ` |
| 677 | name: test_first_group_will_succeed |
| 678 | interval: 15s |
| 679 | rules: |
| 680 | - record: up_rule |
| 681 | expr: up{} |
| 682 | `, |
| 683 | output: "{\"status\":\"success\"}", |
| 684 | }, |
| 685 | { |
| 686 | name: "when exceeding the rule group limit after sending the first group", |
| 687 | status: 400, |
| 688 | input: ` |
| 689 | name: test_second_group_will_fail |
| 690 | interval: 15s |
| 691 | rules: |
| 692 | - record: up_rule |
| 693 | expr: up{} |
| 694 | `, |
| 695 | output: "per-user rule groups limit (limit: 1 actual: 2) exceeded\n", |
| 696 | }, |
| 697 | } |
| 698 | |
| 699 | // define once so the requests build on each other so the number of rules can be tested |
| 700 | router := mux.NewRouter() |
| 701 | router.Path("/api/v1/rules/{namespace}").Methods("POST").HandlerFunc(a.CreateRuleGroup) |
| 702 | |
| 703 | for _, tt := range tc { |
| 704 | t.Run(tt.name, func(t *testing.T) { |
| 705 | // POST |
| 706 | req := requestFor(t, http.MethodPost, "https://localhost:8080/api/v1/rules/namespace", strings.NewReader(tt.input), "user1") |
| 707 | w := httptest.NewRecorder() |
| 708 | |
| 709 | router.ServeHTTP(w, req) |
| 710 | require.Equal(t, tt.status, w.Code) |
| 711 | require.Equal(t, tt.output, w.Body.String()) |
| 712 | }) |
nothing calls this directly
no test coverage detected