Skip to content

Commit

Permalink
Merge pull request #1508 from mdelapenya/tc-go-adoption
Browse files Browse the repository at this point in the history
chore: use testcontainers-go for couchbase and minio
  • Loading branch information
ReneWerner87 authored Aug 29, 2024
2 parents 01d0e07 + 304baba commit 29406b2
Show file tree
Hide file tree
Showing 10 changed files with 584 additions and 135 deletions.
14 changes: 2 additions & 12 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ jobs:
docker run -d -p 1408:1408 -p 30000:30000 ghcr.io/oracle/coherence-ce:22.06.5
sleep 30
- name: Install couchbase
run: |
docker run --name couchbase -d -p 8091-8097:8091-8097 -p 9123:9123 -p 11207:11207 -p 11210:11210 -p 11280:11280 -p 18091-18097:18091-18097 couchbase:enterprise-7.1.1
sleep 10
docker exec --tty couchbase couchbase-cli cluster-init -c localhost:8091 --cluster-username admin --cluster-password 123456 --cluster-ramsize 256 --services data
sleep 10
docker exec --tty couchbase couchbase-cli bucket-create -c localhost:8091 --username admin --password 123456 --bucket fiber_storage --bucket-type couchbase --bucket-ramsize 100 --enable-flush 1
- name: Install etcd
run: |
docker run -d --name Etcd-server \
Expand All @@ -125,10 +117,6 @@ jobs:
--env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 \
bitnami/etcd:latest
- name: Install MinIO
run: |
docker run -d --restart always -p 9000:9000 --name storage-minio -e MINIO_ROOT_USER='minio-user' -e MINIO_ROOT_PASSWORD='minio-password' minio/minio server /data
- name: Install ScyllaDb
run: |
docker run --name scylladb -p 9042:9042 -p 19042:19042 -p 9160:9160 -p 7000:7000 -p 7001:7001 -p 7199:7199 -p 9180:9180 -d scylladb/scylla:latest --broadcast-address 127.0.0.1 --listen-address 0.0.0.0 --broadcast-rpc-address 127.0.0.1
Expand Down Expand Up @@ -174,6 +162,8 @@ jobs:
POSTGRES_USERNAME: username
POSTGRES_PASSWORD: "pass#w%rd"
TEST_CLICKHOUSE_IMAGE: "clickhouse/clickhouse-server:23-alpine"
TEST_COUCHBASE_IMAGE: "couchbase:enterprise-7.1.1"
TEST_MINIO_IMAGE: "docker.io/minio/minio:RELEASE.2024-08-17T01-24-54Z"

- name: Get Previous Benchmark Results
uses: actions/cache@v4
Expand Down
13 changes: 4 additions & 9 deletions .github/workflows/test-couchbase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@ jobs:
strategy:
matrix:
go-version:
- 1.19.x
- 1.20.x
- 1.21.x
- 1.22.x
- 1.23.x
steps:
- name: Fetch Repository
uses: actions/checkout@v4
- name: Install couchbase
run: |
docker run --name couchbase -d -p 8091-8097:8091-8097 -p 9123:9123 -p 11207:11207 -p 11210:11210 -p 11280:11280 -p 18091-18097:18091-18097 couchbase:enterprise-7.1.1
sleep 10
docker exec --tty couchbase couchbase-cli cluster-init -c localhost:8091 --cluster-username admin --cluster-password 123456 --cluster-ramsize 256 --services data
sleep 10
docker exec --tty couchbase couchbase-cli bucket-create -c localhost:8091 --username admin --password 123456 --bucket fiber_storage --bucket-type couchbase --bucket-ramsize 100 --enable-flush 1
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '${{ matrix.go-version }}'
- name: Run Test
env:
TEST_COUCHBASE_IMAGE: couchbase:enterprise-7.1.1
run: cd ./couchbase && go test ./... -v -race
10 changes: 4 additions & 6 deletions .github/workflows/test-minio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ jobs:
strategy:
matrix:
go-version:
- 1.19.x
- 1.20.x
- 1.21.x
- 1.22.x
- 1.23.x
steps:
- name: Install MinIO
run: |
docker run -d --restart always -p 9000:9000 --name storage-minio -e MINIO_ROOT_USER='minio-user' -e MINIO_ROOT_PASSWORD='minio-password' minio/minio server /data
- name: Fetch Repository
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '${{ matrix.go-version }}'
- name: Run Test
env:
TEST_MINIO_IMAGE: docker.io/minio/minio:RELEASE.2024-08-17T01-24-54Z
run: cd ./minio && go test ./... -v -race
8 changes: 2 additions & 6 deletions clickhouse/clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ const (
clickhouseDB string = "fiber"
)

type TestOrBench interface {
Helper()
}

