This repository has been archived by the owner on Dec 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
127 lines (102 loc) · 2.32 KB
/
main_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
package main
import (
"testing"
"github.com/go-kit/kit/log"
"github.com/prometheus/common/model"
"github.com/ssube/prometheus-sql-adapter/metric"
"github.com/ssube/prometheus-sql-adapter/postgres"
)
type MockWriter struct {
MetricCount int
SampleCount int
}
func CreateMockWriter() *MockWriter {
w := MockWriter{
MetricCount: 0,
SampleCount: 0,
}
return &w
}
func (w *MockWriter) Write(metrics metric.Metrics, samples model.Samples) error {
w.MetricCount += len(metrics)
w.SampleCount += len(samples)
return nil
}
func (w *MockWriter) Name() string {
return "MockWriter"
}
func TestSendSamples(t *testing.T) {
log := log.NewNopLogger()
w := CreateMockWriter()
metrics := metric.Metrics{
&model.Metric{},
&model.Metric{},
}
samples := model.Samples{}
sendSamples(log, w, metrics, samples)
if w.MetricCount != 2 {
t.Errorf("expected %d metrics, got %d", 2, w.MetricCount)
}
if w.SampleCount != 0 {
t.Errorf("expected %d samples, got %d", 0, w.SampleCount)
}
}
func TestParseCacheFlags(t *testing.T) {
args := []string{
"prometheus-sql-adapter",
"--pg.cache-size=4",
}
cfg := parseFlags(args)
if cfg == nil {
t.FailNow()
}
testSize := 4
if cfg.Postgres.CacheSize != testSize {
t.Errorf("expected postgres cache size to be %d, got %d", testSize, cfg.Postgres.CacheSize)
}
}
func TestParseConnFlags(t *testing.T) {
args := []string{
"prometheus-sql-adapter",
"--pg.max-idle=4",
"--pg.max-open=8",
}
cfg := parseFlags(args)
if cfg == nil {
t.FailNow()
}
testIdle := 4
if cfg.Postgres.MaxIdle != testIdle {
t.Errorf("expected idle conn limit to be %d, got %d", testIdle, cfg.Postgres.MaxIdle)
}
testOpen := 8
if cfg.Postgres.MaxOpen != testOpen {
t.Errorf("expected open conn limit to be %d, got %d", testOpen, cfg.Postgres.MaxOpen)
}
}
func TestParseFlagsError(t *testing.T) {
args := []string{
"some-exec",
"--no",
"--foop",
}
cfg := parseFlags(args)
if cfg != nil {
t.Error("args were not expected to parse")
}
}
func TestBuildClientsEmpty(t *testing.T) {
log := log.NewNopLogger()
cfg := config{
Postgres: postgres.ClientConfig{
ConnStr: "",
},
}
readers, writers := buildClients(log, &cfg)
if len(readers) > 0 {
t.Errorf("expected 0 readers, got %d", len(readers))
}
if len(writers) > 0 {
t.Errorf("expected 0 writers, got %d", len(writers))
}
}