-
Notifications
You must be signed in to change notification settings - Fork 939
Expand file tree
/
Copy pathredpandatest.go
More file actions
101 lines (84 loc) · 3.01 KB
/
redpandatest.go
File metadata and controls
101 lines (84 loc) · 3.01 KB
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
// Copyright 2025 Redpanda Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redpandatest
import (
"testing"
"github.com/testcontainers/testcontainers-go"
tcredpanda "github.com/testcontainers/testcontainers-go/modules/redpanda"
)
// Endpoints contains the endpoints for the Redpanda container.
type Endpoints struct {
BrokerAddr string
SchemaRegistryURL string
}
// Config contains configuration for starting a Redpanda broker.
type Config struct {
// Nightly uses the nightly Redpanda image instead of the latest stable image.
Nightly bool
// AutoCreateTopics enables automatic topic creation.
AutoCreateTopics bool
// ExtraOpts are additional testcontainers options applied after the default ones.
ExtraOpts []testcontainers.ContainerCustomizer
}
// DefaultConfig returns the default configuration for starting a Redpanda broker.
var DefaultConfig = Config{
AutoCreateTopics: true,
}
// StartSingleBroker starts a single Redpanda broker with default configuration.
// It exposes the broker port and enables auto-create topics by default.
func StartSingleBroker(t *testing.T) (Endpoints, testcontainers.Container, error) {
t.Helper()
return StartSingleBrokerWithConfig(t, DefaultConfig)
}
// StartSingleBrokerWithConfig starts a single Redpanda broker with custom configuration.
func StartSingleBrokerWithConfig(t *testing.T, cfg Config) (Endpoints, testcontainers.Container, error) {
t.Helper()
img := "docker.redpanda.com/redpandadata/redpanda:latest"
if cfg.Nightly {
img = "docker.redpanda.com/redpandadata/redpanda-nightly:latest"
}
var opts []testcontainers.ContainerCustomizer
if cfg.AutoCreateTopics {
opts = append(opts, tcredpanda.WithAutoCreateTopics())
}
opts = append(opts, cfg.ExtraOpts...)
ctr, err := tcredpanda.Run(t.Context(), img, opts...)
testcontainers.CleanupContainer(t, ctr)
if err != nil {
return Endpoints{}, ctr, err
}
broker, err := ctr.KafkaSeedBroker(t.Context())
if err != nil {
return Endpoints{}, ctr, err
}
srAddr, err := ctr.SchemaRegistryAddress(t.Context())
if err != nil {
return Endpoints{}, ctr, err
}
return Endpoints{
BrokerAddr: broker,
SchemaRegistryURL: srAddr,
}, ctr, nil
}
// StartRedpanda starts a Redpanda container.
//
// Deprecated: Use StartSingleBroker or StartSingleBrokerWithConfig instead.
func StartRedpanda(t *testing.T, autocreateTopics bool) (Endpoints, error) {
t.Helper()
cfg := Config{
AutoCreateTopics: autocreateTopics,
}
endpoints, _, err := StartSingleBrokerWithConfig(t, cfg)
return endpoints, err
}