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

feat: use testcontainers-go for clickhouse tests #1506

Merged
merged 6 commits into from
Aug 21, 2024
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
8 changes: 7 additions & 1 deletion clickhouse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ Install the clickhouse implementation:
go get github.com/gofiber/storage/clickhouse
```

Before running or testing this implementation, you must ensure a Clickhouse cluster is available.
### Running the tests

This module uses Testcontainers for Go to run integration tests, which will start a local instance of Clickhouse as a Docker container under the hood. To run the tests, you must have Docker installed on your machine.
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

### Local development

Before running this implementation, you must ensure a Clickhouse cluster is available.
For local development, we recommend using the Clickhouse Docker image; it contains everything
necessary for the client to operate correctly.

Expand Down
58 changes: 38 additions & 20 deletions clickhouse/clickhouse_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package clickhouse

import (
"context"
"strconv"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/modules/clickhouse"
)

const (
clickhouseImage = "clickhouse/clickhouse-server:23.3.8.21-alpine"
clickhouseUser string = "default"
clickhousePass string = "password"
clickhouseDB string = "fiber"
)

type TestOrBench interface {
Expand All @@ -15,15 +27,39 @@ type TestOrBench interface {
func getTestConnection(t TestOrBench, cfg Config) (*Storage, error) {
t.Helper()

tt := t.(*testing.T)

ctx := context.Background()

c, err := clickhouse.Run(ctx,
clickhouseImage,
clickhouse.WithUsername(clickhouseUser),
clickhouse.WithPassword(clickhousePass),
clickhouse.WithDatabase(clickhouseDB),
)
require.NoError(tt, err)

hostPort, err := c.ConnectionHost(ctx)
require.NoError(tt, err)

pair := strings.Split(hostPort, ":")
port, err := strconv.Atoi(pair[1])
require.NoError(tt, err)

// configure the client for the testcontainers clickhouse instance
cfg.Host = pair[0]
cfg.Port = port
cfg.Username = clickhouseUser
cfg.Password = clickhousePass
cfg.Database = clickhouseDB

client, err := New(cfg)

return client, err
}

func Test_Connection(t *testing.T) {
_, err := getTestConnection(t, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -34,8 +70,6 @@ func Test_Connection(t *testing.T) {

func Test_Set(t *testing.T) {
client, err := getTestConnection(t, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -49,8 +83,6 @@ func Test_Set(t *testing.T) {

func Test_Set_With_Exp(t *testing.T) {
client, err := getTestConnection(t, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -64,8 +96,6 @@ func Test_Set_With_Exp(t *testing.T) {

func Test_Get(t *testing.T) {
client, err := getTestConnection(t, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -85,8 +115,6 @@ func Test_Get(t *testing.T) {

func Test_Get_With_Exp(t *testing.T) {
client, err := getTestConnection(t, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -113,8 +141,6 @@ func Test_Get_With_Exp(t *testing.T) {

func Test_Delete(t *testing.T) {
client, err := getTestConnection(t, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -137,8 +163,6 @@ func Test_Delete(t *testing.T) {

func Test_Reset(t *testing.T) {
client, err := getTestConnection(t, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -164,8 +188,6 @@ func Benchmark_Clickhouse_Set(b *testing.B) {
b.ResetTimer()

client, err := getTestConnection(b, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -186,8 +208,6 @@ func Benchmark_Clickhouse_Get(b *testing.B) {
b.ResetTimer()

client, err := getTestConnection(b, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand All @@ -210,8 +230,6 @@ func Benchmark_Clickhouse_Set_And_Delete(b *testing.B) {
b.ResetTimer()

client, err := getTestConnection(b, Config{
Host: "127.0.0.1",
Port: 9001,
Engine: Memory,
Table: "test_table",
Clean: true,
Expand Down
39 changes: 39 additions & 0 deletions clickhouse/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,63 @@ go 1.21
require (
github.com/ClickHouse/clickhouse-go/v2 v2.26.0
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go/modules/clickhouse v0.33.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/ClickHouse/ch-go v0.61.5 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/andybalholm/brotli v1.1.0 // 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/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-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-logr/logr v1.4.2 // 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/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // 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/paulmach/orb v0.11.1 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // 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/segmentio/asm v1.2.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/testcontainers/testcontainers-go v0.33.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/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/sys v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading