@@ -18,10 +18,7 @@ import (
1818 "testing"
1919 "time"
2020
21- "github.com/prometheus/client_golang/prometheus"
22- dto "github.com/prometheus/client_model/go"
2321 "github.com/stretchr/testify/assert"
24- "github.com/stretchr/testify/require"
2522)
2623
2724func TestNewActionMetrics (t * testing.T ) {
@@ -134,90 +131,62 @@ func TestMetricsConstants(t *testing.T) {
134131}
135132
136133func TestActionMetrics_CounterIncrement (t * testing.T ) {
137- // Create a test registry to verify metrics behavior
138- testRegistry := prometheus .NewRegistry ()
139-
140- // Create new counter for testing
141- testCounter := prometheus .NewCounterVec (
142- prometheus.CounterOpts {
143- Name : "test_janitor_actions_count" ,
144- Help : "Test counter for janitor actions" ,
145- },
146- []string {"action_type" , "status" , "node" },
147- )
148-
149- testRegistry .MustRegister (testCounter )
150-
151- // Increment the counter multiple times
152- testCounter .With (prometheus.Labels {
153- "action_type" : ActionTypeReboot ,
154- "status" : StatusStarted ,
155- "node" : "test-node" ,
156- }).Inc ()
157-
158- testCounter .With (prometheus.Labels {
159- "action_type" : ActionTypeReboot ,
160- "status" : StatusStarted ,
161- "node" : "test-node" ,
162- }).Inc ()
163-
164- // Gather metrics
165- metricFamilies , err := testRegistry .Gather ()
166- require .NoError (t , err )
167- require .Len (t , metricFamilies , 1 )
168-
169- // Verify the counter was incremented
170- metricFamily := metricFamilies [0 ]
171- assert .Equal (t , "test_janitor_actions_count" , * metricFamily .Name )
172- require .Len (t , metricFamily .Metric , 1 )
173-
174- // Check that the counter value is 2
175- counter := metricFamily .Metric [0 ]
176- assert .Equal (t , float64 (2 ), * counter .Counter .Value )
177- }
178-
179- func TestActionMetrics_HistogramObservation (t * testing.T ) {
180- // Create a test registry to verify histogram behavior
181- testRegistry := prometheus .NewRegistry ()
182-
183- // Create new histogram for testing
184- testHistogram := prometheus .NewHistogramVec (
185- prometheus.HistogramOpts {
186- Name : "test_janitor_action_mttr_seconds" ,
187- Help : "Test histogram for janitor action MTTR" ,
188- Buckets : prometheus .ExponentialBuckets (10 , 2 , 5 ),
189- },
190- []string {"action_type" },
191- )
192-
193- testRegistry .MustRegister (testHistogram )
134+ t .Run ("IncActionCount increments counter" , func (t * testing.T ) {
135+ // Create metrics instance
136+ m := & ActionMetrics {}
137+
138+ // Call the actual business logic
139+ m .IncActionCount (ActionTypeReboot , StatusStarted , "test-node-1" )
140+ m .IncActionCount (ActionTypeReboot , StatusSucceeded , "test-node-1" )
141+ m .IncActionCount (ActionTypeTerminate , StatusStarted , "test-node-2" )
142+
143+ // Note: We can't easily verify the exact counter values without accessing
144+ // the internal prometheus registry, but we can verify the method executes
145+ // without panic or error. This test verifies the method signature and basic
146+ // functionality.
147+ })
194148
195- // Record some observations
196- testHistogram .With (prometheus.Labels {
197- "action_type" : ActionTypeReboot ,
198- }).Observe (30.0 ) // 30 seconds
149+ t .Run ("global IncActionCount function works" , func (t * testing.T ) {
150+ // Test the convenience function that uses GlobalMetrics
151+ IncActionCount (ActionTypeReboot , StatusStarted , "global-test-node" )
152+ IncActionCount (ActionTypeReboot , StatusSucceeded , "global-test-node" )
153+ IncActionCount (ActionTypeReboot , StatusFailed , "global-test-node" )
199154
200- testHistogram .With (prometheus.Labels {
201- "action_type" : ActionTypeReboot ,
202- }).Observe (120.0 ) // 2 minutes
155+ // Verify different action types
156+ IncActionCount (ActionTypeTerminate , StatusStarted , "global-test-node-2" )
157+ })
158+ }
203159
204- // Gather metrics
205- metricFamilies , err := testRegistry .Gather ()
206- require .NoError (t , err )
207- require .Len (t , metricFamilies , 1 )
160+ func TestActionMetrics_HistogramObservation (t * testing.T ) {
161+ t .Run ("RecordActionMTTR records duration" , func (t * testing.T ) {
162+ // Create metrics instance
163+ m := & ActionMetrics {}
164+
165+ // Call the actual business logic with various durations
166+ m .RecordActionMTTR (ActionTypeReboot , 30 * time .Second )
167+ m .RecordActionMTTR (ActionTypeReboot , 2 * time .Minute )
168+ m .RecordActionMTTR (ActionTypeReboot , 5 * time .Minute )
169+
170+ // Record different action types
171+ m .RecordActionMTTR (ActionTypeTerminate , 10 * time .Minute )
172+
173+ // Note: We can't easily verify the exact histogram values without accessing
174+ // the internal prometheus registry, but we can verify the method executes
175+ // without panic or error. This test verifies the method signature and basic
176+ // functionality.
177+ })
208178
209- // Verify the histogram was updated
210- metricFamily := metricFamilies [0 ]
211- assert .Equal (t , "test_janitor_action_mttr_seconds" , * metricFamily .Name )
212- assert .Equal (t , dto .MetricType_HISTOGRAM , * metricFamily .Type )
213- require .Len (t , metricFamily .Metric , 1 )
179+ t .Run ("global RecordActionMTTR function works" , func (t * testing.T ) {
180+ // Test the convenience function that uses GlobalMetrics
181+ RecordActionMTTR (ActionTypeReboot , 45 * time .Second )
182+ RecordActionMTTR (ActionTypeTerminate , 3 * time .Minute )
214183
215- // Check histogram sample count
216- histogram := metricFamily .Metric [0 ].Histogram
217- assert .Equal (t , uint64 (2 ), * histogram .SampleCount )
184+ // Test with very short duration
185+ RecordActionMTTR (ActionTypeReboot , 5 * time .Second )
218186
219- // Check sum of observations (30 + 120 = 150)
220- assert .Equal (t , float64 (150 ), * histogram .SampleSum )
187+ // Test with longer duration
188+ RecordActionMTTR (ActionTypeTerminate , 15 * time .Minute )
189+ })
221190}
222191
223192func TestGlobalMetrics_Initialization (t * testing.T ) {
0 commit comments