Skip to content

Commit 5b03e3c

Browse files
authored
Merge pull request #1574 from mdelapenya/tc-go-adoption-3
feat: add testcontainers-go tests to DynamoDB and NATS
2 parents c1933ed + 585b3e9 commit 5b03e3c

File tree

10 files changed

+801
-159
lines changed

10 files changed

+801
-159
lines changed

.github/workflows/benchmark.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ jobs:
7171
run: |
7272
docker run -d -p 8529:8529 -e "ARANGO_NO_AUTH=1" arangodb:latest
7373
74-
- name: Install DynamoDB
75-
if: ${{ matrix.package == 'dynamodb' }}
76-
run: |
77-
docker run -d -p 8000:8000 amazon/dynamodb-local:latest
78-
7974
- name: Install Memcached
8075
if: ${{ matrix.package == 'memcache' }}
8176
run: |
@@ -127,13 +122,6 @@ jobs:
127122
run: |
128123
redis-server --port 6379 &
129124
130-
- name: Run NATS
131-
if: ${{ matrix.package == 'nats' }}
132-
run: |
133-
./.github/scripts/gen-test-certs.sh
134-
docker run -d --name nats-jetstream -p 4443:4443 -v ./nats/testdata:/testdata -v ./tls:/tls nats:latest --jetstream -c /testdata/nats-tls.conf
135-
sleep 2
136-
137125
- name: Run Benchmarks
138126
working-directory: ${{ matrix.package }}
139127
run: |
@@ -147,9 +135,11 @@ jobs:
147135
MSSQL_PASSWORD: MsSql!1234
148136
TEST_CLICKHOUSE_IMAGE: "clickhouse/clickhouse-server:23-alpine"
149137
TEST_COUCHBASE_IMAGE: "couchbase:enterprise-7.6.5"
138+
TEST_DYNAMODB_IMAGE: amazon/dynamodb-local:latest
150139
TEST_MINIO_IMAGE: "docker.io/minio/minio:RELEASE.2024-08-17T01-24-54Z"
151140
TEST_MONGODB_IMAGE: "docker.io/mongo:7"
152141
TEST_MYSQL_IMAGE: "docker.io/mysql:9"
142+
TEST_NATS_IMAGE: "nats:2-alpine"
153143
TEST_POSTGRES_IMAGE: "docker.io/postgres:16-alpine"
154144

155145
- name: Get Previous Benchmark Results

.github/workflows/test-dynamodb.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ jobs:
2020
strategy:
2121
matrix:
2222
go-version:
23-
- 1.19.x
24-
- 1.20.x
25-
- 1.21.x
23+
- 1.22.x
24+
- 1.23.x
25+
- 1.24.x
2626
steps:
2727
- name: Fetch Repository
2828
uses: actions/checkout@v4
@@ -31,4 +31,6 @@ jobs:
3131
with:
3232
go-version: '${{ matrix.go-version }}'
3333
- name: Run Test
34+
env:
35+
TEST_DYNAMODB_IMAGE: amazon/dynamodb-local:latest
3436
run: cd ./dynamodb && go test ./... -v -race

.github/workflows/test-nats.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ jobs:
1414
strategy:
1515
matrix:
1616
go-version:
17-
- 1.20.x
18-
- 1.21.x
1917
- 1.22.x
18+
- 1.23.x
19+
- 1.24.x
2020
runs-on: ubuntu-latest
2121
steps:
2222
- name: Fetch Repository
@@ -25,11 +25,7 @@ jobs:
2525
uses: actions/setup-go@v5
2626
with:
2727
go-version: '${{ matrix.go-version }}'
28-
- name: Generate config
29-
run: ./.github/scripts/gen-test-certs.sh
30-
- name: Run NATS
31-
run: |
32-
docker run -d --name nats-jetstream -p 4443:4443 -v ./nats/testdata:/testdata -v ./tls:/tls nats:latest --jetstream -c /testdata/nats-tls.conf
33-
sleep 5
3428
- name: Test Nats
29+
env:
30+
TEST_NATS_IMAGE: "nats:2-alpine"
3531
run: cd ./nats && go test ./... -v -race

dynamodb/dynamodb_test.go

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
package dynamodb
22

33
import (
4+
"context"
45
"os"
56
"testing"
67

78
"github.com/stretchr/testify/require"
9+
"github.com/testcontainers/testcontainers-go"
10+
"github.com/testcontainers/testcontainers-go/modules/dynamodb"
811
)
912

10-
var testStore *Storage
13+
const (
14+
// dynamoDBImage is the default image used for running DynamoDB in tests.
15+
dynamoDBImage = "amazon/dynamodb-local:latest"
16+
dynamoDBImageEnvVar string = "TEST_DYNAMODB_IMAGE"
17+
)
1118

