Skip to content

Commit 90603ed

Browse files
committed
Adding auth to couchbase initCluster functions to support container reuse
1 parent 4a668c6 commit 90603ed

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

modules/couchbase/couchbase.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,12 @@ func (c *CouchbaseContainer) waitUntilNodeIsOnline(ctx context.Context) error {
218218
WithStatusCodeMatcher(func(status int) bool {
219219
return status == http.StatusOK
220220
}).
221+
WithBasicAuth(c.config.username, c.config.password).
221222
WaitUntilReady(ctx, c)
222223
}
223224

224225
func (c *CouchbaseContainer) initializeIsEnterprise(ctx context.Context) error {
225-
response, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools", http.MethodGet, nil, false)
226+
response, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools", http.MethodGet, nil)
226227
if err != nil {
227228
return err
228229
}
@@ -251,7 +252,7 @@ func (c *CouchbaseContainer) renameNode(ctx context.Context) error {
251252
"hostname": hostname,
252253
}
253254

254-
_, err = c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/rename", http.MethodPost, body, false)
255+
_, err = c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/rename", http.MethodPost, body)
255256

256257
return err
257258
}
@@ -260,7 +261,7 @@ func (c *CouchbaseContainer) initializeServices(ctx context.Context) error {
260261
body := map[string]string{
261262
"services": c.getEnabledServices(),
262263
}
263-
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/setupServices", http.MethodPost, body, false)
264+
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/setupServices", http.MethodPost, body)
264265

265266
return err
266267
}
@@ -281,7 +282,7 @@ func (c *CouchbaseContainer) setMemoryQuotas(ctx context.Context) error {
281282
}
282283
}
283284

284-
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools/default", http.MethodPost, body, false)
285+
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools/default", http.MethodPost, body)
285286

286287
return err
287288
}
@@ -293,7 +294,7 @@ func (c *CouchbaseContainer) configureAdminUser(ctx context.Context) error {
293294
"port": "SAME",
294295
}
295296

296-
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/settings/web", http.MethodPost, body, false)
297+
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/settings/web", http.MethodPost, body)
297298

298299
return err
299300
}
@@ -352,7 +353,7 @@ func (c *CouchbaseContainer) configureExternalPorts(ctx context.Context) error {
352353
body["eventingSSL"] = eventingSSL.Port()
353354
}
354355

355-
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/setupAlternateAddresses/external", http.MethodPut, body, true)
356+
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/setupAlternateAddresses/external", http.MethodPut, body)
356357

357358
return err
358359
}
@@ -370,7 +371,7 @@ func (c *CouchbaseContainer) configureIndexer(ctx context.Context) error {
370371
"storageMode": string(c.config.indexStorageMode),
371372
}
372373

373-
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/settings/indexes", http.MethodPost, body, true)
374+
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/settings/indexes", http.MethodPost, body)
374375

375376
return err
376377
}
@@ -473,7 +474,7 @@ func (c *CouchbaseContainer) isPrimaryIndexOnline(ctx context.Context, bucket bu
473474
}
474475

475476
err := backoff.Retry(func() error {
476-
response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body, true)
477+
response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body)
477478
if err != nil {
478479
return err
479480
}
@@ -494,7 +495,7 @@ func (c *CouchbaseContainer) createPrimaryIndex(ctx context.Context, bucket buck
494495
"statement": "CREATE PRIMARY INDEX on `" + bucket.name + "`",
495496
}
496497
err := backoff.Retry(func() error {
497-
response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body, true)
498+
response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body)
498499
firstError := gjson.Get(string(response), "errors.0.code").Int()
499500
if firstError != 0 {
500501
return errors.New("index creation failed")
@@ -510,7 +511,7 @@ func (c *CouchbaseContainer) isQueryKeyspacePresent(ctx context.Context, bucket
510511
}
511512

512513
err := backoff.Retry(func() error {
513-
response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body, true)
514+
response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body)
514515
if err != nil {
515516
return err
516517
}
@@ -556,12 +557,12 @@ func (c *CouchbaseContainer) createBucket(ctx context.Context, bucket bucket) er
556557
"replicaNumber": strconv.Itoa(bucket.numReplicas),
557558
}
558559

559-
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools/default/buckets", http.MethodPost, body, true)
560+
_, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools/default/buckets", http.MethodPost, body)
560561

561562
return err
562563
}
563564

564-
func (c *CouchbaseContainer) doHTTPRequest(ctx context.Context, port, path, method string, body map[string]string, auth bool) ([]byte, error) {
565+
func (c *CouchbaseContainer) doHTTPRequest(ctx context.Context, port, path, method string, body map[string]string) ([]byte, error) {
565566
form := url.Values{}
566567
for k, v := range body {
567568
form.Set(k, v)
@@ -581,10 +582,7 @@ func (c *CouchbaseContainer) doHTTPRequest(ctx context.Context, port, path, meth
581582
}
582583

583584
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
584-
585-
if auth {
586-
request.SetBasicAuth(c.config.username, c.config.password)
587-
}
585+
request.SetBasicAuth(c.config.username, c.config.password)
588586

589587
response, err := http.DefaultClient.Do(request)
590588
if err != nil {

modules/couchbase/couchbase_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,56 @@ func TestCouchbaseWithEnterpriseContainer(t *testing.T) {
6464
testBucketUsage(t, cluster.Bucket(bucketName))
6565
}
6666

67+
type reusableCouchbase struct{}
68+
69+
func (c *reusableCouchbase) Customize(req *testcontainers.GenericContainerRequest) error {
70+
// Enable container reuse
71+
req.Reuse = true
72+
req.Name = "couchbase"
73+
return nil
74+
}
75+
76+
func TestCouchbaseWithReuse(t *testing.T) {
77+
ctx := context.Background()
78+
79+
bucketName := "testBucket"
80+
bucket := tccouchbase.NewBucket(bucketName).
81+
WithQuota(100).
82+
WithReplicas(0).
83+
WithFlushEnabled(true).
84+
WithPrimaryIndex(true)
85+
ctr, err := tccouchbase.Run(ctx,
86+
enterpriseEdition,
87+
tccouchbase.WithBuckets(bucket),
88+
&reusableCouchbase{},
89+
)
90+
testcontainers.CleanupContainer(t, ctr)
91+
require.NoError(t, err)
92+
93+
cluster, err := connectCluster(ctx, ctr)
94+
require.NoError(t, err)
95+
96+
testBucketUsage(t, cluster.Bucket(bucketName))
97+
98+
// Test reuse when first container has had time to fully start up and be configured with auth
99+
// Without enabling auth on the initCluster functions the reuse of this container fails with
100+
// "init cluster: context deadline exceeded".
101+
// This is due to the management endpoints requiring the Basic Auth headers once configureAdminUser
102+
// has completed.
103+
reusedCtr, err := tccouchbase.Run(ctx,
104+
enterpriseEdition,
105+
&reusableCouchbase{},
106+
)
107+
testcontainers.CleanupContainer(t, ctr)
108+
require.NoError(t, err)
109+
require.Equal(t, ctr.GetContainerID(), reusedCtr.GetContainerID())
110+
111+
cluster, err = connectCluster(ctx, reusedCtr)
112+
require.NoError(t, err)
113+
114+
testBucketUsage(t, cluster.Bucket(bucketName))
115+
}
116+
67117
func TestWithCredentials(t *testing.T) {
68118
ctx := context.Background()
69119

0 commit comments

Comments
 (0)