-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_test.go
73 lines (62 loc) · 1.58 KB
/
setup_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 otfranz
import (
"context"
"errors"
"fmt"
"os"
"strings"
"testing"
"github.com/twmb/franz-go/pkg/kerr"
"github.com/twmb/franz-go/pkg/kgo"
"github.com/twmb/franz-go/pkg/kmsg"
)
func TestMain(m *testing.M) {
var cleanup func()
if os.Getenv("KAFKA_ADDR") != "" {
cleanup = setupTopic(os.Getenv("KAFKA_ADDR"))
}
code := m.Run()
if cleanup != nil {
cleanup()
}
os.Exit(code)
}
func testTopics(topics ...string) (*kmsg.CreateTopicsRequest, *kmsg.DeleteTopicsRequest) {
creq := kmsg.NewPtrCreateTopicsRequest()
dreq := kmsg.NewPtrDeleteTopicsRequest()
dreq.TopicNames = topics
for _, topic := range topics {
creqTopic := kmsg.NewCreateTopicsRequestTopic()
creqTopic.Topic = topic
creqTopic.NumPartitions = 1
creqTopic.ReplicationFactor = 1
creq.Topics = append(creq.Topics, creqTopic)
dreqTopic := kmsg.NewDeleteTopicsRequestTopic()
dreqTopic.Topic = kmsg.StringPtr(topic)
dreq.Topics = append(dreq.Topics, dreqTopic)
}
return creq, dreq
}
func setupTopic(addr string) func() {
topics := []string{"test", "tracing", "example", "foo", "bar"}
adm, err := kgo.NewClient(kgo.SeedBrokers(strings.Split(addr, ",")...))
if err != nil {
panic(fmt.Sprintf("unable to create admin client: %v", err))
}
creq, dreq := testTopics(topics...)
resp, err := creq.RequestWith(context.Background(), adm)
if err == nil {
err = kerr.ErrorForCode(resp.Topics[0].ErrorCode)
}
if err != nil {
if !errors.Is(err, kerr.TopicAlreadyExists) {
panic(err)
}
}
return func() {
_, err := dreq.RequestWith(context.Background(), adm)
if err != nil {
panic(err)
}
}
}