Skip to content

Commit 32f372c

Browse files
JJCinAZjcracchiolo-tceddumelendezmdelapenya
authored
feat: add module to support InfluxDB v1.x (#1703)
* Added module to support InfluxDB v1.x * Fix comment Co-authored-by: Eddú Meléndez Gonzales <eddu.melendez@gmail.com> * Update docs/modules/influxdb.md Fix copy/paste error Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com> * chore: bump core to latest * chore: fix lint * chore: format * chore: use t.Cleanup * chore: use testdata * chore: add testable example for influxdb * chore: add tests for withConfig * chore: rename import * chore: change usae example * docs: include code snippet for musthaveconnectionurl * chore: rename function * chore: add to sonar * fix: proper order * chore: mod tidy * chore: always wait for the listening port * chore: remove TLS support for the moment * chore: mod tidy --------- Co-authored-by: Joseph Cracchiolo <jcracchiolo@tucows.com> Co-authored-by: Eddú Meléndez Gonzales <eddu.melendez@gmail.com> Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com> Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
1 parent 3541728 commit 32f372c

15 files changed

+695
-2
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
matrix:
9595
go-version: [1.21.x, 1.x]
9696
platform: [ubuntu-latest]
97-
module: [artemis, cassandra, chroma, clickhouse, cockroachdb, compose, consul, couchbase, elasticsearch, gcloud, inbucket, k3s, k6, kafka, localstack, mariadb, milvus, minio, mockserver, mongodb, mssql, mysql, nats, neo4j, ollama, openfga, openldap, opensearch, postgres, pulsar, qdrant, rabbitmq, redis, redpanda, registry, surrealdb, vault, weaviate]
97+
module: [artemis, cassandra, chroma, clickhouse, cockroachdb, compose, consul, couchbase, elasticsearch, gcloud, inbucket, influxdb, k3s, k6, kafka, localstack, mariadb, milvus, minio, mockserver, mongodb, mssql, mysql, nats, neo4j, ollama, openfga, openldap, opensearch, postgres, pulsar, qdrant, rabbitmq, redis, redpanda, registry, surrealdb, vault, weaviate]
9898
uses: ./.github/workflows/ci-test-go.yml
9999
with:
100100
go-version: ${{ matrix.go-version }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ src/pip-delete-this-directory.txt
1111
.DS_Store
1212

1313
TEST-*.xml
14+
15+
**/go.work

.vscode/.testcontainers-go.code-workspace

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
"name": "module / inbucket",
5858
"path": "../modules/inbucket"
5959
},
60+
{
61+
"name": "module / influxdb",
62+
"path": "../modules/influxdb"
63+
},
6064
{
6165
"name": "module / k3s",
6266
"path": "../modules/k3s"

docs/modules/influxdb.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# InfluxDB
2+
3+
Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>
4+
5+
## Introduction
6+
7+
A testcontainers module for InfluxDB. This module supports v1.x of InfluxDB.
8+
9+
## Adding this module to your project dependencies
10+
11+
Please run the following command to add the InfluxDB module to your Go dependencies:
12+
13+
```
14+
go get github.com/testcontainers/testcontainers-go/modules/influxdb
15+
```
16+
17+
## Usage example
18+
19+
<!--codeinclude-->
20+
[Creating an InfluxDB container](../../modules/influxdb/examples_test.go) inside_block:runInfluxContainer
21+
<!--/codeinclude-->
22+
23+
## Module Reference
24+
25+
The InfluxDB module exposes one entrypoint function to create the container, and this function receives two parameters:
26+
27+
```golang
28+
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*InfluxDbContainer, error) {}
29+
```
30+
31+
- `context.Context`, the Go context.
32+
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.
33+
34+
### Container Options
35+
36+
When starting the container, you can pass options in a variadic way to configure it.
37+
38+
!!!tip
39+
40+
You can find configuration information for the InfluxDB image on [Docker Hub](https://hub.docker.com/_/influxdb) and a list of possible
41+
environment variables on [InfluxDB documentation](https://docs.influxdata.com/influxdb/v1/administration/config/).
42+
43+
#### Image
44+
45+
To use a different Docker image, you can use the `testcontainers.WithImage` option to specify the
46+
image, E.g. `testcontainers.WithImage("influxdb:1.8.0")`. By default, the 1.8.10 image is used. Note that
47+
`influxdb:latest` will get you a version 2 image which is not supported by this module.
48+
49+
50+
{% include "../features/common_functional_options.md" %}
51+
52+
#### Set username, password and database name
53+
54+
By default, authentication is disabled and no credentials are needed to use the Influx API against the test container.
55+
If you want to test with credentials, include the appropriate environment variables to do so.
56+
57+
#### Init Scripts
58+
59+
While the InfluxDB image will obey the `/docker-entrypoint-initdb.d` directory as is common, that directory does not
60+
exist in the default image. Instead, you can use the `WithInitDb` option to pass a directory which will be copied to
61+
when the container starts. Any `*.sh` or `*.iql` files in the directory will be processed by the image upon startup.
62+
When executing these scripts, the `init-influxdb.sh` script in the image will start the InfluxDB server, run the
63+
scripts, stop the server, and restart the server. This makes it tricky to detect the readiness of the container.
64+
This module looks for that and adds some extra tests for readiness, but these could be fragile.
65+
66+
!!!important
67+
The `WithInitDb` option receives a path to the parent directory of one named `docker-entrypoint-initdb.d`. This is
68+
because the `docker-entrypoint-initdb.d` directory is not present in the image.
69+
70+
#### Custom configuration
71+
72+
If you need to set a custom configuration, you can use `WithConfigFile` option to pass the path to a custom configuration file.
73+
74+
### Container Methods
75+
76+
#### ConnectionUrl
77+
78+
This function is a simple helper to return a URL to the container, using the default `8086` port.
79+
80+
<!--codeinclude-->
81+
[ConnectionUrl](../../modules/influxdb/influxdb_test.go) inside_block:influxConnectionUrl
82+
<!--/codeinclude-->
83+
84+
Please check the existence of two methods: `ConnectionUrl` and `MustConnectionUrl`. The latter is used to avoid the need to handle errors,
85+
while the former is used to return the URL and the error. `MustConnectionUrl` will panic if an error occurs.
86+
87+
!!!info
88+
The `ConnectionUrl` and `MustConnectionUrl` methods only support HTTP connections at the moment.

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ nav:
7373
- modules/elasticsearch.md
7474
- modules/gcloud.md
7575
- modules/inbucket.md
76+
- modules/influxdb.md
7677
- modules/k3s.md
7778
- modules/k6.md
7879
- modules/kafka.md

modules/influxdb/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include ../../commons-test.mk
2+
3+
.PHONY: test
4+
test:
5+
$(MAKE) test-influxdb

modules/influxdb/examples_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package influxdb_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/testcontainers/testcontainers-go"
9+
"github.com/testcontainers/testcontainers-go/modules/influxdb"
10+
)
11+
12+
func ExampleRunContainer() {
13+
// runInfluxContainer {
14+
ctx := context.Background()
15+
16+
influxdbContainer, err := influxdb.RunContainer(
17+
ctx, testcontainers.WithImage("influxdb:1.8.10"),
18+
influxdb.WithDatabase("influx"),
19+
influxdb.WithUsername("root"),
20+
influxdb.WithPassword("password"),
21+
)
22+
if err != nil {
23+
log.Fatalf("failed to start container: %s", err)
24+
}
25+
26+
// Clean up the container
27+
defer func() {
28+
if err := influxdbContainer.Terminate(ctx); err != nil {
29+
log.Fatalf("failed to terminate container: %s", err)
30+
}
31+
}()
32+
// }
33+
34+
state, err := influxdbContainer.State(ctx)
35+
if err != nil {
36+
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
37+
}
38+
39+
fmt.Println(state.Running)
40+
41+
// Output:
42+
// true
43+
}

modules/influxdb/go.mod

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module github.com/testcontainers/testcontainers-go/modules/influxdb
2+
3+
go 1.21
4+
5+
require (
6+
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
7+
github.com/stretchr/testify v1.9.0
8+
github.com/testcontainers/testcontainers-go v0.29.1
9+
)
10+
11+
require (
12+
dario.cat/mergo v1.0.0 // indirect
13+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
14+
github.com/Microsoft/go-winio v0.6.1 // indirect
15+
github.com/Microsoft/hcsshim v0.11.4 // indirect
16+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
17+
github.com/containerd/containerd v1.7.12 // indirect
18+
github.com/containerd/log v0.1.0 // indirect
19+
github.com/cpuguy83/dockercfg v0.3.1 // indirect
20+
github.com/davecgh/go-spew v1.1.1 // indirect
21+
github.com/distribution/reference v0.5.0 // indirect
22+
github.com/docker/docker v25.0.5+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
26+
github.com/go-logr/logr v1.2.4 // indirect
27+
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/kr/pretty v0.3.1 // indirect
34+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
35+
github.com/magiconair/properties v1.8.7 // indirect
36+
github.com/moby/patternmatcher v0.6.0 // indirect
37+
github.com/moby/sys/sequential v0.5.0 // indirect
38+
github.com/moby/sys/user v0.1.0 // indirect
39+
github.com/moby/term v0.5.0 // indirect
40+
github.com/morikuni/aec v1.0.0 // indirect
41+
github.com/opencontainers/go-digest v1.0.0 // indirect
42+
github.com/opencontainers/image-spec v1.1.0 // indirect
43+
github.com/pkg/errors v0.9.1 // indirect
44+
github.com/pmezard/go-difflib v1.0.0 // indirect
45+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
46+
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
47+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
48+
github.com/sirupsen/logrus v1.9.3 // indirect
49+
github.com/tklauser/go-sysconf v0.3.12 // indirect
50+
github.com/tklauser/numcpus v0.6.1 // indirect
51+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
52+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
53+
go.opentelemetry.io/otel v1.19.0 // indirect
54+
go.opentelemetry.io/otel/metric v1.19.0 // indirect
55+
go.opentelemetry.io/otel/trace v1.19.0 // indirect
56+
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
57+
golang.org/x/mod v0.16.0 // indirect
58+
golang.org/x/sys v0.16.0 // indirect
59+
golang.org/x/tools v0.13.0 // indirect
60+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
61+
google.golang.org/grpc v1.58.3 // indirect
62+
google.golang.org/protobuf v1.33.0 // indirect
63+
gopkg.in/yaml.v3 v3.0.1 // indirect
64+
)
65+
66+
replace github.com/testcontainers/testcontainers-go => ../..

0 commit comments

Comments
 (0)