Skip to content
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

docs: use Go testable examples in modules #1603

Merged
merged 39 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ba14798
fix: rename vault container in tests
mdelapenya Sep 6, 2023
b192a81
chore: adjust comment in template
mdelapenya Sep 6, 2023
64bd746
docs(vault): use RunContainer example in usage section
mdelapenya Sep 6, 2023
14a6022
docs: update vault docs
mdelapenya Sep 6, 2023
d5b68da
docs: add RunContainer example for redpanda
mdelapenya Sep 7, 2023
4cb9f60
docs: add RunContainer example for redis
mdelapenya Sep 7, 2023
93f2a5c
docs: document creating networks
mdelapenya Sep 7, 2023
61b37ec
docs: add RunContainer example for pulsar
mdelapenya Sep 7, 2023
e547200
docs: add RunContainer example for postgres
mdelapenya Sep 7, 2023
2273da3
docs: add RunContainer example for neo4j
mdelapenya Sep 7, 2023
a2b03fd
docs: add RunContainer example for nats
mdelapenya Sep 7, 2023
8325e51
docs: update quickstart to not use testing libraries
mdelapenya Sep 7, 2023
2886ebd
docs: add RunContainer example for mysql
mdelapenya Sep 7, 2023
72cabe1
docs: add RunContainer example for mongo
mdelapenya Sep 7, 2023
86015c5
docs: add RunContainer example for mariadb
mdelapenya Sep 7, 2023
fc9b9ce
chore: use logger to print out hostname external reason
mdelapenya Sep 7, 2023
7ad9043
docs: document new Go examples file
mdelapenya Sep 7, 2023
fc9bea3
docs: add RunContainer example for localstack
mdelapenya Sep 7, 2023
9bd0377
chore: convert k3s test into a testable example
mdelapenya Sep 7, 2023
9911613
chore: convert mongodb test into testable example
mdelapenya Sep 7, 2023
8f90e0a
fix: handle errors in example tests for elasticsearch
mdelapenya Sep 7, 2023
af18064
fix: do not deprecate used fields
mdelapenya Sep 8, 2023
286c86f
chore: convert couchbase test into testable example
mdelapenya Sep 8, 2023
7a48310
chore: convert clickhouse test into testable example
mdelapenya Sep 8, 2023
ac4c5f0
docs: adjust artemis docs
mdelapenya Sep 8, 2023
a6c4780
chore: simplify postgres example
mdelapenya Sep 8, 2023
b01f6af
fix: lint
mdelapenya Sep 8, 2023
94386b9
Merge branch 'main' into migrate-examples
mdelapenya Sep 10, 2023
d411e3a
chore(deps): bump github.com/hashicorp/vault-client-go in /modules/va…
mmorel-35 Sep 11, 2023
7c5f468
ci(lint): enable gocritic linter (#1605)
mmorel-35 Sep 11, 2023
a85ac93
ci(lint): enable errorlint linter (#1604)
mmorel-35 Sep 11, 2023
d6c4221
fix: handle errors
mdelapenya Sep 11, 2023
9235361
chore(pulsar): create/remove the network properly
mdelapenya Sep 11, 2023
b5d3749
Merge branch 'main' into migrate-examples
mdelapenya Sep 11, 2023
0e28881
chore: better container logs on errors during startup
mdelapenya Sep 11, 2023
25adc7a
Revert "chore: better container logs on errors during startup"
mdelapenya Sep 11, 2023
2d91912
chore: better container logs on errors during startup
mdelapenya Sep 11, 2023
c8a8a7b
fix: do not wrap error but print it
mdelapenya Sep 11, 2023
1f64536
fix: avoid polluting default wait strategies in pulsar
mdelapenya Sep 11, 2023
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
Prev Previous commit
Next Next commit
chore: convert couchbase test into testable example
  • Loading branch information
mdelapenya committed Sep 8, 2023
commit 286c86f429fffa05b6fe26ccb4904f8de638c7b1
35 changes: 15 additions & 20 deletions docs/modules/couchbase.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ go get github.com/testcontainers/testcontainers-go/modules/couchbase
## Usage example

<!--codeinclude-->
[Start Couchbase](../../modules/couchbase/couchbase_test.go) inside_block:withBucket
[Creating a Couchbase container](../../modules/couchbase/examples_test.go) inside_block:runCouchbaseContainer
<!--/codeinclude-->

## Module Reference
Expand Down Expand Up @@ -49,6 +49,11 @@ Once the container is started, it will perform the following operations, **in th

### Container Ports

Here you can find the list with the default ports used by the Couchbase container. The Management ports (`MGMT_PORT` and `MGMT_SSL_PORT`) and the Service ports for `kv`, `query` and `search` are exposed by default.

!!!tip
You can export the service ports for Analytics and Eventing by using the `WithServiceAnalytics` and `WithServiceEventing` optional functions.

<!--codeinclude-->
[Container Ports](../../modules/couchbase/couchbase.go) inside_block:containerPorts
<!--/codeinclude-->
Expand All @@ -68,6 +73,12 @@ By default, the container will use the following Docker image:
[Default Docker image](../../modules/couchbase/couchbase.go) inside_block:defaultImage
<!--/codeinclude-->

You can find the Docker images that are currently tested in this module, for the Enterprise and Community editions, in the following list:

<!--codeinclude-->
[Docker images](../../modules/couchbase/couchbase_test.go) inside_block:dockerImages
<!--/codeinclude-->

#### Wait Strategies

If you need to set a different wait strategy for Couchbase, you can use `testcontainers.WithWaitStrategy` with a valid wait strategy
Expand All @@ -94,12 +105,12 @@ If you need to change the default credentials for the admin user, you can use `W
When the password has less than 6 characters, the container won't be created and the `RunContainer` function will throw an error.

!!!info
The default username is `Administrator` and the default password is `password`.
In the case this optional function is not called, the default username is `Administrator` and the default password is `password`.

#### Buckets

When creating a new Couchbase container, you can create one or more buckets. The module provides with a `WithBuckets` function that accepts an array of buckets to be created.
To create a new bucket, the module exposes a `NewBucket` function, where you can pass the bucket name.
When creating a new Couchbase container, you can create one or more buckets. The module exposes a `WithBuckets` optional function that accepts an slice of buckets to be created.
To create a new bucket, the module also exposes a `NewBucket` function, where you can pass the bucket name.

It's possible to customize a newly created bucket, using the following options:

Expand Down Expand Up @@ -131,33 +142,17 @@ By default, the container will start with the following services: `kv`, `n1ql`,
When running the Enterprise Edition of Couchbase Server, the module provides two functions to enable or disable services:
`WithServiceAnalytics` and `WithServiceEventing`. Else, it will throw an error and the container won't be created.

<!--codeinclude-->
[Docker images](../../modules/couchbase/couchbase_test.go) inside_block:dockerImages
<!--/codeinclude-->

### Container Methods

#### ConnectionString

The `ConnectionString` method returns the connection string to connect to the Couchbase container instance.
It returns a string with the format `couchbase://<host>:<port>`.

<!--codeinclude-->
[Connect to Couchbase](../../modules/couchbase/couchbase_test.go) inside_block:connectToCluster
<!--/codeinclude-->

#### Username

The `Username` method returns the username of the Couchbase administrator.

<!--codeinclude-->
[Connect to Couchbase using Credentials](../../modules/couchbase/couchbase_test.go) inside_block:getCredentials
<!--/codeinclude-->

#### Password

The `Password` method returns the password of the Couchbase administrator.

<!--codeinclude-->
[Connect to Couchbase using Credentials](../../modules/couchbase/couchbase_test.go) inside_block:getCredentials
<!--/codeinclude-->
10 changes: 6 additions & 4 deletions modules/couchbase/couchbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/testcontainers/testcontainers-go/wait"
)

// containerPorts {
const (
// containerPorts {
MGMT_PORT = "8091"
MGMT_SSL_PORT = "18091"

Expand All @@ -40,8 +40,12 @@ const (

KV_PORT = "11210"
KV_SSL_PORT = "11207"
// }
)

// defaultImage {
const defaultImage = "couchbase:6.5.1"

// }

// initialServices is the list of services that are enabled by default
Expand All @@ -65,9 +69,7 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
}

req := testcontainers.ContainerRequest{
// defaultImage {
Image: "couchbase:6.5.1",
// }
Image: defaultImage,
ExposedPorts: []string{MGMT_PORT + "/tcp", MGMT_SSL_PORT + "/tcp"},
}

Expand Down
35 changes: 2 additions & 33 deletions modules/couchbase/couchbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,13 @@ import (
tccouchbase "github.com/testcontainers/testcontainers-go/modules/couchbase"
)

// dockerImages {
const (
// dockerImages {
enterpriseEdition = "couchbase:enterprise-7.1.3"
communityEdition = "couchbase:community-7.1.1"
// }
)

// }

// Deprecated
func TestStartContainer(t *testing.T) {
ctx := context.Background()

bucketName := "testBucket"
container, err := tccouchbase.StartContainer(ctx, tccouchbase.WithImageName(communityEdition), tccouchbase.WithBucket(tccouchbase.NewBucket(bucketName)))
if err != nil {
t.Fatal(err)
}

// Clean up the container after the test is complete
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

cluster, err := connectCluster(ctx, container)
if err != nil {
t.Fatalf("could not connect couchbase: %s", err)
}

testBucketUsage(t, cluster.Bucket(bucketName))
}

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

Expand Down Expand Up @@ -187,19 +161,14 @@ func testBucketUsage(t *testing.T, bucket *gocb.Bucket) {
}
}

