@@ -9,19 +9,6 @@ import (
99 "time"
1010)
1111
12- func TestMonitQueryDiagnoseFlags (t * testing.T ) {
13- cmd := newMonitQueryDiagnoseCmd ()
14- for _ , name := range []string {
15- "ds-type" , "ds-name" , "time-start" , "time-end" ,
16- "input-query" , "operation" ,
17- "max-logs" , "max-patterns" , "timeout-seconds" ,
18- } {
19- if cmd .Flags ().Lookup (name ) == nil {
20- t .Errorf ("flag --%s missing" , name )
21- }
22- }
23- }
24-
2512func TestMonitQueryDataFlags (t * testing.T ) {
2613 cmd := newMonitQueryDataCmd ()
2714 for _ , name := range []string {"ds-type" , "ds-name" , "expr" , "args" , "delay-seconds" } {
@@ -31,175 +18,27 @@ func TestMonitQueryDataFlags(t *testing.T) {
3118 }
3219}
3320
34- // --- monit-query diagnose -------------------------------------------------
35-
36- func TestMonitQueryDiagnoseHappyPath (t * testing.T ) {
37- saveAndResetGlobals (t )
38- stub := newGFStub (t )
39- stub .data = map [string ]any {"operation" : "log_patterns" }
40-
41- _ , err := execCommand (
42- "monit-query" , "diagnose" ,
43- "--ds-type" , "victorialogs" ,
44- "--ds-name" , "vl-prod" ,
45- "--input-query" , `{app="api"}` ,
46- "--operation" , "log_patterns" ,
47- "--max-logs" , "5000" ,
48- "--max-patterns" , "10" ,
49- "--timeout-seconds" , "20" ,
50- )
51- if err != nil {
52- t .Fatalf ("unexpected error: %v" , err )
53- }
54- if stub .lastPath != "/monit/query/diagnose" {
55- t .Fatalf ("expected /monit/query/diagnose, got %q" , stub .lastPath )
56- }
57- body := stub .lastBody
58- if body ["ds_type" ] != "victorialogs" || body ["ds_name" ] != "vl-prod" {
59- t .Errorf ("unexpected ds fields: %#v" , body )
60- }
61- input , _ := body ["input" ].(map [string ]any )
62- if input ["query" ] != `{app="api"}` {
63- t .Errorf ("expected input query %q, got %v" , `{app="api"}` , input ["query" ])
64- }
65- if body ["operation" ] != "log_patterns" {
66- t .Errorf ("expected operation log_patterns, got %v" , body ["operation" ])
67- }
68- options , _ := body ["options" ].(map [string ]any )
69- if fmt .Sprint (options ["max_logs_scanned" ]) != "5000" ||
70- fmt .Sprint (options ["max_patterns" ]) != "10" ||
71- fmt .Sprint (options ["timeout_seconds" ]) != "20" {
72- t .Errorf ("unexpected caps: %#v" , options )
73- }
74- timeRange , _ := body ["time_range" ].(map [string ]any )
75- if fmt .Sprint (timeRange ["start" ]) == "0" || fmt .Sprint (timeRange ["start" ]) == "<nil>" ||
76- fmt .Sprint (timeRange ["end" ]) == "0" || fmt .Sprint (timeRange ["end" ]) == "<nil>" {
77- t .Errorf ("expected non-zero default time range, got %#v" , timeRange )
78- }
79- }
80-
81- func TestMonitQueryDiagnoseRendersMetricEvidence (t * testing.T ) {
82- saveAndResetGlobals (t )
83- stub := newGFStub (t )
84- stub .data = map [string ]any {
85- "schema_version" : "2" ,
86- "operation" : "metric_trends" ,
87- "ds_type" : "prometheus" ,
88- "ds_name" : "prod-prometheus" ,
89- "query" : "up" ,
90- "window" : map [string ]any {"start" : "2026-07-14T06:00:00Z" , "end" : "2026-07-14T07:00:00Z" },
91- "results" : []any {map [string ]any {
92- "method" : "window_compare" ,
93- "window" : map [string ]any {"start" : "2026-07-14T06:00:00Z" , "end" : "2026-07-14T07:00:00Z" },
94- "summary" : map [string ]any {
95- "series_total" : 1 , "series_analyzed" : 1 , "selected_series_total" : 1 , "series_returned" : 1 ,
96- "analysis_truncated" : false , "evidence_summary" : "One series changed." ,
97- },
98- "series_evidence" : []any {map [string ]any {
99- "labels" : map [string ]any {"instance" : "api-1" },
100- "observations" : []any {"The current average increased." },
101- }},
102- "warnings" : []any {},
103- }},
104- }
105-
106- out , err := execCommand (
107- "monit-query" , "diagnose" ,
108- "--ds-type" , "prometheus" ,
109- "--ds-name" , "prod-prometheus" ,
110- "--input-query" , "up" ,
111- "--operation" , "metric_trends" ,
112- "--output-format" , "json" ,
113- )
114- if err != nil {
115- t .Fatalf ("unexpected error: %v" , err )
116- }
117- var rendered map [string ]any
118- if err := json .Unmarshal ([]byte (out ), & rendered ); err != nil {
119- t .Fatalf ("decode CLI JSON: %v\n %s" , err , out )
120- }
121- if _ , found := rendered ["data_handling" ]; found {
122- t .Fatalf ("metric output fabricated data_handling: %s" , out )
123- }
124- evidence := rendered ["results" ].([]any )[0 ].(map [string ]any )["series_evidence" ].([]any )[0 ].(map [string ]any )
125- for _ , field := range []string {"comparison_status" , "current_window_stats" , "baseline_window_stats" } {
126- if _ , found := evidence [field ]; found {
127- t .Fatalf ("metric evidence fabricated %s: %s" , field , out )
128- }
129- }
130- }
131-
132- func TestMonitQueryDiagnoseRequiredFlags (t * testing.T ) {
133- cases := []struct {
134- name string
135- args []string
136- }{
137- {
138- name : "missing ds-type" ,
139- args : []string {
140- "monit-query" , "diagnose" ,
141- "--ds-name" , "vl-prod" ,
142- "--input-query" , `{app="api"}` ,
143- },
144- },
145- {
146- name : "missing ds-name" ,
147- args : []string {
148- "monit-query" , "diagnose" ,
149- "--ds-type" , "victorialogs" ,
150- "--input-query" , `{app="api"}` ,
151- },
152- },
153- {
154- name : "missing input-query" ,
155- args : []string {
156- "monit-query" , "diagnose" ,
157- "--ds-type" , "victorialogs" ,
158- "--ds-name" , "vl-prod" ,
159- },
160- },
161- }
162- for _ , tc := range cases {
163- t .Run (tc .name , func (t * testing.T ) {
21+ func TestRetiredMonitCommandsRejectBeforeRequest (t * testing.T ) {
22+ for _ , args := range [][]string {
23+ {"monit-query" , "diagnose" }, {"monit" , "query-diagnose" },
24+ {"monit" , "rule-counter-status" },
25+ {"monit" , "store-ruleset-create" }, {"monit" , "store-ruleset-update" },
26+ {"monit" , "store-ruleset-list" }, {"monit" , "store-ruleset-info" }, {"monit" , "store-ruleset-delete" },
27+ } {
28+ t .Run (strings .Join (args , " " ), func (t * testing.T ) {
16429 saveAndResetGlobals (t )
16530 stub := newGFStub (t )
166-
167- _ , err := execCommand (tc .args ... )
168- if err == nil {
169- t .Fatal ("expected required-flag error, got nil" )
170- }
171- if ! strings .Contains (err .Error (), "required" ) {
172- t .Errorf ("expected error to mention 'required', got %q" , err .Error ())
31+ _ , err := execCommand (args ... )
32+ if err == nil || ! strings .Contains (err .Error (), "unknown command" ) {
33+ t .Fatalf ("retired command error=%v" , err )
17334 }
17435 if stub .requests != 0 {
175- t .Errorf ( "diagnose should not have been called: %d request(s) " , stub .requests )
36+ t .Fatalf ( "retired command sent %d requests " , stub .requests )
17637 }
17738 })
17839 }
17940}
18041
181- func TestMonitQueryDiagnoseInvalidTimeStart (t * testing.T ) {
182- saveAndResetGlobals (t )
183- stub := newGFStub (t )
184-
185- _ , err := execCommand (
186- "monit-query" , "diagnose" ,
187- "--ds-type" , "victorialogs" ,
188- "--ds-name" , "vl-prod" ,
189- "--input-query" , `{app="api"}` ,
190- "--time-start" , "not-a-time" ,
191- )
192- if err == nil {
193- t .Fatal ("expected error for invalid --time-start, got nil" )
194- }
195- if ! strings .Contains (err .Error (), "--time-start" ) {
196- t .Errorf ("expected error to mention --time-start, got %q" , err .Error ())
197- }
198- if stub .requests != 0 {
199- t .Errorf ("diagnose should not have been called: %d request(s)" , stub .requests )
200- }
201- }
202-
20342// --- monit-query data -----------------------------------------------------
20443
20544func TestMonitQueryDataHappyPath (t * testing.T ) {
0 commit comments