Skip to content

feat(couchbase): adding auth to couchbase initCluster functions to support container reuse #3048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions modules/couchbase/couchbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions modules/couchbase/couchbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading