diff --git a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactoryIntegrationTests.java b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactoryIntegrationTests.java index 7c51914f5b..8b6b3d46f5 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactoryIntegrationTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactoryIntegrationTests.java @@ -30,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Andy Wilkinson * @author Phillip Webb * @author Scott Frederick + * @author Eddú Meléndez */ class RedisDockerComposeConnectionDetailsFactoryIntegrationTests { @@ -43,6 +44,16 @@ class RedisDockerComposeConnectionDetailsFactoryIntegrationTests { assertConnectionDetails(connectionDetails); } + @DockerComposeTest(composeFile = "redis-compose.yaml", image = TestImage.REDIS_STACK) + void runWithRedisStackCreatesConnectionDetails(RedisConnectionDetails connectionDetails) { + assertConnectionDetails(connectionDetails); + } + + @DockerComposeTest(composeFile = "redis-compose.yaml", image = TestImage.REDIS_STACK_SERVER) + void runWithRedisStackServerCreatesConnectionDetails(RedisConnectionDetails connectionDetails) { + assertConnectionDetails(connectionDetails); + } + private void assertConnectionDetails(RedisConnectionDetails connectionDetails) { assertThat(connectionDetails.getUsername()).isNull(); assertThat(connectionDetails.getPassword()).isNull(); diff --git a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactory.java b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactory.java index c41ba25ccb..5a15429d2f 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactory.java +++ b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/redis/RedisDockerComposeConnectionDetailsFactory.java @@ -29,10 +29,12 @@ import org.springframework.boot.docker.compose.service.connection.DockerComposeC * @author Andy Wilkinson * @author Phillip Webb * @author Scott Frederick + * @author Eddú Meléndez */ class RedisDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory { - private static final String[] REDIS_CONTAINER_NAMES = { "redis", "bitnami/redis" }; + private static final String[] REDIS_CONTAINER_NAMES = { "redis", "bitnami/redis", "redis/redis-stack", + "redis/redis-stack-server" }; private static final int REDIS_PORT = 6379; diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/dev-services.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/dev-services.adoc index a977998ca6..7190c0ee32 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/dev-services.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/dev-services.adoc @@ -123,7 +123,7 @@ The following service connections are currently supported: | Containers named "rabbitmq" or "bitnami/rabbitmq" | `RedisConnectionDetails` -| Containers named "redis" or "bitnami/redis" +| Containers named "redis", "bitnami/redis", "redis/redis-stack" or "redis/redis-stack-server" | `ZipkinConnectionDetails` | Containers named "openzipkin/zipkin". diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc index 3d2ff668e6..0ee1dcab5a 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc @@ -87,7 +87,7 @@ The following service connection factories are provided in the `spring-boot-test | Containers of type `RabbitMQContainer` | `RedisConnectionDetails` -| Containers named "redis" +| Containers named "redis", "redis/redis-stack" or "redis/redis-stack-server" | `ZipkinConnectionDetails` | Containers named "openzipkin/zipkin" diff --git a/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/redis/RedisStackContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/redis/RedisStackContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..03bcabb7e5 --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/redis/RedisStackContainerConnectionDetailsFactoryTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.testcontainers.service.connection.redis; + +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.container.RedisStackContainer; +import org.springframework.boot.testsupport.container.TestImage; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link RedisContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + * @author Eddú Meléndez + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class RedisStackContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final RedisStackContainer redis = TestImage.container(RedisStackContainer.class); + + @Autowired(required = false) + private RedisConnectionDetails connectionDetails; + + @Autowired + private RedisConnectionFactory connectionFactory; + + @Test + void connectionCanBeMadeToRedisContainer() { + assertThat(this.connectionDetails).isNotNull(); + try (RedisConnection connection = this.connectionFactory.getConnection()) { + assertThat(connection.commands().echo("Hello, World".getBytes())).isEqualTo("Hello, World".getBytes()); + } + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(RedisAutoConfiguration.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/redis/RedisStackServerContainerConnectionDetailsFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/redis/RedisStackServerContainerConnectionDetailsFactoryTests.java new file mode 100644 index 0000000000..a3ebf8d7d8 --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/redis/RedisStackServerContainerConnectionDetailsFactoryTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.testcontainers.service.connection.redis; + +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testsupport.container.RedisStackServerContainer; +import org.springframework.boot.testsupport.container.TestImage; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link RedisContainerConnectionDetailsFactory}. + * + * @author Andy Wilkinson + * @author Eddú Meléndez + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class RedisStackServerContainerConnectionDetailsFactoryTests { + + @Container + @ServiceConnection + static final RedisStackServerContainer redis = TestImage.container(RedisStackServerContainer.class); + + @Autowired(required = false) + private RedisConnectionDetails connectionDetails; + + @Autowired + private RedisConnectionFactory connectionFactory; + + @Test + void connectionCanBeMadeToRedisContainer() { + assertThat(this.connectionDetails).isNotNull(); + try (RedisConnection connection = this.connectionFactory.getConnection()) { + assertThat(connection.commands().echo("Hello, World".getBytes())).isEqualTo("Hello, World".getBytes()); + } + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(RedisAutoConfiguration.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactory.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactory.java index 4b13a527de..5d10886d69 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactory.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.boot.testcontainers.service.connection.redis; +import java.util.List; + import org.testcontainers.containers.Container; import org.testcontainers.containers.GenericContainer; @@ -32,12 +34,18 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnect * @author Moritz Halbritter * @author Andy Wilkinson * @author Phillip Webb + * @author Eddú Meléndez */ class RedisContainerConnectionDetailsFactory extends ContainerConnectionDetailsFactory, RedisConnectionDetails> { + private static final List REDIS_IMAGE_NAMES = List.of("redis", "bitnami/redis", "redis/redis-stack", + "redis/redis-stack-server"); + + private static final int REDIS_PORT = 6379; + RedisContainerConnectionDetailsFactory() { - super("redis"); + super(REDIS_IMAGE_NAMES); } @Override @@ -57,7 +65,7 @@ class RedisContainerConnectionDetailsFactory @Override public Standalone getStandalone() { - return Standalone.of(getContainer().getHost(), getContainer().getFirstMappedPort()); + return Standalone.of(getContainer().getHost(), getContainer().getMappedPort(REDIS_PORT)); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/RedisStackContainer.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/RedisStackContainer.java new file mode 100644 index 0000000000..47ba11cd58 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/RedisStackContainer.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.testsupport.container; + +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.utility.DockerImageName; + +/** + * A {@link GenericContainer} for Redis Stack. + * + * @author Andy Wilkinson + * @author Madhura Bhave + */ +public class RedisStackContainer extends GenericContainer { + + public RedisStackContainer(DockerImageName dockerImageName) { + super(dockerImageName); + addExposedPorts(6379); + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/RedisStackServerContainer.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/RedisStackServerContainer.java new file mode 100644 index 0000000000..fc891b47da --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/RedisStackServerContainer.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.testsupport.container; + +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.utility.DockerImageName; + +/** + * A {@link GenericContainer} for Redis Stack Server. + * + * @author Andy Wilkinson + * @author Madhura Bhave + */ +public class RedisStackServerContainer extends GenericContainer { + + public RedisStackServerContainer(DockerImageName dockerImageName) { + super(dockerImageName); + addExposedPorts(6379); + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java index be9caa6645..0c3c024a02 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support-docker/src/main/java/org/springframework/boot/testsupport/container/TestImage.java @@ -183,6 +183,20 @@ public enum TestImage { (container) -> ((RedisContainer) container).withStartupAttempts(5) .withStartupTimeout(Duration.ofMinutes(10))), + /** + * A container image suitable for testing Redis Stack. + */ + REDIS_STACK("redis/redis-stack", "7.2.0-v11", () -> RedisStackContainer.class, + (container) -> ((RedisStackContainer) container).withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(10))), + + /** + * A container image suitable for testing Redis Stack Server. + */ + REDIS_STACK_SERVER("redis/redis-stack-server", "7.2.0-v11", () -> RedisStackServerContainer.class, + (container) -> ((RedisStackServerContainer) container).withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(10))), + /** * A container image suitable for testing Redpanda. */ @@ -271,6 +285,10 @@ public enum TestImage { this(name, tag, containerClass, null); } + TestImage(String name, String tag, Consumer containerSetup) { + this(name, tag, null, containerSetup); + } + TestImage(String name, String tag, Supplier> containerClass, Consumer containerSetup) { this.name = name; this.tag = tag;