Skip to content

Commit 01d0e07

Browse files
authored
Merge pull request #1506 from mdelapenya/testcontainers-go-clickhouse
feat: use testcontainers-go for clickhouse tests
2 parents eba6a80 + 07d3b2e commit 01d0e07

File tree

6 files changed

+228
-31
lines changed

6 files changed

+228
-31
lines changed

.github/workflows/benchmark.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
uses: actions/setup-go@v5
8787
with:
8888
# NOTE: Keep this in sync with the version from go.mod
89-
go-version: "1.20.x"
89+
go-version: "1.21.x"
9090

9191
- name: Setup Node.js
9292
uses: actions/setup-node@v4
@@ -134,11 +134,6 @@ jobs:
134134
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
135135
sleep 15 # Wait for ScyllaDb to initialize
136136
137-
- name: Startup Clickhouse
138-
run: |
139-
docker run -d -p 9001:9000 --name clickhouse --ulimit nofile=262144:262144 clickhouse/clickhouse-server
140-
sleep 10 # Wait for Clickhouse to initialize
141-
142137
- name: Setup Redis
143138
uses: shogo82148/actions-setup-redis@v1
144139
with:
@@ -178,6 +173,7 @@ jobs:
178173
POSTGRES_DATABASE: fiber
179174
POSTGRES_USERNAME: username
180175
POSTGRES_PASSWORD: "pass#w%rd"
176+
TEST_CLICKHOUSE_IMAGE: "clickhouse/clickhouse-server:23-alpine"
181177

182178
- name: Get Previous Benchmark Results
183179
uses: actions/cache@v4

.github/workflows/test-clickhouse.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ jobs:
2020
steps:
2121
- name: Fetch Repository
2222
uses: actions/checkout@v4
23-
- name: Startup Clickhouse
24-
run: |
25-
docker run -d -p 9001:9000 --name clickhouse --ulimit nofile=262144:262144 clickhouse/clickhouse-server
26-
sleep 30
2723
- name: Install Go
2824
uses: actions/setup-go@v5
2925
with:
3026
go-version: '${{ matrix.go-version }}'
3127
- name: Run Test
28+
env:
29+
TEST_CLICKHOUSE_IMAGE: clickhouse/clickhouse-server:23-alpine
3230
run: cd ./clickhouse && go clean -testcache && go test ./... -v -race

