Skip to content

Commit 2634d0c

Browse files
eddumelendezwilkinsona
authored andcommitted
Support service connections for redis-stack and redis-stack-server
See gh-41327
1 parent 2216b48 commit 2634d0c

File tree

9 files changed

+253
-7
lines changed

9 files changed

+253
-7
lines changed

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactoryIntegrationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Andy Wilkinson
3131
* @author Phillip Webb
3232
* @author Scott Frederick
33+
* @author Eddú Meléndez
3334
*/
3435
class RedisDockerComposeConnectionDetailsFactoryIntegrationTests {
3536

@@ -43,6 +44,16 @@ void runWithBitnamiImageCreatesConnectionDetails(RedisConnectionDetails connecti
4344
assertConnectionDetails(connectionDetails);
4445
}
4546

47+
@DockerComposeTest(composeFile = "redis-compose.yaml", image = TestImage.REDIS_STACK)
48+
void runWithRedisStackCreatesConnectionDetails(RedisConnectionDetails connectionDetails) {
49+
assertConnectionDetails(connectionDetails);
50+
}
51+
52+
@DockerComposeTest(composeFile = "redis-compose.yaml", image = TestImage.REDIS_STACK_SERVER)
53+
void runWithRedisStackServerCreatesConnectionDetails(RedisConnectionDetails connectionDetails) {
54+
assertConnectionDetails(connectionDetails);
55+
}
56+
4657
private void assertConnectionDetails(RedisConnectionDetails connectionDetails) {
4758
assertThat(connectionDetails.getUsername()).isNull();
4859
assertThat(connectionDetails.getPassword()).isNull();

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@
2929
* @author Andy Wilkinson
3030
* @author Phillip Webb
3131
* @author Scott Frederick
32+
* @author Eddú Meléndez
3233
*/
3334
class RedisDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<RedisConnectionDetails> {
3435

35-
private static final String[] REDIS_CONTAINER_NAMES = { "redis", "bitnami/redis" };
36+
private static final String[] REDIS_IMAGE_NAMES = { "redis", "bitnami/redis", "redis/redis-stack",
37+
"redis/redis-stack-server" };
3638

3739
private static final int REDIS_PORT = 6379;
3840

3941
RedisDockerComposeConnectionDetailsFactory() {
40-
super(REDIS_CONTAINER_NAMES);
42+
super(REDIS_IMAGE_NAMES);
4143
}
4244

4345
@Override

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/dev-services.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The following service connections are currently supported:
123123
| Containers named "rabbitmq" or "bitnami/rabbitmq"
124124

125125
| `RedisConnectionDetails`
126-
| Containers named "redis" or "bitnami/redis"
126+
| Containers named "redis", "bitnami/redis", "redis/redis-stack" or "redis/redis-stack-server"
127127

128128
| `ZipkinConnectionDetails`
129129
| Containers named "openzipkin/zipkin".

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ The following service connection factories are provided in the `spring-boot-test
8787
| Containers of type `RabbitMQContainer`
8888

8989
| `RedisConnectionDetails`
90-
| Containers named "redis"
90+
| Containers named "redis", "bitnami/redis", "redis/redis-stack" or "redis/redis-stack-server"
9191

9292
| `ZipkinConnectionDetails`
9393
| Containers named "openzipkin/zipkin"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection.redis;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.boot.testsupport.container.TestImage;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.data.redis.connection.RedisConnection;
32+
import org.springframework.data.redis.connection.RedisConnectionFactory;
33+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* Tests for {@link RedisContainerConnectionDetailsFactory}.
39+
*
40+
* @author Andy Wilkinson
41+
* @author Eddú Meléndez
42+
*/
43+
@SpringJUnitConfig
44+
@Testcontainers(disabledWithoutDocker = true)
45+
class BitnamiRedisContainerConnectionDetailsFactoryTests {
46+
47+
@Container
48+
@ServiceConnection
49+
static final GenericContainer<?> redis = TestImage.BITNAMI_REDIS.genericContainer()
50+
.withExposedPorts(6379)
51+
.withEnv("ALLOW_EMPTY_PASSWORD", "yes");
52+
53+
@Autowired(required = false)
54+
private RedisConnectionDetails connectionDetails;
55+
56+
@Autowired
57+
private RedisConnectionFactory connectionFactory;
58+
59+
@Test
60+
void connectionCanBeMadeToRedisContainer() {
61+
assertThat(this.connectionDetails).isNotNull();
62+
try (RedisConnection connection = this.connectionFactory.getConnection()) {
63+
assertThat(connection.commands().echo("Hello, World".getBytes())).isEqualTo("Hello, World".getBytes());
64+
}
65+
}
66+
67+
@Configuration(proxyBeanMethods = false)
68+
@ImportAutoConfiguration(RedisAutoConfiguration.class)
69+
static class TestConfiguration {
70+
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection.redis;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.boot.testsupport.container.TestImage;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.data.redis.connection.RedisConnection;
32+
import org.springframework.data.redis.connection.RedisConnectionFactory;
33+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* Tests for {@link RedisContainerConnectionDetailsFactory}.
39+
*
40+
* @author Andy Wilkinson
41+
* @author Eddú Meléndez
42+
*/
43+
@SpringJUnitConfig
44+
@Testcontainers(disabledWithoutDocker = true)
45+
class RedisStackContainerConnectionDetailsFactoryTests {
46+
47+
@Container
48+
@ServiceConnection
49+
static final GenericContainer<?> redis = TestImage.REDIS_STACK.genericContainer().withExposedPorts(6379);
50+
51+
@Autowired(required = false)
52+
private RedisConnectionDetails connectionDetails;
53+
54+
@Autowired
55+
private RedisConnectionFactory connectionFactory;
56+
57+
@Test
58+
void connectionCanBeMadeToRedisContainer() {
59+
assertThat(this.connectionDetails).isNotNull();
60+
try (RedisConnection connection = this.connectionFactory.getConnection()) {
61+
assertThat(connection.commands().echo("Hello, World".getBytes())).isEqualTo("Hello, World".getBytes());
62+
}
63+
}
64+
65+
@Configuration(proxyBeanMethods = false)
66+
@ImportAutoConfiguration(RedisAutoConfiguration.class)
67+
static class TestConfiguration {
68+
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection.redis;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.boot.testsupport.container.TestImage;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.data.redis.connection.RedisConnection;
32+
import org.springframework.data.redis.connection.RedisConnectionFactory;
33+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* Tests for {@link RedisContainerConnectionDetailsFactory}.
39+
*
40+
* @author Andy Wilkinson
41+
* @author Eddú Meléndez
42+
*/
43+
@SpringJUnitConfig
44+
@Testcontainers(disabledWithoutDocker = true)
45+
class RedisStackServerContainerConnectionDetailsFactoryTests {
46+
47+
@Container
48+
@ServiceConnection
49+
static final GenericContainer<?> redis = TestImage.REDIS_STACK_SERVER.genericContainer().withExposedPorts(6379);
50+
51+
@Autowired(required = false)
52+
private RedisConnectionDetails connectionDetails;
53+
54+
@Autowired
55+
private RedisConnectionFactory connectionFactory;
56+
57+
@Test
58+
void connectionCanBeMadeToRedisContainer() {
59+
assertThat(this.connectionDetails).isNotNull();
60+
try (RedisConnection connection = this.connectionFactory.getConnection()) {
61+
assertThat(connection.commands().echo("Hello, World".getBytes())).isEqualTo("Hello, World".getBytes());
62+
}
63+
}
64+
65+
@Configuration(proxyBeanMethods = false)
66+
@ImportAutoConfiguration(RedisAutoConfiguration.class)
67+
static class TestConfiguration {
68+
69+
}
70+
71+
}

spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactory.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.testcontainers.service.connection.redis;
1818

19+
import java.util.List;
20+
1921
import org.testcontainers.containers.Container;
2022
import org.testcontainers.containers.GenericContainer;
2123

@@ -32,12 +34,18 @@
3234
* @author Moritz Halbritter
3335
* @author Andy Wilkinson
3436
* @author Phillip Webb
37+
* @author Eddú Meléndez
3538
*/
3639
class RedisContainerConnectionDetailsFactory
3740
extends ContainerConnectionDetailsFactory<Container<?>, RedisConnectionDetails> {
3841

42+
private static final List<String> REDIS_IMAGE_NAMES = List.of("redis", "bitnami/redis", "redis/redis-stack",
43+
"redis/redis-stack-server");
44+
45+
private static final int REDIS_PORT = 6379;
46+
3947
RedisContainerConnectionDetailsFactory() {
40-
super("redis");
48+
super(REDIS_IMAGE_NAMES);
4149
}
4250

4351
@Override
@@ -57,7 +65,7 @@ private RedisContainerConnectionDetails(ContainerConnectionSource<Container<?>>
5765

5866
@Override
5967
public Standalone getStandalone() {
60-
return Standalone.of(getContainer().getHost(), getContainer().getFirstMappedPort());
68+
return Standalone.of(getContainer().getHost(), getContainer().getMappedPort(REDIS_PORT));
6169
}
6270

6371
}

spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ public enum TestImage {
183183
(container) -> ((RedisContainer) container).withStartupAttempts(5)
184184
.withStartupTimeout(Duration.ofMinutes(10))),
185185

186+
/**
187+
* A container image suitable for testing Redis Stack.
188+
*/
189+
REDIS_STACK("redis/redis-stack", "7.2.0-v11"),
190+
191+
/**
192+
* A container image suitable for testing Redis Stack Server.
193+
*/
194+
REDIS_STACK_SERVER("redis/redis-stack-server", "7.2.0-v11"),
195+
186196
/**
187197
* A container image suitable for testing Redpanda.
188198
*/

0 commit comments

Comments
 (0)