func getTestConnection(t TestOrBench, cfg Config) (*Storage, error) {
func getTestConnection(t testing.TB, cfg Config) (*Storage, error) {
t.Helper()

img := clickhouseImage
if imgFromEnv := os.Getenv("TEST_CLICKHOUSE_IMAGE"); imgFromEnv != "" {
if imgFromEnv := os.Getenv(clickhouseImageEnvVar); imgFromEnv != "" {
img = imgFromEnv
}

Expand Down
97 changes: 71 additions & 26 deletions couchbase/couchbase_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,70 @@
package couchbase

import (
"context"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/modules/couchbase"
)

func newTestStore(t testing.TB) *Storage {
const (
// couchbaseImage is the default image used for running couchbase in tests.
couchbaseImage = "couchbase:enterprise-7.1.1"
couchbaseImageEnvVar string = "TEST_COUCHBASE_IMAGE"
couchbaseUser string = "default"
couchbasePass string = "password"
couchbaseBucket string = "fiber_storage"
)

func newTestStore(t testing.TB) (*Storage, error) {
t.Helper()

img := couchbaseImage
if imgFromEnv := os.Getenv(couchbaseImageEnvVar); imgFromEnv != "" {
img = imgFromEnv
}

ctx := context.Background()

bucket := couchbase.NewBucket(couchbaseBucket).WithReplicas(1).WithFlushEnabled(true)

c, err := couchbase.Run(ctx,
img,
couchbase.WithAdminCredentials(couchbaseUser, couchbasePass),
couchbase.WithBuckets(bucket),
)
if err != nil {
return nil, err
}

conn, err := c.ConnectionString(ctx)
if err != nil {
return nil, err
}

return New(Config{
Username: "admin",
Password: "123456",
Host: "127.0.0.1:8091",
Bucket: "fiber_storage",
})
Username: couchbaseUser,
Password: couchbasePass,
Host: conn,
Bucket: couchbaseBucket,
}), nil
}

func TestSetCouchbase_ShouldReturnNoError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)

err := testStore.Set("test", []byte("test"), 0)
err = testStore.Set("test", []byte("test"), 0)

require.NoError(t, err)
}

func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)

val, err := testStore.Get("not_found_key")

Expand All @@ -35,9 +73,10 @@ func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) {
}

func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)

err := testStore.Set("test", []byte("fiber_test_value"), 0)
err = testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)

val, err := testStore.Get("test")
Expand All @@ -47,9 +86,10 @@ func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) {
}

func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)

err := testStore.Set("test", []byte("fiber_test_value"), 3*time.Second)
err = testStore.Set("test", []byte("fiber_test_value"), 3*time.Second)
require.NoError(t, err)

time.Sleep(6 * time.Second)
Expand All @@ -61,9 +101,10 @@ func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
}

func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)

err := testStore.Set("test", []byte("fiber_test_value"), 0)
err = testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)

err = testStore.Delete("test")
Expand All @@ -74,9 +115,10 @@ func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
}

func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)

err := testStore.Set("test", []byte("fiber_test_value"), 0)
err = testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)

err = testStore.Reset()
Expand All @@ -87,25 +129,27 @@ func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
}

func TestClose_CloseShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)

err := testStore.Close()
err = testStore.Close()
require.NoError(t, err)
}

func TestGetConn_ReturnsNotNil(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)

require.True(t, testStore.Conn() != nil)
}

func Benchmark_Couchbase_Set(b *testing.B) {
testStore := newTestStore(b)
testStore, err := newTestStore(b)
require.NoError(b, err)

b.ReportAllocs()
b.ResetTimer()

var err error
for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0)
}
Expand All @@ -114,9 +158,10 @@ func Benchmark_Couchbase_Set(b *testing.B) {
}

func Benchmark_Couchbase_Get(b *testing.B) {
testStore := newTestStore(b)
testStore, err := newTestStore(b)
require.NoError(b, err)

err := testStore.Set("john", []byte("doe"), 0)
err = testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)

b.ReportAllocs()
Expand All @@ -130,12 +175,12 @@ func Benchmark_Couchbase_Get(b *testing.B) {
}

func Benchmark_Couchbase_SetAndDelete(b *testing.B) {
testStore := newTestStore(b)
testStore, err := newTestStore(b)
require.NoError(b, err)

b.ReportAllocs()
b.ResetTimer()

var err error
for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john")
Expand Down
51 changes: 46 additions & 5 deletions couchbase/go.mod
Original file line number Diff line number Diff line change
@@ -1,35 +1,76 @@
module github.com/gofiber/storage/couchbase/v2

go 1.19
go 1.21

require (
github.com/couchbase/gocb/v2 v2.9.1
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go/modules/couchbase v0.33.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/couchbase/gocbcore/v10 v10.5.1 // indirect
github.com/couchbase/gocbcoreps v0.1.3 // indirect
github.com/couchbase/goprotostellar v1.0.2 // indirect
github.com/couchbaselabs/gocbconnstr/v2 v2.0.0-20240607131231-fb385523de28 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/testcontainers/testcontainers-go v0.33.0 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 29406b2

Please sign in to comment.