12-
func TestMain(m *testing.M) {
13-
testStore = New(Config{
14-
Table: "fiber_storage",
15-
Endpoint: "http://localhost:8000/",
16-
Region: "us-east-1",
17-
Credentials: Credentials{
18-
AccessKey: "dummy",
19-
SecretAccessKey: "dummy",
20-
},
21-
Reset: true,
22-
})
19+
func newTestStore(t testing.TB) (*Storage, error) {
20+
t.Helper()
2321

24-
code := m.Run()
22+
img := dynamoDBImage
23+
if imgFromEnv := os.Getenv(dynamoDBImageEnvVar); imgFromEnv != "" {
24+
img = imgFromEnv
25+
}
2526

26-
_ = testStore.Close()
27-
os.Exit(code)
27+
ctx := context.Background()
28+
29+
c, err := dynamodb.Run(ctx, img, dynamodb.WithDisableTelemetry())
30+
testcontainers.CleanupContainer(t, c)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
hostPort, err := c.ConnectionString(ctx)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
return New(
41+
Config{
42+
Table: "fiber_storage",
43+
Endpoint: "http://" + hostPort,
44+
Region: "us-east-1",
45+
Credentials: Credentials{
46+
AccessKey: "dummy",
47+
SecretAccessKey: "dummy",
48+
},
49+
Reset: true,
50+
},
51+
), nil
2852
}
2953

3054
func Test_DynamoDB_Set(t *testing.T) {
@@ -33,7 +57,11 @@ func Test_DynamoDB_Set(t *testing.T) {
3357
val = []byte("doe")
3458
)
3559

36-
err := testStore.Set(key, val, 0)
60+
testStore, err := newTestStore(t)
61+
require.NoError(t, err)
62+
defer testStore.Close()
63+
64+
err = testStore.Set(key, val, 0)
3765
require.NoError(t, err)
3866
}
3967

@@ -43,7 +71,11 @@ func Test_DynamoDB_Set_Override(t *testing.T) {
4371
val = []byte("doe")
4472
)
4573

46-
err := testStore.Set(key, val, 0)
74+
testStore, err := newTestStore(t)
75+
require.NoError(t, err)
76+
defer testStore.Close()
77+
78+
err = testStore.Set(key, val, 0)
4779
require.NoError(t, err)
4880

4981
err = testStore.Set(key, val, 0)
@@ -56,7 +88,11 @@ func Test_DynamoDB_Get(t *testing.T) {
5688
val = []byte("doe")
5789
)
5890

59-
err := testStore.Set(key, val, 0)
91+
testStore, err := newTestStore(t)
92+
require.NoError(t, err)
93+
defer testStore.Close()
94+
95+
err = testStore.Set(key, val, 0)
6096
require.NoError(t, err)
6197

6298
result, err := testStore.Get(key)
@@ -65,6 +101,10 @@ func Test_DynamoDB_Get(t *testing.T) {
65101
}
66102

67103
func Test_DynamoDB_Get_NotExist(t *testing.T) {
104+
testStore, err := newTestStore(t)
105+
require.NoError(t, err)
106+
defer testStore.Close()
107+
68108
result, err := testStore.Get("notexist")
69109
require.NoError(t, err)
70110
require.Zero(t, len(result))
@@ -76,7 +116,11 @@ func Test_DynamoDB_Delete(t *testing.T) {
76116
val = []byte("doe")
77117
)
78118

79-
err := testStore.Set(key, val, 0)
119+
testStore, err := newTestStore(t)
120+
require.NoError(t, err)
121+
defer testStore.Close()
122+
123+
err = testStore.Set(key, val, 0)
80124
require.NoError(t, err)
81125

82126
err = testStore.Delete(key)
@@ -90,7 +134,11 @@ func Test_DynamoDB_Delete(t *testing.T) {
90134
func Test_DynamoDB_Reset(t *testing.T) {
91135
val := []byte("doe")
92136

93-
err := testStore.Set("john1", val, 0)
137+
testStore, err := newTestStore(t)
138+
require.NoError(t, err)
139+
defer testStore.Close()
140+
141+
err = testStore.Set("john1", val, 0)
94142
require.NoError(t, err)
95143

96144
err = testStore.Set("john2", val, 0)
@@ -109,18 +157,28 @@ func Test_DynamoDB_Reset(t *testing.T) {
109157
}
110158

111159
func Test_DynamoDB_Close(t *testing.T) {
160+
testStore, err := newTestStore(t)
161+
require.NoError(t, err)
162+
112163
require.Nil(t, testStore.Close())
113164
}
114165

115166
func Test_DynamoDB_Conn(t *testing.T) {
167+
testStore, err := newTestStore(t)
168+
require.NoError(t, err)
169+
defer testStore.Close()
170+
116171
require.True(t, testStore.Conn() != nil)
117172
}
118173

119174
func Benchmark_DynamoDB_Set(b *testing.B) {
120175
b.ReportAllocs()
121176
b.ResetTimer()
122177

123-
var err error
178+
testStore, err := newTestStore(b)
179+
require.NoError(b, err)
180+
defer testStore.Close()
181+
124182
for i := 0; i < b.N; i++ {
125183
err = testStore.Set("john", []byte("doe"), 0)
126184
}
@@ -129,7 +187,11 @@ func Benchmark_DynamoDB_Set(b *testing.B) {
129187
}
130188

131189
func Benchmark_DynamoDB_Get(b *testing.B) {
132-
err := testStore.Set("john", []byte("doe"), 0)
190+
testStore, err := newTestStore(b)
191+
require.NoError(b, err)
192+
defer testStore.Close()
193+
194+
err = testStore.Set("john", []byte("doe"), 0)
133195
require.NoError(b, err)
134196

135197
b.ReportAllocs()
@@ -146,7 +208,10 @@ func Benchmark_DynamoDB_SetAndDelete(b *testing.B) {
146208
b.ReportAllocs()
147209
b.ResetTimer()
148210

149-
var err error
211+
testStore, err := newTestStore(b)
212+
require.NoError(b, err)
213+
defer testStore.Close()
214+
150215
for i := 0; i < b.N; i++ {
151216
_ = testStore.Set("john", []byte("doe"), 0)
152217
err = testStore.Delete("john")

dynamodb/go.mod

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,76 @@
11
module github.com/gofiber/storage/dynamodb/v2
22

3-
go 1.19
3+
go 1.22
44

55
require (
6-
github.com/aws/aws-sdk-go-v2 v1.30.3
7-
github.com/aws/aws-sdk-go-v2/config v1.27.26
8-
github.com/aws/aws-sdk-go-v2/credentials v1.17.27
6+
github.com/aws/aws-sdk-go-v2 v1.31.0
7+
github.com/aws/aws-sdk-go-v2/config v1.27.37
8+
github.com/aws/aws-sdk-go-v2/credentials v1.17.35
99
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.14.10
10-
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.4
10+
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.35.1
1111
github.com/stretchr/testify v1.9.0
12+
github.com/testcontainers/testcontainers-go v0.35.0
13+
github.com/testcontainers/testcontainers-go/modules/dynamodb v0.35.0
1214
)
1315

1416
require (
15-
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 // indirect
16-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
17-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
18-
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
17+
dario.cat/mergo v1.0.0 // indirect
18+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
19+
github.com/Microsoft/go-winio v0.6.2 // indirect
20+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14 // indirect
21+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.18 // indirect
22+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.18 // indirect
23+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
1924
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.22.3 // indirect
20-
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect
21-
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.16 // indirect
22-
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect
23-
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect
24-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
25-
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
26-
github.com/aws/smithy-go v1.20.3 // indirect
25+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.5 // indirect
26+
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.19 // indirect
27+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.20 // indirect
28+
github.com/aws/aws-sdk-go-v2/service/sso v1.23.1 // indirect
29+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.1 // indirect
30+
github.com/aws/aws-sdk-go-v2/service/sts v1.31.1 // indirect
31+
github.com/aws/smithy-go v1.21.0 // indirect
32+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
33+
github.com/containerd/containerd v1.7.18 // indirect
34+
github.com/containerd/log v0.1.0 // indirect
35+
github.com/containerd/platforms v0.2.1 // indirect
36+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
2737
github.com/davecgh/go-spew v1.1.1 // indirect
38+
github.com/distribution/reference v0.6.0 // indirect
39+
github.com/docker/docker v27.1.1+incompatible // indirect
40+
github.com/docker/go-connections v0.5.0 // indirect
41+
github.com/docker/go-units v0.5.0 // indirect
42+
github.com/felixge/httpsnoop v1.0.4 // indirect
43+
github.com/go-logr/logr v1.4.1 // indirect
44+
github.com/go-logr/stdr v1.2.2 // indirect
45+
github.com/go-ole/go-ole v1.2.6 // indirect
46+
github.com/gogo/protobuf v1.3.2 // indirect
47+
github.com/google/uuid v1.6.0 // indirect
2848
github.com/jmespath/go-jmespath v0.4.0 // indirect
49+
github.com/klauspost/compress v1.17.4 // indirect
50+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
51+
github.com/magiconair/properties v1.8.7 // indirect
52+
github.com/moby/docker-image-spec v1.3.1 // indirect
53+
github.com/moby/patternmatcher v0.6.0 // indirect
54+
github.com/moby/sys/sequential v0.5.0 // indirect
55+
github.com/moby/sys/user v0.1.0 // indirect
56+
github.com/moby/term v0.5.0 // indirect
57+
github.com/morikuni/aec v1.0.0 // indirect
58+
github.com/opencontainers/go-digest v1.0.0 // indirect
59+
github.com/opencontainers/image-spec v1.1.0 // indirect
60+
github.com/pkg/errors v0.9.1 // indirect
2961
github.com/pmezard/go-difflib v1.0.0 // indirect
62+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
63+
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
64+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
65+
github.com/sirupsen/logrus v1.9.3 // indirect
66+
github.com/tklauser/go-sysconf v0.3.12 // indirect
67+
github.com/tklauser/numcpus v0.6.1 // indirect
68+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
69+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
70+
go.opentelemetry.io/otel v1.24.0 // indirect
71+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
72+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
73+
golang.org/x/crypto v0.31.0 // indirect
74+
golang.org/x/sys v0.28.0 // indirect
3075
gopkg.in/yaml.v3 v3.0.1 // indirect
3176
)

0 commit comments

Comments
 (0)