forked from aerospike/aerospike-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher_users_test.go
223 lines (163 loc) · 6.66 KB
/
watcher_users_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
)
func TestUsers_PassOneKeys(t *testing.T) {
fmt.Println("initializing config ... TestXdr_PassOneKeys")
watcher := new(UserWatcher)
// Check passoneKeys
passOneKeys := watcher.passOneKeys()
assert.Nil(t, passOneKeys)
}
func TestUsers_PassTwoKeys(t *testing.T) {
watcher := new(UserWatcher)
rawMetrics := getRawMetrics()
// simulate, as if we are sending requestInfo to AS and get the NodeStats, these are coming from mock-data-generator
outputs := watcher.passTwoKeys(rawMetrics)
assert.Nil(t, outputs)
}
func TestUsers_Allowlist(t *testing.T) {
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_TRUE)
fmt.Println("initializing config ... TestUsers_Allowlist")
// Initialize and validate config
config = new(Config)
initConfig(NS_ALLOWLIST_APE_TOML, config)
config.validateAndUpdate()
// this is required as UserWatcher cheks for user/password in the properties file
config.Aerospike.User = "admin"
config.Aerospike.Password = "admin"
gaugeStatHandler = new(GaugeStats)
initGaugeStats(METRICS_CONFIG_FILE, gaugeStatHandler)
users_runTestCase(t)
}
func TestUsers_RefreshWithLabelsConfig(t *testing.T) {
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_TRUE)
fmt.Println("initializing config ... TestUsers_RefreshWithLabelsConfig")
// Initialize and validate config
config = new(Config)
initConfig(LABELS_APE_TOML, config)
config.validateAndUpdate()
watcher := new(UserWatcher)
// this is required as UserWatcher cheks for user/password in the properties file
config.Aerospike.User = "admin"
config.Aerospike.Password = "admin"
gaugeStatHandler = new(GaugeStats)
initGaugeStats(METRICS_CONFIG_FILE, gaugeStatHandler)
rawMetrics := getRawMetrics()
lObserver := &Observer{}
ch := make(chan prometheus.Metric, 1000)
userInfoKeys := watcher.passTwoKeys(rawMetrics)
// get user data from mock
mUsers := new(MockUsersDataGen)
users := mUsers.createDummyUserRoles()
watcher.passTwoKeys(rawMetrics)
err := watcher.refreshUserStats(lObserver, userInfoKeys, rawMetrics, ch, users)
if err != nil {
fmt.Println("watcher_users_test : Unable to refresh Users")
} else {
domore := 1
for domore == 1 {
select {
case nsMetric := <-ch:
description := nsMetric.Desc().String()
var protobuffer dto.Metric
err := nsMetric.Write(&protobuffer)
if err != nil {
fmt.Println(" unable to get metric ", description, " data into protobuf ", err)
}
metricLabel := fmt.Sprintf("%s", protobuffer.Label)
// Desc{fqName: "aerospike_users_write_single_record_tps", help: "write single record tps", constLabels: {sample="sample_label_value"}, variableLabels: [cluster_name service user]}
// [name:"cluster_name" value:"null" name:"sample" value:"sample_label_value" name:"service" value:"172.17.0.3:3000" name:"user" value:"" ]
for eachConfigMetricLabel := range config.AeroProm.MetricLabels {
modifiedConfigMetricLabels := strings.ReplaceAll(eachConfigMetricLabel, "=", ":")
assert.Contains(t, metricLabel, modifiedConfigMetricLabels)
}
case <-time.After(1 * time.Second):
domore = 0
} // end select
}
} // end else-refresh-failure
}
/**
* complete logic to call watcher, generate-mock data and asset is part of this function
*/
func users_runTestCase(t *testing.T) {
watcher := new(UserWatcher)
gaugeStatHandler = new(GaugeStats)
initGaugeStats(METRICS_CONFIG_FILE, gaugeStatHandler)
rawMetrics := getRawMetrics()
lObserver := &Observer{}
ch := make(chan prometheus.Metric, 1000)
userInfoKeys := watcher.passTwoKeys(rawMetrics)
// get user data from mock
mockUserGen := new(MockUsersDataGen)
userRoles := mockUserGen.createDummyUserRoles()
// get user data from mock
watcher.passTwoKeys(rawMetrics)
err := watcher.refreshUserStats(lObserver, userInfoKeys, rawMetrics, ch, userRoles)
if err != nil {
fmt.Println("users_runTestCase : Unable to refresh User stats")
} else {
domore := 1
// map of string ==> map["namespace/metric-name"]["<Label>"]
// both used to assert the return values from actual code against calculated values
lOutputValues := map[string]string{}
lOutputLabels := map[string]string{}
arrUserRoles := map[string]string{}
for domore == 1 {
select {
case nsMetric := <-ch:
description := nsMetric.Desc().String()
var protobuffer dto.Metric
err := nsMetric.Write(&protobuffer)
if err != nil {
fmt.Println(" unable to get metric ", description, " data into protobuf ", err)
}
metricValue := ""
metricLabel := fmt.Sprintf("%s", protobuffer.Label)
if protobuffer.Gauge != nil {
metricValue = fmt.Sprintf("%.0f", *protobuffer.Gauge.Value)
} else if protobuffer.Counter != nil {
metricValue = fmt.Sprintf("%.0f", *protobuffer.Counter.Value)
}
// Desc{fqName: "aerospike_users_write_single_record_tps", help: "write single record tps", constLabels: {sample="sample_label_value"}, variableLabels: [cluster_name service user]}
// [name:"cluster_name" value:"null" name:"sample" value:"sample_label_value" name:"service" value:"172.17.0.3:3000" name:"user" value:"" ]
metricNameFromDesc := extractMetricNameFromDesc(description)
userFromLabel := extractLabelNameValueFromFullLabel(metricLabel, "user")
serviceFromLabel := extractLabelNameValueFromFullLabel(metricLabel, "service")
// key will be like namespace/set/<metric_name>, this we use this check during assertion
keyName := makeKeyname(userFromLabel, metricNameFromDesc, true)
keyName = makeKeyname(serviceFromLabel, keyName, true)
// appends to the sets array
namespaceSetKey := makeKeyname(serviceFromLabel, userFromLabel, true)
arrUserRoles[namespaceSetKey] = namespaceSetKey
lOutputValues[keyName] = metricValue
lOutputLabels[keyName] = metricLabel
case <-time.After(1 * time.Second):
domore = 0
} // end select
}
// we have only 1 service in our mock-data, however loop thru service array
for _, userRole := range arrUserRoles {
lExpectedMetricNamedValues, lExpectedMetricLabels := mockUserGen.createMockUserData()
for key := range lOutputValues {
expectedValues := lExpectedMetricNamedValues[key]
expectedLabels := lExpectedMetricLabels[key]
outputMetricValues := lOutputValues[key]
outpuMetricLabels := lOutputLabels[key]
// assert - only if the value belongs to the namespace/set we read expected values and processing
if strings.HasPrefix(key, userRole) {
assert.Contains(t, expectedValues, outputMetricValues)
assert.Contains(t, expectedLabels, outpuMetricLabels)
}
}
}
} // end else-refresh-failure
}