Skip to content

Commit ded7e68

Browse files
authored
feat: use testcontainers-go for the integration tests (#824)
* feat: use testcontainers-go for the integration tests * chore: bump to latest release * chore: wrap the ES container into a struct
1 parent b7bfbae commit ded7e68

File tree

4 files changed

+323
-14
lines changed

4 files changed

+323
-14
lines changed

elasticsearch_integration_test.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ import (
2727
"encoding/json"
2828
"errors"
2929
"fmt"
30-
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
31-
"github.com/elastic/go-elasticsearch/v8/typedapi/some"
32-
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
33-
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/refresh"
34-
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/result"
35-
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/sortorder"
3630
"log"
3731
"net"
3832
"net/http"
@@ -47,11 +41,40 @@ import (
4741
"github.com/elastic/elastic-transport-go/v8/elastictransport"
4842
"github.com/elastic/go-elasticsearch/v8"
4943
"github.com/elastic/go-elasticsearch/v8/esapi"
44+
"github.com/elastic/go-elasticsearch/v8/internal/containertest"
45+
"github.com/elastic/go-elasticsearch/v8/internal/version"
46+
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
47+
"github.com/elastic/go-elasticsearch/v8/typedapi/some"
48+
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
49+
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/refresh"
50+
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/result"
51+
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/sortorder"
5052
)
5153

5254
func TestClientTransport(t *testing.T) {
55+
stackVersion := version.Client
56+
if v := os.Getenv("STACK_VERSION"); v != "" {
57+
stackVersion = v
58+
}
59+
60+
elasticsearchSrv, err := containertest.NewElasticsearchService(stackVersion)
61+
if err != nil {
62+
t.Fatalf("Error setting up Elasticsearch container: %s", err)
63+
}
64+
defer func() {
65+
if err := elasticsearchSrv.Terminate(context.Background()); err != nil {
66+
t.Fatalf("Error terminating Elasticsearch container: %s", err)
67+
}
68+
}()
69+
70+
tcCfg := elasticsearchSrv.ESConfig()
71+
72+
t.Setenv("ELASTICSEARCH_URL", tcCfg.Addresses[0])
73+
t.Setenv("ELASTICSEARCH_USERNAME", tcCfg.Username)
74+
t.Setenv("ELASTICSEARCH_PASSWORD", tcCfg.Password)
75+
5376
t.Run("Persistent", func(t *testing.T) {
54-
es, err := elasticsearch.NewDefaultClient()
77+
es, err := elasticsearch.NewClient(tcCfg)
5578
if err != nil {
5679
t.Fatalf("Error creating the client: %s", err)
5780
}
@@ -103,7 +126,7 @@ func TestClientTransport(t *testing.T) {
103126
t.Run("Concurrent", func(t *testing.T) {
104127
var wg sync.WaitGroup
105128

106-
es, err := elasticsearch.NewDefaultClient()
129+
es, err := elasticsearch.NewClient(tcCfg)
107130
if err != nil {
108131
t.Fatalf("Error creating the client: %s", err)
109132
}
@@ -126,7 +149,7 @@ func TestClientTransport(t *testing.T) {
126149
})
127150

128151
t.Run("WithContext", func(t *testing.T) {
129-
es, err := elasticsearch.NewDefaultClient()
152+
es, err := elasticsearch.NewClient(tcCfg)
130153
if err != nil {
131154
t.Fatalf("Error creating the client: %s", err)
132155
}
@@ -154,6 +177,12 @@ func TestClientTransport(t *testing.T) {
154177
InsecureSkipVerify: true,
155178
},
156179
},
180+
Addresses: []string{
181+
tcCfg.Addresses[0],
182+
},
183+
Username: tcCfg.Username,
184+
Password: tcCfg.Password,
185+
CACert: tcCfg.CACert,
157186
}
158187

159188
es, err := elasticsearch.NewClient(cfg)

go.mod

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,57 @@ go 1.21
44

55
require (
66
github.com/elastic/elastic-transport-go/v8 v8.5.0
7+
github.com/testcontainers/testcontainers-go v0.29.1
8+
github.com/testcontainers/testcontainers-go/modules/elasticsearch v0.29.1
79
go.opentelemetry.io/otel/trace v1.21.0
810
)
911

1012
require (
13+
dario.cat/mergo v1.0.0 // indirect
14+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
15+
github.com/Microsoft/go-winio v0.6.1 // indirect
16+
github.com/Microsoft/hcsshim v0.11.4 // indirect
17+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
18+
github.com/containerd/containerd v1.7.12 // indirect
19+
github.com/containerd/log v0.1.0 // indirect
20+
github.com/cpuguy83/dockercfg v0.3.1 // indirect
21+
github.com/distribution/reference v0.5.0 // indirect
22+
github.com/docker/docker v25.0.3+incompatible // indirect
23+
github.com/docker/go-connections v0.5.0 // indirect
24+
github.com/docker/go-units v0.5.0 // indirect
25+
github.com/felixge/httpsnoop v1.0.3 // indirect
1126
github.com/go-logr/logr v1.3.0 // indirect
1227
github.com/go-logr/stdr v1.2.2 // indirect
28+
github.com/go-ole/go-ole v1.2.6 // indirect
29+
github.com/gogo/protobuf v1.3.2 // indirect
30+
github.com/golang/protobuf v1.5.3 // indirect
31+
github.com/google/uuid v1.6.0 // indirect
32+
github.com/klauspost/compress v1.16.0 // indirect
33+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
34+
github.com/magiconair/properties v1.8.7 // indirect
35+
github.com/moby/patternmatcher v0.6.0 // indirect
36+
github.com/moby/sys/sequential v0.5.0 // indirect
37+
github.com/moby/sys/user v0.1.0 // indirect
38+
github.com/moby/term v0.5.0 // indirect
39+
github.com/morikuni/aec v1.0.0 // indirect
40+
github.com/opencontainers/go-digest v1.0.0 // indirect
41+
github.com/opencontainers/image-spec v1.1.0 // indirect
42+
github.com/pkg/errors v0.9.1 // indirect
43+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
44+
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
45+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
46+
github.com/sirupsen/logrus v1.9.3 // indirect
47+
github.com/tklauser/go-sysconf v0.3.12 // indirect
48+
github.com/tklauser/numcpus v0.6.1 // indirect
49+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
50+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
1351
go.opentelemetry.io/otel v1.21.0 // indirect
1452
go.opentelemetry.io/otel/metric v1.21.0 // indirect
53+
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
54+
golang.org/x/mod v0.16.0 // indirect
55+
golang.org/x/sys v0.16.0 // indirect
56+
golang.org/x/tools v0.13.0 // indirect
57+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
58+
google.golang.org/grpc v1.58.3 // indirect
59+
google.golang.org/protobuf v1.31.0 // indirect
1560
)

0 commit comments

Comments
 (0)