This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
end_to_end_test.go
209 lines (181 loc) · 5.09 KB
/
end_to_end_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"sort"
"sync"
"testing"
"time"
"github.com/ChronixDB/chronix.go/chronix"
"github.com/ChronixDB/chronix.ingester/ingester"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/storage/remote"
)
type erroringChronix struct{}
func (c *erroringChronix) Store(ts []*chronix.TimeSeries, commit bool, commitWithin time.Duration) error {
return fmt.Errorf("this is a purposefully erroring Chronix client")
}
func (c *erroringChronix) Query(q, fq, fl string) ([]byte, error) {
panic("not implemented")
}
// A testChronix instance acts as a chronix.Client that records any series sent
// to it and can return them as a model.Matrix.
type testChronix struct {
mtx sync.Mutex
sampleStreams map[model.Fingerprint]*model.SampleStream
}
func (c *testChronix) Store(ts []*chronix.TimeSeries, commit bool, commitWithin time.Duration) error {
c.mtx.Lock()
defer c.mtx.Unlock()
for _, s := range ts {
m := model.Metric{
model.MetricNameLabel: model.LabelValue(s.Name),
}
for k, v := range s.Attributes {
m[model.LabelName(k)] = model.LabelValue(v)
}
fp := m.Fingerprint()
ss, exists := c.sampleStreams[fp]
if !exists {
ss = &model.SampleStream{
Metric: m,
}
c.sampleStreams[fp] = ss
}
for _, p := range s.Points {
ss.Values = append(ss.Values, model.SamplePair{
Timestamp: model.TimeFromUnixNano(p.Timestamp * 1e6),
Value: model.SampleValue(p.Value),
})
}
}
return nil
}
func (c *testChronix) Query(q, fq, fl string) ([]byte, error) {
panic("not implemented")
}
func (c *testChronix) toMatrix() model.Matrix {
m := make(model.Matrix, 0, len(c.sampleStreams))
for _, ss := range c.sampleStreams {
m = append(m, ss)
}
return m
}
func buildTestMatrix(numSeries int, samplesPerSeries int) model.Matrix {
m := make(model.Matrix, 0, numSeries)
for i := 0; i < numSeries; i++ {
ss := model.SampleStream{
Metric: model.Metric{
model.MetricNameLabel: model.LabelValue(fmt.Sprintf("testmetric_%d", i)),
model.JobLabel: "testjob",
},
Values: make([]model.SamplePair, 0, samplesPerSeries),
}
for j := 0; j < samplesPerSeries; j++ {
ss.Values = append(ss.Values, model.SamplePair{
Timestamp: model.Time(i + j),
Value: model.SampleValue(i + j),
})
}
m = append(m, &ss)
}
sort.Sort(m)
return m
}
func matrixToSamples(m model.Matrix) []*model.Sample {
var samples []*model.Sample
for _, ss := range m {
for _, sp := range ss.Values {
samples = append(samples, &model.Sample{
Metric: ss.Metric,
Timestamp: sp.Timestamp,
Value: sp.Value,
})
}
}
return samples
}
func TestEndToEnd(t *testing.T) {
chronix := &testChronix{
sampleStreams: map[model.Fingerprint]*model.SampleStream{},
}
checkpointFile := "test-checkpoint.db"
defer os.Remove(checkpointFile)
ing, err := ingester.NewIngester(
ingester.Config{
MaxChunkAge: 9999 * time.Hour,
CheckpointFile: checkpointFile,
},
&chronixStore{chronix: chronix},
)
if err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
serv := httptest.NewServer(mux)
defer serv.Close()
mux.Handle("/", ingestHandler(ing))
u, err := url.Parse(serv.URL)
if err != nil {
panic(err)
}
ingClient, err := remote.NewClient(0, &remote.ClientConfig{
URL: &config.URL{URL: u},
Timeout: model.Duration(time.Second),
})
// Create test samples.
testData := buildTestMatrix(10, 1000)
// Shove test samples into the ingester.
if err := ingClient.Store(matrixToSamples(testData)); err != nil {
t.Fatal(err)
}
// Stop the ingester, causing it to checkpoint its state to disk.
ing.Stop()
// Create a new ingester that recovers from the checkpoint, but tries
// to store chunks into an erroring Chronix client.
ing, err = ingester.NewIngester(
ingester.Config{
MaxChunkAge: 9999 * time.Hour,
CheckpointFile: checkpointFile,
FlushOnShutdown: true,
},
&chronixStore{chronix: &erroringChronix{}},
)
if err != nil {
t.Fatal(err)
}
// Stop the ingester, causing it to try and flush its chunks to Chronix.
// But storing chunks in the erroring Chronix client will fail, so it will
// still checkpoint all chunks to disk (again).
ing.Stop()
// No samples should have been stored in the working Chronix client yet.
if len(chronix.toMatrix()) != 0 {
t.Fatal("Unexpected samples were stored in Chronix client:", chronix.toMatrix())
}
// Create a new ingester that recovers from the checkpoint again, but talks
// to a working Chronix client this time.
ing, err = ingester.NewIngester(
ingester.Config{
MaxChunkAge: 9999 * time.Hour,
CheckpointFile: checkpointFile,
FlushOnShutdown: true,
},
&chronixStore{chronix: chronix},
)
if err != nil {
t.Fatal(err)
}
// Stop the ingester, causing it to flush its chunks to Chronix.
ing.Stop()
// Compare stored samples from Chronix with expected samples.
want := chronix.toMatrix()
sort.Sort(want)
if !reflect.DeepEqual(want, testData) {
t.Fatalf("unexpected stored data\n\nwant:\n\n%v\n\ngot:\n\n%v\n\n", testData, want)
}
}