Add tests for EncodeMetrics and DecodeMetrics

This commit is contained in:
Evgeniy Kulikov 2020-02-10 15:35:23 +03:00
parent 02e82156ba
commit 1deea42f5a
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2

View file

@ -1,14 +1,31 @@
package state package state
import ( import (
"bytes"
"encoding/json" "encoding/json"
"expvar" "expvar"
"testing" "testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type testCollector struct {
testA *prometheus.Desc
testB *prometheus.Desc
}
func (c *testCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.testA
ch <- c.testB
}
func (c *testCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(c.testA, prometheus.GaugeValue, 1, "label_1")
ch <- prometheus.MustNewConstMetric(c.testB, prometheus.GaugeValue, 2, "label_2")
}
func TestEncodeVariables(t *testing.T) { func TestEncodeVariables(t *testing.T) {
dump := make(map[string]interface{}) dump := make(map[string]interface{})
@ -47,3 +64,55 @@ func TestEncodeConfig(t *testing.T) {
require.Contains(t, dump, "test2") require.Contains(t, dump, "test2")
require.Equal(t, dump["test2"], "test2") require.Equal(t, dump["test2"], "test2")
} }
func TestEncodeAndDecodeMetrics(t *testing.T) {
registry := prometheus.NewRegistry()
collector := &testCollector{
testA: prometheus.NewDesc("test1", "test1", []string{"test1"}, prometheus.Labels{"label_1": "test1"}),
testB: prometheus.NewDesc("test2", "test2", []string{"test2"}, prometheus.Labels{"label_2": "test2"}),
}
require.NoError(t, registry.Register(collector))
gather, err := registry.Gather()
require.NoError(t, err)
res, err := EncodeMetrics(registry)
require.NoError(t, err)
metrics, err := DecodeMetrics(res)
require.NoError(t, err)
require.Len(t, metrics, len(gather))
{ // Check that JSON encoded metrics are equal:
expect := new(bytes.Buffer)
actual := new(bytes.Buffer)
require.NoError(t, json.NewEncoder(expect).Encode(gather))
require.NoError(t, json.NewEncoder(actual).Encode(metrics))
require.Equal(t, expect.Bytes(), actual.Bytes())
}
{ // Deep comparison of metrics:
for i := range metrics {
require.Equal(t, gather[i].Help, metrics[i].Help)
require.Equal(t, gather[i].Name, metrics[i].Name)
require.Equal(t, gather[i].Type, metrics[i].Type)
require.Len(t, metrics[i].Metric, len(gather[i].Metric))
for j := range metrics[i].Metric {
require.Equal(t, gather[i].Metric[j].Gauge, metrics[i].Metric[j].Gauge)
require.Len(t, metrics[i].Metric[j].Label, len(gather[i].Metric[j].Label))
for k := range metrics[i].Metric[j].Label {
require.Equal(t, gather[i].Metric[j].Label[k].Name, metrics[i].Metric[j].Label[k].Name)
require.Equal(t, gather[i].Metric[j].Label[k].Value, metrics[i].Metric[j].Label[k].Value)
}
}
}
}
}