InfluxDB¶
Since v0.30.0
Introduction¶
A testcontainers module for InfluxDB V1 and V2.
Adding this module to your project dependencies¶
Please run the following command to add the InfluxDB module to your Go dependencies:
go get github.com/testcontainers/testcontainers-go/modules/influxdb
Usage example¶
InfluxDB¶
ctx := context.Background()
influxdbContainer, err := influxdb.Run(ctx,
"influxdb:1.8.10",
influxdb.WithDatabase("influx"),
influxdb.WithUsername("root"),
influxdb.WithPassword("password"),
)
defer func() {
if err := testcontainers.TerminateContainer(influxdbContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
ctx := context.Background()
username := "username"
password := "password"
org := "org"
bucket := "bucket"
token := "influxdbv2token"
influxdbContainer, err := influxdb.Run(ctx, "influxdb:2.7.11",
influxdb.WithV2Auth(org, bucket, username, password), // Set the username and password
influxdb.WithV2AdminToken(token), // Set the admin token
)
defer func() {
if err := testcontainers.TerminateContainer(influxdbContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
Module Reference¶
Run function¶
- Since v0.32.0
Info
The RunContainer(ctx, opts...)
function is deprecated and will be removed in the next major release of Testcontainers for Go.
The InfluxDB module exposes one entrypoint function to create the container, and this function receives three parameters:
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*InfluxDbContainer, error)
context.Context
, the Go context.string
, the Docker image to use.testcontainers.ContainerCustomizer
, a variadic argument for passing options.
Image¶
Use the second argument in the Run
function to set a valid Docker image.
In example: Run(context.Background(), "influxdb:1.8.0")
.
Info
Note that influxdb:latest
will pull a version 2 image.
Container Options¶
When starting the container, you can pass options in a variadic way to configure it.
Tip
You can find configuration information for the InfluxDB image on Docker Hub and a list of possible environment variables on InfluxDB documentation.
Set username, password and database name¶
- Since v0.30.0
By default, authentication is disabled and no credentials are needed to use the Influx API against the test container. If you want to test with credentials, include the appropriate environment variables to do so.
Configuring InfluxDB V2¶
- Since v0.37.0
When running the InfluxDB V2 image, you can override the default configuration by using options prefixed by influxdb.WithV2
.
The following options are available:
WithV2(org, bucket string)
: Configures organization and bucket name. This option is required to run the InfluxDB V2 image.WithV2Auth(org, bucket, username, password string)
: Sets the username and password for the initial user.WithV2SecretsAuth(org, bucket, usernameFile, passwordFile string)
: Sets the username and password file path.WithV2Retention(retention time.Duration)
: Sets the default bucket retention policy.WithV2AdminToken(token string)
: Sets the admin token for the initial user.WithV2SecretsAdminToken(tokenFile string)
: Sets the admin token file path.
WithInitDb¶
- Since v0.30.0
While the InfluxDB image will obey the /docker-entrypoint-initdb.d
directory as is common, that directory does not
exist in the default image. Instead, you can use the WithInitDb
option to pass a directory which will be copied to
when the container starts. Any *.sh
or *.iql
files in the directory will be processed by the image upon startup.
When executing these scripts, the init-influxdb.sh
script in the image will start the InfluxDB server, run the
scripts, stop the server, and restart the server. This makes it tricky to detect the readiness of the container.
This module looks for that and adds some extra tests for readiness, but these could be fragile.
Important
The WithInitDb
option receives a path to the parent directory of one named docker-entrypoint-initdb.d
. This is
because the docker-entrypoint-initdb.d
directory is not present in the image.
WithConfigFile¶
- Since v0.30.0
If you need to set a custom configuration, you can use WithConfigFile
option to pass the path to a custom configuration file.
The following options are exposed by the testcontainers
package.
Basic Options¶
WithExposedPorts
Since v0.37.0WithEnv
Since v0.29.0WithWaitStrategy
Since v0.20.0WithAdditionalWaitStrategy
Not available until the next release mainWithWaitStrategyAndDeadline
Since v0.20.0WithAdditionalWaitStrategyAndDeadline
Not available until the next release mainWithEntrypoint
Since v0.37.0WithEntrypointArgs
Since v0.37.0WithCmd
Since v0.37.0WithCmdArgs
Since v0.37.0WithLabels
Since v0.37.0
Lifecycle Options¶
WithLifecycleHooks
Not available until the next release mainWithAdditionalLifecycleHooks
Not available until the next release mainWithStartupCommand
Since v0.25.0WithAfterReadyCommand
Since v0.28.0
Files & Mounts Options¶
WithFiles
Since v0.37.0WithMounts
Since v0.37.0WithTmpfs
Since v0.37.0WithImageMount
Since v0.37.0
Build Options¶
WithDockerfile
Since v0.37.0
Logging Options¶
WithLogConsumers
Since v0.28.0WithLogConsumerConfig
Not available until the next release mainWithLogger
Since v0.29.0
Image Options¶
WithAlwaysPull
Not available until the next release mainWithImageSubstitutors
Since v0.26.0WithImagePlatform
Not available until the next release main
Networking Options¶
WithNetwork
Since v0.27.0WithNetworkByName
Not available until the next release mainWithBridgeNetwork
Not available until the next release mainWithNewNetwork
Since v0.27.0
Advanced Options¶
WithHostPortAccess
Since v0.31.0WithConfigModifier
Since v0.20.0WithHostConfigModifier
Since v0.20.0WithEndpointSettingsModifier
Since v0.20.0CustomizeRequest
Since v0.20.0WithName
Not available until the next release mainWithNoStart
Not available until the next release main
Experimental Options¶
WithReuseByName
Since v0.37.0
Container Methods¶
ConnectionUrl¶
- Since v0.30.0
This function is a simple helper to return a URL to the container, using the default 8086
port.
cli, err := influxclient.NewHTTPClient(influxclient.HTTPConfig{
Addr: influxDBContainer.MustConnectionUrl(context.Background()),
})
Please check the existence of two methods: ConnectionUrl
and MustConnectionUrl
. The latter is used to avoid the need to handle errors,
while the former is used to return the URL and the error. MustConnectionUrl
will panic if an error occurs.
Info
The ConnectionUrl
and MustConnectionUrl
methods only support HTTP connections at the moment.