Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -218,11 +218,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 @@ -251,7 +252,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 @@ -260,7 +261,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 @@ -281,7 +282,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 @@ -293,7 +294,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 @@ -352,7 +353,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 @@ -370,7 +371,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 @@ -473,7 +474,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 @@ -494,7 +495,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 @@ -510,7 +511,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 @@ -556,12 +557,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 @@ -581,10 +582,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
50 changes: 50 additions & 0 deletions modules/couchbase/couchbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,56 @@ func TestCouchbaseWithEnterpriseContainer(t *testing.T) {
testBucketUsage(t, cluster.Bucket(bucketName))
}

type reusableCouchbase struct{}

func (c *reusableCouchbase) Customize(req *testcontainers.GenericContainerRequest) error {
// Enable container reuse
req.Reuse = true
req.Name = "couchbase"
return nil
}

func TestCouchbaseWithReuse(t *testing.T) {
ctx := context.Background()

bucketName := "testBucket"
bucket := tccouchbase.NewBucket(bucketName).
WithQuota(100).
WithReplicas(0).
WithFlushEnabled(true).
WithPrimaryIndex(true)
ctr, err := tccouchbase.Run(ctx,
enterpriseEdition,
tccouchbase.WithBuckets(bucket),
&reusableCouchbase{},
)
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,
&reusableCouchbase{},
)
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