-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_test.go
More file actions
456 lines (405 loc) · 14.5 KB
/
module_test.go
File metadata and controls
456 lines (405 loc) · 14.5 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
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
package eventbus_test
import (
"context"
"os"
"strings"
"testing"
"time"
tcgo "github.com/testcontainers/testcontainers-go"
postgrestc "github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
eventbus "github.com/GoCodeAlone/workflow-plugin-eventbus"
eventbusv1 "github.com/GoCodeAlone/workflow-plugin-eventbus/gen"
)
// ── ClusterModuleFactory (TypedModuleProvider) ────────────────────────────────
func TestClusterModuleFactory_TypedModuleTypes(t *testing.T) {
f := &eventbus.ClusterModuleFactory{}
types := f.TypedModuleTypes()
if len(types) != 1 || types[0] != "eventbus.broker" {
t.Errorf("TypedModuleTypes() = %v, want [eventbus.broker]", types)
}
}
func TestClusterModuleFactory_CreateTypedModule_WrongType(t *testing.T) {
f := &eventbus.ClusterModuleFactory{}
_, err := f.CreateTypedModule("eventbus.stream", "x", nil)
if err == nil {
t.Fatal("expected error for wrong type")
}
}
func TestClusterModuleFactory_CreateTypedModule_NilConfig(t *testing.T) {
f := &eventbus.ClusterModuleFactory{}
// nil config → ClusterConfig zero value → empty provider → expect error
_, err := f.CreateTypedModule("eventbus.broker", "bus-factory-nil", nil)
if err == nil {
t.Fatal("expected error from NewClusterModule for empty provider")
}
}
// ── NewClusterModule validation ───────────────────────────────────────────────
func TestNewClusterModule_ValidConfig(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
DeployTarget: "digitalocean.app_platform",
}
m, err := eventbus.NewClusterModule("bus-valid", cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if m == nil {
t.Fatal("expected non-nil module")
}
}
func TestNewClusterModule_EmptyProvider(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
DeployTarget: "digitalocean.app_platform",
}
_, err := eventbus.NewClusterModule("bus-empty-provider", cfg)
if err == nil {
t.Fatal("expected error for empty provider")
}
}
func TestNewClusterModule_EmptyDeployTarget(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
}
_, err := eventbus.NewClusterModule("bus-empty-target", cfg)
if err == nil {
t.Fatal("expected error for empty deploy_target")
}
}
func TestNewClusterModule_UnsupportedProviderTarget(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "kinesis",
DeployTarget: "digitalocean.app_platform", // kinesis only supports aws.kinesis
}
_, err := eventbus.NewClusterModule("bus-bad-combo", cfg)
if err == nil {
t.Fatal("expected error for unsupported provider × target combination")
}
}
// ── clusterModule lifecycle ───────────────────────────────────────────────────
func TestClusterModule_InitRegistersConfig(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
DeployTarget: "digitalocean.app_platform",
}
m, err := eventbus.NewClusterModule("bus-init-reg", cfg)
if err != nil {
t.Fatalf("create: %v", err)
}
if err := m.Init(); err != nil {
t.Fatalf("init: %v", err)
}
t.Cleanup(func() { _ = m.Stop(context.Background()) })
got, ok := eventbus.GetCluster("bus-init-reg")
if !ok {
t.Fatal("cluster not found in registry after Init")
}
if got.GetProvider() != "nats" {
t.Errorf("provider = %q, want nats", got.GetProvider())
}
}
func TestClusterModule_StopUnregisters(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
DeployTarget: "digitalocean.app_platform",
}
m, err := eventbus.NewClusterModule("bus-stop-unreg", cfg)
if err != nil {
t.Fatalf("create: %v", err)
}
_ = m.Init()
_ = m.Stop(context.Background())
_, ok := eventbus.GetCluster("bus-stop-unreg")
if ok {
t.Fatal("cluster still in registry after Stop")
}
}
func TestClusterModule_InitRegistersURIFromNATSURL(t *testing.T) {
t.Setenv("NATS_URL", "nats://test-host:4222")
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
DeployTarget: "digitalocean.app_platform",
}
m, _ := eventbus.NewClusterModule("bus-nats-url", cfg)
_ = m.Init()
t.Cleanup(func() { _ = m.Stop(context.Background()) })
uri, ok := eventbus.GetBusURI("bus-nats-url")
if !ok {
t.Fatal("expected URI in registry when NATS_URL is set")
}
if uri != "nats://test-host:4222" {
t.Errorf("uri = %q, want nats://test-host:4222", uri)
}
}
func TestClusterModule_InitRegistersURIFromInstanceEnvVar(t *testing.T) {
t.Setenv("EVENTBUS_BMW_EVENTBUS_URI", "nats://bmw-host:4222")
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
DeployTarget: "digitalocean.app_platform",
}
m, _ := eventbus.NewClusterModule("bmw-eventbus", cfg)
_ = m.Init()
t.Cleanup(func() { _ = m.Stop(context.Background()) })
uri, ok := eventbus.GetBusURI("bmw-eventbus")
if !ok {
t.Fatal("expected URI in registry when instance env var is set")
}
if uri != "nats://bmw-host:4222" {
t.Errorf("uri = %q, want nats://bmw-host:4222", uri)
}
}
func TestClusterModule_InitNoURIWhenEnvNotSet(t *testing.T) {
// Unset both vars only for the duration of this test, restoring any
// pre-existing value on exit. Bare os.Unsetenv would permanently remove
// NATS_URL from the process environment, breaking tests that run after.
if prev, ok := os.LookupEnv("NATS_URL"); ok {
t.Cleanup(func() { os.Setenv("NATS_URL", prev) })
os.Unsetenv("NATS_URL")
}
if prev, ok := os.LookupEnv("EVENTBUS_BUS_NO_URI_URI"); ok {
t.Cleanup(func() { os.Setenv("EVENTBUS_BUS_NO_URI_URI", prev) })
os.Unsetenv("EVENTBUS_BUS_NO_URI_URI")
}
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
DeployTarget: "digitalocean.app_platform",
}
m, _ := eventbus.NewClusterModule("bus-no-uri", cfg)
_ = m.Init()
t.Cleanup(func() { _ = m.Stop(context.Background()) })
_, ok := eventbus.GetBusURI("bus-no-uri")
if ok {
t.Fatal("expected no URI in registry when env vars are absent")
}
}
// TestClusterModule_StopEvictsNATSConn verifies that Stop() evicts a connection
// pre-seeded into the cache (simulating a step or trigger that dialled on behalf
// of the module). Wire-level close behaviour is exercised by the integration test;
// here we only verify cache eviction using a nil sentinel entry.
func TestClusterModule_StopEvictsNATSConn(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
DeployTarget: "digitalocean.app_platform",
}
m, err := eventbus.NewClusterModule("bus-conn-evict", cfg)
if err != nil {
t.Fatalf("create: %v", err)
}
_ = m.Init()
// Pre-seed with a nil sentinel (nil is safe: closeNATSConn guards against nil).
eventbus.RegisterNATSConn("bus-conn-evict", nil)
_, inCache := eventbus.GetNATSConn("bus-conn-evict")
if !inCache {
t.Fatal("expected sentinel in cache before Stop")
}
_ = m.Stop(context.Background())
_, inCacheAfter := eventbus.GetNATSConn("bus-conn-evict")
if inCacheAfter {
t.Fatal("expected sentinel evicted from cache after Stop")
}
}
// ── broker instance registry + LookupRuntime ─────────────────────────────────
//
// Tests of LookupBrokerInstance / LookupRuntime semantics that don't need to
// hand-construct a *clusterModule (which is unexported) live here. Construction-
// dependent tests live in module_internal_test.go alongside the package types.
func TestBrokerInstanceRegistry_LookupNotFound(t *testing.T) {
if m, ok := eventbus.LookupBrokerInstance("does-not-exist"); ok || m != nil {
t.Fatalf("expected (nil, false) for unknown name; got (%v, %v)", m, ok)
}
}
func TestLookupRuntime_NotRegistered(t *testing.T) {
_, _, err := eventbus.LookupRuntime("not-registered-broker")
if err == nil {
t.Fatal("expected error for unregistered broker")
}
if !strings.Contains(err.Error(), "not registered") {
t.Errorf("error = %q, want substring \"not registered\"", err.Error())
}
}
// ── Start runtime selection (legacy nats fallback) ───────────────────────────
//
// TestClusterModule_StartSelectsNats verifies the provider==nats branch of
// Start: a NATS URL is required (cfg.Dsn or NATS_URL env fallback). The test
// skips when no NATS broker is reachable so it can run on developer laptops
// without infrastructure.
func TestClusterModule_StartSelectsNats(t *testing.T) {
natsURL := os.Getenv("NATS_URL")
if natsURL == "" {
t.Skip("NATS_URL not set; skipping live-broker Start test")
}
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
DeployTarget: "digitalocean.app_platform",
Dsn: natsURL,
}
m, err := eventbus.NewClusterModule("bus-start-nats", cfg)
if err != nil {
t.Fatalf("create: %v", err)
}
if err := m.Init(); err != nil {
t.Fatalf("init: %v", err)
}
t.Cleanup(func() { _ = m.Stop(context.Background()) })
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := m.Start(ctx); err != nil {
t.Fatalf("start: %v", err)
}
rt, conn, err := eventbus.LookupRuntime("bus-start-nats")
if err != nil {
t.Fatalf("LookupRuntime after Start: %v", err)
}
if rt == nil || conn == nil {
t.Fatal("expected non-nil runtime + conn after Start")
}
if got := conn.Provider(); got != "nats" {
t.Errorf("Connection.Provider() = %q, want \"nats\"", got)
}
}
// ── Per-provider validation (Task 9.5) ───────────────────────────────────────
//
// NewClusterModule's validation diverges by provider — pgchannel rejects
// deploy_target-style configs and requires broker_target + dsn; nats/kafka/
// kinesis continue to require deploy_target as before.
func TestNewClusterModule_PgchannelRequiresBrokerTarget(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "pgchannel",
Dsn: "postgres://x@y/z",
// BrokerTarget intentionally omitted
}
_, err := eventbus.NewClusterModule("bus-pg-no-target", cfg)
if err == nil {
t.Fatal("expected error when pgchannel broker_target is missing")
}
if !strings.Contains(err.Error(), "broker_target=in_process") {
t.Errorf("error = %q, want substring \"broker_target=in_process\"", err.Error())
}
}
func TestNewClusterModule_PgchannelRequiresDsn(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "pgchannel",
BrokerTarget: "in_process",
// Dsn intentionally omitted
}
_, err := eventbus.NewClusterModule("bus-pg-no-dsn", cfg)
if err == nil {
t.Fatal("expected error when pgchannel dsn is missing")
}
if !strings.Contains(err.Error(), "dsn") {
t.Errorf("error = %q, want substring \"dsn\"", err.Error())
}
}
func TestNewClusterModule_PgchannelValid(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "pgchannel",
BrokerTarget: "in_process",
Dsn: "postgres://eventbus:eventbus@localhost:5432/eventbus_test",
}
m, err := eventbus.NewClusterModule("bus-pg-valid", cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if m == nil {
t.Fatal("expected non-nil module")
}
}
func TestNewClusterModule_NatsStillRequiresDeployTarget(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "nats",
// DeployTarget intentionally omitted
}
_, err := eventbus.NewClusterModule("bus-nats-no-target", cfg)
if err == nil {
t.Fatal("expected error when nats deploy_target is missing")
}
if !strings.Contains(err.Error(), "deploy_target") {
t.Errorf("error = %q, want substring \"deploy_target\"", err.Error())
}
}
func TestNewClusterModule_UnsupportedProvider(t *testing.T) {
cfg := &eventbusv1.ClusterConfig{
Provider: "redis",
DeployTarget: "digitalocean.app_platform",
}
_, err := eventbus.NewClusterModule("bus-redis", cfg)
if err == nil {
t.Fatal("expected error for unsupported provider")
}
if !strings.Contains(err.Error(), "unsupported provider") {
t.Errorf("error = %q, want substring \"unsupported provider\"", err.Error())
}
}
// ── Start runtime selection (pgchannel — live container) ─────────────────────
//
// TestClusterModule_StartSelectsPgchannel verifies the provider==pgchannel
// branch of Start: cfg.Dsn carries a Postgres DSN; broker_target is
// "in_process". Uses testcontainers Postgres inline because the pgchannel
// testutil lives in internal/testutil and is unimportable from this
// external test package. Skips when Docker is unavailable.
func TestClusterModule_StartSelectsPgchannel(t *testing.T) {
tcgo.SkipIfProviderIsNotHealthy(t)
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
container, err := postgrestc.Run(ctx,
"postgres:16-alpine",
postgrestc.WithDatabase("eventbus_test"),
postgrestc.WithUsername("eventbus"),
postgrestc.WithPassword("eventbus"),
tcgo.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(60*time.Second),
),
)
if err != nil {
t.Fatalf("start postgres container: %v", err)
}
t.Cleanup(func() {
stopCtx, stopCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer stopCancel()
_ = container.Terminate(stopCtx)
})
dsn, err := container.ConnectionString(ctx, "sslmode=disable")
if err != nil {
t.Fatalf("container connection string: %v", err)
}
cfg := &eventbusv1.ClusterConfig{
Provider: "pgchannel",
BrokerTarget: "in_process",
Dsn: dsn,
}
m, err := eventbus.NewClusterModule("bus-start-pg", cfg)
if err != nil {
t.Fatalf("create: %v", err)
}
if err := m.Init(); err != nil {
t.Fatalf("init: %v", err)
}
// Stop guard: register cleanup before Start so a Start failure still
// triggers cleanup of any partial state. Stop is idempotent.
t.Cleanup(func() { _ = m.Stop(context.Background()) })
startCtx, startCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer startCancel()
if err := m.Start(startCtx); err != nil {
t.Fatalf("start: %v", err)
}
rt, conn, err := eventbus.LookupRuntime("bus-start-pg")
if err != nil {
t.Fatalf("LookupRuntime after Start: %v", err)
}
if rt == nil || conn == nil {
t.Fatal("expected non-nil runtime + conn after Start")
}
if got := conn.Provider(); got != "pgchannel" {
t.Errorf("Connection.Provider() = %q, want \"pgchannel\"", got)
}
// After Stop the broker should be unregistered and LookupRuntime should
// fail with "not registered".
if err := m.Stop(context.Background()); err != nil {
t.Fatalf("stop: %v", err)
}
if _, _, err := eventbus.LookupRuntime("bus-start-pg"); err == nil {
t.Fatal("expected LookupRuntime to fail after Stop")
}
}