Skip to content

Support service connections for redis/redis-stack and redis/redis-stack-server #41327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author Eddú Meléndez
*/
class RedisDockerComposeConnectionDetailsFactoryIntegrationTests {

Expand All @@ -43,6 +44,16 @@ void runWithBitnamiImageCreatesConnectionDetails(RedisConnectionDetails connecti
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author Eddú Meléndez
*/
class RedisDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<RedisConnectionDetails> {

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

private static final int REDIS_PORT = 6379;

RedisDockerComposeConnectionDetailsFactory() {
super(REDIS_CONTAINER_NAMES);
super(REDIS_IMAGE_NAMES);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", "bitnami/redis", "redis/redis-stack" or "redis/redis-stack-server"

| `ZipkinConnectionDetails`
| Containers named "openzipkin/zipkin"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.containers.GenericContainer;
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.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 BitnamiRedisContainerConnectionDetailsFactoryTests {

@Container
@ServiceConnection
static final GenericContainer<?> redis = TestImage.BITNAMI_REDIS.genericContainer()
.withExposedPorts(6379)
.withEnv("ALLOW_EMPTY_PASSWORD", "yes");

@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 {

}

}
Original file line number Diff line number Diff line change
@@ -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.containers.GenericContainer;
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.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 GenericContainer<?> redis = TestImage.REDIS_STACK.genericContainer().withExposedPorts(6379);

@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 {

}

}
Original file line number Diff line number Diff line change
@@ -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.containers.GenericContainer;
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.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 GenericContainer<?> redis = TestImage.REDIS_STACK_SERVER.genericContainer().withExposedPorts(6379);

@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 {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Eddú Meléndez
* @since 3.1.0
*/
public abstract class ContainerConnectionDetailsFactory<C extends Container<?>, D extends ConnectionDetails>
Expand All @@ -61,7 +62,7 @@ public abstract class ContainerConnectionDetailsFactory<C extends Container<?>,
*/
protected static final String ANY_CONNECTION_NAME = null;

private final String connectionName;
private final String[] connectionNames;

private final String[] requiredClassNames;

Expand All @@ -80,7 +81,12 @@ protected ContainerConnectionDetailsFactory() {
* @param requiredClassNames the names of classes that must be present
*/
protected ContainerConnectionDetailsFactory(String connectionName, String... requiredClassNames) {
this.connectionName = connectionName;
this.connectionNames = new String[] { connectionName };
this.requiredClassNames = requiredClassNames;
}

protected ContainerConnectionDetailsFactory(String[] connectionNames, String... requiredClassNames) {
this.connectionNames = connectionNames;
this.requiredClassNames = requiredClassNames;
}

Expand All @@ -93,8 +99,10 @@ public final D getConnectionDetails(ContainerConnectionSource<C> source) {
Class<?>[] generics = resolveGenerics();
Class<?> containerType = generics[0];
Class<?> connectionDetailsType = generics[1];
if (source.accepts(this.connectionName, containerType, connectionDetailsType)) {
return getContainerConnectionDetails(source);
for (String connectionName : this.connectionNames) {
if (source.accepts(connectionName, containerType, connectionDetailsType)) {
return getContainerConnectionDetails(source);
}
}
}
catch (NoClassDefFoundError ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -32,12 +32,18 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Eddú Meléndez
*/
class RedisContainerConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<Container<?>, RedisConnectionDetails> {

private static final String[] REDIS_IMAGE_NAMES = { "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
Expand All @@ -57,7 +63,7 @@ private RedisContainerConnectionDetails(ContainerConnectionSource<Container<?>>

@Override
public Standalone getStandalone() {
return Standalone.of(getContainer().getHost(), getContainer().getFirstMappedPort());
return Standalone.of(getContainer().getHost(), getContainer().getMappedPort(REDIS_PORT));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ 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"),

/**
* A container image suitable for testing Redis Stack Server.
*/
REDIS_STACK_SERVER("redis/redis-stack-server", "7.2.0-v11"),

/**
* A container image suitable for testing Redpanda.
*/
Expand Down
Loading