// connectToCluster {
func connectCluster(ctx context.Context, container *tccouchbase.CouchbaseContainer) (*gocb.Cluster, error) {
connectionString, err := container.ConnectionString(ctx)
if err != nil {
return nil, err
}

// getCredentials {
return gocb.Connect(connectionString, gocb.ClusterOptions{
Username: container.Username(),
Password: container.Password(),
})
// }
}

// }
71 changes: 71 additions & 0 deletions modules/couchbase/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package couchbase_test

import (
"context"
"fmt"

"github.com/couchbase/gocb/v2"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/couchbase"
)

func ExampleRunContainer() {
// runCouchbaseContainer {
ctx := context.Background()

bucketName := "testBucket"
bucket := couchbase.NewBucket(bucketName)

bucket = bucket.WithQuota(100).
WithReplicas(0).
WithFlushEnabled(false).
WithPrimaryIndex(true)

couchbaseContainer, err := couchbase.RunContainer(ctx,
testcontainers.WithImage("couchbase:community-7.1.1"),
couchbase.WithAdminCredentials("testcontainers", "testcontainers.IS.cool!"),
couchbase.WithBuckets(bucket),
)
if err != nil {
panic(err)
}
defer func() {
if err := couchbaseContainer.Terminate(ctx); err != nil {
panic(err)
}
}()
// }

state, err := couchbaseContainer.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

connectionString, err := couchbaseContainer.ConnectionString(ctx)
if err != nil {
panic(err)
}

cluster, err := gocb.Connect(connectionString, gocb.ClusterOptions{
Username: couchbaseContainer.Username(),
Password: couchbaseContainer.Password(),
})
if err != nil {
panic(err)
}

buckets, err := cluster.Buckets().GetAllBuckets(nil)
if err != nil {
panic(err)
}

fmt.Println(len(buckets))
fmt.Println(buckets[bucketName].Name)

// Output:
// true
// 1
// testBucket
}