Skip to content

Commit d50c434

Browse files
committed
Merge pull request #41327 from eddumelendez
* gh-41327: Polish "Support service connections for redis-stack and redis-stack-server" Support service connections for redis-stack and redis-stack-server Closes gh-41327
2 parents 2216b48 + 46ec3e3 commit d50c434

File tree

10 files changed

+257
-6
lines changed

10 files changed

+257
-6
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
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_CONTAINER_NAMES = { "redis", "bitnami/redis", "redis/redis-stack",
37+
"redis/redis-stack-server" };
3638

3739
private static final int REDIS_PORT = 6379;
3840

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.junit.jupiter.Container;
21+
import org.testcontainers.junit.jupiter.Testcontainers;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
27+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
28+
import org.springframework.boot.testsupport.container.RedisStackContainer;
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 RedisStackContainer redis = TestImage.container(RedisStackContainer.class);
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.junit.jupiter.Container;
21+
import org.testcontainers.junit.jupiter.Testcontainers;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
27+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
28+
import org.springframework.boot.testsupport.container.RedisStackServerContainer;
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 RedisStackServerContainer redis = TestImage.container(RedisStackServerContainer.class);
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.testsupport.container;
18+
19+
import org.testcontainers.containers.GenericContainer;
20+
import org.testcontainers.utility.DockerImageName;
21+
22+
/**
23+
* A {@link GenericContainer} for Redis Stack.
24+
*
25+
* @author Andy Wilkinson
26+
* @author Madhura Bhave
27+
*/
28+
public class RedisStackContainer extends GenericContainer<RedisStackContainer> {
29+
30+
public RedisStackContainer(DockerImageName dockerImageName) {
31+
super(dockerImageName);
32+
addExposedPorts(6379);
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.testsupport.container;
18+
19+
import org.testcontainers.containers.GenericContainer;
20+
import org.testcontainers.utility.DockerImageName;
21+
22+
/**
23+
* A {@link GenericContainer} for Redis Stack Server.
24+
*
25+
* @author Andy Wilkinson
26+
* @author Madhura Bhave
27+
*/
28+
public class RedisStackServerContainer extends GenericContainer<RedisStackServerContainer> {
29+
30+
public RedisStackServerContainer(DockerImageName dockerImageName) {
31+
super(dockerImageName);
32+
addExposedPorts(6379);
33+
}
34+
35+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ 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", () -> RedisStackContainer.class,
190+
(container) -> ((RedisStackContainer) container).withStartupAttempts(5)
191+
.withStartupTimeout(Duration.ofMinutes(10))),
192+
193+
/**
194+
* A container image suitable for testing Redis Stack Server.
195+
*/
196+
REDIS_STACK_SERVER("redis/redis-stack-server", "7.2.0-v11", () -> RedisStackServerContainer.class,
197+
(container) -> ((RedisStackServerContainer) container).withStartupAttempts(5)
198+
.withStartupTimeout(Duration.ofMinutes(10))),
199+
186200
/**
187201
* A container image suitable for testing Redpanda.
188202
*/
@@ -271,6 +285,10 @@ public enum TestImage {
271285
this(name, tag, containerClass, null);
272286
}
273287

288+
TestImage(String name, String tag, Consumer<?> containerSetup) {
289+
this(name, tag, null, containerSetup);
290+
}
291+
274292
TestImage(String name, String tag, Supplier<Class<?>> containerClass, Consumer<?> containerSetup) {
275293
this.name = name;
276294
this.tag = tag;

0 commit comments

Comments
 (0)