diff --git a/modules/couchbase/couchbase.go b/modules/couchbase/couchbase.go index 5206acdf48..2daa94b131 100644 --- a/modules/couchbase/couchbase.go +++ b/modules/couchbase/couchbase.go @@ -219,11 +219,12 @@ func (c *CouchbaseContainer) waitUntilNodeIsOnline(ctx context.Context) error { WithStatusCodeMatcher(func(status int) bool { return status == http.StatusOK }). + WithBasicAuth(c.config.username, c.config.password). WaitUntilReady(ctx, c) } func (c *CouchbaseContainer) initializeIsEnterprise(ctx context.Context) error { - response, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools", http.MethodGet, nil, false) + response, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools", http.MethodGet, nil) if err != nil { return err } @@ -252,7 +253,7 @@ func (c *CouchbaseContainer) renameNode(ctx context.Context) error { "hostname": hostname, } - _, err = c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/rename", http.MethodPost, body, false) + _, err = c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/rename", http.MethodPost, body) return err } @@ -261,7 +262,7 @@ func (c *CouchbaseContainer) initializeServices(ctx context.Context) error { body := map[string]string{ "services": c.getEnabledServices(), } - _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/setupServices", http.MethodPost, body, false) + _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/setupServices", http.MethodPost, body) return err } @@ -282,7 +283,7 @@ func (c *CouchbaseContainer) setMemoryQuotas(ctx context.Context) error { } } - _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools/default", http.MethodPost, body, false) + _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools/default", http.MethodPost, body) return err } @@ -294,7 +295,7 @@ func (c *CouchbaseContainer) configureAdminUser(ctx context.Context) error { "port": "SAME", } - _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/settings/web", http.MethodPost, body, false) + _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/settings/web", http.MethodPost, body) return err } @@ -353,7 +354,7 @@ func (c *CouchbaseContainer) configureExternalPorts(ctx context.Context) error { body["eventingSSL"] = eventingSSL.Port() } - _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/setupAlternateAddresses/external", http.MethodPut, body, true) + _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/node/controller/setupAlternateAddresses/external", http.MethodPut, body) return err } @@ -371,7 +372,7 @@ func (c *CouchbaseContainer) configureIndexer(ctx context.Context) error { "storageMode": string(c.config.indexStorageMode), } - _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/settings/indexes", http.MethodPost, body, true) + _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/settings/indexes", http.MethodPost, body) return err } @@ -474,7 +475,7 @@ func (c *CouchbaseContainer) isPrimaryIndexOnline(ctx context.Context, bucket bu } err := backoff.Retry(func() error { - response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body, true) + response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body) if err != nil { return err } @@ -495,7 +496,7 @@ func (c *CouchbaseContainer) createPrimaryIndex(ctx context.Context, bucket buck "statement": "CREATE PRIMARY INDEX on `" + bucket.name + "`", } err := backoff.Retry(func() error { - response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body, true) + response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body) firstError := gjson.Get(string(response), "errors.0.code").Int() if firstError != 0 { return errors.New("index creation failed") @@ -511,7 +512,7 @@ func (c *CouchbaseContainer) isQueryKeyspacePresent(ctx context.Context, bucket } err := backoff.Retry(func() error { - response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body, true) + response, err := c.doHTTPRequest(ctx, QUERY_PORT, "/query/service", http.MethodPost, body) if err != nil { return err } @@ -557,12 +558,12 @@ func (c *CouchbaseContainer) createBucket(ctx context.Context, bucket bucket) er "replicaNumber": strconv.Itoa(bucket.numReplicas), } - _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools/default/buckets", http.MethodPost, body, true) + _, err := c.doHTTPRequest(ctx, MGMT_PORT, "/pools/default/buckets", http.MethodPost, body) return err } -func (c *CouchbaseContainer) doHTTPRequest(ctx context.Context, port, path, method string, body map[string]string, auth bool) ([]byte, error) { +func (c *CouchbaseContainer) doHTTPRequest(ctx context.Context, port, path, method string, body map[string]string) ([]byte, error) { form := url.Values{} for k, v := range body { form.Set(k, v) @@ -582,10 +583,7 @@ func (c *CouchbaseContainer) doHTTPRequest(ctx context.Context, port, path, meth } request.Header.Add("Content-Type", "application/x-www-form-urlencoded") - - if auth { - request.SetBasicAuth(c.config.username, c.config.password) - } + request.SetBasicAuth(c.config.username, c.config.password) response, err := http.DefaultClient.Do(request) if err != nil { diff --git a/modules/couchbase/couchbase_test.go b/modules/couchbase/couchbase_test.go index 37f7a086a3..7cf1597b22 100644 --- a/modules/couchbase/couchbase_test.go +++ b/modules/couchbase/couchbase_test.go @@ -64,6 +64,49 @@ func TestCouchbaseWithEnterpriseContainer(t *testing.T) { testBucketUsage(t, cluster.Bucket(bucketName)) } +func TestCouchbaseWithReuse(t *testing.T) { + ctx := context.Background() + + containerName := "couchbase-" + testcontainers.SessionID() + + bucketName := "testBucket" + bucket := tccouchbase.NewBucket(bucketName). + WithQuota(100). + WithReplicas(0). + WithFlushEnabled(true). + WithPrimaryIndex(true) + ctr, err := tccouchbase.Run(ctx, + enterpriseEdition, + tccouchbase.WithBuckets(bucket), + testcontainers.WithReuseByName(containerName), + ) + testcontainers.CleanupContainer(t, ctr) + require.NoError(t, err) + + cluster, err := connectCluster(ctx, ctr) + require.NoError(t, err) + + testBucketUsage(t, cluster.Bucket(bucketName)) + + // Test reuse when first container has had time to fully start up and be configured with auth + // Without enabling auth on the initCluster functions the reuse of this container fails with + // "init cluster: context deadline exceeded". + // This is due to the management endpoints requiring the Basic Auth headers once configureAdminUser + // has completed. + reusedCtr, err := tccouchbase.Run(ctx, + enterpriseEdition, + testcontainers.WithReuseByName(containerName), + ) + testcontainers.CleanupContainer(t, ctr) + require.NoError(t, err) + require.Equal(t, ctr.GetContainerID(), reusedCtr.GetContainerID()) + + cluster, err = connectCluster(ctx, reusedCtr) + require.NoError(t, err) + + testBucketUsage(t, cluster.Bucket(bucketName)) +} + func TestWithCredentials(t *testing.T) { ctx := context.Background()