如何将SSL添加到由testcontainers创建的Redis Docker

时间:2019-12-31 06:38:56

标签: java redis amazon-elasticache testcontainers

我正在尝试为下面的代码创建集成测试:

public int insertIntoCache() {

        String key = "test1";
        String value = "{test1,test2}";
        int count = 0; 

        int port = 6379;
        String host = "localhost";

        try (Jedis jd = new Jedis(host,port,true)){

            jd.flushDB();

            System.out.println("flushed");
            if(jd.ping().equalsIgnoreCase("pong")) {

                jd.set(key,value);

                count++;
            }


        }catch(JedisException je) {
            je.printStackTrace();
            throw new RuntimeException("exception",je);
        }

        return count;


    }

我正在使用testcontainers的FixedHostPortGenericContainer创建Redis映像并运行测试用例

public class TestRedis {

    private RedisCache rd;

    @Rule
    public FixedHostPortGenericContainer redis = new FixedHostPortGenericContainer("redis:3.0.6").withFixedExposedPort(6379, 6379);

    @Test
    public void testFindingAnInsertedValue() {
        redis.addEnv("xpack.security.enabled", "true");
        rd = new RedisCache();
        int t = rd.insertIntoCache();
        System.out.println(t);
    }
}

但是我收到以下错误

Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
    at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:750)
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
    at redis.clients.jedis.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:52)
    at redis.clients.jedis.util.RedisOutputStream.flush(RedisOutputStream.java:133)
    at redis.clients.jedis.Connection.flush(Connection.java:305)
    ... 29 more

但是当我使Jedis jd = new Jedis(host,port,false)时,我可以运行测试用例。

请帮助我找到一种为testcontainer lib启用ssl的方法。

0 个答案:

没有答案
豫ICP备18024241号-1