github.com/verrazzano/verrazzano@v1.7.0/application-operator/metricsexporter/metricsexporter_utils_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 9 "github.com/stretchr/testify/assert" 10 ) 11 12 var ( 13 testNameValid = "Valid Metric Name, no error" 14 testNameInvalid = "Invalid Metric name, error" 15 ) 16 17 // TestGetSimpleCounterMetric tests GetSimpleCounterMetric function 18 // GIVEN a metricName 19 // WHEN a call to GetSimpleCounterMetric is made 20 // THEN return no error if the metricName is valid, else return an error 21 func TestGetSimpleCounterMetric(t *testing.T) { 22 tests := []struct { 23 name string 24 metricName metricName 25 wantErr bool 26 }{ 27 { 28 testNameValid, 29 "appconfig reconcile counter", 30 false, 31 }, 32 { 33 testNameValid, 34 "istio handle error", 35 false, 36 }, 37 { 38 testNameInvalid, 39 "MultiClusterConfigmap handle duration", 40 true, 41 }, 42 } 43 44 for _, tt := range tests { 45 t.Run(tt.name, func(t *testing.T) { 46 counterObject, err := GetSimpleCounterMetric(tt.metricName) 47 if tt.wantErr { 48 assert.Error(t, err) 49 assert.Nil(t, counterObject) 50 } else { 51 assert.NoError(t, err) 52 assert.NotNil(t, counterObject) 53 } 54 }) 55 } 56 } 57 58 // TestGetDurationMetric tests GetDurationMetric function 59 // GIVEN a metricName 60 // WHEN a call to GetDurationMetric is made 61 // THEN return no error if the metricName is valid, else return an error 62 func TestGetDurationMetric(t *testing.T) { 63 tests := []struct { 64 name string 65 metricName metricName 66 wantErr bool 67 }{ 68 { 69 testNameValid, 70 "MultiClusterConfigmap handle duration", 71 false, 72 }, 73 { 74 testNameValid, 75 "BindingUpdater handle duration", 76 false, 77 }, 78 { 79 testNameInvalid, 80 "VzProj handle counter", 81 true, 82 }, 83 } 84 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 counterObject, err := GetDurationMetric(tt.metricName) 88 if tt.wantErr { 89 assert.Error(t, err) 90 assert.Nil(t, counterObject) 91 } else { 92 assert.NoError(t, err) 93 assert.NotNil(t, counterObject) 94 } 95 }) 96 } 97 } 98 99 // TestExposeControllerMetrics tests the ExposeControllerMetrics function 100 // GIVEN a set of 3 metricNames 101 // WHEN a call to ExposeControllerMetrics is made 102 // THEN return respective MetricObject and MetricLogger if the metricNames are valid, else return an error 103 func TestExposeControllerMetrics(t *testing.T) { 104 tests := []struct { 105 name string 106 controllerName string 107 counterMetricName metricName 108 errorCounterMetricName metricName 109 durationCounterMetricName metricName 110 wantErr bool 111 }{ 112 { 113 testNameValid, 114 "appconfig", 115 AppconfigHandleCounter, 116 AppconfigHandleError, 117 AppconfigHandleDuration, 118 false, 119 }, 120 { 121 testNameInvalid, 122 "istio", 123 IstioHandleDuration, 124 IstioHandleCounter, 125 IstioHandleError, 126 true, 127 }, 128 { 129 testNameInvalid, 130 "coh", 131 CohworkloadReconcileCounter, 132 CohworkloadReconcileDuration, 133 CohworkloadReconcileDuration, 134 true, 135 }, 136 { 137 testNameInvalid, 138 "helidon", 139 HelidonReconcileCounter, 140 HelidonReconcileError, 141 HelidonReconcileError, 142 true, 143 }, 144 } 145 146 for _, tt := range tests { 147 t.Run(tt.name, func(t *testing.T) { 148 counterMetricObject, errorCounterMetricObject, handleDurationMetricObject, zapLogForMetrics, err := ExposeControllerMetrics(tt.controllerName, tt.counterMetricName, tt.errorCounterMetricName, tt.durationCounterMetricName) 149 if tt.wantErr { 150 assert.Error(t, err) 151 assert.Nil(t, counterMetricObject) 152 assert.Nil(t, errorCounterMetricObject) 153 assert.Nil(t, handleDurationMetricObject) 154 assert.Nil(t, zapLogForMetrics) 155 } else { 156 assert.NoError(t, err) 157 assert.NotNil(t, counterMetricObject) 158 assert.NotNil(t, errorCounterMetricObject) 159 assert.NotNil(t, handleDurationMetricObject) 160 assert.NotNil(t, zapLogForMetrics) 161 } 162 }) 163 } 164 }