github.com/verrazzano/verrazzano@v1.7.0/application-operator/metricsexporter/metricsexporter_test.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package metricsexporter 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/prometheus/client_golang/prometheus/testutil" 11 asserts "github.com/stretchr/testify/assert" 12 "go.uber.org/zap" 13 ) 14 15 var logForTest = zap.S() 16 17 func TestCollectReconcileMetrics(t *testing.T) { 18 assert := asserts.New(t) 19 test := struct { 20 name string 21 }{ 22 name: "Test that reoncile counter is incremented by one when function is Successful & reconcile counter is incremented by one when function is Failed", 23 } 24 t.Run(test.name, func(t *testing.T) { 25 26 reconcileCounterObject, err := GetSimpleCounterMetric(AppconfigReconcileCounter) 27 assert.NoError(err) 28 reconcileSuccessfulCounterBefore := testutil.ToFloat64(reconcileCounterObject.Get()) 29 reconcileCounterObject.Inc(logForTest, nil) 30 reconcileSuccessfulCounterAfter := testutil.ToFloat64(reconcileCounterObject.Get()) 31 assert.Equal(reconcileSuccessfulCounterBefore, reconcileSuccessfulCounterAfter-1) 32 reconcileSuccessfulCounterBefore = reconcileSuccessfulCounterAfter 33 reconcileCounterObject.Add(3) 34 reconcileSuccessfulCounterAfter = testutil.ToFloat64(reconcileCounterObject.Get()) 35 assert.Equal(reconcileSuccessfulCounterBefore, reconcileSuccessfulCounterAfter-3) 36 37 reconcileerrorCounterObject, err := GetSimpleCounterMetric(AppconfigReconcileError) 38 assert.NoError(err) 39 reconcileFailedCounterBefore := testutil.ToFloat64(reconcileerrorCounterObject.Get()) 40 reconcileerrorCounterObject.Inc(logForTest, nil) 41 reconcileFailedCounterAfter := testutil.ToFloat64(reconcileerrorCounterObject.Get()) 42 assert.Equal(reconcileFailedCounterBefore, reconcileFailedCounterAfter-1) 43 reconcileFailedCounterBefore = reconcileFailedCounterAfter 44 reconcileerrorCounterObject.Add(3) 45 reconcileFailedCounterAfter = testutil.ToFloat64(reconcileerrorCounterObject.Get()) 46 assert.Equal(reconcileFailedCounterBefore, reconcileFailedCounterAfter-3) 47 48 // Duration Metric test 49 reconcileDurationCount, _ := GetDurationMetric(AppconfigReconcileDuration) 50 reconcileDurationCount.TimerStart() 51 time.Sleep(time.Second) 52 reconcileDurationCount.TimerStop() 53 }) 54 }