-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector_test.go
63 lines (54 loc) · 2.75 KB
/
collector_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
package kafka_exporter
import (
"sync"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCollector_Describe(t *testing.T) {
ch := make(chan *prometheus.Desc, len(allMetrics)*2)
c := new(Collector)
c.Describe(ch)
require.Equal(t, len(allMetrics), len(ch))
}
func TestCollector_Collect(t *testing.T) {
ch := make(chan prometheus.Metric)
var (
metrics []prometheus.Metric
wg sync.WaitGroup
)
wg.Add(1)
go func() {
for m := range ch {
metrics = append(metrics, m)
}
wg.Done()
}()
c := NewCollector(newMockKafkaClient(), nopLogger)
c.Collect(ch)
close(ch)
wg.Wait()
assert.EqualValues(t, 1, testutil.ToFloat64(brokersTotal))
assert.EqualValues(t, 1, testutil.ToFloat64(brokerInfo))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitions.WithLabelValues("topic-1")))
assert.EqualValues(t, 2, testutil.ToFloat64(topicPartitions.WithLabelValues("topic-2")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionLeader.WithLabelValues("topic-1", "0")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionLeader.WithLabelValues("topic-2", "0")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionLeader.WithLabelValues("topic-2", "1")))
assert.EqualValues(t, 10, testutil.ToFloat64(topicPartitionCurrentOffset.WithLabelValues("topic-1", "0")))
assert.EqualValues(t, 10, testutil.ToFloat64(topicPartitionCurrentOffset.WithLabelValues("topic-2", "0")))
assert.EqualValues(t, 20, testutil.ToFloat64(topicPartitionCurrentOffset.WithLabelValues("topic-2", "1")))
assert.EqualValues(t, 0, testutil.ToFloat64(topicPartitionOldestOffset.WithLabelValues("topic-1", "0")))
assert.EqualValues(t, 0, testutil.ToFloat64(topicPartitionOldestOffset.WithLabelValues("topic-2", "0")))
assert.EqualValues(t, 0, testutil.ToFloat64(topicPartitionOldestOffset.WithLabelValues("topic-2", "1")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionReplicas.WithLabelValues("topic-1", "0")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionReplicas.WithLabelValues("topic-2", "0")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionReplicas.WithLabelValues("topic-2", "1")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionInSyncReplicas.WithLabelValues("topic-1", "0")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionInSyncReplicas.WithLabelValues("topic-2", "0")))
assert.EqualValues(t, 1, testutil.ToFloat64(topicPartitionInSyncReplicas.WithLabelValues("topic-2", "1")))
assert.EqualValues(t, 1, testutil.ToFloat64(consumerGroups))
assert.EqualValues(t, 5, testutil.ToFloat64(consumerGroupCurrentOffset.WithLabelValues("group-1", "topic-1", "0")))
}