(t *testing.T)
| 714 | } |
| 715 | |
| 716 | func TestRuler_ProtoToRuleGroupYamlConvertion(t *testing.T) { |
| 717 | store := newMockRuleStore(make(map[string]rulespb.RuleGroupList), nil) |
| 718 | cfg := defaultRulerConfig(t) |
| 719 | |
| 720 | r := newTestRuler(t, cfg, store, nil) |
| 721 | defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck |
| 722 | |
| 723 | a := NewAPI(r, r.store, log.NewNopLogger()) |
| 724 | |
| 725 | tc := []struct { |
| 726 | name string |
| 727 | input string |
| 728 | output string |
| 729 | err error |
| 730 | status int |
| 731 | }{ |
| 732 | { |
| 733 | name: "when pushing group that can be safely converted from RuleGroupDesc to RuleGroup yaml", |
| 734 | status: 202, |
| 735 | input: ` |
| 736 | name: test_first_group_will_succeed |
| 737 | interval: 15s |
| 738 | rules: |
| 739 | - record: up_rule |
| 740 | expr: |2+ |
| 741 | up{} |
| 742 | `, |
| 743 | output: "{\"status\":\"success\"}", |
| 744 | }, |
| 745 | { |
| 746 | name: "when pushing group that CANNOT be safely converted from RuleGroupDesc to RuleGroup yaml", |
| 747 | status: 400, |
| 748 | input: ` |
| 749 | name: test_first_group_will_succeed |
| 750 | interval: 15s |
| 751 | rules: |
| 752 | - record: up_rule |
| 753 | expr: |2+ |
| 754 | |
| 755 | up{} |
| 756 | `, |
| 757 | output: "unable to decoded rule group\n", |
| 758 | }, |
| 759 | } |
| 760 | |
| 761 | router := mux.NewRouter() |
| 762 | router.Path("/api/v1/rules/{namespace}").Methods("POST").HandlerFunc(a.CreateRuleGroup) |
| 763 | |
| 764 | for _, tt := range tc { |
| 765 | t.Run(tt.name, func(t *testing.T) { |
| 766 | // POST |
| 767 | req := requestFor(t, http.MethodPost, "https://localhost:8080/api/v1/rules/namespace", strings.NewReader(tt.input), "user1") |
| 768 | w := httptest.NewRecorder() |
| 769 | |
| 770 | router.ServeHTTP(w, req) |
| 771 | require.Equal(t, tt.status, w.Code) |
| 772 | require.Equal(t, tt.output, w.Body.String()) |
| 773 | }) |
nothing calls this directly
no test coverage detected