-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathapi_suite_test.go
More file actions
199 lines (165 loc) · 5.44 KB
/
api_suite_test.go
File metadata and controls
199 lines (165 loc) · 5.44 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
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
// Copyright 2023 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0
//go:build integration
package integration
import (
"context"
"math/rand"
"net"
"strconv"
"testing"
"time"
"github.com/cloudhut/common/rest"
adminapi "github.com/redpanda-data/common-go/rpadmin"
"github.com/redpanda-data/common-go/rpsr"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/redpanda"
"github.com/testcontainers/testcontainers-go/network"
"github.com/twmb/franz-go/pkg/kadm"
"github.com/twmb/franz-go/pkg/kgo"
"github.com/twmb/franz-go/pkg/sr"
"github.com/redpanda-data/console/backend/pkg/api"
"github.com/redpanda-data/console/backend/pkg/config"
"github.com/redpanda-data/console/backend/pkg/testutil"
)
type APISuite struct {
suite.Suite
redpandaContainer *redpanda.Container
kConnectContainer testcontainers.Container
network *testcontainers.DockerNetwork
kafkaClient *kgo.Client
kafkaAdminClient *kadm.Client
redpandaAdminClient *adminapi.AdminAPI
schemaRegistryClient *rpsr.Client
cfg *config.Config
api *api.API
testSeedBroker string
}
func TestSuite(t *testing.T) {
suite.Run(t, &APISuite{})
}
func (s *APISuite) SetupSuite() {
t := s.T()
require := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
// 1. setup docker network
ntw, err := network.New(ctx)
require.NoError(err)
s.network = ntw
// 2. Start Redpanda Docker container
container, err := redpanda.Run(ctx,
testutil.RedpandaImage(),
redpanda.WithEnableWasmTransform(),
network.WithNetwork([]string{"redpanda"}, s.network),
redpanda.WithListener("redpanda:29092"),
)
testutil.LogContainerLogsIfFailed(ctx, t, container, err)
require.NoError(err)
s.redpandaContainer = container
// 2. Retrieve Redpanda connection details
seedBroker, err := container.KafkaSeedBroker(ctx)
require.NoError(err)
schemaRegistryAddress, err := container.SchemaRegistryAddress(ctx)
require.NoError(err)
adminApiAddr, err := container.AdminAPIAddress(ctx)
require.NoError(err)
require.NoError(err)
s.testSeedBroker = seedBroker
// 3. Start KafkaConnect Docker container
kConnectContainer, err := testutil.RunRedpandaConnectorsContainer(
ctx,
[]string{"redpanda:29092"},
network.WithNetwork([]string{"kconnect"}, s.network),
)
testutil.LogContainerLogsIfFailed(ctx, t, kConnectContainer, err)
require.NoError(err)
s.kConnectContainer = kConnectContainer
// 4. Create Kafka clients
s.kafkaClient, s.kafkaAdminClient = testutil.CreateClients(t, []string{seedBroker})
kConnectClusterURL, err := kConnectContainer.PortEndpoint(ctx, "8083/tcp", "http")
require.NoError(err)
// 5. Create Redpanda client
adminApiClient, err := adminapi.NewAdminAPI([]string{adminApiAddr}, &adminapi.NopAuth{}, nil)
require.NoError(err)
s.redpandaAdminClient = adminApiClient
srCl, err := sr.NewClient(sr.URLs(schemaRegistryAddress))
require.NoError(err)
rpsrCl, err := rpsr.NewClient(srCl)
require.NoError(err)
s.schemaRegistryClient = rpsrCl
// 5. Configure & start Redpanda Console
httpListenPort := rand.Intn(50000) + 10000
s.cfg = &config.Config{}
s.cfg.SetDefaults()
s.cfg.ServeFrontend = false
s.cfg.REST = config.Server{
Config: rest.Config{
HTTPListenAddress: "0.0.0.0",
HTTPListenPort: httpListenPort,
},
}
s.cfg.Kafka.Brokers = []string{s.testSeedBroker}
s.cfg.SchemaRegistry.Enabled = true
s.cfg.SchemaRegistry.URLs = []string{schemaRegistryAddress}
s.cfg.Redpanda.AdminAPI.Enabled = true
s.cfg.Redpanda.AdminAPI.URLs = []string{adminApiAddr}
s.cfg.Redpanda.AdminAPI.TLS.Enabled = false
s.cfg.KafkaConnect = config.KafkaConnect{
Enabled: true,
Clusters: []config.KafkaConnectCluster{
{
Name: "connect-cluster",
URL: kConnectClusterURL,
},
},
}
s.api, err = api.New(s.cfg)
require.NoError(err)
go func() {
if err := s.api.Start(context.Background()); err != nil {
s.T().Errorf("failed to start API: %v", err)
}
}()
// 5. Wait until Console API is up
httpServerAddress := net.JoinHostPort("localhost", strconv.Itoa(httpListenPort))
retries := 60
for retries > 0 {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
if _, err := (&net.Dialer{}).DialContext(ctx, "tcp", httpServerAddress); err == nil {
cancel()
break
}
cancel()
time.Sleep(100 * time.Millisecond)
retries--
}
}
func (s *APISuite) TearDownSuite() {
t := s.T()
assert := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
// 1. Terminate KafkaConnect container
assert.NoError(s.kConnectContainer.Terminate(ctx))
// 2. Terminate Redpanda container
assert.NoError(s.redpandaContainer.Terminate(ctx))
// 3. Remove docker network
assert.NoError(s.network.Remove(ctx))
// 4. Stop API
assert.NoError(s.api.Stop(ctx))
}
func (s *APISuite) httpAddress() string {
if s.api.Cfg.REST.TLS.Enabled {
return "https://" + s.api.Cfg.REST.HTTPListenAddress + ":" + strconv.FormatInt(int64(s.api.Cfg.REST.HTTPSListenPort), 10)
}
return "http://" + s.api.Cfg.REST.HTTPListenAddress + ":" + strconv.FormatInt(int64(s.api.Cfg.REST.HTTPListenPort), 10)
}