Writing tests for Couchbase with Testcontainers on Golang

This a brief example of using Testcontainers with a vanilla Couchabse Community docker image to spin up a container on which database tests can be run against. I was not able to find any good example online which uses the vanilla image, and had to spend more time than I wished to have. To save time for the next person, I write this blog (being aware of the irony in spending even more time).

This setup requires two basic steps:

1. Starting a Couchbase container with the required ports exposed

The following set of port forwards can be good place to start, and can be extended as needed. Forwarding the container ports to the same host ports will make life easier for us as we would not have to discover the allocated host ports for each service port, and configure it when using the SDK.

2. Initializing the Couchabse Cluster

  1. Initialize the node
  2. Set up the Couchbase Server services
  3. Set memory quotas
  4. Establish the administrator name and password

As already discussed in the given reference, curl commands are used for these steps. Once these commands are successfully run, the cluster will be ready for testing. You can also login to the UI at http://localhost:8091 with the credentials admin:password which are hard coded in the below code.

Running a Test

This setup can be extended to write a DB test suite with the container creation and cluster setup as the Suite Setup and container termination as the Suite Teardown.

Senior Consultant | Trekking & Cycling enthusiast