Skip to content

Commit 8acb5f9

Browse files
committed
Add service connection from redis/redis-stack and redis/redis-stack-server
* Add new images to `RedisDockerComposeConnectionDetailsFactory` * Support more than one connection name in `ContainerConnectionDetailsFactory` * Add new images to `RedisContainerConnectionDetailsFactory`
1 parent 2a20ceb commit 8acb5f9

File tree

9 files changed

+187
-10
lines changed

9 files changed

+187
-10
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", "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,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/ContainerConnectionDetailsFactory.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Moritz Halbritter
5252
* @author Andy Wilkinson
5353
* @author Phillip Webb
54+
* @author Eddú Meléndez
5455
* @since 3.1.0
5556
*/
5657
public abstract class ContainerConnectionDetailsFactory<C extends Container<?>, D extends ConnectionDetails>
@@ -61,7 +62,7 @@ public abstract class ContainerConnectionDetailsFactory<C extends Container<?>,
6162
*/
6263
protected static final String ANY_CONNECTION_NAME = null;
6364

64-
private final String connectionName;
65+
private final String[] connectionNames;
6566

6667
private final String[] requiredClassNames;
6768

@@ -80,7 +81,12 @@ protected ContainerConnectionDetailsFactory() {
8081
* @param requiredClassNames the names of classes that must be present
8182
*/
8283
protected ContainerConnectionDetailsFactory(String connectionName, String... requiredClassNames) {
83-
this.connectionName = connectionName;
84+
this.connectionNames = new String[] { connectionName };
85+
this.requiredClassNames = requiredClassNames;
86+
}
87+
88+
protected ContainerConnectionDetailsFactory(String[] connectionNames, String... requiredClassNames) {
89+
this.connectionNames = connectionNames;
8490
this.requiredClassNames = requiredClassNames;
8591
}
8692

@@ -93,8 +99,10 @@ public final D getConnectionDetails(ContainerConnectionSource<C> source) {
9399
Class<?>[] generics = resolveGenerics();
94100
Class<?> containerType = generics[0];
95101
Class<?> connectionDetailsType = generics[1];
96-
if (source.accepts(this.connectionName, containerType, connectionDetailsType)) {
97-
return getContainerConnectionDetails(source);
102+
for (String connectionName : this.connectionNames) {
103+
if (source.accepts(connectionName, containerType, connectionDetailsType)) {
104+
return getContainerConnectionDetails(source);
105+
}
98106
}
99107
}
100108
catch (NoClassDefFoundError ex) {

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

Lines changed: 6 additions & 2 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.
@@ -32,12 +32,16 @@
3232
* @author Moritz Halbritter
3333
* @author Andy Wilkinson
3434
* @author Phillip Webb
35+
* @author Eddú Meléndez
3536
*/
3637
class RedisContainerConnectionDetailsFactory
3738
extends ContainerConnectionDetailsFactory<Container<?>, RedisConnectionDetails> {
3839

40+
private static final String[] REDIS_IMAGE_NAMES = { "redis", "bitnami/redis", "redis/redis-stack",
41+
"redis/redis-stack-server" };
42+
3943
RedisContainerConnectionDetailsFactory() {
40-
super("redis");
44+
super(REDIS_IMAGE_NAMES);
4145
}
4246

4347
@Override

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
@@ -173,6 +173,16 @@ public enum TestImage {
173173
(container) -> ((RedisContainer) container).withStartupAttempts(5)
174174
.withStartupTimeout(Duration.ofMinutes(10))),
175175

176+
/**
177+
* A container image suitable for testing Redis Stack.
178+
*/
179+
REDIS_STACK("redis/redis-stack", "7.2.0-v11"),
180+
181+
/**
182+
* A container image suitable for testing Redis Stack Server.
183+
*/
184+
REDIS_STACK_SERVER("redis/redis-stack-server"),
185+
176186
/**
177187
* A container image suitable for testing Redpanda.
178188
*/

0 commit comments

Comments
 (0)