@@ -25,18 +25,17 @@ private static class SnapshottingSettings {
2525 }
2626
2727 private static final DockerImageName DEFAULT_IMAGE = DockerImageName .parse (
28- "valkey/valkey:7.2.5 " );
28+ "valkey/valkey:8.1 " );
2929
3030 private static final String DEFAULT_CONFIG_FILE = "/usr/local/valkey.conf" ;
3131
3232 private static final int CONTAINER_PORT = 6379 ;
3333
34- @ Getter
3534 private String username ;
36- @ Getter
3735 private String password ;
3836 private String persistenceVolume ;
3937 private String initialImportScriptFile ;
38+ private String configFile ;
4039 private ValkeyLogLevel logLevel ;
4140 private SnapshottingSettings snapshottingSettings ;
4241
@@ -112,9 +111,7 @@ public ValkeyContainer withSnapshotting(int seconds, int changedKeys) {
112111 * Sets the config file to be used for the Valkey container.
113112 */
114113 public ValkeyContainer withConfigFile (String configFile ) {
115- withCopyFileToContainer (MountableFile .forHostPath (configFile ), DEFAULT_CONFIG_FILE );
116-
117- // TODO check whether config path needs to be specified on startup
114+ this .configFile = configFile ;
118115
119116 return this ;
120117 }
@@ -124,13 +121,17 @@ public void start() {
124121 List <String > command = new ArrayList <>();
125122 command .add ("valkey-server" );
126123
124+ if (configFile != null && !configFile .isEmpty ()) {
125+ withCopyToContainer (MountableFile .forHostPath (configFile ), DEFAULT_CONFIG_FILE );
126+ command .add (DEFAULT_CONFIG_FILE );
127+ }
128+
127129 if (password != null && !password .isEmpty ()) {
128130 command .add ("--requirepass" );
129131 command .add (password );
130132
131133 if (username != null && !username .isEmpty ()) {
132- command .add ("--user" );
133- command .add (username + " on >" + password + " ~* +@all" );
134+ command .add ("--user " + username + " on >" + password + " ~* +@all" );
134135 }
135136 }
136137
@@ -161,38 +162,56 @@ public void start() {
161162
162163 super .start ();
163164
164- if (initialImportScriptFile != null && !initialImportScriptFile .isEmpty ()) {
165- try {
166- ExecResult result = this .execInContainer ("/bin/sh" , "/tmp/import.sh" ,
167- password != null ? password : "" );
168- if (result .getExitCode () != 0 || result .getStdout ().contains ("ERR" )) {
169- throw new RuntimeException (
170- "Could not import initial data: " + result .getStdout ());
171- }
172- } catch (Exception e ) {
173- throw new RuntimeException (e );
174- }
175- }
165+ evaluateImportScript ();
176166 }
177167
178168 public int getPort () {
179169 return getMappedPort (CONTAINER_PORT );
180170 }
181171
172+ /**
173+ * Executes a command in the Valkey CLI inside the container.
174+ */
175+ public String executeCli (String cmd , String ... flags ) {
176+ try {
177+ List <String > args = new ArrayList <>();
178+ args .add ("redis-cli" );
179+
180+ if (password != null && !password .isEmpty ()) {
181+ args .addAll (username != null && !username .isEmpty ()
182+ ? Arrays .asList ("--user" , username , "--pass" , password )
183+ : Arrays .asList ("--pass" , password )
184+ );
185+ }
186+
187+ args .add (cmd );
188+ args .addAll (Arrays .asList (flags ));
189+
190+ ExecResult result = execInContainer (args .toArray (new String [0 ]));
191+ if (result .getExitCode () != 0 ) {
192+ throw new RuntimeException (result .getStdout () + result .getStderr ());
193+ }
194+
195+ return result .getStdout ();
196+ } catch (Exception e ) {
197+ throw new RuntimeException (e );
198+ }
199+ }
200+
182201 public String createConnectionUrl () {
183202 String userInfo = null ;
184203 if (username != null && !username .isEmpty () && password != null && !password .isEmpty ()) {
185204 userInfo = username + ":" + password ;
186205 } else if (password != null && !password .isEmpty ()) {
187- userInfo = password ;
206+ userInfo = ":" + password ;
188207 }
189208
190209 try {
191210 URI uri = new URI (
192211 "redis" ,
193212 userInfo ,
194- this . getHost (),
195- this . getPort (),
213+ getHost (),
214+ getPort (),
196215 null ,
197216 null ,
198217 null
@@ -203,4 +222,22 @@ public String createConnectionUrl() {
203222 }
204223 }
205224
225+ private void evaluateImportScript () {
226+ if (initialImportScriptFile == null || initialImportScriptFile .isEmpty ()) {
227+ return ;
228+ }
229+
230+ try {
231+ ExecResult result = execInContainer ("/bin/sh" , "/tmp/import.sh" ,
232+ password != null ? password : "" );
233+
234+ if (result .getExitCode () != 0 || result .getStdout ().contains ("ERR" )) {
235+ throw new RuntimeException (
236+ "Could not import initial data: " + result .getStdout ());
237+ }
238+ } catch (Exception e ) {
239+ throw new RuntimeException (e );
240+ }
241+ }
242+
206243}
0 commit comments