-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathspinup_test.go
621 lines (528 loc) · 16.1 KB
/
spinup_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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package e2e_test
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
"strconv"
"syscall"
"testing"
"time"
"github.com/oklog/run"
"github.com/pkg/errors"
"github.com/thanos-io/thanos/pkg/objstore/s3"
"github.com/thanos-io/thanos/pkg/receive"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/testutil"
"google.golang.org/grpc"
)
const portMin = 10000
func remoteWriteEndpoint(addr address) string { return fmt.Sprintf("%s/api/v1/receive", addr.URL()) }
type address struct {
host string
Port string
}
func (a address) HostPort() string {
return net.JoinHostPort(a.host, a.Port)
}
func (a address) URL() string {
return fmt.Sprintf("http://%s", net.JoinHostPort(a.host, a.Port))
}
// portPool allows to reserve ports within unit test. This naive implementation assumes that all ports from portMin-X are free outside.
// No top boundary, no thread safety.
// TODO(bwplotka): Make it more resilient.
type portPool struct {
lastPort int
}
func (pp *portPool) New() int {
if pp.lastPort < portMin {
pp.lastPort = portMin - 1
}
pp.lastPort++
return pp.lastPort
}
type addresser struct {
host string
pp *portPool
}
func (a *addresser) New() address {
return address{host: a.host, Port: fmt.Sprintf("%d", a.pp.New())}
}
func newLocalAddresser() *addresser {
// We keep this one with localhost, not 127.0.0.1 to have perfect match with what Prometheus will expose in up metric.
return &addresser{host: "localhost", pp: &portPool{}}
}
type Exec interface {
Start(stdout io.Writer, stderr io.Writer) error
Wait() error
Kill() error
String() string
}
type cmdExec struct {
*exec.Cmd
}
func newCmdExec(cmd *exec.Cmd) *cmdExec {
return &cmdExec{Cmd: cmd}
}
func (c *cmdExec) Start(stdout io.Writer, stderr io.Writer) error {
c.Stderr = stderr
c.Stdout = stdout
c.SysProcAttr = &syscall.SysProcAttr{
// For linux only, kill this if the go test process dies before the cleanup.
Pdeathsig: syscall.SIGKILL,
}
return c.Cmd.Start()
}
func (c *cmdExec) Kill() error { return c.Process.Signal(syscall.SIGKILL) }
func (c *cmdExec) String() string { return fmt.Sprintf("%s %v", c.Path, c.Args[1:]) }
type scheduler interface {
Schedule(workDir string) (Exec, error)
}
type serverScheduler struct {
schedule func(workDir string) (Exec, error)
HTTP address
GRPC address
}
func (s *serverScheduler) Schedule(workDir string) (Exec, error) { return s.schedule(workDir) }
type prometheusScheduler struct {
serverScheduler
RelDir string
}
func prometheus(http address, config string) *prometheusScheduler {
s := &prometheusScheduler{
RelDir: path.Join("data", "prom", http.Port),
}
s.serverScheduler = serverScheduler{
HTTP: http,
schedule: func(workDir string) (execs Exec, e error) {
promDir := path.Join(workDir, s.RelDir)
if err := os.MkdirAll(promDir, 0777); err != nil {
return nil, errors.Wrap(err, "create prom dir failed")
}
if err := ioutil.WriteFile(promDir+"/prometheus.yml", []byte(config), 0666); err != nil {
return nil, errors.Wrap(err, "creating prom config failed")
}
return newCmdExec(exec.Command(testutil.PrometheusBinary(),
"--config.file", promDir+"/prometheus.yml",
"--storage.tsdb.path", promDir,
"--storage.tsdb.max-block-duration", "2h",
"--log.level", "info",
"--web.listen-address", http.HostPort(),
)), nil
},
}
return s
}
func sidecar(http, grpc address, prom *prometheusScheduler) *serverScheduler {
return &serverScheduler{
HTTP: http,
GRPC: grpc,
schedule: func(workDir string) (Exec, error) {
promDir := path.Join(workDir, prom.RelDir)
return newCmdExec(exec.Command("thanos", "sidecar",
"--debug.name", fmt.Sprintf("sidecar-%s", http.Port),
"--grpc-address", grpc.HostPort(),
"--http-address", http.HostPort(),
"--prometheus.url", prom.HTTP.URL(),
"--tsdb.path", promDir,
"--log.level", "debug")), nil
},
}
}
func receiver(http, grpc, metric address, replicationFactor int, hashring ...receive.HashringConfig) *serverScheduler {
if len(hashring) == 0 {
hashring = []receive.HashringConfig{{Endpoints: []string{remoteWriteEndpoint(http)}}}
}
return &serverScheduler{
HTTP: http,
GRPC: grpc,
schedule: func(workDir string) (Exec, error) {
receiveDir := path.Join(workDir, "data", "receive", http.Port)
if err := os.MkdirAll(receiveDir, 0777); err != nil {
return nil, errors.Wrap(err, "create receive dir")
}
b, err := json.Marshal(hashring)
if err != nil {
return nil, errors.Wrapf(err, "generate hashring file: %v", hashring)
}
if err := ioutil.WriteFile(path.Join(receiveDir, "hashrings.json"), b, 0666); err != nil {
return nil, errors.Wrap(err, "creating receive config")
}
return newCmdExec(exec.Command("thanos", "receive",
"--debug.name", fmt.Sprintf("receive-%s", http.Port),
"--grpc-address", grpc.HostPort(),
"--http-address", metric.HostPort(),
"--remote-write.address", http.HostPort(),
"--labels", fmt.Sprintf(`receive="%s"`, http.Port),
"--tsdb.path", path.Join(receiveDir, "tsdb"),
"--log.level", "debug",
"--receive.replication-factor", strconv.Itoa(replicationFactor),
"--receive.local-endpoint", remoteWriteEndpoint(http),
"--receive.hashrings-file", path.Join(receiveDir, "hashrings.json"),
"--receive.hashrings-file-refresh-interval", "5s")), nil
},
}
}
func querier(http, grpc address, storeAddresses []address, fileSDStoreAddresses []address) *serverScheduler {
const replicaLabel = "replica"
return &serverScheduler{
HTTP: http,
GRPC: grpc,
schedule: func(workDir string) (Exec, error) {
args := []string{
"query",
"--debug.name", fmt.Sprintf("querier-%s", http.Port),
"--grpc-address", grpc.HostPort(),
"--http-address", http.HostPort(),
"--log.level", "debug",
"--query.replica-label", replicaLabel,
"--store.sd-dns-interval", "5s",
}
for _, addr := range storeAddresses {
args = append(args, "--store", addr.HostPort())
}
if len(fileSDStoreAddresses) > 0 {
queryFileSDDir := path.Join(workDir, "data", "querier", http.Port)
if err := os.MkdirAll(queryFileSDDir, 0777); err != nil {
return nil, errors.Wrap(err, "create query dir failed")
}
if err := ioutil.WriteFile(queryFileSDDir+"/filesd.json", []byte(generateFileSD(fileSDStoreAddresses)), 0666); err != nil {
return nil, errors.Wrap(err, "creating query SD config failed")
}
args = append(args,
"--store.sd-files", path.Join(queryFileSDDir, "filesd.json"),
"--store.sd-interval", "5s",
)
}
return newCmdExec(exec.Command("thanos", args...)), nil
},
}
}
func storeGateway(http, grpc address, bucketConfig []byte) *serverScheduler {
return &serverScheduler{
HTTP: http,
GRPC: grpc,
schedule: func(workDir string) (Exec, error) {
dbDir := path.Join(workDir, "data", "store-gateway", http.Port)
if err := os.MkdirAll(dbDir, 0777); err != nil {
return nil, errors.Wrap(err, "creating store gateway dir failed")
}
return newCmdExec(exec.Command("thanos",
"store",
"--debug.name", fmt.Sprintf("store-gw-%s", http.Port),
"--data-dir", dbDir,
"--grpc-address", grpc.HostPort(),
"--http-address", http.HostPort(),
"--log.level", "debug",
"--objstore.config", string(bucketConfig),
// Accelerated sync time for quicker test (3m by default)
"--sync-block-duration", "5s",
)), nil
},
}
}
func alertManager(http address) *serverScheduler {
return &serverScheduler{
HTTP: http,
schedule: func(workDir string) (Exec, error) {
dir := path.Join(workDir, "data", "alertmanager", http.Port)
if err := os.MkdirAll(dir, 0777); err != nil {
return nil, errors.Wrap(err, "creating alertmanager dir failed")
}
config := `
route:
group_by: ['alertname']
group_wait: 1s
group_interval: 1s
receiver: 'null'
receivers:
- name: 'null'
`
if err := ioutil.WriteFile(dir+"/config.yaml", []byte(config), 0666); err != nil {
return nil, errors.Wrap(err, "creating alertmanager config file failed")
}
return newCmdExec(exec.Command(testutil.AlertmanagerBinary(),
"--config.file", dir+"/config.yaml",
"--web.listen-address", http.HostPort(),
"--log.level", "debug",
)), nil
},
}
}
func rule(http, grpc address, rules []string, am address, queryAddresses []address, queryFileSDAddresses []address) *serverScheduler {
return ruleWithDir(http, grpc, "", rules, am, queryAddresses, queryFileSDAddresses)
}
func ruleWithDir(http, grpc address, dir string, rules []string, am address, queryAddresses []address, queryFileSDAddresses []address) *serverScheduler {
return &serverScheduler{
HTTP: http,
GRPC: grpc,
schedule: func(workDir string) (Exec, error) {
ruleDir := path.Join(workDir, "data", "rule", http.Port)
if dir != "" {
ruleDir = dir
}
if err := os.MkdirAll(ruleDir, 0777); err != nil {
return nil, errors.Wrap(err, "creating ruler dir")
}
for i, rule := range rules {
if err := ioutil.WriteFile(path.Join(ruleDir, fmt.Sprintf("/rules-%d.yaml", i)), []byte(rule), 0666); err != nil {
return nil, errors.Wrapf(err, "writing rule %s", path.Join(ruleDir, fmt.Sprintf("/rules-%d.yaml", i)))
}
}
args := []string{
"rule",
"--debug.name", fmt.Sprintf("rule-%s", http.Port),
"--label", fmt.Sprintf(`replica="%s"`, http.Port),
"--data-dir", path.Join(ruleDir, "data"),
"--rule-file", path.Join(ruleDir, "*.yaml"),
"--eval-interval", "1s",
"--alertmanagers.url", am.URL(),
"--grpc-address", grpc.HostPort(),
"--http-address", http.HostPort(),
"--log.level", "debug",
"--query.sd-dns-interval", "5s",
}
for _, addr := range queryAddresses {
args = append(args, "--query", addr.HostPort())
}
if len(queryFileSDAddresses) > 0 {
if err := ioutil.WriteFile(path.Join(ruleDir, "filesd.json"), []byte(generateFileSD(queryFileSDAddresses)), 0666); err != nil {
return nil, errors.Wrap(err, "creating ruler filesd config")
}
args = append(args, "--query.sd-files", path.Join(ruleDir, "filesd.json"))
}
return newCmdExec(exec.Command("thanos", args...)), nil
},
}
}
type sameProcessGRPCServiceExec struct {
addr string
stdout io.Writer
stderr io.Writer
ctx context.Context
cancel context.CancelFunc
srvChan <-chan error
srv *grpc.Server
}
func (c *sameProcessGRPCServiceExec) Start(stdout io.Writer, stderr io.Writer) error {
c.stderr = stderr
c.stdout = stdout
if c.ctx != nil {
return errors.New("process already started")
}
c.ctx, c.cancel = context.WithCancel(context.Background())
l, err := net.Listen("tcp", c.addr)
if err != nil {
return errors.Wrap(err, "listen API address")
}
srvChan := make(chan error)
go func() {
defer close(srvChan)
if err := c.srv.Serve(l); err != nil {
srvChan <- err
_, _ = c.stderr.Write([]byte(fmt.Sprintf("server failed: %s", err)))
}
}()
c.srvChan = srvChan
return nil
}
func (c *sameProcessGRPCServiceExec) Wait() error {
err := <-c.srvChan
if c.ctx.Err() == nil && err != nil {
return err
}
return err
}
func (c *sameProcessGRPCServiceExec) Kill() error {
c.cancel()
c.srv.Stop()
return nil
}
func (c *sameProcessGRPCServiceExec) String() string {
return fmt.Sprintf("gRPC service %v", c.addr)
}
func fakeStoreAPI(grpcAddr address, svc storepb.StoreServer) *serverScheduler {
return &serverScheduler{
GRPC: grpcAddr,
schedule: func(_ string) (Exec, error) {
srv := grpc.NewServer()
storepb.RegisterStoreServer(srv, svc)
return &sameProcessGRPCServiceExec{addr: grpcAddr.HostPort(), srv: srv}, nil
},
}
}
func minio(http address, config s3.Config) *serverScheduler {
return &serverScheduler{
HTTP: http,
schedule: func(workDir string) (Exec, error) {
dbDir := path.Join(workDir, "data", "minio", http.Port)
if err := os.MkdirAll(dbDir, 0777); err != nil {
return nil, errors.Wrap(err, "creating minio dir failed")
}
cmd := exec.Command(testutil.MinioBinary(),
"server",
"--address", http.HostPort(),
dbDir,
)
cmd.Env = append(os.Environ(),
fmt.Sprintf("MINIO_ACCESS_KEY=%s", config.AccessKey),
fmt.Sprintf("MINIO_SECRET_KEY=%s", config.SecretKey))
return newCmdExec(cmd), nil
},
}
}
// NOTE: It is important to install Thanos before using this function to compile latest changes.
// This means that export GOCACHE=/unique/path is must have to avoid having this test cached locally.
func e2eSpinup(t testing.TB, ctx context.Context, cmds ...scheduler) (exit chan struct{}, err error) {
return e2eSpinupWithS3ObjStorage(t, ctx, address{}, nil, cmds...)
}
func e2eSpinupWithS3ObjStorage(t testing.TB, ctx context.Context, minioAddr address, s3Config *s3.Config, cmds ...scheduler) (exit chan struct{}, err error) {
dir, err := ioutil.TempDir("", "spinup_test")
if err != nil {
return nil, err
}
defer func() {
if err != nil {
if rerr := os.RemoveAll(dir); rerr != nil {
t.Log(rerr)
}
}
}()
var s3Exit chan struct{}
if s3Config != nil {
s3Exit, err = e2eSpinupWithS3ObjStorage(t, ctx, address{}, nil, minio(minioAddr, *s3Config))
if err != nil {
return nil, errors.Wrap(err, "start minio")
}
ctx, cancel := context.WithCancel(ctx)
if err := runutil.Retry(time.Second, ctx.Done(), func() error {
select {
case <-s3Exit:
cancel()
return nil
default:
}
bkt, _, err := s3.NewTestBucketFromConfig(t, "eu-west1", *s3Config, false)
if err != nil {
return errors.Wrap(err, "create bkt client for minio healthcheck")
}
return bkt.Close()
}); err != nil {
return nil, errors.Wrap(err, "minio not ready in time")
}
}
var g run.Group
// Interrupt go routine.
{
ctx, cancel := context.WithCancel(ctx)
g.Add(func() error {
// This go routine will return only when:
// 1) Any other process from group exited unexpectedly
// 2) Global context will be cancelled.
// 3) Minio (if started) exited unexpectedly.
if s3Exit != nil {
select {
case <-ctx.Done():
case <-s3Exit:
}
return nil
}
<-ctx.Done()
return nil
}, func(error) {
cancel()
if err := os.RemoveAll(dir); err != nil {
t.Log(err)
}
})
}
var stdFiles []*os.File
// Run go routine for each command.
for _, command := range cmds {
c, err := command.Schedule(dir)
if err != nil {
return nil, err
}
// Store buffers in temp files to avoid excessive memory consumption.
stdout, err := ioutil.TempFile(dir, "stdout")
if err != nil {
return nil, errors.Wrap(err, "create file for stdout")
}
stderr, err := ioutil.TempFile(dir, "stderr")
if err != nil {
return nil, errors.Wrap(err, "create file for stderr")
}
stdFiles = append(stdFiles, stdout, stderr)
if err := c.Start(stdout, stderr); err != nil {
// Let already started commands finish.
go func() { _ = g.Run() }()
return nil, errors.Wrap(err, "start")
}
cmd := c
g.Add(func() error {
return errors.Wrap(cmd.Wait(), cmd.String())
}, func(error) {
// This's accepted scenario to kill a process immediately for sure and run tests as fast as possible.
_ = cmd.Kill()
})
}
exit = make(chan struct{})
go func(g run.Group) {
if err := g.Run(); err != nil && ctx.Err() == nil {
t.Errorf("Some process exited unexpectedly: %v", err)
}
if s3Exit != nil {
<-s3Exit
}
printAndCloseFiles(t, stdFiles)
close(exit)
}(g)
return exit, nil
}
func generateFileSD(addresses []address) string {
conf := "[ { \"targets\": ["
for index, addr := range addresses {
conf += fmt.Sprintf("\"%s\"", addr.HostPort())
if index+1 < len(addresses) {
conf += ","
}
}
conf += "] } ]"
return conf
}
func printAndCloseFiles(t testing.TB, files []*os.File) {
defer func() {
for _, f := range files {
_ = f.Close()
}
}()
for _, f := range files {
info, err := f.Stat()
if err != nil {
t.Error(err)
}
if info.Size() == 0 {
continue
}
if _, err := f.Seek(0, 0); err != nil {
t.Error(err)
}
t.Logf("-------------------------------------------")
t.Logf("------------------- %s ------------------", f.Name())
t.Logf("-------------------------------------------")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
t.Log(scanner.Text())
}
if err := scanner.Err(); err != nil {
t.Error(err)
}
}
}