clickhouse/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ Install the clickhouse implementation:
3131
go get github.com/gofiber/storage/clickhouse
3232
```
3333

34-
Before running or testing this implementation, you must ensure a Clickhouse cluster is available.
34+
### Running the tests
35+
36+
This module uses [Testcontainers for Go](https://github.com/testcontainers/testcontainers-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 (or another container runtime 100% compatible with the Docker APIs) installed on your machine.
37+
38+
### Local development
39+
40+
Before running this implementation, you must ensure a Clickhouse cluster is available.
3541
For local development, we recommend using the Clickhouse Docker image; it contains everything
3642
necessary for the client to operate correctly.
3743

clickhouse/clickhouse_test.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
package clickhouse
22

33
import (
4+
"context"
5+
"os"
6+
"strconv"
7+
"strings"
48
"testing"
59
"time"
610

711
"github.com/stretchr/testify/assert"
812
"github.com/stretchr/testify/require"
13+
14+
"github.com/testcontainers/testcontainers-go/modules/clickhouse"
15+
)
16+
17+
const (
18+
// clickhouseImage is the default image used for running clickhouse in tests.
19+
clickhouseImage = "clickhouse/clickhouse-server:23-alpine"
20+
clickhouseImageEnvVar string = "TEST_CLICKHOUSE_IMAGE"
21+
clickhouseUser string = "default"
22+
clickhousePass string = "password"
23+
clickhouseDB string = "fiber"
924
)
1025

1126
type TestOrBench interface {
@@ -15,15 +30,48 @@ type TestOrBench interface {
1530
func getTestConnection(t TestOrBench, cfg Config) (*Storage, error) {
1631
t.Helper()
1732

33+
img := clickhouseImage
34+
if imgFromEnv := os.Getenv("TEST_CLICKHOUSE_IMAGE"); imgFromEnv != "" {
35+
img = imgFromEnv
36+
}
37+
38+
ctx := context.Background()
39+
40+
c, err := clickhouse.Run(ctx,
41+
img,
42+
clickhouse.WithUsername(clickhouseUser),
43+
clickhouse.WithPassword(clickhousePass),
44+
clickhouse.WithDatabase(clickhouseDB),
45+
)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
hostPort, err := c.ConnectionHost(ctx)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
pair := strings.Split(hostPort, ":")
56+
port, err := strconv.Atoi(pair[1])
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
// configure the client for the testcontainers clickhouse instance
62+
cfg.Host = pair[0]
63+
cfg.Port = port
64+
cfg.Username = clickhouseUser
65+
cfg.Password = clickhousePass
66+
cfg.Database = clickhouseDB
67+
1868
client, err := New(cfg)
1969

2070
return client, err
2171
}
2272

2373
func Test_Connection(t *testing.T) {
2474
_, err := getTestConnection(t, Config{
25-
Host: "127.0.0.1",
26-
Port: 9001,
2775
Engine: Memory,
2876
Table: "test_table",
2977
Clean: true,
@@ -34,8 +82,6 @@ func Test_Connection(t *testing.T) {
3482

3583
func Test_Set(t *testing.T) {
3684
client, err := getTestConnection(t, Config{
37-
Host: "127.0.0.1",
38-
Port: 9001,
3985
Engine: Memory,
4086
Table: "test_table",
4187
Clean: true,
@@ -49,8 +95,6 @@ func Test_Set(t *testing.T) {
4995

5096
func Test_Set_With_Exp(t *testing.T) {
5197
client, err := getTestConnection(t, Config{
52-
Host: "127.0.0.1",
53-
Port: 9001,
5498
Engine: Memory,
5599
Table: "test_table",
56100
Clean: true,
@@ -64,8 +108,6 @@ func Test_Set_With_Exp(t *testing.T) {
64108

65109
func Test_Get(t *testing.T) {
66110
client, err := getTestConnection(t, Config{
67-
Host: "127.0.0.1",
68-
Port: 9001,
69111
Engine: Memory,
70112
Table: "test_table",
71113
Clean: true,
@@ -85,8 +127,6 @@ func Test_Get(t *testing.T) {
85127

86128
func Test_Get_With_Exp(t *testing.T) {
87129
client, err := getTestConnection(t, Config{
88-
Host: "127.0.0.1",
89-
Port: 9001,
90130
Engine: Memory,
91131
Table: "test_table",
92132
Clean: true,
@@ -113,8 +153,6 @@ func Test_Get_With_Exp(t *testing.T) {
113153

114154
func Test_Delete(t *testing.T) {
115155
client, err := getTestConnection(t, Config{
116-
Host: "127.0.0.1",
117-
Port: 9001,
118156
Engine: Memory,
119157
Table: "test_table",
120158
Clean: true,
@@ -137,8 +175,6 @@ func Test_Delete(t *testing.T) {
137175

138176
func Test_Reset(t *testing.T) {
139177
client, err := getTestConnection(t, Config{
140-
Host: "127.0.0.1",
141-
Port: 9001,
142178
Engine: Memory,
143179
Table: "test_table",
144180
Clean: true,
@@ -164,8 +200,6 @@ func Benchmark_Clickhouse_Set(b *testing.B) {
164200
b.ResetTimer()
165201

166202
client, err := getTestConnection(b, Config{
167-
Host: "127.0.0.1",
168-
Port: 9001,
169203
Engine: Memory,
170204
Table: "test_table",
171205
Clean: true,
@@ -186,8 +220,6 @@ func Benchmark_Clickhouse_Get(b *testing.B) {
186220
b.ResetTimer()
187221

188222
client, err := getTestConnection(b, Config{
189-
Host: "127.0.0.1",
190-
Port: 9001,
191223
Engine: Memory,
192224
Table: "test_table",
193225
Clean: true,
@@ -210,8 +242,6 @@ func Benchmark_Clickhouse_Set_And_Delete(b *testing.B) {
210242
b.ResetTimer()
211243

212244
client, err := getTestConnection(b, Config{
213-
Host: "127.0.0.1",
214-
Port: 9001,
215245
Engine: Memory,
216246
Table: "test_table",
217247
Clean: true,

clickhouse/go.mod

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,63 @@ go 1.21
55
require (
66
github.com/ClickHouse/clickhouse-go/v2 v2.26.0
77
github.com/stretchr/testify v1.9.0
8+
github.com/testcontainers/testcontainers-go/modules/clickhouse v0.33.0
89
)
910

1011
require (
12+
dario.cat/mergo v1.0.0 // indirect
13+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
1114
github.com/ClickHouse/ch-go v0.61.5 // indirect
15+
github.com/Microsoft/go-winio v0.6.2 // indirect
1216
github.com/andybalholm/brotli v1.1.0 // indirect
17+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
18+
github.com/containerd/containerd v1.7.18 // indirect
19+
github.com/containerd/log v0.1.0 // indirect
20+
github.com/containerd/platforms v0.2.1 // indirect
21+
github.com/cpuguy83/dockercfg v0.3.1 // indirect
1322
github.com/davecgh/go-spew v1.1.1 // indirect
23+
github.com/distribution/reference v0.6.0 // indirect
24+
github.com/docker/docker v27.1.1+incompatible // indirect
25+
github.com/docker/go-connections v0.5.0 // indirect
26+
github.com/docker/go-units v0.5.0 // indirect
27+
github.com/felixge/httpsnoop v1.0.4 // indirect
1428
github.com/go-faster/city v1.0.1 // indirect
1529
github.com/go-faster/errors v0.7.1 // indirect
30+
github.com/go-logr/logr v1.4.2 // indirect
31+
github.com/go-logr/stdr v1.2.2 // indirect
32+
github.com/go-ole/go-ole v1.2.6 // indirect
33+
github.com/gogo/protobuf v1.3.2 // indirect
1634
github.com/google/uuid v1.6.0 // indirect
1735
github.com/klauspost/compress v1.17.9 // indirect
36+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
37+
github.com/magiconair/properties v1.8.7 // indirect
38+
github.com/moby/docker-image-spec v1.3.1 // indirect
39+
github.com/moby/patternmatcher v0.6.0 // indirect
40+
github.com/moby/sys/sequential v0.5.0 // indirect
41+
github.com/moby/sys/user v0.1.0 // indirect
42+
github.com/moby/term v0.5.0 // indirect
43+
github.com/morikuni/aec v1.0.0 // indirect
44+
github.com/opencontainers/go-digest v1.0.0 // indirect
45+
github.com/opencontainers/image-spec v1.1.0 // indirect
1846
github.com/paulmach/orb v0.11.1 // indirect
1947
github.com/pierrec/lz4/v4 v4.1.21 // indirect
2048
github.com/pkg/errors v0.9.1 // indirect
2149
github.com/pmezard/go-difflib v1.0.0 // indirect
50+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
2251
github.com/segmentio/asm v1.2.0 // indirect
52+
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
53+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
2354
github.com/shopspring/decimal v1.4.0 // indirect
55+
github.com/sirupsen/logrus v1.9.3 // indirect
56+
github.com/testcontainers/testcontainers-go v0.33.0 // indirect
57+
github.com/tklauser/go-sysconf v0.3.12 // indirect
58+
github.com/tklauser/numcpus v0.6.1 // indirect
59+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
60+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
2461
go.opentelemetry.io/otel v1.28.0 // indirect
62+
go.opentelemetry.io/otel/metric v1.28.0 // indirect
2563
go.opentelemetry.io/otel/trace v1.28.0 // indirect
64+
golang.org/x/crypto v0.24.0 // indirect
2665
golang.org/x/sys v0.21.0 // indirect
2766
gopkg.in/yaml.v3 v3.0.1 // indirect
2867
)

0 commit comments

Comments
 (0)