@@ -28,38 +28,28 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
2828
2929// Run creates an instance of the InfluxDB container type
3030func Run (ctx context.Context , img string , opts ... testcontainers.ContainerCustomizer ) (* InfluxDbContainer , error ) {
31- req := testcontainers.ContainerRequest {
32- Image : img ,
33- ExposedPorts : []string {"8086/tcp" , "8088/tcp" },
34- Env : map [string ]string {
31+ moduleOpts := []testcontainers.ContainerCustomizer {
32+ testcontainers .WithExposedPorts ("8086/tcp" , "8088/tcp" ),
33+ testcontainers .WithEnv (map [string ]string {
3534 "INFLUXDB_BIND_ADDRESS" : ":8088" ,
3635 "INFLUXDB_HTTP_BIND_ADDRESS" : ":8086" ,
3736 "INFLUXDB_REPORTING_DISABLED" : "true" ,
3837 "INFLUXDB_MONITOR_STORE_ENABLED" : "false" ,
3938 "INFLUXDB_HTTP_HTTPS_ENABLED" : "false" ,
4039 "INFLUXDB_HTTP_AUTH_ENABLED" : "false" ,
41- },
42- WaitingFor : waitForHTTPHealth (),
43- }
44- genericContainerReq := testcontainers.GenericContainerRequest {
45- ContainerRequest : req ,
46- Started : true ,
47- }
48-
49- for _ , opt := range opts {
50- if err := opt .Customize (& genericContainerReq ); err != nil {
51- return nil , err
52- }
40+ }),
41+ testcontainers .WithWaitStrategy (waitForHTTPHealth ()),
5342 }
43+ moduleOpts = append (moduleOpts , opts ... )
5444
55- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
45+ container , err := testcontainers .Run (ctx , img , moduleOpts ... )
5646 var c * InfluxDbContainer
5747 if container != nil {
5848 c = & InfluxDbContainer {Container : container }
5949 }
6050
6151 if err != nil {
62- return c , fmt .Errorf ("generic container : %w" , err )
52+ return c , fmt .Errorf ("run influxdb : %w" , err )
6353 }
6454
6555 return c , nil
@@ -80,36 +70,29 @@ func (c *InfluxDbContainer) ConnectionUrl(ctx context.Context) (string, error) {
8070}
8171
8272func WithUsername (username string ) testcontainers.CustomizeRequestOption {
83- return func (req * testcontainers.GenericContainerRequest ) error {
84- req .Env ["INFLUXDB_USER" ] = username
85- return nil
86- }
73+ return testcontainers .WithEnv (map [string ]string {
74+ "INFLUXDB_USER" : username ,
75+ })
8776}
8877
8978func WithPassword (password string ) testcontainers.CustomizeRequestOption {
90- return func (req * testcontainers.GenericContainerRequest ) error {
91- req .Env ["INFLUXDB_PASSWORD" ] = password
92- return nil
93- }
79+ return testcontainers .WithEnv (map [string ]string {
80+ "INFLUXDB_PASSWORD" : password ,
81+ })
9482}
9583
9684func WithDatabase (database string ) testcontainers.CustomizeRequestOption {
97- return func (req * testcontainers.GenericContainerRequest ) error {
98- req .Env ["INFLUXDB_DATABASE" ] = database
99- return nil
100- }
85+ return testcontainers .WithEnv (map [string ]string {
86+ "INFLUXDB_DATABASE" : database ,
87+ })
10188}
10289
10390func WithConfigFile (configFile string ) testcontainers.CustomizeRequestOption {
104- return func (req * testcontainers.GenericContainerRequest ) error {
105- cf := testcontainers.ContainerFile {
106- HostFilePath : configFile ,
107- ContainerFilePath : "/etc/influxdb/influxdb.conf" ,
108- FileMode : 0o755 ,
109- }
110- req .Files = append (req .Files , cf )
111- return nil
112- }
91+ return testcontainers .WithFiles (testcontainers.ContainerFile {
92+ HostFilePath : configFile ,
93+ ContainerFilePath : "/etc/influxdb/influxdb.conf" ,
94+ FileMode : 0o755 ,
95+ })
11396}
11497
11598// withV2 configures the influxdb container to be compatible with InfluxDB v2
@@ -122,21 +105,17 @@ func withV2(req *testcontainers.GenericContainerRequest, org, bucket string) err
122105 return errors .New ("bucket name is required" )
123106 }
124107
125- req .Env ["DOCKER_INFLUXDB_INIT_ORG" ] = org
126- req .Env ["DOCKER_INFLUXDB_INIT_BUCKET" ] = bucket
127- req .Env ["DOCKER_INFLUXDB_INIT_MODE" ] = "setup" // Always setup, we wont be migrating from v1 to v2
128- return nil
108+ return testcontainers .WithEnv (map [string ]string {
109+ "DOCKER_INFLUXDB_INIT_ORG" : org ,
110+ "DOCKER_INFLUXDB_INIT_BUCKET" : bucket ,
111+ "DOCKER_INFLUXDB_INIT_MODE" : "setup" , // Always setup, we wont be migrating from v1 to v2
112+ })(req )
129113}
130114
131115// WithV2 configures the influxdb container to be compatible with InfluxDB v2
132116func WithV2 (org , bucket string ) testcontainers.CustomizeRequestOption {
133117 return func (req * testcontainers.GenericContainerRequest ) error {
134- err := withV2 (req , org , bucket )
135- if err != nil {
136- return err
137- }
138-
139- return nil
118+ return withV2 (req , org , bucket )
140119 }
141120}
142121
@@ -168,9 +147,10 @@ func WithV2Auth(org, bucket, username, password string) testcontainers.Customize
168147 return errors .New ("username and password file already set, use either WithV2Auth or WithV2SecretsAuth" )
169148 }
170149
171- req .Env ["DOCKER_INFLUXDB_INIT_USERNAME" ] = username
172- req .Env ["DOCKER_INFLUXDB_INIT_PASSWORD" ] = password
173- return nil
150+ return testcontainers .WithEnv (map [string ]string {
151+ "DOCKER_INFLUXDB_INIT_USERNAME" : username ,
152+ "DOCKER_INFLUXDB_INIT_PASSWORD" : password ,
153+ })(req )
174154 }
175155}
176156
@@ -195,9 +175,10 @@ func WithV2SecretsAuth(org, bucket, usernameFile, passwordFile string) testconta
195175 return err
196176 }
197177
198- req .Env ["DOCKER_INFLUXDB_INIT_USERNAME_FILE" ] = secretsPath (usernameFile )
199- req .Env ["DOCKER_INFLUXDB_INIT_PASSWORD_FILE" ] = secretsPath (passwordFile )
200- return nil
178+ return testcontainers .WithEnv (map [string ]string {
179+ "DOCKER_INFLUXDB_INIT_USERNAME_FILE" : secretsPath (usernameFile ),
180+ "DOCKER_INFLUXDB_INIT_PASSWORD_FILE" : secretsPath (passwordFile ),
181+ })(req )
201182 }
202183}
203184
@@ -208,9 +189,9 @@ func WithV2Retention(retention time.Duration) testcontainers.CustomizeRequestOpt
208189 return errors .New ("retention is required" )
209190 }
210191
211- req . Env [ "DOCKER_INFLUXDB_INIT_RETENTION" ] = retention . String ()
212-
213- return nil
192+ return testcontainers . WithEnv ( map [ string ] string {
193+ "DOCKER_INFLUXDB_INIT_RETENTION" : retention . String (),
194+ })( req )
214195 }
215196}
216197
@@ -225,9 +206,9 @@ func WithV2AdminToken(token string) testcontainers.CustomizeRequestOption {
225206 return errors .New ("admin token file already set, use either WithV2AdminToken or WithV2SecretsAdminToken" )
226207 }
227208
228- req . Env [ "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN" ] = token
229-
230- return nil
209+ return testcontainers . WithEnv ( map [ string ] string {
210+ "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN" : token ,
211+ })( req )
231212 }
232213}
233214
@@ -242,9 +223,9 @@ func WithV2SecretsAdminToken(tokenFile string) testcontainers.CustomizeRequestOp
242223 return errors .New ("admin token already set, use either WithV2AdminToken or WithV2SecretsAdminToken" )
243224 }
244225
245- req . Env [ "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN_FILE" ] = secretsPath ( tokenFile )
246-
247- return nil
226+ return testcontainers . WithEnv ( map [ string ] string {
227+ "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN_FILE" : secretsPath ( tokenFile ),
228+ })( req )
248229 }
249230}
250231
@@ -254,18 +235,18 @@ func WithV2SecretsAdminToken(tokenFile string) testcontainers.CustomizeRequestOp
254235//nolint:staticcheck //FIXME
255236func WithInitDb (srcPath string ) testcontainers.CustomizeRequestOption {
256237 return func (req * testcontainers.GenericContainerRequest ) error {
257- cf := testcontainers.ContainerFile {
238+ if err := testcontainers . WithFiles ( testcontainers.ContainerFile {
258239 HostFilePath : path .Join (srcPath , "docker-entrypoint-initdb.d" ),
259240 ContainerFilePath : "/" ,
260241 FileMode : 0o755 ,
242+ })(req ); err != nil {
243+ return err
261244 }
262- req .Files = append (req .Files , cf )
263245
264- req . WaitingFor = wait . ForAll (
246+ return testcontainers . WithAdditionalWaitStrategy (
265247 wait .ForLog ("Server shutdown completed" ),
266248 waitForHTTPHealth (),
267- )
268- return nil
249+ )(req )
269250 }
270251}
271252
0 commit comments