forked from aerospike/aerospike-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher_node_stats_test.go
173 lines (130 loc) · 5.13 KB
/
watcher_node_stats_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
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 TestNodeStats_PassOneKeys(t *testing.T) {
watcher := new(StatsWatcher)
// Check passoneKeys
passOneKeys := watcher.passOneKeys()
assert.Nil(t, passOneKeys)
}
func TestNodeStats_PassTwoKeys(t *testing.T) {
watcher := new(StatsWatcher)
// simulate, as if we are sending requestInfo to AS and get the NodeStats, these are coming from mock-data-generator
pass2Keys := make(map[string]string)
outputs := watcher.passTwoKeys(pass2Keys)
fmt.Println("TestNodeStats_PassTwoKeys: outputs: ", outputs)
assert.Equal(t, outputs, []string{"statistics"})
}
func TestNodeStats_RefreshDefault(t *testing.T) {
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_TRUE)
fmt.Println("initializing config ... TestNodeStats_RefreshDefault")
// Initialize and validate config
config = new(Config)
initConfig(DEFAULT_APE_TOML, config)
config.validateAndUpdate()
// run the test-case logic
nodeStats_runTestCase(t)
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_FALSE)
}
func TestNodeStats_Allowlist(t *testing.T) {
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_TRUE)
fmt.Println("initializing config ... TestNodeStats_Allowlist")
// Initialize and validate config
config = new(Config)
initConfig(NS_ALLOWLIST_APE_TOML, config)
config.validateAndUpdate()
// run the test-case logic
nodeStats_runTestCase(t)
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_FALSE)
}
func TestNodeStats_Blocklist(t *testing.T) {
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_TRUE)
fmt.Println("initializing config ... TestNodeStats_Blocklist")
// Initialize and validate config
config = new(Config)
initConfig(NS_BLOCKLIST_APE_TOML, config)
config.validateAndUpdate()
// run the test-case logic
nodeStats_runTestCase(t)
os.Setenv(TESTCASE_MODE, TESTCASE_MODE_FALSE)
}
/**
* complete logic to call watcher, generate-mock data and asset is part of this function
*/
func nodeStats_runTestCase(t *testing.T) {
watcher := new(StatsWatcher)
gaugeStatHandler = new(GaugeStats)
initGaugeStats(METRICS_CONFIG_FILE, gaugeStatHandler)
rawMetrics := getRawMetrics()
lObserver := &Observer{}
ch := make(chan prometheus.Metric, 1000)
nodeStatsInfoKeys := []string{}
watcher.passTwoKeys(rawMetrics)
err := watcher.refresh(lObserver, nodeStatsInfoKeys, rawMetrics, ch)
if err != nil {
fmt.Println("watcher_node_stats_test : Unable to refresh NodeStats")
} 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{}
arrServices := []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_namespac_memory_free_pct", help: "memory free pct", constLabels: {}, variableLabels: [cluster_name service ns]}
// Description: Desc{fqName: "aerospike_node_stats_fabric_rw_recv_rate", help: "fabric rw recv rate", constLabels: {}, variableLabels: [cluster_name service]}
// Label: [name:"cluster_name" value:"null" name:"service" value:"172.17.0.3:3000" ]
metricNameFromDesc := extractMetricNameFromDesc(description)
serviceFromLabel := extractLabelNameValueFromFullLabel(metricLabel, "service")
// clusterFromLabel := extractLabelNameValueFromFullLabel(metricLabel, "cluster_name")
// appends to the service array
arrServices = append(arrServices, serviceFromLabel)
// key will be like namespace/<metric_name>, this we use this check during assertion
keyName := makeKeyname(serviceFromLabel, metricNameFromDesc, true)
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 serviceIndex := range arrServices {
serviceIp := arrServices[serviceIndex]
lExpectedMetricNamedValues, lExpectedMetricLabels := createNodeStatsWatcherExpectedOutputs(serviceIp)
for key := range lOutputValues {
expectedValues := lExpectedMetricNamedValues[key]
expectedLabels := lExpectedMetricLabels[key]
outputMetricValues := lOutputValues[key]
outpuMetricLabels := lOutputLabels[key]
// assert - only if the value belongs to the service we read expected values and processing
if strings.HasPrefix(key, serviceIp) {
assert.Contains(t, expectedValues, outputMetricValues)
assert.Contains(t, expectedLabels, outpuMetricLabels)
}
}
}
} // end else-refresh-failure
}