Testcontainers: valkey images instead of redis
This commit is contained in:
		
							parent
							
								
									c9ef125911
								
							
						
					
					
						commit
						2867082776
					
				@ -16,6 +16,7 @@
 | 
			
		||||
package org.thingsboard.server.dao;
 | 
			
		||||
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.assertj.core.api.Assertions;
 | 
			
		||||
import org.junit.ClassRule;
 | 
			
		||||
import org.junit.rules.ExternalResource;
 | 
			
		||||
import org.testcontainers.containers.GenericContainer;
 | 
			
		||||
@ -23,27 +24,35 @@ import org.testcontainers.containers.Network;
 | 
			
		||||
import org.testcontainers.containers.output.OutputFrame;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class AbstractRedisClusterContainer {
 | 
			
		||||
 | 
			
		||||
    static final String nodes = "127.0.0.1:6371,127.0.0.1:6372,127.0.0.1:6373,127.0.0.1:6374,127.0.0.1:6375,127.0.0.1:6376";
 | 
			
		||||
 | 
			
		||||
    static Map<String,String> envs = Map.of(
 | 
			
		||||
            "VALKEY_CLUSTER_ANNOUNCE_IP", "127.0.0.1",
 | 
			
		||||
            "VALKEY_CLUSTER_DYNAMIC_IPS", "no",
 | 
			
		||||
            "ALLOW_EMPTY_PASSWORD", "yes",
 | 
			
		||||
            "VALKEY_NODES", nodes
 | 
			
		||||
            
 | 
			
		||||
    ); 
 | 
			
		||||
    
 | 
			
		||||
    @ClassRule(order = 0)
 | 
			
		||||
    public static Network network = Network.newNetwork();
 | 
			
		||||
    @ClassRule(order = 1)
 | 
			
		||||
    public static GenericContainer redis1 = new GenericContainer("bitnami/redis-cluster:latest").withEnv("REDIS_PORT_NUMBER", "6371").withNetworkMode("host").withLogConsumer(x -> log.warn("{}", ((OutputFrame) x).getUtf8StringWithoutLineEnding())).withEnv("ALLOW_EMPTY_PASSWORD", "yes").withEnv("REDIS_NODES", nodes);
 | 
			
		||||
    public static GenericContainer redis1 = new GenericContainer("bitnami/valkey-cluster:8.0").withEnv(envs).withEnv("VALKEY_PORT_NUMBER", "6371").withNetworkMode("host").withLogConsumer(AbstractRedisClusterContainer::consumeLog);
 | 
			
		||||
    @ClassRule(order = 2)
 | 
			
		||||
    public static GenericContainer redis2 = new GenericContainer("bitnami/redis-cluster:latest").withEnv("REDIS_PORT_NUMBER", "6372").withNetworkMode("host").withLogConsumer(x -> log.warn("{}", ((OutputFrame) x).getUtf8StringWithoutLineEnding())).withEnv("ALLOW_EMPTY_PASSWORD", "yes").withEnv("REDIS_NODES", nodes);
 | 
			
		||||
    public static GenericContainer redis2 = new GenericContainer("bitnami/valkey-cluster:8.0").withEnv(envs).withEnv("VALKEY_PORT_NUMBER", "6372").withNetworkMode("host").withLogConsumer(AbstractRedisClusterContainer::consumeLog);
 | 
			
		||||
    @ClassRule(order = 3)
 | 
			
		||||
    public static GenericContainer redis3 = new GenericContainer("bitnami/redis-cluster:latest").withEnv("REDIS_PORT_NUMBER", "6373").withNetworkMode("host").withLogConsumer(x -> log.warn("{}", ((OutputFrame) x).getUtf8StringWithoutLineEnding())).withEnv("ALLOW_EMPTY_PASSWORD", "yes").withEnv("REDIS_NODES", nodes);
 | 
			
		||||
    public static GenericContainer redis3 = new GenericContainer("bitnami/valkey-cluster:8.0").withEnv(envs).withEnv("VALKEY_PORT_NUMBER", "6373").withNetworkMode("host").withLogConsumer(AbstractRedisClusterContainer::consumeLog);
 | 
			
		||||
    @ClassRule(order = 4)
 | 
			
		||||
    public static GenericContainer redis4 = new GenericContainer("bitnami/redis-cluster:latest").withEnv("REDIS_PORT_NUMBER", "6374").withNetworkMode("host").withLogConsumer(x -> log.warn("{}", ((OutputFrame) x).getUtf8StringWithoutLineEnding())).withEnv("ALLOW_EMPTY_PASSWORD", "yes").withEnv("REDIS_NODES", nodes);
 | 
			
		||||
    public static GenericContainer redis4 = new GenericContainer("bitnami/valkey-cluster:8.0").withEnv(envs).withEnv("VALKEY_PORT_NUMBER", "6374").withNetworkMode("host").withLogConsumer(AbstractRedisClusterContainer::consumeLog);
 | 
			
		||||
    @ClassRule(order = 5)
 | 
			
		||||
    public static GenericContainer redis5 = new GenericContainer("bitnami/redis-cluster:latest").withEnv("REDIS_PORT_NUMBER", "6375").withNetworkMode("host").withLogConsumer(x -> log.warn("{}", ((OutputFrame) x).getUtf8StringWithoutLineEnding())).withEnv("ALLOW_EMPTY_PASSWORD", "yes").withEnv("REDIS_NODES", nodes);
 | 
			
		||||
    public static GenericContainer redis5 = new GenericContainer("bitnami/valkey-cluster:8.0").withEnv(envs).withEnv("VALKEY_PORT_NUMBER", "6375").withNetworkMode("host").withLogConsumer(AbstractRedisClusterContainer::consumeLog);
 | 
			
		||||
    @ClassRule(order = 6)
 | 
			
		||||
    public static GenericContainer redis6 = new GenericContainer("bitnami/redis-cluster:latest").withEnv("REDIS_PORT_NUMBER", "6376").withNetworkMode("host").withLogConsumer(x -> log.warn("{}", ((OutputFrame) x).getUtf8StringWithoutLineEnding())).withEnv("ALLOW_EMPTY_PASSWORD", "yes").withEnv("REDIS_NODES", nodes);
 | 
			
		||||
    public static GenericContainer redis6 = new GenericContainer("bitnami/valkey-cluster:8.0").withEnv(envs).withEnv("VALKEY_PORT_NUMBER", "6376").withNetworkMode("host").withLogConsumer(AbstractRedisClusterContainer::consumeLog);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @ClassRule(order = 100)
 | 
			
		||||
@ -59,12 +68,16 @@ public class AbstractRedisClusterContainer {
 | 
			
		||||
 | 
			
		||||
            Thread.sleep(TimeUnit.SECONDS.toMillis(5)); // otherwise not all containers have time to start
 | 
			
		||||
 | 
			
		||||
            String clusterCreateCommand = "echo yes | redis-cli --cluster create " +
 | 
			
		||||
                    "127.0.0.1:6371 127.0.0.1:6372 127.0.0.1:6373 127.0.0.1:6374 127.0.0.1:6375 127.0.0.1:6376 " +
 | 
			
		||||
                    "--cluster-replicas 1";
 | 
			
		||||
            log.warn("Command to init Redis Cluster: {}", clusterCreateCommand);
 | 
			
		||||
            var result = redis6.execInContainer("/bin/sh", "-c", clusterCreateCommand);
 | 
			
		||||
            redis6.execInContainer("/bin/sh", "-c", "printenv | grep VALKEY"); //"sleep infinity"
 | 
			
		||||
 | 
			
		||||
            String clusterCreateCommand = "" +
 | 
			
		||||
                    "valkey-cli --cluster create " +nodes.replace(","," ") + " --cluster-replicas 1 --cluster-yes" +
 | 
			
		||||
                    "";
 | 
			
		||||
            log.warn("Command to init ValKey Cluster: {}", clusterCreateCommand);
 | 
			
		||||
            var result = redis6.execInContainer("/bin/sh", "-c", clusterCreateCommand); //"sleep infinity"
 | 
			
		||||
//            result.wait(TimeUnit.SECONDS.toMillis(300));
 | 
			
		||||
            log.warn("Init cluster result: {}", result);
 | 
			
		||||
            Assertions.assertThat(result.getExitCode()).isEqualTo(0);
 | 
			
		||||
 | 
			
		||||
            Thread.sleep(TimeUnit.SECONDS.toMillis(5)); // otherwise cluster not always ready
 | 
			
		||||
 | 
			
		||||
@ -83,9 +96,12 @@ public class AbstractRedisClusterContainer {
 | 
			
		||||
            redis4.stop();
 | 
			
		||||
            redis5.stop();
 | 
			
		||||
            redis6.stop();
 | 
			
		||||
            List.of("cache.type", "redis.connection.type", "redis.cluster.nodes", "redis.cluster.useDefaultPoolConfig\"")
 | 
			
		||||
            List.of("cache.type", "redis.connection.type", "redis.cluster.nodes", "redis.cluster.useDefaultPoolConfig")
 | 
			
		||||
                    .forEach(System.getProperties()::remove);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    private static void consumeLog(Object x) {
 | 
			
		||||
        log.warn("{}", ((OutputFrame) x).getUtf8StringWithoutLineEnding());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.junit.ClassRule;
 | 
			
		||||
import org.junit.rules.ExternalResource;
 | 
			
		||||
import org.testcontainers.containers.GenericContainer;
 | 
			
		||||
import org.testcontainers.containers.output.OutputFrame;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@ -26,7 +27,9 @@ import java.util.List;
 | 
			
		||||
public class AbstractRedisContainer {
 | 
			
		||||
 | 
			
		||||
    @ClassRule(order = 0)
 | 
			
		||||
    public static GenericContainer redis = new GenericContainer("redis:7.2")
 | 
			
		||||
    public static GenericContainer redis = new GenericContainer("bitnami/valkey:8.0")
 | 
			
		||||
            .withEnv("ALLOW_EMPTY_PASSWORD","yes")
 | 
			
		||||
            .withLogConsumer(s -> log.warn(((OutputFrame) s).getUtf8String().trim()))
 | 
			
		||||
            .withExposedPorts(6379);
 | 
			
		||||
 | 
			
		||||
    @ClassRule(order = 1)
 | 
			
		||||
 | 
			
		||||
@ -33,7 +33,8 @@ import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		||||
public class RedisJUnit5Test {
 | 
			
		||||
 | 
			
		||||
    @Container
 | 
			
		||||
    private static final GenericContainer REDIS = new GenericContainer("redis:7.2-bookworm")
 | 
			
		||||
    private static final GenericContainer REDIS = new GenericContainer("bitnami/valkey:8.0")
 | 
			
		||||
            .withEnv("ALLOW_EMPTY_PASSWORD","yes")
 | 
			
		||||
            .withLogConsumer(s -> log.error(((OutputFrame) s).getUtf8String().trim()))
 | 
			
		||||
            .withExposedPorts(6379);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user