From 1deea42f5ac0dd2b3ae681eedbc6f5b570de1039 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 15:35:23 +0300 Subject: [PATCH] Add tests for EncodeMetrics and DecodeMetrics --- state/service_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/state/service_test.go b/state/service_test.go index 21dc0bc..d545cb0 100644 --- a/state/service_test.go +++ b/state/service_test.go @@ -1,14 +1,31 @@ package state import ( + "bytes" "encoding/json" "expvar" "testing" + "github.com/prometheus/client_golang/prometheus" "github.com/spf13/viper" "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) { dump := make(map[string]interface{}) @@ -47,3 +64,55 @@ func TestEncodeConfig(t *testing.T) { require.Contains(t, dump, "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) + } + } + } + } +}