forked from danielqsj/kafka_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_test.go
73 lines (62 loc) · 1.29 KB
/
simple_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
package main
import (
"errors"
"io/ioutil"
"log"
"net/http"
"testing"
"time"
"github.com/Shopify/sarama"
)
var bootstrap_servers = []string{"localhost:9092"}
func TestSmoke(t *testing.T) {
log.Print("testing " + t.Name())
if !assumeKafka() {
t.Skip("Kafka is not running ... skipping the test")
return
}
go runServer()
execute(func(resp *http.Response) {
log.Println(resp.Status)
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
} else {
log.Println(string(bytes))
}
})
}
func assumeKafka() bool {
client, err := sarama.NewClient(bootstrap_servers, nil)
if err != nil {
return false
}
defer client.Close()
_, err = client.Topics()
if err != nil {
return false
}
return true
}
func execute(handler func(response *http.Response)) {
var e = errors.New("dummy")
for e != nil {
resp, err := http.Get("http://localhost:9304/metrics")
if err != nil {
time.Sleep(time.Millisecond * 100)
}
e = err
if resp != nil {
handler(resp)
}
}
}
func runServer() {
opts := kafkaOpts{}
opts.uri = bootstrap_servers
opts.uriZookeeper = []string{"localhost:2181"}
opts.kafkaVersion = sarama.V1_0_0_0.String()
opts.metadataRefreshInterval = "30s"
setup("localhost:9304", "/metrics", ".*", ".*", false, opts, nil)
}