11package org .testcontainers .valkey ;
22
33import com .google .common .base .Preconditions ;
4- import java .net .URI ;
5- import java .net .URISyntaxException ;
6- import java .time .Duration ;
7- import java .util .ArrayList ;
8- import java .util .Arrays ;
9- import java .util .List ;
104import lombok .AllArgsConstructor ;
115import lombok .Getter ;
126import org .testcontainers .containers .GenericContainer ;
137import org .testcontainers .containers .wait .strategy .Wait ;
148import org .testcontainers .utility .DockerImageName ;
159import org .testcontainers .utility .MountableFile ;
1610
11+ import java .net .URI ;
12+ import java .net .URISyntaxException ;
13+ import java .time .Duration ;
14+ import java .util .ArrayList ;
15+ import java .util .Arrays ;
16+ import java .util .List ;
17+
1718/**
1819 * Testcontainers implementation for Valkey.
1920 * <p>
@@ -31,22 +32,28 @@ public class ValkeyContainer extends GenericContainer<ValkeyContainer> {
3132 private static class SnapshottingSettings {
3233
3334 int seconds ;
35+
3436 int changedKeys ;
3537 }
3638
37- private static final DockerImageName DEFAULT_IMAGE = DockerImageName .parse (
38- "valkey/valkey:8.1" );
39+ private static final DockerImageName DEFAULT_IMAGE = DockerImageName .parse ("valkey/valkey:8.1" );
3940
4041 private static final String DEFAULT_CONFIG_FILE = "/usr/local/valkey.conf" ;
4142
4243 private static final int CONTAINER_PORT = 6379 ;
4344
4445 private String username ;
46+
4547 private String password ;
48+
4649 private String persistenceVolume ;
50+
4751 private String initialImportScriptFile ;
52+
4853 private String configFile ;
54+
4955 private ValkeyLogLevel logLevel ;
56+
5057 private SnapshottingSettings snapshottingSettings ;
5158
5259 public ValkeyContainer () {
@@ -59,7 +66,6 @@ public ValkeyContainer(String dockerImageName) {
5966
6067 public ValkeyContainer (DockerImageName dockerImageName ) {
6168 super (dockerImageName );
62-
6369 withExposedPorts (CONTAINER_PORT );
6470 withStartupTimeout (Duration .ofMinutes (2 ));
6571 waitingFor (Wait .forLogMessage (".*Ready to accept connections.*" , 1 ));
@@ -151,21 +157,18 @@ public void start() {
151157 }
152158
153159 if (snapshottingSettings != null ) {
154- command .addAll (Arrays .asList (
155- "--save" ,
156- snapshottingSettings .getSeconds () + " " + snapshottingSettings .getChangedKeys ()
157- ));
160+ command .addAll (
161+ Arrays .asList ("--save" , snapshottingSettings .getSeconds () + " " + snapshottingSettings .getChangedKeys ())
162+ );
158163 }
159164
160165 if (logLevel != null ) {
161166 command .addAll (Arrays .asList ("--loglevel" , logLevel .name ()));
162167 }
163168
164169 if (initialImportScriptFile != null && !initialImportScriptFile .isEmpty ()) {
165- withCopyToContainer (MountableFile .forHostPath (initialImportScriptFile ),
166- "/tmp/import.valkey" );
167- withCopyToContainer (MountableFile .forClasspathResource ("import.sh" ),
168- "/tmp/import.sh" );
170+ withCopyToContainer (MountableFile .forHostPath (initialImportScriptFile ), "/tmp/import.valkey" );
171+ withCopyToContainer (MountableFile .forClasspathResource ("import.sh" ), "/tmp/import.sh" );
169172 }
170173
171174 withCommand (command .toArray (new String [0 ]));
@@ -188,9 +191,10 @@ public String executeCli(String cmd, String... flags) {
188191 args .add ("redis-cli" );
189192
190193 if (password != null && !password .isEmpty ()) {
191- args .addAll (username != null && !username .isEmpty ()
192- ? Arrays .asList ("--user" , username , "--pass" , password )
193- : Arrays .asList ("--pass" , password )
194+ args .addAll (
195+ username != null && !username .isEmpty ()
196+ ? Arrays .asList ("--user" , username , "--pass" , password )
197+ : Arrays .asList ("--pass" , password )
194198 );
195199 }
196200
@@ -217,15 +221,7 @@ public String createConnectionUrl() {
217221 }
218222
219223 try {
220- URI uri = new URI (
221- "redis" ,
222- userInfo ,
223- getHost (),
224- getPort (),
225- null ,
226- null ,
227- null
228- );
224+ URI uri = new URI ("redis" , userInfo , getHost (), getPort (), null , null , null );
229225 return uri .toString ();
230226 } catch (URISyntaxException e ) {
231227 throw new RuntimeException ("Failed to build Redis URI" , e );
@@ -238,16 +234,13 @@ private void evaluateImportScript() {
238234 }
239235
240236 try {
241- ExecResult result = execInContainer ("/bin/sh" , "/tmp/import.sh" ,
242- password != null ? password : "" );
237+ ExecResult result = execInContainer ("/bin/sh" , "/tmp/import.sh" , password != null ? password : "" );
243238
244239 if (result .getExitCode () != 0 || result .getStdout ().contains ("ERR" )) {
245- throw new RuntimeException (
246- "Could not import initial data: " + result .getStdout ());
240+ throw new RuntimeException ("Could not import initial data: " + result .getStdout ());
247241 }
248242 } catch (Exception e ) {
249243 throw new RuntimeException (e );
250244 }
251245 }
252-
253246}
0 commit comments