-
Notifications
You must be signed in to change notification settings - Fork 69
CNV-87532: router: add GET /rules endpoint #1172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sradco
wants to merge
1
commit into
openshift:main-alerts-management-api
Choose a base branch
from
sradco:alert-mgmt-restructured-07-get-rules
base: main-alerts-management-api
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,302
−177
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package managementrouter | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseStateLabelsAndMatchers(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| query string | ||
| wantState string | ||
| wantLabels map[string]string | ||
| wantMatchers []string | ||
| wantMatchersLen int | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "empty query", | ||
| query: "", | ||
| wantState: "", | ||
| wantLabels: map[string]string{}, | ||
| wantMatchers: nil, | ||
| }, | ||
| { | ||
| name: "state only", | ||
| query: "state=firing", | ||
| wantState: "firing", | ||
| wantLabels: map[string]string{}, | ||
| }, | ||
| { | ||
| name: "flat labels only", | ||
| query: "severity=critical&namespace=openshift-monitoring", | ||
| wantState: "", | ||
| wantLabels: map[string]string{ | ||
| "severity": "critical", | ||
| "namespace": "openshift-monitoring", | ||
| }, | ||
| }, | ||
| { | ||
| name: "match[] only with equality", | ||
| query: `match[]=severity="critical"`, | ||
| wantState: "", | ||
| wantLabels: map[string]string{}, | ||
| wantMatchers: []string{ | ||
| `severity="critical"`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "match[] with regex", | ||
| query: `match[]=alertname=~"Kube.*CPU.*"`, | ||
| wantState: "", | ||
| wantLabels: map[string]string{}, | ||
| wantMatchers: []string{ | ||
| `alertname=~"Kube.*CPU.*"`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "multiple match[] values", | ||
| query: `match[]=severity="critical"&match[]=namespace="openshift-monitoring"`, | ||
| wantState: "", | ||
| wantLabels: map[string]string{}, | ||
| wantMatchersLen: 2, | ||
| }, | ||
| { | ||
| name: "mixed flat labels and match[]", | ||
| query: `state=firing&team=sre&match[]=severity=~"critical|warning"`, | ||
| wantState: "firing", | ||
| wantLabels: map[string]string{ | ||
| "team": "sre", | ||
| }, | ||
| wantMatchers: []string{ | ||
| `severity=~"critical|warning"`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "match[] is not treated as a label", | ||
| query: `match[]=severity="critical"`, | ||
| wantState: "", | ||
| wantLabels: map[string]string{}, | ||
| }, | ||
| { | ||
| name: "invalid state", | ||
| query: "state=invalid", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "repeated state values are rejected", | ||
| query: "state=&state=firing", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty match[] values are skipped", | ||
| query: `match[]=&match[]=%20&match[]=severity="warning"`, | ||
| wantState: "", | ||
| wantLabels: map[string]string{}, | ||
| wantMatchersLen: 1, | ||
| }, | ||
| { | ||
| name: "repeated label values are rejected", | ||
| query: "severity=critical&severity=warning", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "repeated label with leading empty value is rejected", | ||
| query: "namespace=&namespace=ns1", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "invalid match[] is rejected", | ||
| query: `match[]=severity=`, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| q, err := url.ParseQuery(tt.query) | ||
| if err != nil { | ||
| t.Fatalf("invalid test query: %v", err) | ||
| } | ||
|
|
||
| state, labels, matchers, err := parseStateLabelsAndMatchers(q) | ||
| if tt.wantErr { | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if state != tt.wantState { | ||
| t.Errorf("state = %q, want %q", state, tt.wantState) | ||
| } | ||
|
|
||
| if tt.wantLabels != nil { | ||
| if len(labels) != len(tt.wantLabels) { | ||
| t.Errorf("labels length = %d, want %d", len(labels), len(tt.wantLabels)) | ||
| } | ||
| for k, v := range tt.wantLabels { | ||
| if labels[k] != v { | ||
| t.Errorf("labels[%q] = %q, want %q", k, labels[k], v) | ||
| } | ||
| } | ||
| if _, found := labels["match[]"]; found { | ||
| t.Error("match[] should not appear in labels map") | ||
| } | ||
| } | ||
|
|
||
| if tt.wantMatchers != nil { | ||
| if len(matchers) != len(tt.wantMatchers) { | ||
| t.Errorf("matchers length = %d, want %d", len(matchers), len(tt.wantMatchers)) | ||
| } | ||
| for i, want := range tt.wantMatchers { | ||
| if i < len(matchers) && matchers[i] != want { | ||
| t.Errorf("matchers[%d] = %q, want %q", i, matchers[i], want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if tt.wantMatchersLen > 0 && len(matchers) != tt.wantMatchersLen { | ||
| t.Errorf("matchers length = %d, want %d", len(matchers), tt.wantMatchersLen) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestParseStateAndLabelsBackcompat(t *testing.T) { | ||
| q, _ := url.ParseQuery(`state=firing&severity=critical&match[]=alertname=~"Foo.*"`) | ||
|
|
||
| state, labels, err := parseStateAndLabels(q) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if state != "firing" { | ||
| t.Errorf("state = %q, want %q", state, "firing") | ||
| } | ||
| if labels["severity"] != "critical" { | ||
| t.Errorf("severity = %q, want %q", labels["severity"], "critical") | ||
| } | ||
| if _, found := labels["match[]"]; found { | ||
| t.Error("match[] should not appear in labels map") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.