Skip to content

Commit

Permalink
add couchbase module (#876)
Browse files Browse the repository at this point in the history
* create couchbase module

* expose enabled service ports

* add wait until all nodes are healthy

* add cluster initialization functions

* refactor enabledServices for loop with contains method

* add configureExternalPorts

* refactor doHttpRequest to create new request with context

* add create buckets

* complete create bucket flow

* add docker image name to config

* fix port suffix, mapped port, enabled services and refactor checkAllServicesEnabled with gjson

* add initCluster method

* refactor bucket definition

* update module name

* add ConnectionString, Username, Password for test case and fix configure external port

* add test

* goimports and go mod tidy

* fix: use modules instead of example

* change WithBasicCredentials with WithBasicAuth

* add test for community edition

* fix eventing service with community container test

* update depandabot.yml and go.mod

* update couchbase doc

* reorder modules couchbase dependabot.yml

* Update modules/couchbase/go.mod

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>

* add comments for public methods

* add option for index storage mode

* add comments for index storage modes

---------

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>
  • Loading branch information
alihanyalcin and mdelapenya committed Mar 9, 2023
1 parent a0293bf commit f90c104
Show file tree
Hide file tree
Showing 14 changed files with 1,380 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ updates:
interval: monthly
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /modules/couchbase
schedule:
interval: monthly
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /modules/localstack
schedule:
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/module-couchbase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Couchbase module pipeline

on: [push, pull_request]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.sha }}
cancel-in-progress: true

jobs:
test-couchbase:
strategy:
matrix:
go-version: [1.18.x, 1.x]
runs-on: "ubuntu-latest"
steps:

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: modVerify
working-directory: ./modules/couchbase
run: go mod verify

- name: modTidy
working-directory: ./modules/couchbase
run: make tools-tidy

- name: gotestsum
working-directory: ./modules/couchbase
run: make test-unit

- name: Run checker
run: |
./scripts/check_environment.sh
- name: Test Summary
uses: test-summary/action@4ee9ece4bca777a38f05c8fc578ac2007fe266f7
with:
paths: "**/TEST-couchbase*.xml"
if: always()
40 changes: 40 additions & 0 deletions docs/modules/couchbase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Couchbase

<img src="https://cdn.worldvectorlogo.com/logos/couchbase.svg" width="300" />

Testcontainers module for Couchbase. [Couchbase](https://www.couchbase.com/) is a document oriented NoSQL database.

## Adding this module to your project dependencies

Please run the following command to add the Couchbase module to your Go dependencies:

```
go get github.com/testcontainers/testcontainers-go/modules/couchbase
```

## Usage example

1. The **StartContainer** function is the main entry point to create a new CouchbaseContainer instance.
It takes a context and zero or more Option values to configure the container.
It creates a new container instance, initializes the couchbase cluster, and creates buckets.
If successful, it returns the **CouchbaseContainer** instance.
```go
container, err := couchbase.StartContainer(ctx,
WithImageName("couchbase:community-7.1.1"),
WithBucket(NewBucket(bucketName)))
```
2. The **ConnectionString** method returns the connection string to connect to the Couchbase container instance.
It returns a string with the format `couchbase://<host>:<port>`.
The **Username** method returns the username of the Couchbase administrator.
The **Password** method returns the password of the Couchbase administrator.
```go
connectionString, err := container.ConnectionString(ctx)
if err != nil {
return nil, err
}

cluster, err := gocb.Connect(connectionString, gocb.ClusterOptions{
Username: container.Username(),
Password: container.Password(),
})
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ nav:
- SQL: features/wait/sql.md
- Modules:
- modules/index.md
- modules/couchbase.md
- modules/localstack.md
- modules/pulsar.md
- Examples:
Expand Down
5 changes: 5 additions & 0 deletions modules/couchbase/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../../commons-test.mk

.PHONY: test
test:
$(MAKE) test-couchbase
49 changes: 49 additions & 0 deletions modules/couchbase/bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package couchbase

type bucket struct {
name string
flushEnabled bool
queryPrimaryIndex bool
quota int
numReplicas int
}

func NewBucket(name string) bucket {
return bucket{
name: name,
flushEnabled: false,
queryPrimaryIndex: true,
quota: 100,
numReplicas: 0,
}
}

func (b bucket) WithReplicas(numReplicas int) bucket {
if numReplicas < 0 {
numReplicas = 0
} else if numReplicas > 3 {
numReplicas = 3
}

b.numReplicas = numReplicas
return b
}

func (b bucket) WithFlushEnabled(flushEnabled bool) bucket {
b.flushEnabled = flushEnabled
return b
}

func (b bucket) WithQuota(quota int) bucket {
if quota < 100 {
quota = 100
}

b.quota = quota
return b
}

func (b bucket) WithPrimaryIndex(primaryIndex bool) bucket {
b.queryPrimaryIndex = primaryIndex
return b
}

0 comments on commit f90c104

Please sign in to comment.