diff --git a/src/test/java/redis/clients/jedis/ACLJedisPoolTest.java b/src/test/java/redis/clients/jedis/ACLJedisPoolTest.java index 36474e12d5..5d7579b367 100644 --- a/src/test/java/redis/clients/jedis/ACLJedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/ACLJedisPoolTest.java @@ -21,19 +21,21 @@ * This test is only executed when the server/cluster is Redis 6. or more. */ public class ACLJedisPoolTest { - private static final HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + private static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0-acl"); + + private static final EndpointConfig endpointWithDefaultUser = HostAndPorts.getRedisEndpoint("standalone0"); @BeforeClass public static void prepare() throws Exception { // Use to check if the ACL test should be ran. ACL are available only in 6.0 and later org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis", - RedisVersionUtil.checkRedisMajorVersionNumber(6)); + RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint)); } @Test public void checkConnections() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), "acljedis", - "fizzbuzz"); + JedisPool pool = new JedisPool(new JedisPoolConfig(), endpoint.getHost(), endpoint.getPort(), + endpoint.getUsername(), endpoint.getPassword()); try (Jedis jedis = pool.getResource()) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); @@ -44,7 +46,8 @@ public void checkConnections() { @Test public void checkCloseableConnections() throws Exception { - JedisPool pool = new JedisPool(hnp.getHost(), hnp.getPort(), "acljedis", "fizzbuzz"); + JedisPool pool = new JedisPool(endpoint.getHost(), endpoint.getPort(), endpoint.getUsername(), + endpoint.getPassword()); try (Jedis jedis = pool.getResource()) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); @@ -58,9 +61,9 @@ public void checkResourceIsClosableAndReusable() { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - try (JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), - Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, 0 /* infinite */, "acljedis", - "fizzbuzz", Protocol.DEFAULT_DATABASE, "closable-reusable-pool", false, null, null, null)) { + try (JedisPool pool = new JedisPool(config, endpoint.getHost(), endpoint.getPort(), + Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, 0 /* infinite */, endpoint.getUsername(), + endpoint.getPassword(), Protocol.DEFAULT_DATABASE, "closable-reusable-pool", false, null, null, null)) { Jedis jedis = pool.getResource(); jedis.set("hello", "jedis"); @@ -78,8 +81,8 @@ public void checkResourceWithConfigIsClosableAndReusable() { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - try (JedisPool pool = new JedisPool(config, hnp, DefaultJedisClientConfig.builder() - .user("acljedis").password("fizzbuzz").clientName("closable-reusable-pool") + try (JedisPool pool = new JedisPool(config, endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().clientName("closable-reusable-pool") .build())) { Jedis jedis = pool.getResource(); @@ -96,9 +99,9 @@ public void checkResourceWithConfigIsClosableAndReusable() { @Test public void checkPoolRepairedWhenJedisIsBroken() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), - Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, 0 /* infinite */, "acljedis", - "fizzbuzz", Protocol.DEFAULT_DATABASE, "repairable-pool"); + JedisPool pool = new JedisPool(new JedisPoolConfig(), endpoint.getHost(), endpoint.getPort(), + Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, 0 /* infinite */, endpoint.getUsername(), + endpoint.getPassword(), Protocol.DEFAULT_DATABASE, "repairable-pool"); try (Jedis jedis = pool.getResource()) { jedis.set("foo", "0"); jedis.disconnect(); @@ -116,12 +119,12 @@ public void checkPoolOverflow() { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - try (JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort()); + try (JedisPool pool = new JedisPool(config, endpoint.getHost(), endpoint.getPort()); Jedis jedis = pool.getResource()) { - jedis.auth("acljedis", "fizzbuzz"); + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); try (Jedis jedis2 = pool.getResource()) { - jedis2.auth("acljedis", "fizzbuzz"); + jedis2.auth(endpoint.getUsername(), endpoint.getPassword()); } } } @@ -130,8 +133,8 @@ public void checkPoolOverflow() { public void securePool() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "acljedis", - "fizzbuzz"); + JedisPool pool = new JedisPool(config, endpoint.getHost(), endpoint.getPort(), 2000, endpoint.getUsername(), + endpoint.getPassword()); try (Jedis jedis = pool.getResource()) { jedis.set("foo", "bar"); } @@ -143,8 +146,8 @@ public void securePool() { public void securePoolNonSSL() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "acljedis", - "fizzbuzz", false); + JedisPool pool = new JedisPool(config, endpoint.getHost(), endpoint.getPort(), 2000, endpoint.getUsername(), + endpoint.getPassword(), false); try (Jedis jedis = pool.getResource()) { jedis.set("foo", "bar"); } @@ -154,27 +157,28 @@ public void securePoolNonSSL() { @Test public void nonDefaultDatabase() { - try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "acljedis", "fizzbuzz"); Jedis jedis0 = pool0.getResource()) { + try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), endpoint.getHost(), endpoint.getPort(), 2000, + endpoint.getUsername(), endpoint.getPassword()); Jedis jedis0 = pool0.getResource()) { jedis0.set("foo", "bar"); assertEquals("bar", jedis0.get("foo")); } - try (JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "acljedis", "fizzbuzz", 1); Jedis jedis1 = pool1.getResource()) { + try (JedisPool pool1 = new JedisPool(new JedisPoolConfig(), endpoint.getHost(), endpoint.getPort(), 2000, + endpoint.getUsername(), endpoint.getPassword(), 1); Jedis jedis1 = pool1.getResource()) { assertNull(jedis1.get("foo")); } } @Test public void startWithUrlString() { - try (Jedis j = new Jedis("localhost", 6379)) { - j.auth("acljedis", "fizzbuzz"); + try (Jedis j = new Jedis(endpoint.getHost(), endpoint.getPort())) { + j.auth(endpoint.getUsername(), endpoint.getPassword()); j.select(2); j.set("foo", "bar"); } - try (JedisPool pool = new JedisPool("redis://acljedis:fizzbuzz@localhost:6379/2"); + try (JedisPool pool = new JedisPool( + endpoint.getURIBuilder().defaultCredentials().path("/2").build()); Jedis jedis = pool.getResource()) { assertEquals("bar", jedis.get("foo")); } @@ -182,18 +186,20 @@ public void startWithUrlString() { @Test public void startWithUrl() throws URISyntaxException { - try (Jedis j = new Jedis("localhost", 6379)) { - j.auth("acljedis", "fizzbuzz"); + try (Jedis j = new Jedis(endpoint.getHost(), endpoint.getPort())) { + j.auth(endpoint.getUsername(), endpoint.getPassword()); j.select(2); j.set("foo", "bar"); } - try (JedisPool pool = new JedisPool(new URI("redis://acljedis:fizzbuzz@localhost:6379/2")); + try (JedisPool pool = new JedisPool( + endpoint.getURIBuilder().defaultCredentials().path("/2").build()); Jedis jedis = pool.getResource()) { assertEquals("bar", jedis.get("foo")); } - try (JedisPool pool = new JedisPool(new URI("redis://default:foobared@localhost:6379/2")); + try (JedisPool pool = new JedisPool( + endpointWithDefaultUser.getURIBuilder().defaultCredentials().path("/2").build()); Jedis jedis = pool.getResource()) { assertEquals("bar", jedis.get("foo")); } @@ -206,14 +212,14 @@ public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxExcept @Test public void allowUrlWithNoDBAndNoPassword() throws URISyntaxException { - new JedisPool("redis://localhost:6379").close(); - new JedisPool(new URI("redis://localhost:6379")).close(); + new JedisPool(endpoint.getURI().toString()).close(); + new JedisPool(endpoint.getURI()).close(); } @Test public void selectDatabaseOnActivation() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "acljedis", "fizzbuzz")) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpoint.getHost(), endpoint.getPort(), 2000, + endpoint.getUsername(), endpoint.getPassword())) { Jedis jedis0 = pool.getResource(); assertEquals(0, jedis0.getDB()); @@ -233,8 +239,8 @@ public void selectDatabaseOnActivation() { @Test public void customClientName() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "acljedis", "fizzbuzz", 0, "my_shiny_client_name"); Jedis jedis = pool.getResource()) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpoint.getHost(), endpoint.getPort(), 2000, + endpoint.getUsername(), endpoint.getPassword(), 0, "my_shiny_client_name"); Jedis jedis = pool.getResource()) { assertEquals("my_shiny_client_name", jedis.clientGetname()); } @@ -242,8 +248,8 @@ public void customClientName() { @Test public void customClientNameNoSSL() { - try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "acljedis", "fizzbuzz", 0, "my_shiny_client_name_no_ssl", false); + try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), endpoint.getHost(), endpoint.getPort(), 2000, + endpoint.getUsername(), endpoint.getPassword(), 0, "my_shiny_client_name_no_ssl", false); Jedis jedis = pool0.getResource()) { assertEquals("my_shiny_client_name_no_ssl", jedis.clientGetname()); @@ -254,8 +260,10 @@ public void customClientNameNoSSL() { public void testCloseConnectionOnMakeObject() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "acljedis", "foobared"); Jedis jedis = new Jedis("redis://:foobared@localhost:6379/")) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpoint.getHost(), + endpoint.getPort(), 2000, endpoint.getUsername(), "wrongpassword"); + Jedis jedis = new Jedis(endpointWithDefaultUser.getURIBuilder() + .credentials("", endpointWithDefaultUser.getPassword()).build())) { int currentClientCount = getClientCount(jedis.clientList()); try { pool.getResource(); diff --git a/src/test/java/redis/clients/jedis/ACLJedisSentinelPoolTest.java b/src/test/java/redis/clients/jedis/ACLJedisSentinelPoolTest.java index 75325c43be..b62809f0bb 100644 --- a/src/test/java/redis/clients/jedis/ACLJedisSentinelPoolTest.java +++ b/src/test/java/redis/clients/jedis/ACLJedisSentinelPoolTest.java @@ -24,15 +24,15 @@ public class ACLJedisSentinelPoolTest { private static final String MASTER_NAME = "aclmaster"; - //protected static HostAndPort master = HostAndPortUtil.getRedisServers().get(8); protected static HostAndPort sentinel1 = HostAndPorts.getSentinelServers().get(4); protected Set sentinels = new HashSet<>(); @BeforeClass public static void prepare() throws Exception { + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone2-primary"); org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis", - RedisVersionUtil.checkRedisMajorVersionNumber(6)); + RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint)); } @Before diff --git a/src/test/java/redis/clients/jedis/ACLJedisTest.java b/src/test/java/redis/clients/jedis/ACLJedisTest.java index a6a9deff77..d1ef40b95b 100644 --- a/src/test/java/redis/clients/jedis/ACLJedisTest.java +++ b/src/test/java/redis/clients/jedis/ACLJedisTest.java @@ -2,7 +2,6 @@ import static org.junit.Assert.assertEquals; -import java.net.URI; import java.net.URISyntaxException; import org.junit.BeforeClass; import org.junit.Test; @@ -20,6 +19,8 @@ @RunWith(Parameterized.class) public class ACLJedisTest extends JedisCommandsTestBase { + protected static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0-acl"); + /** * Use to check if the ACL test should be ran. ACL are available only in 6.0 and later * @throws Exception @@ -27,7 +28,7 @@ public class ACLJedisTest extends JedisCommandsTestBase { @BeforeClass public static void prepare() throws Exception { org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis", - RedisVersionUtil.checkRedisMajorVersionNumber(6)); + RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint)); } public ACLJedisTest(RedisProtocol redisProtocol) { @@ -37,39 +38,38 @@ public ACLJedisTest(RedisProtocol redisProtocol) { @Test public void useWithoutConnecting() { try (Jedis j = new Jedis()) { - assertEquals("OK", j.auth("acljedis", "fizzbuzz")); + assertEquals("OK", j.auth(endpoint.getUsername(), endpoint.getPassword())); j.dbSize(); } } @Test public void connectWithConfig() { - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().build())) { - jedis.auth("acljedis", "fizzbuzz"); + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), DefaultJedisClientConfig.builder().build())) { + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().user("acljedis") - .password("fizzbuzz").build())) { + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder().build())) { assertEquals("PONG", jedis.ping()); } } @Test public void connectWithConfigInterface() { - try (Jedis jedis = new Jedis(hnp, new JedisClientConfig() { + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), new JedisClientConfig() { })) { - jedis.auth("acljedis", "fizzbuzz"); + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } - try (Jedis jedis = new Jedis(hnp, new JedisClientConfig() { + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), new JedisClientConfig() { @Override public String getUser() { - return "acljedis"; + return endpoint.getUsername(); } @Override public String getPassword() { - return "fizzbuzz"; + return endpoint.getPassword(); } })) { assertEquals("PONG", jedis.ping()); @@ -78,12 +78,13 @@ public String getPassword() { @Test public void startWithUrl() { - try (Jedis j = new Jedis("localhost", 6379)) { - assertEquals("OK", j.auth("acljedis", "fizzbuzz")); + try (Jedis j = new Jedis(endpoint.getHostAndPort())) { + assertEquals("OK", j.auth(endpoint.getUsername(), endpoint.getPassword())); assertEquals("OK", j.select(2)); j.set("foo", "bar"); } - try (Jedis j2 = new Jedis("redis://acljedis:fizzbuzz@localhost:6379/2")) { + try (Jedis j2 = new Jedis( + endpoint.getURIBuilder().defaultCredentials().path("/2").build().toString())) { assertEquals("PONG", j2.ping()); assertEquals("bar", j2.get("foo")); } @@ -91,16 +92,16 @@ public void startWithUrl() { @Test public void startWithUri() throws URISyntaxException { - try (Jedis j = new Jedis("localhost", 6379)) { - assertEquals("OK", j.auth("acljedis", "fizzbuzz")); + try (Jedis j = new Jedis(endpoint.getHostAndPort())) { + assertEquals("OK", j.auth(endpoint.getUsername(), endpoint.getPassword())); assertEquals("OK", j.select(2)); j.set("foo", "bar"); } - try (Jedis j1 = new Jedis(new URI("redis://acljedis:fizzbuzz@localhost:6379/2"))) { + try (Jedis j1 = new Jedis(endpoint.getURIBuilder().defaultCredentials().path("/2").build())) { assertEquals("PONG", j1.ping()); assertEquals("bar", j1.get("foo")); } - try (Jedis j2 = new Jedis(new URI("redis://acljedis:fizzbuzz@localhost:6379/2"))) { + try (Jedis j2 = new Jedis(endpoint.getURIBuilder().defaultCredentials().path("/2").build())) { assertEquals("PONG", j2.ping()); assertEquals("bar", j2.get("foo")); } diff --git a/src/test/java/redis/clients/jedis/EndpointConfig.java b/src/test/java/redis/clients/jedis/EndpointConfig.java new file mode 100644 index 0000000000..5cb322224d --- /dev/null +++ b/src/test/java/redis/clients/jedis/EndpointConfig.java @@ -0,0 +1,129 @@ +package redis.clients.jedis; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import redis.clients.jedis.util.JedisURIHelper; + +import java.io.FileReader; +import java.net.URI; +import java.util.*; + +public class EndpointConfig { + + private final boolean tls; + private final String username; + private final String password; + private final int bdbId; + private final List endpoints; + + public EndpointConfig(HostAndPort hnp, String username, String password, boolean tls) { + this.tls = tls; + this.username = username; + this.password = password; + this.bdbId = 0; + this.endpoints = Collections.singletonList( + URI.create(getURISchema(tls) + hnp.getHost() + ":" + hnp.getPort())); + } + + public HostAndPort getHostAndPort() { + return JedisURIHelper.getHostAndPort(endpoints.get(0)); + } + + public String getPassword() { + return password; + } + + public String getUsername() { + return username == null? "default" : username; + } + + public String getHost() { + return getHostAndPort().getHost(); + } + + public int getPort() { + return getHostAndPort().getPort(); + } + + public int getBdbId() { return bdbId; } + + public URI getURI() { + return endpoints.get(0); + } + + public class EndpointURIBuilder { + private boolean tls; + + private String username; + + private String password; + + private String path; + + public EndpointURIBuilder() { + this.username = ""; + this.password = ""; + this.path = ""; + this.tls = EndpointConfig.this.tls; + } + + public EndpointURIBuilder defaultCredentials() { + this.username = EndpointConfig.this.username == null ? "" : getUsername(); + this.password = EndpointConfig.this.getPassword(); + return this; + } + + public EndpointURIBuilder tls(boolean v) { + this.tls = v; + return this; + } + + public EndpointURIBuilder path(String v) { + this.path = v; + return this; + } + + public EndpointURIBuilder credentials(String u, String p) { + this.username = u; + this.password = p; + return this; + } + + public URI build() { + String userInfo = !(this.username.isEmpty() && this.password.isEmpty()) ? + this.username + ':' + this.password + '@' : + ""; + return URI.create( + getURISchema(this.tls) + userInfo + getHost() + ":" + getPort() + this.path); + } + } + + public EndpointURIBuilder getURIBuilder() { + return new EndpointURIBuilder(); + } + + public DefaultJedisClientConfig.Builder getClientConfigBuilder() { + DefaultJedisClientConfig.Builder builder = DefaultJedisClientConfig.builder() + .password(password).ssl(tls); + + if (username != null) { + return builder.user(username); + } + + return builder; + } + + protected String getURISchema(boolean tls) { + return (tls ? "rediss" : "redis") + "://"; + } + + public static HashMap loadFromJSON(String filePath) throws Exception { + Gson gson = new Gson(); + HashMap configs; + try (FileReader reader = new FileReader(filePath)) { + configs = gson.fromJson(reader, new TypeToken>() { + }.getType()); + } + return configs; + } +} diff --git a/src/test/java/redis/clients/jedis/HostAndPorts.java b/src/test/java/redis/clients/jedis/HostAndPorts.java index 1f919117bb..2a1fc5581d 100644 --- a/src/test/java/redis/clients/jedis/HostAndPorts.java +++ b/src/test/java/redis/clients/jedis/HostAndPorts.java @@ -1,27 +1,24 @@ package redis.clients.jedis; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; public final class HostAndPorts { - private static List redisHostAndPortList = new ArrayList<>(); + private static HashMap endpointConfigs; + private static List sentinelHostAndPortList = new ArrayList<>(); private static List clusterHostAndPortList = new ArrayList<>(); private static List stableClusterHostAndPortList = new ArrayList<>(); static { - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 1)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 2)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 3)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 4)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 5)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 6)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 7)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 8)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 9)); - redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 10)); + String endpointsPath = System.getenv().getOrDefault("REDIS_ENDPOINTS_CONFIG_PATH", "src/test/resources/endpoints.json"); + try { + endpointConfigs = EndpointConfig.loadFromJSON(endpointsPath); + } catch (Exception e) { + throw new RuntimeException(e); + } sentinelHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT)); sentinelHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1)); @@ -41,31 +38,12 @@ public final class HostAndPorts { stableClusterHostAndPortList.add(new HostAndPort("localhost", 7481)); } - public static List parseHosts(String envHosts, - List existingHostsAndPorts) { - - if (null != envHosts && 0 < envHosts.length()) { - - String[] hostDefs = envHosts.split(","); - - if (null != hostDefs && 2 <= hostDefs.length) { - - List envHostsAndPorts = new ArrayList<>(hostDefs.length); - - for (String hostDef : hostDefs) { - - envHostsAndPorts.add(HostAndPort.from(hostDef)); - } - - return envHostsAndPorts; - } + public static EndpointConfig getRedisEndpoint(String endpointName) { + if (!endpointConfigs.containsKey(endpointName)) { + throw new IllegalArgumentException("Unknown Redis endpoint: " + endpointName); } - return existingHostsAndPorts; - } - - public static List getRedisServers() { - return redisHostAndPortList; + return endpointConfigs.get(endpointName); } public static List getSentinelServers() { diff --git a/src/test/java/redis/clients/jedis/JedisPoolTest.java b/src/test/java/redis/clients/jedis/JedisPoolTest.java index a256abcae0..29b3fd8438 100644 --- a/src/test/java/redis/clients/jedis/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/JedisPoolTest.java @@ -22,13 +22,15 @@ public class JedisPoolTest { - private static final HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + private static final EndpointConfig endpointStandalone0 = HostAndPorts.getRedisEndpoint("standalone0"); + + private static final EndpointConfig endpointStandalone1 = HostAndPorts.getRedisEndpoint("standalone1"); @Test public void checkConnections() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); + JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000); try (Jedis jedis = pool.getResource()) { - jedis.auth("foobared"); + jedis.auth(endpointStandalone0.getPassword()); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); } @@ -38,7 +40,7 @@ public void checkConnections() { @Test public void checkResourceWithConfig() { - try (JedisPool pool = new JedisPool(HostAndPorts.getRedisServers().get(7), + try (JedisPool pool = new JedisPool(HostAndPorts.getRedisEndpoint("standalone7-with-lfu-policy").getHostAndPort(), DefaultJedisClientConfig.builder().socketTimeoutMillis(5000).build())) { try (Jedis jedis = pool.getResource()) { @@ -51,9 +53,9 @@ public void checkResourceWithConfig() { @Test public void checkCloseableConnections() throws Exception { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); + JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000); try (Jedis jedis = pool.getResource()) { - jedis.auth("foobared"); + jedis.auth(endpointStandalone0.getPassword()); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); } @@ -65,7 +67,7 @@ public void checkCloseableConnections() throws Exception { public void checkConnectionWithDefaultHostAndPort() { JedisPool pool = new JedisPool(new JedisPoolConfig()); try (Jedis jedis = pool.getResource()) { - jedis.auth("foobared"); + jedis.auth(endpointStandalone0.getPassword()); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); } @@ -78,7 +80,7 @@ public void checkResourceIsClosableAndReusable() { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - try (JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared", 0, + try (JedisPool pool = new JedisPool(config, endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, endpointStandalone0.getPassword(), 0, "closable-reusable-pool", false, null, null, null)) { Jedis jedis = pool.getResource(); @@ -94,15 +96,15 @@ public void checkResourceIsClosableAndReusable() { @Test public void checkPoolRepairedWhenJedisIsBroken() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); + JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort()); try (Jedis jedis = pool.getResource()) { - jedis.auth("foobared"); + jedis.auth(endpointStandalone0.getPassword()); jedis.set("foo", "0"); jedis.disconnect(); } try (Jedis jedis = pool.getResource()) { - jedis.auth("foobared"); + jedis.auth(endpointStandalone0.getPassword()); jedis.incr("foo"); } pool.close(); @@ -114,12 +116,12 @@ public void checkPoolOverflow() { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - try (JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort()); + try (JedisPool pool = new JedisPool(config, endpointStandalone0.getHost(), endpointStandalone0.getPort()); Jedis jedis = pool.getResource()) { - jedis.auth("foobared"); + jedis.auth(endpointStandalone0.getPassword()); try (Jedis jedis2 = pool.getResource()) { - jedis2.auth("foobared"); + jedis2.auth(endpointStandalone0.getPassword()); } } } @@ -128,7 +130,7 @@ public void checkPoolOverflow() { public void securePool() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared"); + JedisPool pool = new JedisPool(config, endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, endpointStandalone0.getPassword()); try (Jedis jedis = pool.getResource()) { jedis.set("foo", "bar"); } @@ -138,27 +140,28 @@ public void securePool() { @Test public void nonDefaultDatabase() { - try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared"); Jedis jedis0 = pool0.getResource()) { + try (JedisPool pool0 = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, + endpointStandalone0.getPassword()); Jedis jedis0 = pool0.getResource()) { jedis0.set("foo", "bar"); assertEquals("bar", jedis0.get("foo")); } - try (JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared", 1); Jedis jedis1 = pool1.getResource()) { + try (JedisPool pool1 = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, + endpointStandalone0.getPassword(), 1); Jedis jedis1 = pool1.getResource()) { assertNull(jedis1.get("foo")); } } @Test public void startWithUrlString() { - try (Jedis j = new Jedis("localhost", 6380)) { - j.auth("foobared"); + try (Jedis j = new Jedis(endpointStandalone1.getHostAndPort())) { + j.auth(endpointStandalone1.getPassword()); j.select(2); j.set("foo", "bar"); } - try (JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2"); + try (JedisPool pool = new JedisPool( + endpointStandalone1.getURIBuilder().credentials("", endpointStandalone1.getPassword()).path("/2").build()); Jedis jedis = pool.getResource()) { assertEquals("PONG", jedis.ping()); assertEquals("bar", jedis.get("foo")); @@ -167,13 +170,14 @@ public void startWithUrlString() { @Test public void startWithUrl() throws URISyntaxException { - try (Jedis j = new Jedis("localhost", 6380)) { - j.auth("foobared"); + try (Jedis j = new Jedis(endpointStandalone1.getHostAndPort())) { + j.auth(endpointStandalone1.getPassword()); j.select(2); j.set("foo", "bar"); } - try (JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2")); + try (JedisPool pool = new JedisPool( + endpointStandalone1.getURIBuilder().credentials("", endpointStandalone1.getPassword()).path("/2").build()); Jedis jedis = pool.getResource()) { assertEquals("bar", jedis.get("foo")); } @@ -186,14 +190,14 @@ public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxExcept @Test public void allowUrlWithNoDBAndNoPassword() throws URISyntaxException { - new JedisPool("redis://localhost:6380").close(); - new JedisPool(new URI("redis://localhost:6380")).close(); + new JedisPool(endpointStandalone1.getURI().toString()).close(); + new JedisPool(endpointStandalone1.getURI()).close(); } @Test public void selectDatabaseOnActivation() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared")) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, + endpointStandalone0.getPassword())) { Jedis jedis0 = pool.getResource(); assertEquals(0, jedis0.getDB()); @@ -213,8 +217,8 @@ public void selectDatabaseOnActivation() { @Test public void customClientName() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared", 0, "my_shiny_client_name"); Jedis jedis = pool.getResource()) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, + endpointStandalone0.getPassword(), 0, "my_shiny_client_name"); Jedis jedis = pool.getResource()) { assertEquals("my_shiny_client_name", jedis.clientGetname()); } @@ -222,8 +226,8 @@ public void customClientName() { @Test public void invalidClientName() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared", 0, "invalid client name"); Jedis jedis = pool.getResource()) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, + endpointStandalone0.getPassword(), 0, "invalid client name"); Jedis jedis = pool.getResource()) { } catch (Exception e) { if (!e.getMessage().startsWith("client info cannot contain space")) { Assert.fail("invalid client name test fail"); @@ -287,7 +291,7 @@ public void returnResourceShouldResetState() { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared"); + JedisPool pool = new JedisPool(config, endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, endpointStandalone0.getPassword()); Jedis jedis = pool.getResource(); try { @@ -312,8 +316,8 @@ public void returnResourceShouldResetState() { @Test public void getNumActiveWhenPoolIsClosed() { - JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "foobared", 0, "my_shiny_client_name"); + JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000, + endpointStandalone0.getPassword(), 0, "my_shiny_client_name"); try (Jedis j = pool.getResource()) { j.ping(); @@ -325,16 +329,16 @@ public void getNumActiveWhenPoolIsClosed() { @Test public void getNumActiveReturnsTheCorrectNumber() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000)) { Jedis jedis = pool.getResource(); - jedis.auth("foobared"); + jedis.auth(endpointStandalone0.getPassword()); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); assertEquals(1, pool.getNumActive()); Jedis jedis2 = pool.getResource(); - jedis.auth("foobared"); + jedis.auth(endpointStandalone0.getPassword()); jedis.set("foo", "bar"); assertEquals(2, pool.getNumActive()); @@ -350,7 +354,7 @@ public void getNumActiveReturnsTheCorrectNumber() { @Test public void testAddObject() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000)) { pool.addObjects(1); assertEquals(1, pool.getNumIdle()); } @@ -358,9 +362,9 @@ public void testAddObject() { @Test public void closeResourceTwice() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000)) { Jedis j = pool.getResource(); - j.auth("foobared"); + j.auth(endpointStandalone0.getPassword()); j.ping(); j.close(); j.close(); @@ -369,7 +373,7 @@ public void closeResourceTwice() { @Test public void closeBrokenResourceTwice() { - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000)) { Jedis j = pool.getResource(); try { // make connection broken @@ -388,8 +392,9 @@ public void closeBrokenResourceTwice() { public void testCloseConnectionOnMakeObject() { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); - try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, - "wrong pass"); Jedis jedis = new Jedis("redis://:foobared@localhost:6379/")) { + try (JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), + endpointStandalone0.getPort(), 2000, "wrong pass"); + Jedis jedis = new Jedis(endpointStandalone0.getURIBuilder().defaultCredentials().build())) { int currentClientCount = getClientCount(jedis.clientList()); try { pool.getResource(); @@ -407,8 +412,8 @@ private int getClientCount(final String clientList) { @Test public void testResetInvalidCredentials() { DefaultRedisCredentialsProvider credentialsProvider - = new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, "foobared")); - JedisFactory factory = new JedisFactory(hnp, DefaultJedisClientConfig.builder() + = new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, endpointStandalone0.getPassword())); + JedisFactory factory = new JedisFactory(endpointStandalone0.getHostAndPort(), DefaultJedisClientConfig.builder() .credentialsProvider(credentialsProvider).clientName("my_shiny_client_name").build()); try (JedisPool pool = new JedisPool(new JedisPoolConfig(), factory)) { @@ -437,7 +442,7 @@ public void testResetInvalidCredentials() { public void testResetValidCredentials() { DefaultRedisCredentialsProvider credentialsProvider = new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, "bad password")); - JedisFactory factory = new JedisFactory(hnp, DefaultJedisClientConfig.builder() + JedisFactory factory = new JedisFactory(endpointStandalone0.getHostAndPort(), DefaultJedisClientConfig.builder() .credentialsProvider(credentialsProvider).clientName("my_shiny_client_name").build()); try (JedisPool pool = new JedisPool(new JedisPoolConfig(), factory)) { @@ -446,7 +451,7 @@ public void testResetValidCredentials() { } catch (JedisException e) { } assertEquals(0, pool.getNumActive()); - credentialsProvider.setCredentials(new DefaultRedisCredentials(null, "foobared")); + credentialsProvider.setCredentials(new DefaultRedisCredentials(null, endpointStandalone0.getPassword())); try (Jedis obj2 = pool.getResource()) { obj2.set("foo", "bar"); assertEquals("bar", obj2.get("foo")); diff --git a/src/test/java/redis/clients/jedis/JedisPooledTest.java b/src/test/java/redis/clients/jedis/JedisPooledTest.java index 6036062f5b..12fa2c0e3e 100644 --- a/src/test/java/redis/clients/jedis/JedisPooledTest.java +++ b/src/test/java/redis/clients/jedis/JedisPooledTest.java @@ -21,12 +21,15 @@ public class JedisPooledTest { - private static final HostAndPort hnp = HostAndPorts.getRedisServers().get(7); - private static final HostAndPort pwp = HostAndPorts.getRedisServers().get(1); // password protected + private static final EndpointConfig endpointStandalone7 = HostAndPorts.getRedisEndpoint( + "standalone7-with-lfu-policy"); + private static final EndpointConfig endpointStandalone1 = HostAndPorts.getRedisEndpoint( + "standalone1"); // password protected @Test public void checkCloseableConnections() { - JedisPooled pool = new JedisPooled(new ConnectionPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); + JedisPooled pool = new JedisPooled(new ConnectionPoolConfig(), endpointStandalone7.getHost(), + endpointStandalone7.getPort(), 2000); pool.set("foo", "bar"); assertEquals("bar", pool.get("foo")); pool.close(); @@ -35,7 +38,7 @@ public void checkCloseableConnections() { @Test public void checkResourceWithConfig() { - try (JedisPooled pool = new JedisPooled(hnp, + try (JedisPooled pool = new JedisPooled(endpointStandalone7.getHostAndPort(), DefaultJedisClientConfig.builder().socketTimeoutMillis(5000).build())) { try (Connection jedis = pool.getPool().getResource()) { @@ -50,7 +53,7 @@ public void checkPoolOverflow() { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); - try (JedisPooled pool = new JedisPooled(hnp, config); + try (JedisPooled pool = new JedisPooled(endpointStandalone7.getHostAndPort(), config); Connection jedis = pool.getPool().getResource()) { try (Connection jedis2 = pool.getPool().getResource()) { @@ -60,26 +63,29 @@ public void checkPoolOverflow() { @Test public void startWithUrlString() { - try (Jedis j = new Jedis("localhost", 6380)) { - j.auth("foobared"); + try (Jedis j = new Jedis(endpointStandalone1.getHostAndPort())) { + j.auth(endpointStandalone1.getPassword()); j.select(2); j.set("foo", "bar"); } - try (JedisPooled pool = new JedisPooled("redis://:foobared@localhost:6380/2")) { + try (JedisPooled pool = new JedisPooled( + endpointStandalone1.getURIBuilder().credentials("", endpointStandalone1.getPassword()).path("/2").build() + .toString())) { assertEquals("bar", pool.get("foo")); } } @Test public void startWithUrl() throws URISyntaxException { - try (Jedis j = new Jedis("localhost", 6380)) { - j.auth("foobared"); + try (Jedis j = new Jedis(endpointStandalone1.getHostAndPort())) { + j.auth(endpointStandalone1.getPassword()); j.select(2); j.set("foo", "bar"); } - try (JedisPooled pool = new JedisPooled(new URI("redis://:foobared@localhost:6380/2"))) { + try (JedisPooled pool = new JedisPooled( + endpointStandalone1.getURIBuilder().credentials("", endpointStandalone1.getPassword()).path("/2").build())) { assertEquals("bar", pool.get("foo")); } } @@ -91,13 +97,13 @@ public void shouldThrowExceptionForInvalidURI() throws URISyntaxException { @Test public void allowUrlWithNoDBAndNoPassword() throws URISyntaxException { - new JedisPooled("redis://localhost:6380").close(); - new JedisPooled(new URI("redis://localhost:6380")).close(); + new JedisPooled(endpointStandalone1.getURI().toString()).close(); + new JedisPooled(endpointStandalone1.getURI()).close(); } @Test public void customClientName() { - try (JedisPooled pool = new JedisPooled(hnp, DefaultJedisClientConfig.builder() + try (JedisPooled pool = new JedisPooled(endpointStandalone7.getHostAndPort(), DefaultJedisClientConfig.builder() .clientName("my_shiny_client_name").build()); Connection jedis = pool.getPool().getResource()) { assertEquals("my_shiny_client_name", new Jedis(jedis).clientGetname()); @@ -106,7 +112,7 @@ public void customClientName() { @Test public void invalidClientName() { - try (JedisPooled pool = new JedisPooled(hnp, DefaultJedisClientConfig.builder() + try (JedisPooled pool = new JedisPooled(endpointStandalone7.getHostAndPort(), DefaultJedisClientConfig.builder() .clientName("invalid client name").build()); Connection jedis = pool.getPool().getResource()) { } catch (Exception e) { @@ -118,7 +124,7 @@ public void invalidClientName() { @Test public void getNumActiveWhenPoolIsClosed() { - JedisPooled pool = new JedisPooled(hnp); + JedisPooled pool = new JedisPooled(endpointStandalone7.getHostAndPort()); try (Connection j = pool.getPool().getResource()) { j.ping(); @@ -130,7 +136,8 @@ public void getNumActiveWhenPoolIsClosed() { @Test public void getNumActiveReturnsTheCorrectNumber() { - try (JedisPooled pool = new JedisPooled(new ConnectionPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + try (JedisPooled pool = new JedisPooled(new ConnectionPoolConfig(), + endpointStandalone7.getHost(), endpointStandalone7.getPort(), 2000)) { Connection jedis = pool.getPool().getResource(); assertEquals(1, pool.getPool().getNumActive()); @@ -148,7 +155,8 @@ public void getNumActiveReturnsTheCorrectNumber() { @Test public void closeResourceTwice() { - try (JedisPooled pool = new JedisPooled(new ConnectionPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + try (JedisPooled pool = new JedisPooled(new ConnectionPoolConfig(), + endpointStandalone7.getHost(), endpointStandalone7.getPort(), 2000)) { Connection j = pool.getPool().getResource(); j.ping(); j.close(); @@ -158,7 +166,8 @@ public void closeResourceTwice() { @Test public void closeBrokenResourceTwice() { - try (JedisPooled pool = new JedisPooled(new ConnectionPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) { + try (JedisPooled pool = new JedisPooled(new ConnectionPoolConfig(), + endpointStandalone7.getHost(), endpointStandalone7.getPort(), 2000)) { Connection j = pool.getPool().getResource(); try { // make connection broken @@ -178,7 +187,7 @@ public void testResetValidCredentials() { DefaultRedisCredentialsProvider credentialsProvider = new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, "bad password")); - try (JedisPooled pool = new JedisPooled(pwp, DefaultJedisClientConfig.builder() + try (JedisPooled pool = new JedisPooled(endpointStandalone1.getHostAndPort(), DefaultJedisClientConfig.builder() .credentialsProvider(credentialsProvider).build())) { try { pool.get("foo"); @@ -186,7 +195,7 @@ public void testResetValidCredentials() { } catch (JedisException e) { } assertEquals(0, pool.getPool().getNumActive()); - credentialsProvider.setCredentials(new DefaultRedisCredentials(null, "foobared")); + credentialsProvider.setCredentials(new DefaultRedisCredentials(null, endpointStandalone1.getPassword())); assertThat(pool.get("foo"), anything()); } } @@ -223,7 +232,7 @@ public String getUser() { @Override public char[] getPassword() { - return "foobared".toCharArray(); + return endpointStandalone1.getPassword().toCharArray(); } }; } @@ -238,7 +247,7 @@ public void cleanUp() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig<>(); poolConfig.setMaxTotal(1); poolConfig.setTestOnBorrow(true); - try (JedisPooled pool = new JedisPooled(pwp, DefaultJedisClientConfig.builder() + try (JedisPooled pool = new JedisPooled(endpointStandalone1.getHostAndPort(), DefaultJedisClientConfig.builder() .credentialsProvider(credentialsProvider).build(), poolConfig)) { try { pool.get("foo"); diff --git a/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java b/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java index d00da82447..3329c45647 100644 --- a/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java +++ b/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java @@ -18,9 +18,6 @@ public class JedisSentinelPoolTest { private static final String MASTER_NAME = "mymaster"; - //protected static HostAndPort master = HostAndPorts.getRedisServers().get(2); - //protected static HostAndPort slave1 = HostAndPorts.getRedisServers().get(3); - protected static final HostAndPort sentinel1 = HostAndPorts.getSentinelServers().get(1); protected static final HostAndPort sentinel2 = HostAndPorts.getSentinelServers().get(3); diff --git a/src/test/java/redis/clients/jedis/JedisSentinelTest.java b/src/test/java/redis/clients/jedis/JedisSentinelTest.java index 6fc01eb2f8..eb956d5eb1 100644 --- a/src/test/java/redis/clients/jedis/JedisSentinelTest.java +++ b/src/test/java/redis/clients/jedis/JedisSentinelTest.java @@ -24,12 +24,10 @@ public class JedisSentinelTest { private static final String FAILOVER_MASTER_NAME = "mymasterfailover"; private static final String MASTER_IP = "127.0.0.1"; - protected static HostAndPort master = HostAndPorts.getRedisServers().get(0); - protected static HostAndPort slave = HostAndPorts.getRedisServers().get(4); + protected static EndpointConfig master = HostAndPorts.getRedisEndpoint("standalone0"); protected static HostAndPort sentinel = HostAndPorts.getSentinelServers().get(0); protected static HostAndPort sentinelForFailover = HostAndPorts.getSentinelServers().get(2); - protected static HostAndPort masterForFailover = HostAndPorts.getRedisServers().get(5); @Before public void setup() throws InterruptedException { diff --git a/src/test/java/redis/clients/jedis/JedisTest.java b/src/test/java/redis/clients/jedis/JedisTest.java index 52d8c80c02..560c5a0577 100644 --- a/src/test/java/redis/clients/jedis/JedisTest.java +++ b/src/test/java/redis/clients/jedis/JedisTest.java @@ -36,7 +36,7 @@ public JedisTest(RedisProtocol protocol) { @Test public void useWithoutConnecting() { try (Jedis j = new Jedis()) { - j.auth("foobared"); + j.auth(endpoint.getPassword()); j.dbSize(); } } @@ -56,31 +56,31 @@ public void checkBinaryData() { @Test public void connectWithConfig() { - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().build())) { - jedis.auth("foobared"); + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), DefaultJedisClientConfig.builder().build())) { + jedis.auth(endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared") - .build())) { + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().build())) { assertEquals("PONG", jedis.ping()); } } @Test public void connectWithEmptyConfigInterface() { - try (Jedis jedis = new Jedis(hnp, new JedisClientConfig() { + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), new JedisClientConfig() { })) { - jedis.auth("foobared"); + jedis.auth(endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } } @Test public void connectWithConfigInterface() { - try (Jedis jedis = new Jedis(hnp, new JedisClientConfig() { + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), new JedisClientConfig() { @Override public String getPassword() { - return "foobared"; + return endpoint.getPassword(); } })) { assertEquals("PONG", jedis.ping()); @@ -89,8 +89,8 @@ public String getPassword() { @Test public void connectOnResp3Protocol() { - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder() - .protocol(RedisProtocol.RESP3).password("foobared").build())) { + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder() + .protocol(RedisProtocol.RESP3).build())) { assertEquals("PONG", jedis.ping()); assertEquals(RedisProtocol.RESP3, jedis.getConnection().getRedisProtocol()); } @@ -98,8 +98,8 @@ public void connectOnResp3Protocol() { @Test public void connectOnResp3ProtocolShortcut() { - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().resp3() - .password("foobared").build())) { + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder() + .resp3().build())) { assertEquals("PONG", jedis.ping()); assertEquals(RedisProtocol.RESP3, jedis.getConnection().getRedisProtocol()); } @@ -109,8 +109,9 @@ public void connectOnResp3ProtocolShortcut() { public void timeoutConnection() throws Exception { final String TIMEOUT_STR = "timeout"; - Jedis jedis = new Jedis("localhost", 6379, 15000); - jedis.auth("foobared"); + Jedis jedis = new Jedis(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().timeoutMillis(15000).build()); + // read current config final String timeout = jedis.configGet(TIMEOUT_STR).get(TIMEOUT_STR); try { @@ -125,8 +126,7 @@ public void timeoutConnection() throws Exception { jedis.close(); } finally { // reset config - jedis = new Jedis("localhost", 6379); - jedis.auth("foobared"); + jedis = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder().build()); jedis.configSet(TIMEOUT_STR, timeout); jedis.close(); } @@ -134,8 +134,8 @@ public void timeoutConnection() throws Exception { @Test public void infiniteTimeout() throws Exception { - try (Jedis timeoutJedis = new Jedis("localhost", 6379, 200, 200, 200)) { - timeoutJedis.auth("foobared"); + try (Jedis timeoutJedis = new Jedis(endpoint.getHost(), endpoint.getPort(), 200, 200, 200)) { + timeoutJedis.auth(endpoint.getPassword()); try { timeoutJedis.blpop(0, "foo"); fail("SocketTimeoutException should occur"); @@ -168,13 +168,15 @@ public void shouldThrowInvalidURIExceptionForInvalidURI() throws URISyntaxExcept @Test public void connectWithUrl() { - try (Jedis j = new Jedis("localhost", 6380)) { - j.auth("foobared"); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone1"); + try (Jedis j = new Jedis(endpoint.getHostAndPort())) { + j.auth(endpoint.getPassword()); j.select(2); j.set("foo", "bar"); } - try (Jedis j2 = new Jedis("redis://:foobared@localhost:6380/2")) { + try (Jedis j2 = new Jedis( + endpoint.getURIBuilder().defaultCredentials().path("/2").build().toString())) { assertEquals("PONG", j2.ping()); assertEquals("bar", j2.get("foo")); } @@ -182,13 +184,15 @@ public void connectWithUrl() { @Test public void connectWithUri() throws URISyntaxException { - try (Jedis j = new Jedis("localhost", 6380)) { - j.auth("foobared"); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone1"); + try (Jedis j = new Jedis(endpoint.getHostAndPort())) { + j.auth(endpoint.getPassword()); j.select(2); j.set("foo", "bar"); } - try (Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"))) { + try (Jedis jedis = new Jedis( + endpoint.getURIBuilder().defaultCredentials().path("/2").build())) { assertEquals("PONG", jedis.ping()); assertEquals("bar", jedis.get("foo")); } @@ -196,13 +200,16 @@ public void connectWithUri() throws URISyntaxException { @Test public void connectWithUrlOnResp3() { - try (Jedis j = new Jedis("localhost", 6380)) { - j.auth("foobared"); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone1"); + + try (Jedis j = new Jedis(endpoint.getHostAndPort())) { + j.auth(endpoint.getPassword()); j.select(2); j.set("foo", "bar"); } - try (Jedis j2 = new Jedis("redis://:foobared@localhost:6380/2?protocol=3")) { + try (Jedis j2 = new Jedis( + endpoint.getURIBuilder().defaultCredentials().path("/2?protocol=3").build().toString())) { assertEquals("PONG", j2.ping()); assertEquals("bar", j2.get("foo")); } @@ -210,13 +217,16 @@ public void connectWithUrlOnResp3() { @Test public void connectWithUriOnResp3() throws URISyntaxException { - try (Jedis j = new Jedis("localhost", 6380)) { - j.auth("foobared"); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone1"); + + try (Jedis j = new Jedis(endpoint.getHostAndPort())) { + j.auth(endpoint.getPassword()); j.select(2); j.set("foo", "bar"); } - try (Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2?protocol=3"))) { + try (Jedis jedis = new Jedis( + endpoint.getURIBuilder().defaultCredentials().path("/2?protocol=3").build())) { assertEquals("PONG", jedis.ping()); assertEquals("bar", jedis.get("foo")); } @@ -237,15 +247,17 @@ public void shouldNotUpdateDbIndexIfSelectFails() { @Test public void allowUrlWithNoDBAndNoPassword() { - try (Jedis j1 = new Jedis("redis://localhost:6380")) { - j1.auth("foobared"); + EndpointConfig endpointStandalone1 = HostAndPorts.getRedisEndpoint("standalone1"); + + try (Jedis j1 = new Jedis(endpointStandalone1.getURI().toString())) { + j1.auth(endpointStandalone1.getPassword()); // assertEquals("localhost", j1.getClient().getHost()); // assertEquals(6380, j1.getClient().getPort()); assertEquals(0, j1.getDB()); } - try (Jedis j2 = new Jedis("redis://localhost:6380/")) { - j2.auth("foobared"); + try (Jedis j2 = new Jedis(endpointStandalone1.getURI().toString())) { + j2.auth(endpointStandalone1.getPassword()); // assertEquals("localhost", j2.getClient().getHost()); // assertEquals(6380, j2.getClient().getPort()); assertEquals(0, j2.getDB()); @@ -285,7 +297,7 @@ public void checkCloseableAfterConnect() { @Test public void checkCloseableAfterCommand() { Jedis bj = new Jedis(); - bj.auth("foobared"); + bj.auth(endpoint.getPassword()); bj.close(); } @@ -297,7 +309,7 @@ public void checkDisconnectOnQuit() { @Test public void clientSetInfoDefault() { - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared") + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder() .clientSetInfoConfig(ClientSetInfoConfig.DEFAULT).build())) { assertEquals("PONG", jedis.ping()); String info = jedis.clientInfo(); @@ -308,7 +320,7 @@ public void clientSetInfoDefault() { @Test public void clientSetInfoDisabled() { - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared") + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder() .clientSetInfoConfig(ClientSetInfoConfig.DISABLED).build())) { assertEquals("PONG", jedis.ping()); String info = jedis.clientInfo(); @@ -321,7 +333,7 @@ public void clientSetInfoDisabled() { public void clientSetInfoLibNameSuffix() { final String libNameSuffix = "for-redis"; ClientSetInfoConfig setInfoConfig = ClientSetInfoConfig.withLibNameSuffix(libNameSuffix); - try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared") + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder() .clientSetInfoConfig(setInfoConfig).build())) { assertEquals("PONG", jedis.ping()); String info = jedis.clientInfo(); diff --git a/src/test/java/redis/clients/jedis/ManagedConnectionProviderTest.java b/src/test/java/redis/clients/jedis/ManagedConnectionProviderTest.java index 84bca8d1fa..44ca793ef5 100644 --- a/src/test/java/redis/clients/jedis/ManagedConnectionProviderTest.java +++ b/src/test/java/redis/clients/jedis/ManagedConnectionProviderTest.java @@ -14,8 +14,8 @@ public class ManagedConnectionProviderTest { @Before public void setUp() { - connection = new Connection(HostAndPorts.getRedisServers().get(0), - DefaultJedisClientConfig.builder().user("acljedis").password("fizzbuzz").build()); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); + connection = new Connection(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder().build()); } @After diff --git a/src/test/java/redis/clients/jedis/MigratePipeliningTest.java b/src/test/java/redis/clients/jedis/MigratePipeliningTest.java index f8e7d8278c..8cac62c1f0 100644 --- a/src/test/java/redis/clients/jedis/MigratePipeliningTest.java +++ b/src/test/java/redis/clients/jedis/MigratePipeliningTest.java @@ -33,9 +33,17 @@ public class MigratePipeliningTest extends JedisCommandsTestBase { private static final byte[] bfoo3 = { 0x07, 0x08, 0x03 }; private static final byte[] bbar3 = { 0x09, 0x00, 0x03 }; - private static final String host = hnp.getHost(); - private static final int port = 6386; - private static final int portAuth = hnp.getPort() + 1; + private static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0-acl"); + + private static final EndpointConfig destEndpoint = HostAndPorts.getRedisEndpoint( + "standalone7-with-lfu-policy"); + + private static final EndpointConfig destEndpointWithAuth = HostAndPorts.getRedisEndpoint( + "standalone1"); + + private static final String host = destEndpoint.getHost(); + private static final int port = destEndpoint.getPort(); + private static final int portAuth = destEndpointWithAuth.getPort(); private static final int db = 2; private static final int dbAuth = 3; private static final int timeout = Protocol.DEFAULT_TIMEOUT; @@ -56,8 +64,8 @@ public void setUp() throws Exception { dest.flushAll(); dest.select(db); - destAuth = new Jedis(host, portAuth, 500); - destAuth.auth("foobared"); + destAuth = new Jedis(destEndpointWithAuth.getHostAndPort(), + destEndpointWithAuth.getClientConfigBuilder().build()); destAuth.flushAll(); destAuth.select(dbAuth); } @@ -258,7 +266,8 @@ public void migrateAuth() { Pipeline p = jedis.pipelined(); p.set("foo", "bar"); - p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), "foo"); + p.migrate(host, portAuth, dbAuth, timeout, + new MigrateParams().auth(destEndpointWithAuth.getPassword()), "foo"); p.get("foo"); assertThat(p.syncAndReturnAll(), @@ -274,7 +283,8 @@ public void migrateAuthBinary() { Pipeline p = jedis.pipelined(); p.set(bfoo, bbar); - p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), bfoo); + p.migrate(host, portAuth, dbAuth, timeout, + new MigrateParams().auth(destEndpointWithAuth.getPassword()), bfoo); p.get(bfoo); assertThat(p.syncAndReturnAll(), @@ -290,8 +300,9 @@ public void migrateAuth2() { Pipeline p = destAuth.pipelined(); p.set("foo", "bar"); - p.migrate(host, hnp.getPort(), 0, timeout, - new MigrateParams().auth2("acljedis", "fizzbuzz"), "foo"); + p.migrate(endpoint.getHost(), endpoint.getPort(), 0, timeout, + new MigrateParams().auth2(endpoint.getUsername(), endpoint.getPassword()), + "foo"); p.get("foo"); assertThat(p.syncAndReturnAll(), @@ -307,8 +318,9 @@ public void migrateAuth2Binary() { Pipeline p = dest.pipelined(); p.set(bfoo, bbar); - p.migrate(host, hnp.getPort(), 0, timeout, - new MigrateParams().auth2("acljedis", "fizzbuzz"), bfoo); + p.migrate(endpoint.getHost(), endpoint.getPort(), 0, timeout, + new MigrateParams().auth2(endpoint.getUsername(), endpoint.getPassword()), + bfoo); p.get(bfoo); assertThat(p.syncAndReturnAll(), diff --git a/src/test/java/redis/clients/jedis/PipeliningTest.java b/src/test/java/redis/clients/jedis/PipeliningTest.java index fffaa06a89..9340f366db 100644 --- a/src/test/java/redis/clients/jedis/PipeliningTest.java +++ b/src/test/java/redis/clients/jedis/PipeliningTest.java @@ -323,8 +323,10 @@ public void waitReplicas() { p.waitReplicas(1, 10); p.sync(); - try (Jedis j = new Jedis(HostAndPorts.getRedisServers().get(4))) { - j.auth("foobared"); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone4-replica-of-standalone1"); + + try (Jedis j = new Jedis(endpoint.getHostAndPort())) { + j.auth(endpoint.getPassword()); assertEquals("replicas", j.get("wait")); } } @@ -336,8 +338,10 @@ public void waitAof() { p.waitAOF(1L, 0L, 0L); p.sync(); - try (Jedis j = new Jedis(HostAndPorts.getRedisServers().get(4))) { - j.auth("foobared"); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone4-replica-of-standalone1"); + + try (Jedis j = new Jedis(endpoint.getHostAndPort())) { + j.auth(endpoint.getPassword()); assertEquals("aof", j.get("wait")); } } @@ -523,14 +527,14 @@ public void testEvalshaKeyAndArgWithBinary() { @Test public void testSyncWithNoCommandQueued() { // we need to test with fresh instance of Jedis - Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500); + Jedis jedis2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500); Pipeline pipeline = jedis2.pipelined(); pipeline.sync(); jedis2.close(); - jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500); + jedis2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500); pipeline = jedis2.pipelined(); List resp = pipeline.syncAndReturnAll(); @@ -542,8 +546,8 @@ public void testSyncWithNoCommandQueued() { @Test public void testCloseable() throws IOException { // we need to test with fresh instance of Jedis - Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500); - jedis2.auth("foobared"); + Jedis jedis2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500); + jedis2.auth(endpoint.getPassword()); Pipeline pipeline = jedis2.pipelined(); Response retFuture1 = pipeline.set("a", "1"); diff --git a/src/test/java/redis/clients/jedis/ReliableTransactionTest.java b/src/test/java/redis/clients/jedis/ReliableTransactionTest.java index 5abd5cf126..e3218d02d2 100644 --- a/src/test/java/redis/clients/jedis/ReliableTransactionTest.java +++ b/src/test/java/redis/clients/jedis/ReliableTransactionTest.java @@ -23,16 +23,18 @@ public class ReliableTransactionTest { final byte[] bmykey = { 0x42, 0x02, 0x03, 0x04 }; - private static final HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + private static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); private Connection conn; private Jedis nj; @Before public void setUp() throws Exception { - conn = new Connection(hnp, DefaultJedisClientConfig.builder().timeoutMillis(500).password("foobared").build()); + conn = new Connection(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().timeoutMillis(500).build()); - nj = new Jedis(hnp, DefaultJedisClientConfig.builder().timeoutMillis(500).password("foobared").build()); + nj = new Jedis(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().timeoutMillis(500).build()); nj.flushAll(); } @@ -211,10 +213,6 @@ public void transactionResponseWithError() { @Test public void testCloseable() { - // we need to test with fresh instance of Jedis -// Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500); -// jedis2.auth("foobared"); - ReliableTransaction transaction = new ReliableTransaction(conn); transaction.set("a", "1"); transaction.set("b", "2"); diff --git a/src/test/java/redis/clients/jedis/SSLACLJedisClusterTest.java b/src/test/java/redis/clients/jedis/SSLACLJedisClusterTest.java index c2f0e0ee3a..16eb63c0e0 100644 --- a/src/test/java/redis/clients/jedis/SSLACLJedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/SSLACLJedisClusterTest.java @@ -43,10 +43,15 @@ public class SSLACLJedisClusterTest extends JedisClusterTestBase { @BeforeClass public static void prepare() { - org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis", - RedisVersionUtil.checkRedisMajorVersionNumber(6)); - + // We need to set up certificates first before connecting to the endpoint with enabled TLS SSLJedisTest.setupTrustStore(); + + // TODO(imalinovskyi): Remove hardcoded connection settings + // once this test is refactored to support RE + org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis", + RedisVersionUtil.checkRedisMajorVersionNumber(6, + new EndpointConfig(new HostAndPort("localhost", 8379), + "default", "cluster", true))); } @Test diff --git a/src/test/java/redis/clients/jedis/SSLACLJedisTest.java b/src/test/java/redis/clients/jedis/SSLACLJedisTest.java index 0d0ca4e7eb..8151729e6e 100644 --- a/src/test/java/redis/clients/jedis/SSLACLJedisTest.java +++ b/src/test/java/redis/clients/jedis/SSLACLJedisTest.java @@ -4,7 +4,6 @@ import java.io.FileInputStream; import java.io.InputStream; -import java.net.URI; import java.security.InvalidAlgorithmParameterException; import java.security.KeyStore; import java.security.SecureRandom; @@ -23,28 +22,32 @@ */ public class SSLACLJedisTest { + protected static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0-acl-tls"); + + protected static final EndpointConfig endpointWithDefaultUser = HostAndPorts.getRedisEndpoint("standalone0-tls"); + @BeforeClass public static void prepare() { + // We need to set up certificates first before connecting to the endpoint with enabled TLS + SSLJedisTest.setupTrustStore(); // Use to check if the ACL test should be ran. ACL are available only in 6.0 and later org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis", - RedisVersionUtil.checkRedisMajorVersionNumber(6)); - - SSLJedisTest.setupTrustStore(); + RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint)); } @Test public void connectWithSsl() { - try (Jedis jedis = new Jedis("localhost", 6390, true)) { - jedis.auth("acljedis", "fizzbuzz"); + try (Jedis jedis = new Jedis(endpoint.getHost(), endpoint.getPort(), true)) { + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } } @Test public void connectWithConfig() { - try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), DefaultJedisClientConfig.builder().ssl(true).build())) { - jedis.auth("acljedis", "fizzbuzz"); + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } } @@ -52,10 +55,12 @@ public void connectWithConfig() { @Test public void connectWithUrl() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - try (Jedis jedis = new Jedis("rediss://default:foobared@localhost:6390")) { + try (Jedis jedis = new Jedis( + endpointWithDefaultUser.getURIBuilder().defaultCredentials().build().toString())) { assertEquals("PONG", jedis.ping()); } - try (Jedis jedis = new Jedis("rediss://acljedis:fizzbuzz@localhost:6390")) { + try (Jedis jedis = new Jedis( + endpoint.getURIBuilder().defaultCredentials().build().toString())) { assertEquals("PONG", jedis.ping()); } } @@ -63,10 +68,11 @@ public void connectWithUrl() { @Test public void connectWithUri() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - try (Jedis jedis = new Jedis(URI.create("rediss://default:foobared@localhost:6390"))) { + try (Jedis jedis = new Jedis( + endpointWithDefaultUser.getURIBuilder().defaultCredentials().build())) { assertEquals("PONG", jedis.ping()); } - try (Jedis jedis = new Jedis(URI.create("rediss://acljedis:fizzbuzz@localhost:6390"))) { + try (Jedis jedis = new Jedis(endpoint.getURIBuilder().defaultCredentials().build())) { assertEquals("PONG", jedis.ping()); } } diff --git a/src/test/java/redis/clients/jedis/SSLJedisTest.java b/src/test/java/redis/clients/jedis/SSLJedisTest.java index 75619dfef9..d3a5853f42 100644 --- a/src/test/java/redis/clients/jedis/SSLJedisTest.java +++ b/src/test/java/redis/clients/jedis/SSLJedisTest.java @@ -6,7 +6,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; -import java.net.URI; import java.security.InvalidAlgorithmParameterException; import java.security.KeyStore; import java.security.SecureRandom; @@ -26,6 +25,8 @@ public class SSLJedisTest { + protected static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0-tls"); + @BeforeClass public static void prepare() { setupTrustStore(); @@ -44,31 +45,31 @@ private static void setJvmTrustStore(String trustStoreFilePath, String trustStor @Test public void connectWithSsl() { - try (Jedis jedis = new Jedis("localhost", 6390, true)) { - jedis.auth("foobared"); + try (Jedis jedis = new Jedis(endpoint.getHost(), endpoint.getPort(), true)) { + jedis.auth(endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } } @Test public void connectWithConfig() { - try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), DefaultJedisClientConfig.builder().ssl(true).build())) { - jedis.auth("foobared"); + jedis.auth(endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } } @Test public void connectWithConfigInterface() { - try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), new JedisClientConfig() { @Override public boolean isSsl() { return true; } })) { - jedis.auth("foobared"); + jedis.auth(endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } } @@ -79,8 +80,8 @@ public boolean isSsl() { @Test public void connectWithUrl() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - try (Jedis jedis = new Jedis("rediss://localhost:6390")) { - jedis.auth("foobared"); + try (Jedis jedis = new Jedis(endpoint.getURI().toString())) { + jedis.auth(endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } } @@ -91,8 +92,8 @@ public void connectWithUrl() { @Test public void connectWithUri() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. - try (Jedis jedis = new Jedis(URI.create("rediss://localhost:6390"))) { - jedis.auth("foobared"); + try (Jedis jedis = new Jedis(endpoint.getURI())) { + jedis.auth(endpoint.getPassword()); assertEquals("PONG", jedis.ping()); } } diff --git a/src/test/java/redis/clients/jedis/ShardedConnectionTest.java b/src/test/java/redis/clients/jedis/ShardedConnectionTest.java index 2318b7b3d2..f849315d2e 100644 --- a/src/test/java/redis/clients/jedis/ShardedConnectionTest.java +++ b/src/test/java/redis/clients/jedis/ShardedConnectionTest.java @@ -9,13 +9,15 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Before; import org.junit.Test; -import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; public class ShardedConnectionTest { - private static final HostAndPort redis1 = HostAndPorts.getRedisServers().get(0); - private static final HostAndPort redis2 = HostAndPorts.getRedisServers().get(1); + /** + * NOTE(imalinovskyi): Both endpoints should share the same password. + */ + private static final EndpointConfig redis1 = HostAndPorts.getRedisEndpoint("standalone0"); + private static final EndpointConfig redis2 = HostAndPorts.getRedisEndpoint("standalone1"); private List shards; private JedisClientConfig clientConfig; @@ -23,10 +25,10 @@ public class ShardedConnectionTest { @Before public void startUp() { shards = new ArrayList<>(); - shards.add(redis1); - shards.add(redis2); + shards.add(redis1.getHostAndPort()); + shards.add(redis2.getHostAndPort()); - clientConfig = DefaultJedisClientConfig.builder().password("foobared").build(); + clientConfig = redis1.getClientConfigBuilder().build(); for (HostAndPort shard : shards) { try (Jedis j = new Jedis(shard, clientConfig)) { diff --git a/src/test/java/redis/clients/jedis/ShardedPipelineTest.java b/src/test/java/redis/clients/jedis/ShardedPipelineTest.java index a0d5a66d62..86349bf68c 100644 --- a/src/test/java/redis/clients/jedis/ShardedPipelineTest.java +++ b/src/test/java/redis/clients/jedis/ShardedPipelineTest.java @@ -10,20 +10,20 @@ public class ShardedPipelineTest { - private static final HostAndPort redis1 = HostAndPorts.getRedisServers().get(0); - private static final HostAndPort redis2 = HostAndPorts.getRedisServers().get(1); + private static final EndpointConfig redis1 = HostAndPorts.getRedisEndpoint("standalone0"); + private static final EndpointConfig redis2 = HostAndPorts.getRedisEndpoint("standalone1"); private static final ConnectionPoolConfig DEFAULT_POOL_CONFIG = new ConnectionPoolConfig(); - private static final DefaultJedisClientConfig DEFAULT_CLIENT_CONFIG = DefaultJedisClientConfig - .builder().password("foobared").build(); + private static final DefaultJedisClientConfig DEFAULT_CLIENT_CONFIG = redis1.getClientConfigBuilder() + .build(); - private List shards = Arrays.asList(redis1, redis2); + private List shards = Arrays.asList(redis1.getHostAndPort(), redis2.getHostAndPort()); @Before public void setUp() { for (HostAndPort shard : shards) { try (Jedis j = new Jedis(shard)) { - j.auth("foobared"); + j.auth(redis1.getPassword()); j.flushAll(); } } diff --git a/src/test/java/redis/clients/jedis/ShardingTest.java b/src/test/java/redis/clients/jedis/ShardingTest.java index b673b581ac..a865740ee6 100644 --- a/src/test/java/redis/clients/jedis/ShardingTest.java +++ b/src/test/java/redis/clients/jedis/ShardingTest.java @@ -14,17 +14,17 @@ public class ShardingTest { - private static final HostAndPort redis1 = HostAndPorts.getRedisServers().get(0); - private static final HostAndPort redis2 = HostAndPorts.getRedisServers().get(1); + private static final EndpointConfig redis1 = HostAndPorts.getRedisEndpoint("standalone0"); + private static final EndpointConfig redis2 = HostAndPorts.getRedisEndpoint("standalone1"); - private JedisClientConfig clientConfig = DefaultJedisClientConfig.builder().password("foobared").build(); + private JedisClientConfig clientConfig = redis1.getClientConfigBuilder().build(); @Before public void setUp() { - try (Jedis j = new Jedis(redis1, clientConfig)) { + try (Jedis j = new Jedis(redis1.getHostAndPort(), redis1.getClientConfigBuilder().build())) { j.flushAll(); } - try (Jedis j = new Jedis(redis2, clientConfig)) { + try (Jedis j = new Jedis(redis2.getHostAndPort(), redis2.getClientConfigBuilder().build())) { j.flushAll(); } } @@ -32,8 +32,8 @@ public void setUp() { @Test public void trySharding() { List shards = new ArrayList<>(); - shards.add(redis1); - shards.add(redis2); + shards.add(redis1.getHostAndPort()); + shards.add(redis2.getHostAndPort()); try (JedisSharding jedis = new JedisSharding(shards, clientConfig)) { for (int i = 0; i < 1000; i++) { jedis.set("key" + i, Integer.toString(i)); @@ -41,14 +41,14 @@ public void trySharding() { } long totalDbSize = 0; - try (Jedis j = new Jedis(redis1)) { - j.auth("foobared"); + try (Jedis j = new Jedis(redis1.getHostAndPort())) { + j.auth(redis1.getPassword()); long dbSize = j.dbSize(); assertTrue(dbSize > 400); totalDbSize += dbSize; } - try (Jedis j = new Jedis(redis2)) { - j.auth("foobared"); + try (Jedis j = new Jedis(redis2.getHostAndPort())) { + j.auth(redis2.getPassword()); long dbSize = j.dbSize(); assertTrue(dbSize > 400); totalDbSize += dbSize; @@ -59,8 +59,8 @@ public void trySharding() { @Test public void tryShardingWithMurmur() { List shards = new ArrayList<>(); - shards.add(redis1); - shards.add(redis2); + shards.add(redis1.getHostAndPort()); + shards.add(redis2.getHostAndPort()); try (JedisSharding jedis = new JedisSharding(shards, clientConfig, Hashing.MURMUR_HASH)) { for (int i = 0; i < 1000; i++) { jedis.set("key" + i, Integer.toString(i)); @@ -68,14 +68,14 @@ public void tryShardingWithMurmur() { } long totalDbSize = 0; - try (Jedis j = new Jedis(redis1)) { - j.auth("foobared"); + try (Jedis j = new Jedis(redis1.getHostAndPort())) { + j.auth(redis1.getPassword()); long dbSize = j.dbSize(); assertTrue(dbSize > 400); totalDbSize += dbSize; } - try (Jedis j = new Jedis(redis2)) { - j.auth("foobared"); + try (Jedis j = new Jedis(redis2.getHostAndPort())) { + j.auth(redis2.getPassword()); long dbSize = j.dbSize(); assertTrue(dbSize > 400); totalDbSize += dbSize; @@ -86,8 +86,8 @@ public void tryShardingWithMurmur() { @Test public void tryShardingWithMD5() { List shards = new ArrayList<>(); - shards.add(redis1); - shards.add(redis2); + shards.add(redis1.getHostAndPort()); + shards.add(redis2.getHostAndPort()); try (JedisSharding jedis = new JedisSharding(shards, clientConfig, Hashing.MD5)) { for (int i = 0; i < 1000; i++) { jedis.set("key" + i, Integer.toString(i)); @@ -95,14 +95,14 @@ public void tryShardingWithMD5() { } long totalDbSize = 0; - try (Jedis j = new Jedis(redis1)) { - j.auth("foobared"); + try (Jedis j = new Jedis(redis1.getHostAndPort())) { + j.auth(redis1.getPassword()); long dbSize = j.dbSize(); assertTrue(dbSize > 400); totalDbSize += dbSize; } - try (Jedis j = new Jedis(redis2)) { - j.auth("foobared"); + try (Jedis j = new Jedis(redis2.getHostAndPort())) { + j.auth(redis2.getPassword()); long dbSize = j.dbSize(); totalDbSize += dbSize; } @@ -124,8 +124,8 @@ public void checkKeyTags() { @Test public void checkCloseable() { List shards = new ArrayList<>(); - shards.add(redis1); - shards.add(redis2); + shards.add(redis1.getHostAndPort()); + shards.add(redis2.getHostAndPort()); JedisSharding jedis = new JedisSharding(shards, clientConfig); jedis.set("closeable", "true"); @@ -141,8 +141,8 @@ public void checkCloseable() { @Test public void testGeneralCommand() { List shards = new ArrayList<>(); - shards.add(redis1); - shards.add(redis2); + shards.add(redis1.getHostAndPort()); + shards.add(redis2.getHostAndPort()); try (JedisSharding jedis = new JedisSharding(shards, clientConfig)) { jedis.sendCommand("command", SET, "command", "general"); diff --git a/src/test/java/redis/clients/jedis/TransactionV2Test.java b/src/test/java/redis/clients/jedis/TransactionV2Test.java index cf63db1c03..8126724662 100644 --- a/src/test/java/redis/clients/jedis/TransactionV2Test.java +++ b/src/test/java/redis/clients/jedis/TransactionV2Test.java @@ -25,16 +25,19 @@ public class TransactionV2Test { final byte[] bmykey = { 0x42, 0x02, 0x03, 0x04 }; - private static final HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + private static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); private Connection conn; private Jedis nj; @Before public void setUp() throws Exception { - conn = new Connection(hnp, DefaultJedisClientConfig.builder().timeoutMillis(500).password("foobared").build()); + conn = new Connection(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().timeoutMillis(500).build()); + + nj = new Jedis(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().timeoutMillis(500).build()); - nj = new Jedis(hnp, DefaultJedisClientConfig.builder().timeoutMillis(500).password("foobared").build()); nj.flushAll(); } @@ -212,10 +215,6 @@ public void transactionResponseWithError() { @Test public void testCloseable() { - // we need to test with fresh instance of Jedis -// Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500); -// jedis2.auth("foobared"); - Transaction transaction = new Transaction(conn); transaction.set("a", "1"); transaction.set("b", "2"); diff --git a/src/test/java/redis/clients/jedis/benchmark/GetSetBenchmark.java b/src/test/java/redis/clients/jedis/benchmark/GetSetBenchmark.java index 93d450abc1..59a50d6e5e 100644 --- a/src/test/java/redis/clients/jedis/benchmark/GetSetBenchmark.java +++ b/src/test/java/redis/clients/jedis/benchmark/GetSetBenchmark.java @@ -4,19 +4,19 @@ import java.net.UnknownHostException; import java.util.Calendar; -import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.EndpointConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.HostAndPorts; public class GetSetBenchmark { - private static HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + private static EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); private static final int TOTAL_OPERATIONS = 100000; public static void main(String[] args) throws UnknownHostException, IOException { - Jedis jedis = new Jedis(hnp); + Jedis jedis = new Jedis(endpoint.getHostAndPort()); jedis.connect(); - jedis.auth("foobared"); + jedis.auth(endpoint.getPassword()); jedis.flushAll(); long begin = Calendar.getInstance().getTimeInMillis(); diff --git a/src/test/java/redis/clients/jedis/benchmark/PipelinedGetSetBenchmark.java b/src/test/java/redis/clients/jedis/benchmark/PipelinedGetSetBenchmark.java index 413fa55428..c19c5444bc 100644 --- a/src/test/java/redis/clients/jedis/benchmark/PipelinedGetSetBenchmark.java +++ b/src/test/java/redis/clients/jedis/benchmark/PipelinedGetSetBenchmark.java @@ -4,20 +4,17 @@ import java.net.UnknownHostException; import java.util.Calendar; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.Pipeline; -import redis.clients.jedis.HostAndPorts; +import redis.clients.jedis.*; public class PipelinedGetSetBenchmark { - private static HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + private static EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); private static final int TOTAL_OPERATIONS = 200000; public static void main(String[] args) throws UnknownHostException, IOException { - Jedis jedis = new Jedis(hnp); + Jedis jedis = new Jedis(endpoint.getHostAndPort()); jedis.connect(); - jedis.auth("foobared"); + jedis.auth(endpoint.getPassword()); jedis.flushAll(); long begin = Calendar.getInstance().getTimeInMillis(); diff --git a/src/test/java/redis/clients/jedis/benchmark/PoolBenchmark.java b/src/test/java/redis/clients/jedis/benchmark/PoolBenchmark.java index 5b8dc5dc06..b7861950ec 100644 --- a/src/test/java/redis/clients/jedis/benchmark/PoolBenchmark.java +++ b/src/test/java/redis/clients/jedis/benchmark/PoolBenchmark.java @@ -6,20 +6,17 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.HostAndPorts; +import redis.clients.jedis.*; public class PoolBenchmark { - private static HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + private static EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); private static final int TOTAL_OPERATIONS = 100000; public static void main(String[] args) throws Exception { - Jedis j = new Jedis(hnp); + Jedis j = new Jedis(endpoint.getHostAndPort()); j.connect(); - j.auth("foobared"); + j.auth(endpoint.getPassword()); j.flushAll(); j.disconnect(); long t = System.currentTimeMillis(); @@ -30,8 +27,8 @@ public static void main(String[] args) throws Exception { } private static void withPool() throws Exception { - final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), hnp.getHost(), - hnp.getPort(), 2000, "foobared"); + final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), endpoint.getHost(), + endpoint.getPort(), 2000, endpoint.getPassword()); List tds = new ArrayList(); final AtomicInteger ind = new AtomicInteger(); diff --git a/src/test/java/redis/clients/jedis/benchmark/PooledBenchmark.java b/src/test/java/redis/clients/jedis/benchmark/PooledBenchmark.java index 2c34beef31..9641d10d0c 100644 --- a/src/test/java/redis/clients/jedis/benchmark/PooledBenchmark.java +++ b/src/test/java/redis/clients/jedis/benchmark/PooledBenchmark.java @@ -4,19 +4,16 @@ import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.HostAndPorts; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPooled; +import redis.clients.jedis.*; public class PooledBenchmark { - private static HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + private static EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); private static final int TOTAL_OPERATIONS = 100000; public static void main(String[] args) throws Exception { - try (Jedis j = new Jedis(hnp.getHost(), hnp.getPort())) { - j.auth("foobared"); + try (Jedis j = new Jedis(endpoint.getHost(), endpoint.getPort())) { + j.auth(endpoint.getPassword()); j.flushAll(); j.disconnect(); } @@ -27,7 +24,7 @@ public static void main(String[] args) throws Exception { } private static void withPool() throws Exception { - final JedisPooled j = new JedisPooled(hnp.getHost(), hnp.getPort(), null, "foobared"); + final JedisPooled j = new JedisPooled(endpoint.getHost(), endpoint.getPort(), null, endpoint.getPassword()); List tds = new ArrayList<>(); final AtomicInteger ind = new AtomicInteger(); diff --git a/src/test/java/redis/clients/jedis/benchmark/ShardingBenchmark.java b/src/test/java/redis/clients/jedis/benchmark/ShardingBenchmark.java index ea3c490bcc..4bee304f4e 100644 --- a/src/test/java/redis/clients/jedis/benchmark/ShardingBenchmark.java +++ b/src/test/java/redis/clients/jedis/benchmark/ShardingBenchmark.java @@ -5,20 +5,17 @@ import java.util.Arrays; import java.util.Calendar; -import redis.clients.jedis.DefaultJedisClientConfig; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.HostAndPorts; -import redis.clients.jedis.JedisSharding; +import redis.clients.jedis.*; public class ShardingBenchmark { - private static HostAndPort hnp1 = HostAndPorts.getRedisServers().get(0); - private static HostAndPort hnp2 = HostAndPorts.getRedisServers().get(1); + private static EndpointConfig endpointStandalone0 = HostAndPorts.getRedisEndpoint("standalone0"); + private static EndpointConfig endpointStandalone1 = HostAndPorts.getRedisEndpoint("standalone1"); private static final int TOTAL_OPERATIONS = 100000; public static void main(String[] args) throws UnknownHostException, IOException { - try (JedisSharding jedis = new JedisSharding(Arrays.asList(hnp1, hnp2), - DefaultJedisClientConfig.builder().password("foobared").build())) { + try (JedisSharding jedis = new JedisSharding(Arrays.asList(endpointStandalone0.getHostAndPort(), endpointStandalone1.getHostAndPort()), + endpointStandalone0.getClientConfigBuilder().build())) { long begin = Calendar.getInstance().getTimeInMillis(); diff --git a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsGenericCommandsTest.java b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsGenericCommandsTest.java index 1849d26687..f558a4f686 100644 --- a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsGenericCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsGenericCommandsTest.java @@ -1082,8 +1082,8 @@ public void testCopyToDbBinary() { private void assertKeyExists(int dstDb, String key, Object expectedValue) { // Cheat and use Jedis, it gives us access to any db. - try (Jedis jedis = new Jedis(nodeInfo)) { - jedis.auth("foobared"); + try (Jedis jedis = new Jedis(endpoint.getHostAndPort())) { + jedis.auth(endpoint.getPassword()); jedis.select(dstDb); assertThat(jedis.get(key), equalTo(expectedValue)); } diff --git a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsModulesTestBase.java b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsModulesTestBase.java index 3bac716fa4..37c6500aeb 100644 --- a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsModulesTestBase.java +++ b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsModulesTestBase.java @@ -1,6 +1,7 @@ package redis.clients.jedis.commands.commandobjects; import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.HostAndPorts; import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; @@ -9,11 +10,8 @@ */ public abstract class CommandObjectsModulesTestBase extends CommandObjectsTestBase { - private static final String address = - System.getProperty("modulesDocker", Protocol.DEFAULT_HOST + ':' + 6479); - public CommandObjectsModulesTestBase(RedisProtocol protocol) { - super(protocol, HostAndPort.from(address), null); + super(protocol, HostAndPorts.getRedisEndpoint("modules-docker")); } } diff --git a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsStandaloneTestBase.java b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsStandaloneTestBase.java index fa4a43ea80..128642d2a7 100644 --- a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsStandaloneTestBase.java +++ b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsStandaloneTestBase.java @@ -9,7 +9,7 @@ public abstract class CommandObjectsStandaloneTestBase extends CommandObjectsTestBase { public CommandObjectsStandaloneTestBase(RedisProtocol protocol) { - super(protocol, HostAndPorts.getRedisServers().get(0), "foobared"); + super(protocol, HostAndPorts.getRedisEndpoint("standalone0")); } } diff --git a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsTestBase.java b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsTestBase.java index 5fde288839..45e0c71ad4 100644 --- a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsTestBase.java +++ b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsTestBase.java @@ -8,11 +8,7 @@ import org.junit.Before; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import redis.clients.jedis.CommandObject; -import redis.clients.jedis.CommandObjects; -import redis.clients.jedis.DefaultJedisClientConfig; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.RedisProtocol; +import redis.clients.jedis.*; import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.commands.CommandsTestsParameters; import redis.clients.jedis.executors.CommandExecutor; @@ -52,12 +48,7 @@ public static Collection data() { /** * Host and port of the Redis server to connect to. Injected from subclasses. */ - protected final HostAndPort nodeInfo; - - /** - * Password to use when connecting to the Redis server, if needed. Injected from subclasses. - */ - private final String authPassword; + protected final EndpointConfig endpoint; /** * The {@link CommandObjects} to use for the tests. This is the subject-under-test. @@ -70,10 +61,9 @@ public static Collection data() { */ private CommandExecutor commandExecutor; - public CommandObjectsTestBase(RedisProtocol protocol, HostAndPort nodeInfo, String authPassword) { + public CommandObjectsTestBase(RedisProtocol protocol, EndpointConfig endpoint) { this.protocol = protocol; - this.nodeInfo = nodeInfo; - this.authPassword = authPassword; + this.endpoint = endpoint; commandObjects = new CommandObjects(); commandObjects.setProtocol(protocol); } @@ -81,10 +71,11 @@ public CommandObjectsTestBase(RedisProtocol protocol, HostAndPort nodeInfo, Stri @Before public void setUp() { // Configure a default command executor. - DefaultJedisClientConfig clientConfig = DefaultJedisClientConfig.builder() - .protocol(protocol).password(authPassword).build(); + DefaultJedisClientConfig clientConfig = endpoint.getClientConfigBuilder().protocol(protocol) + .build(); - ConnectionProvider connectionProvider = new PooledConnectionProvider(nodeInfo, clientConfig); + ConnectionProvider connectionProvider = new PooledConnectionProvider(endpoint.getHostAndPort(), + clientConfig); commandExecutor = new DefaultCommandExecutor(connectionProvider); diff --git a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsTriggersAndFunctionsCommandsTest.java b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsTriggersAndFunctionsCommandsTest.java index 31528e8092..6403f57404 100644 --- a/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsTriggersAndFunctionsCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsTriggersAndFunctionsCommandsTest.java @@ -7,9 +7,11 @@ import java.util.ArrayList; import java.util.List; - +import org.junit.After; import org.junit.Test; + import redis.clients.jedis.RedisProtocol; +import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.gears.TFunctionListParams; import redis.clients.jedis.gears.TFunctionLoadParams; import redis.clients.jedis.gears.resps.GearsLibraryInfo; @@ -23,6 +25,13 @@ public CommandObjectsTriggersAndFunctionsCommandsTest(RedisProtocol protocol) { super(protocol); } + @After + public void tearDown() throws Exception { + try { + exec(commandObjects.tFunctionDelete("lib")); + } catch (JedisDataException de) { } + } + @Test public void testTFunctionLoadAndCall() { String libraryCode = "#!js api_version=1.0 name=lib\n" + diff --git a/src/test/java/redis/clients/jedis/commands/jedis/AccessControlListCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/AccessControlListCommandsTest.java index c8f56bc7eb..150211383e 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/AccessControlListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/AccessControlListCommandsTest.java @@ -46,7 +46,7 @@ public class AccessControlListCommandsTest extends JedisCommandsTestBase { public static void prepare() throws Exception { // Use to check if the ACL test should be ran. ACL are available only in 6.0 and later org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis", - RedisVersionUtil.checkRedisMajorVersionNumber(6)); + RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint)); } public AccessControlListCommandsTest(RedisProtocol protocol) { @@ -359,7 +359,7 @@ public void aclLogTest() { } // test the ACL Log - jedis.auth("default", "foobared"); + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); List aclEntries = jedis.aclLog(); assertEquals("Number of log messages ", 1, aclEntries.size()); @@ -385,7 +385,7 @@ public void aclLogTest() { } // test the ACL Log - jedis.auth("default", "foobared"); + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); assertEquals("Number of log messages ", 1, jedis.aclLog().size()); assertEquals(10, jedis.aclLog().get(0).getCount()); assertEquals("get", jedis.aclLog().get(0).getObject()); @@ -399,7 +399,7 @@ public void aclLogTest() { } // test the ACL Log - jedis.auth("default", "foobared"); + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); assertEquals("Number of log messages ", 2, jedis.aclLog().size()); assertEquals(1, jedis.aclLog().get(0).getCount()); assertEquals("somekeynotallowed", jedis.aclLog().get(0).getObject()); @@ -418,7 +418,7 @@ public void aclLogTest() { } t.close(); - jedis.auth("default", "foobared"); + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); assertEquals("Number of log messages ", 1, jedis.aclLog().size()); assertEquals(1, jedis.aclLog().get(0).getCount()); assertEquals("multi", jedis.aclLog().get(0).getContext()); @@ -439,7 +439,7 @@ public void aclLogTest() { } catch (JedisAccessControlException e) { } - jedis.auth("default", "foobared"); + jedis.auth(endpoint.getUsername(), endpoint.getPassword()); assertEquals("Number of log messages ", 3, jedis.aclLog().size()); assertEquals("Number of log messages ", 2, jedis.aclLog(2).size()); diff --git a/src/test/java/redis/clients/jedis/commands/jedis/AllKindOfValuesCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/AllKindOfValuesCommandsTest.java index e7381a7440..868195961e 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/AllKindOfValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/AllKindOfValuesCommandsTest.java @@ -21,17 +21,12 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.Transaction; +import redis.clients.jedis.*; import redis.clients.jedis.args.ExpiryOption; import redis.clients.jedis.params.ScanParams; import redis.clients.jedis.resps.ScanResult; -import redis.clients.jedis.StreamEntryID; import redis.clients.jedis.args.FlushMode; import redis.clients.jedis.params.RestoreParams; -import redis.clients.jedis.HostAndPorts; -import redis.clients.jedis.RedisProtocol; import redis.clients.jedis.util.SafeEncoder; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.params.SetParams; @@ -57,7 +52,7 @@ public class AllKindOfValuesCommandsTest extends JedisCommandsTestBase { final byte[] bex = { 0x65, 0x78 }; final int expireSeconds = 2; - private static final HostAndPort lfuHnp = HostAndPorts.getRedisServers().get(7); + private static final EndpointConfig lfuEndpoint = HostAndPorts.getRedisEndpoint("standalone7-with-lfu-policy"); public AllKindOfValuesCommandsTest(RedisProtocol redisProtocol) { super(redisProtocol); @@ -604,8 +599,8 @@ public void dumpAndRestore() { @Test public void restoreParams() { // take a separate instance - Jedis jedis2 = new Jedis(hnp.getHost(), 6380, 500); - jedis2.auth("foobared"); + Jedis jedis2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500); + jedis2.auth(endpoint.getPassword()); jedis2.flushAll(); jedis2.set("foo", "bar"); @@ -637,7 +632,7 @@ public void restoreParams() { assertEquals(1000, jedis2.objectIdletime("bar1").longValue()); jedis2.close(); - Jedis lfuJedis = new Jedis(lfuHnp.getHost(), lfuHnp.getPort(), 500); + Jedis lfuJedis = new Jedis(lfuEndpoint.getHostAndPort(), lfuEndpoint.getClientConfigBuilder().timeoutMillis(500).build());; lfuJedis.restore("bar1", 1000, serialized, RestoreParams.restoreParams().replace().frequency(90)); assertEquals(90, lfuJedis.objectFreq("bar1").longValue()); lfuJedis.close(); @@ -1146,7 +1141,7 @@ public void reset() { assertEquals("NOAUTH Authentication required.", ex1.getMessage()); // multi reset - jedis.auth("foobared"); + jedis.auth(endpoint.getPassword()); jedis.set(counter, "1"); Transaction trans = jedis.multi(); @@ -1156,7 +1151,7 @@ public void reset() { Exception ex2 = assertThrows(JedisDataException.class, trans::exec); assertEquals("EXECABORT Transaction discarded because of: NOAUTH Authentication required.", ex2.getMessage()); - jedis.auth("foobared"); + jedis.auth(endpoint.getPassword()); assertEquals("1", jedis.get(counter)); } } diff --git a/src/test/java/redis/clients/jedis/commands/jedis/ClientCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/ClientCommandsTest.java index 2a4657ecd5..1b221cfc55 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/ClientCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/ClientCommandsTest.java @@ -21,7 +21,6 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.RedisProtocol; import redis.clients.jedis.args.ClientAttributeOption; @@ -47,8 +46,8 @@ public ClientCommandsTest(RedisProtocol protocol) { @Override public void setUp() throws Exception { super.setUp(); - client = new Jedis(hnp.getHost(), hnp.getPort(), 500); - client.auth("foobared"); + client = new Jedis(endpoint.getHost(), endpoint.getPort(), 500); + client.auth(endpoint.getPassword()); client.clientSetname(clientName); } @@ -97,8 +96,8 @@ public void clientId() { @Test public void clientIdmultipleConnection() { - try (Jedis client2 = new Jedis(hnp.getHost(), hnp.getPort(), 500)) { - client2.auth("foobared"); + try (Jedis client2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500)) { + client2.auth(endpoint.getPassword()); client2.clientSetname("fancy_jedis_another_name"); // client-id is monotonically increasing @@ -111,7 +110,7 @@ public void clientIdReconnect() { long clientIdInitial = client.clientId(); client.disconnect(); client.connect(); - client.auth("foobared"); + client.auth(endpoint.getPassword()); long clientIdAfterReconnect = client.clientId(); assertTrue(clientIdInitial < clientIdAfterReconnect); @@ -233,7 +232,7 @@ public void killAddrIpPort() { @Test public void killUser() { client.aclSetUser("test_kill", "on", "+acl", ">password1"); - try (Jedis client2 = new Jedis(hnp.getHost(), hnp.getPort(), 500)) { + try (Jedis client2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500)) { client2.auth("test_kill", "password1"); assertEquals(1, jedis.clientKill(new ClientKillParams().user("test_kill"))); @@ -250,8 +249,8 @@ public void killMaxAge() throws InterruptedException { // sleep twice the maxAge, to be sure Thread.sleep(maxAge * 2 * 1000); - try (Jedis client2 = new Jedis(hnp.getHost(), hnp.getPort(), 500)) { - client2.auth("foobared"); + try (Jedis client2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500)) { + client2.auth(endpoint.getPassword()); long killedClients = jedis.clientKill(new ClientKillParams().maxAge(maxAge)); @@ -300,8 +299,8 @@ public void trackingInfo() { @Test public void trackingInfoResp3() { - Jedis clientResp3 = new Jedis(hnp, DefaultJedisClientConfig.builder() - .protocol(RedisProtocol.RESP3).password("foobared").build()); + Jedis clientResp3 = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder() + .protocol(RedisProtocol.RESP3).build()); TrackingInfo trackingInfo = clientResp3.clientTrackingInfo(); assertEquals(1, trackingInfo.getFlags().size()); diff --git a/src/test/java/redis/clients/jedis/commands/jedis/ControlCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/ControlCommandsTest.java index 0287095e4c..4c9059f8d0 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/ControlCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/ControlCommandsTest.java @@ -27,15 +27,10 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import redis.clients.jedis.DefaultJedisClientConfig; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisMonitor; -import redis.clients.jedis.Protocol; -import redis.clients.jedis.RedisProtocol; +import redis.clients.jedis.*; import redis.clients.jedis.args.ClientPauseMode; import redis.clients.jedis.args.LatencyEvent; import redis.clients.jedis.exceptions.JedisDataException; -import redis.clients.jedis.HostAndPorts; import redis.clients.jedis.params.CommandListFilterByParams; import redis.clients.jedis.params.LolwutParams; import redis.clients.jedis.resps.CommandDocument; @@ -129,8 +124,10 @@ public void readwrite() { @Test public void roleMaster() { - try (Jedis master = new Jedis(HostAndPorts.getRedisServers().get(0), - DefaultJedisClientConfig.builder().password("foobared").build())) { + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); + + try (Jedis master = new Jedis(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().build())) { List role = master.role(); assertEquals("master", role.get(0)); @@ -147,19 +144,23 @@ public void roleMaster() { @Test public void roleSlave() { - try (Jedis slave = new Jedis(HostAndPorts.getRedisServers().get(4), - DefaultJedisClientConfig.builder().password("foobared").build())) { + EndpointConfig primaryEndpoint = HostAndPorts.getRedisEndpoint("standalone0"); + EndpointConfig secondaryEndpoint = HostAndPorts.getRedisEndpoint( + "standalone4-replica-of-standalone1"); + + try (Jedis slave = new Jedis(secondaryEndpoint.getHostAndPort(), + secondaryEndpoint.getClientConfigBuilder().build())) { List role = slave.role(); assertEquals("slave", role.get(0)); - assertEquals((long) HostAndPorts.getRedisServers().get(0).getPort(), role.get(2)); + assertEquals((long) primaryEndpoint.getPort(), role.get(2)); assertEquals("connected", role.get(3)); assertTrue(role.get(4) instanceof Long); // binary List brole = slave.roleBinary(); assertArrayEquals("slave".getBytes(), (byte[]) brole.get(0)); - assertEquals((long) HostAndPorts.getRedisServers().get(0).getPort(), brole.get(2)); + assertEquals((long) primaryEndpoint.getPort(), brole.get(2)); assertArrayEquals("connected".getBytes(), (byte[]) brole.get(3)); assertTrue(brole.get(4) instanceof Long); } @@ -192,8 +193,8 @@ public void run() { Thread.sleep(100); } catch (InterruptedException e) { } - Jedis j = new Jedis(); - j.auth("foobared"); + Jedis j = new Jedis(endpoint.getHostAndPort()); + j.auth(endpoint.getPassword()); for (int i = 0; i < 5; i++) { j.incr("foobared"); } diff --git a/src/test/java/redis/clients/jedis/commands/jedis/FailoverCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/FailoverCommandsTest.java index 20ec8009cd..cd5bd5b025 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/FailoverCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/FailoverCommandsTest.java @@ -6,6 +6,7 @@ import org.junit.Before; import org.junit.Test; +import redis.clients.jedis.EndpointConfig; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.exceptions.JedisDataException; @@ -16,8 +17,8 @@ public class FailoverCommandsTest { private static final int INVALID_PORT = 6000; - private static final HostAndPort node1 = HostAndPorts.getRedisServers().get(9); - private static final HostAndPort node2 = HostAndPorts.getRedisServers().get(10); + private static final EndpointConfig node1 = HostAndPorts.getRedisEndpoint("standalone9"); + private static final EndpointConfig node2 = HostAndPorts.getRedisEndpoint("standalone10-replica-of-standalone9"); private HostAndPort masterAddress; private HostAndPort replicaAddress; @@ -25,19 +26,19 @@ public class FailoverCommandsTest { @Before public void prepare() { String role1, role2; - try (Jedis jedis1 = new Jedis(node1)) { + try (Jedis jedis1 = new Jedis(node1.getHostAndPort(), node1.getClientConfigBuilder().build())) { role1 = (String) jedis1.role().get(0); } - try (Jedis jedis2 = new Jedis(node2)) { + try (Jedis jedis2 = new Jedis(node2.getHostAndPort(), node2.getClientConfigBuilder().build())) { role2 = (String) jedis2.role().get(0); } if ("master".equals(role1) && "slave".equals(role2)) { - masterAddress = node1; - replicaAddress = node2; + masterAddress = node1.getHostAndPort(); + replicaAddress = node2.getHostAndPort(); } else if ("master".equals(role2) && "slave".equals(role1)) { - masterAddress = node2; - replicaAddress = node1; + masterAddress = node2.getHostAndPort(); + replicaAddress = node1.getHostAndPort(); } else { fail(); } diff --git a/src/test/java/redis/clients/jedis/commands/jedis/JedisCommandsTestBase.java b/src/test/java/redis/clients/jedis/commands/jedis/JedisCommandsTestBase.java index 1d40a22b5f..cec36a0866 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/JedisCommandsTestBase.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/JedisCommandsTestBase.java @@ -5,11 +5,7 @@ import org.junit.After; import org.junit.Before; import org.junit.runners.Parameterized.Parameters; -import redis.clients.jedis.DefaultJedisClientConfig; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.HostAndPorts; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.RedisProtocol; +import redis.clients.jedis.*; import redis.clients.jedis.commands.CommandsTestsParameters; public abstract class JedisCommandsTestBase { @@ -25,7 +21,7 @@ public static Collection data() { return CommandsTestsParameters.respVersions(); } - protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(0); + protected static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); protected final RedisProtocol protocol; @@ -46,9 +42,8 @@ public JedisCommandsTestBase(RedisProtocol protocol) { @Before public void setUp() throws Exception { -// jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().timeoutMillis(500).password("foobared").build()); - jedis = new Jedis(hnp, DefaultJedisClientConfig.builder() - .protocol(protocol).timeoutMillis(500).password("foobared").build()); + jedis = new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder() + .protocol(protocol).timeoutMillis(500).build()); jedis.flushAll(); } @@ -58,8 +53,7 @@ public void tearDown() throws Exception { } protected Jedis createJedis() { -// return new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared").build()); - return new Jedis(hnp, DefaultJedisClientConfig.builder() - .protocol(protocol).password("foobared").build()); + return new Jedis(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder() + .protocol(protocol).build()); } } diff --git a/src/test/java/redis/clients/jedis/commands/jedis/MigrateTest.java b/src/test/java/redis/clients/jedis/commands/jedis/MigrateTest.java index 529d64ebbf..9d44302e37 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/MigrateTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/MigrateTest.java @@ -12,9 +12,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.Protocol; -import redis.clients.jedis.RedisProtocol; +import redis.clients.jedis.*; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.params.MigrateParams; @@ -32,9 +30,18 @@ public class MigrateTest extends JedisCommandsTestBase { private Jedis dest; private Jedis destAuth; - private static final String host = hnp.getHost(); - private static final int port = 6386; - private static final int portAuth = hnp.getPort() + 1; + + private static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0-acl"); + + private static final EndpointConfig destEndpoint = HostAndPorts.getRedisEndpoint( + "standalone7-with-lfu-policy"); + + private static final EndpointConfig destEndpointWithAuth = HostAndPorts.getRedisEndpoint( + "standalone1"); + + private static final String host = destEndpoint.getHost(); + private static final int port = destEndpoint.getPort(); + private static final int portAuth = destEndpointWithAuth.getPort(); private static final int db = 2; private static final int dbAuth = 3; private static final int timeout = Protocol.DEFAULT_TIMEOUT; @@ -52,8 +59,8 @@ public void setUp() throws Exception { dest.flushAll(); dest.select(db); - destAuth = new Jedis(host, portAuth, 500); - destAuth.auth("foobared"); + destAuth = new Jedis(destEndpointWithAuth.getHostAndPort(), + destEndpointWithAuth.getClientConfigBuilder().build()); destAuth.flushAll(); destAuth.select(dbAuth); } @@ -150,14 +157,14 @@ public void migrateCopyReplace() { @Test public void migrateAuth() { jedis.set("foo", "bar"); - assertEquals("OK", - jedis.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), "foo")); + assertEquals("OK", jedis.migrate(host, portAuth, dbAuth, timeout, + new MigrateParams().auth(destEndpointWithAuth.getPassword()), "foo")); assertEquals("bar", destAuth.get("foo")); assertNull(jedis.get("foo")); jedis.set(bfoo, bbar); - assertEquals("OK", - jedis.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), bfoo)); + assertEquals("OK", jedis.migrate(host, portAuth, dbAuth, timeout, + new MigrateParams().auth(destEndpointWithAuth.getPassword()), bfoo)); assertArrayEquals(bbar, destAuth.get(bfoo)); assertNull(jedis.get(bfoo)); } @@ -165,15 +172,15 @@ public void migrateAuth() { @Test public void migrateAuth2() { destAuth.set("foo", "bar"); - assertEquals("OK", destAuth.migrate(host, hnp.getPort(), 0, timeout, - new MigrateParams().auth2("acljedis", "fizzbuzz"), "foo")); + assertEquals("OK", destAuth.migrate(host, endpoint.getPort(), 0, timeout, + new MigrateParams().auth2(endpoint.getUsername(), endpoint.getPassword()), "foo")); assertEquals("bar", jedis.get("foo")); assertNull(destAuth.get("foo")); // binary dest.set(bfoo1, bbar1); - assertEquals("OK", dest.migrate(host, hnp.getPort(), 0, timeout, - new MigrateParams().auth2("acljedis", "fizzbuzz"), bfoo1)); + assertEquals("OK", dest.migrate(host, endpoint.getPort(), 0, timeout, + new MigrateParams().auth2(endpoint.getUsername(), endpoint.getPassword()), bfoo1)); assertArrayEquals(bbar1, jedis.get(bfoo1)); assertNull(dest.get(bfoo1)); } @@ -182,10 +189,8 @@ public void migrateAuth2() { public void migrateCopyReplaceAuth() { jedis.set("foo", "bar1"); destAuth.set("foo", "bar2"); - assertEquals( - "OK", - jedis.migrate(host, portAuth, dbAuth, timeout, - new MigrateParams().copy().replace().auth("foobared"), "foo")); + assertEquals("OK", jedis.migrate(host, portAuth, dbAuth, timeout, + new MigrateParams().copy().replace().auth(destEndpointWithAuth.getPassword()), "foo")); assertEquals("bar1", destAuth.get("foo")); assertEquals("bar1", jedis.get("foo")); @@ -194,7 +199,7 @@ public void migrateCopyReplaceAuth() { assertEquals( "OK", jedis.migrate(host, portAuth, dbAuth, timeout, - new MigrateParams().copy().replace().auth("foobared"), bfoo)); + new MigrateParams().copy().replace().auth(destEndpointWithAuth.getPassword()), bfoo)); assertArrayEquals(bbar1, destAuth.get(bfoo)); assertArrayEquals(bbar1, jedis.get(bfoo)); } diff --git a/src/test/java/redis/clients/jedis/commands/jedis/ObjectCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/ObjectCommandsTest.java index 0f4cf70b15..a62cbea52a 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/ObjectCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/ObjectCommandsTest.java @@ -13,11 +13,8 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.RedisProtocol; +import redis.clients.jedis.*; import redis.clients.jedis.exceptions.JedisDataException; -import redis.clients.jedis.HostAndPorts; import redis.clients.jedis.util.SafeEncoder; @RunWith(Parameterized.class) @@ -25,7 +22,7 @@ public class ObjectCommandsTest extends JedisCommandsTestBase { private final String key = "mylist"; private final byte[] binaryKey = SafeEncoder.encode(key); - private final HostAndPort lfuHnp = HostAndPorts.getRedisServers().get(7); + private final EndpointConfig lfuEndpoint = HostAndPorts.getRedisEndpoint("standalone7-with-lfu-policy"); private Jedis lfuJedis; public ObjectCommandsTest(RedisProtocol protocol) { @@ -37,7 +34,8 @@ public ObjectCommandsTest(RedisProtocol protocol) { public void setUp() throws Exception { super.setUp(); - lfuJedis = new Jedis(lfuHnp.getHost(), lfuHnp.getPort(), 500); + lfuJedis = new Jedis(lfuEndpoint.getHostAndPort(), + lfuEndpoint.getClientConfigBuilder().build()); lfuJedis.connect(); lfuJedis.flushAll(); } diff --git a/src/test/java/redis/clients/jedis/commands/jedis/SentinelCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/SentinelCommandsTest.java index babaf28a10..48fa43e4e6 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/SentinelCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/SentinelCommandsTest.java @@ -22,7 +22,9 @@ public class SentinelCommandsTest { protected static final String MASTER_NAME = "mymaster"; protected static final List nodes = - Arrays.asList(HostAndPorts.getRedisServers().get(2), HostAndPorts.getRedisServers().get(3)); + Arrays.asList( + HostAndPorts.getRedisEndpoint("standalone2-primary").getHostAndPort(), + HostAndPorts.getRedisEndpoint("standalone3-replica-of-standalone2").getHostAndPort()); protected static final Set nodesPorts = nodes.stream() .map(HostAndPort::getPort).map(String::valueOf).collect(Collectors.toSet()); diff --git a/src/test/java/redis/clients/jedis/commands/jedis/TransactionCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/TransactionCommandsTest.java index 0d06fb3b86..aeb9ab4e4f 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/TransactionCommandsTest.java @@ -20,7 +20,6 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; -import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; @@ -50,7 +49,8 @@ public TransactionCommandsTest(RedisProtocol protocol) { public void setUp() throws Exception { super.setUp(); - nj = new Jedis(hnp, DefaultJedisClientConfig.builder().timeoutMillis(500).password("foobared").build()); + nj = new Jedis(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().timeoutMillis(500).build()); } @After @@ -352,7 +352,6 @@ public void testResetStateWhenInWatch() { @Test public void testResetStateWithFullyExecutedTransaction() { Jedis jedis2 = createJedis(); - jedis2.auth("foobared"); Transaction t = jedis2.multi(); t.set("mykey", "foo"); @@ -369,8 +368,8 @@ public void testResetStateWithFullyExecutedTransaction() { @Test public void testCloseable() { // we need to test with fresh instance of Jedis - Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500); - jedis2.auth("foobared"); + Jedis jedis2 = new Jedis(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().timeoutMillis(500).build());; Transaction transaction = jedis2.multi(); transaction.set("a", "1"); diff --git a/src/test/java/redis/clients/jedis/commands/unified/pooled/PooledCommandsTestHelper.java b/src/test/java/redis/clients/jedis/commands/unified/pooled/PooledCommandsTestHelper.java index 82c84f8890..ab69c57f4e 100644 --- a/src/test/java/redis/clients/jedis/commands/unified/pooled/PooledCommandsTestHelper.java +++ b/src/test/java/redis/clients/jedis/commands/unified/pooled/PooledCommandsTestHelper.java @@ -1,24 +1,19 @@ package redis.clients.jedis.commands.unified.pooled; -import redis.clients.jedis.DefaultJedisClientConfig; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPooled; -import redis.clients.jedis.HostAndPorts; -import redis.clients.jedis.RedisProtocol; +import redis.clients.jedis.*; public class PooledCommandsTestHelper { - private static final HostAndPort nodeInfo = HostAndPorts.getRedisServers().get(0); + private static final EndpointConfig nodeInfo = HostAndPorts.getRedisEndpoint("standalone0"); public static JedisPooled getPooled(RedisProtocol redisProtocol) { - return new JedisPooled(nodeInfo, DefaultJedisClientConfig.builder() - .protocol(redisProtocol).password("foobared").build()); + return new JedisPooled(nodeInfo.getHostAndPort(), nodeInfo.getClientConfigBuilder() + .protocol(redisProtocol).build()); } public static void clearData() { - try (Jedis node = new Jedis(nodeInfo)) { - node.auth("foobared"); + try (Jedis node = new Jedis(nodeInfo.getHostAndPort())) { + node.auth(nodeInfo.getPassword()); node.flushAll(); } } diff --git a/src/test/java/redis/clients/jedis/misc/AutomaticFailoverTest.java b/src/test/java/redis/clients/jedis/misc/AutomaticFailoverTest.java index c6b25d764b..46c40fae6a 100644 --- a/src/test/java/redis/clients/jedis/misc/AutomaticFailoverTest.java +++ b/src/test/java/redis/clients/jedis/misc/AutomaticFailoverTest.java @@ -16,15 +16,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.AbstractPipeline; -import redis.clients.jedis.AbstractTransaction; -import redis.clients.jedis.DefaultJedisClientConfig; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.HostAndPorts; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisClientConfig; -import redis.clients.jedis.MultiClusterClientConfig; -import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.*; import redis.clients.jedis.exceptions.JedisAccessControlException; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.providers.MultiClusterPooledConnectionProvider; @@ -34,9 +26,9 @@ public class AutomaticFailoverTest { private static final Logger log = LoggerFactory.getLogger(AutomaticFailoverTest.class); - private final HostAndPort hostPort_1 = new HostAndPort(HostAndPorts.getRedisServers().get(0).getHost(), 6378); - private final HostAndPort hostPort_1_2 = HostAndPorts.getRedisServers().get(0); - private final HostAndPort hostPort_2 = HostAndPorts.getRedisServers().get(7); + private final HostAndPort hostPortWithFailure = new HostAndPort(HostAndPorts.getRedisEndpoint("standalone0").getHost(), 6378); + private final EndpointConfig endpointForAuthFailure = HostAndPorts.getRedisEndpoint("standalone0"); + private final EndpointConfig workingEndpoint = HostAndPorts.getRedisEndpoint("standalone7-with-lfu-policy"); private final JedisClientConfig clientConfig = DefaultJedisClientConfig.builder().build(); @@ -51,7 +43,8 @@ private List getClusterConfigs( @Before public void setUp() { - jedis2 = new Jedis(hostPort_2, clientConfig); + jedis2 = new Jedis(workingEndpoint.getHostAndPort(), + workingEndpoint.getClientConfigBuilder().build()); jedis2.flushAll(); } @@ -63,7 +56,7 @@ public void cleanUp() { @Test public void pipelineWithSwitch() { MultiClusterPooledConnectionProvider provider = new MultiClusterPooledConnectionProvider( - new MultiClusterClientConfig.Builder(getClusterConfigs(clientConfig, hostPort_1, hostPort_2)).build()); + new MultiClusterClientConfig.Builder(getClusterConfigs(clientConfig, hostPortWithFailure, workingEndpoint.getHostAndPort())).build()); try (UnifiedJedis client = new UnifiedJedis(provider)) { AbstractPipeline pipe = client.pipelined(); @@ -80,7 +73,7 @@ public void pipelineWithSwitch() { @Test public void transactionWithSwitch() { MultiClusterPooledConnectionProvider provider = new MultiClusterPooledConnectionProvider( - new MultiClusterClientConfig.Builder(getClusterConfigs(clientConfig, hostPort_1, hostPort_2)).build()); + new MultiClusterClientConfig.Builder(getClusterConfigs(clientConfig, hostPortWithFailure, workingEndpoint.getHostAndPort())).build()); try (UnifiedJedis client = new UnifiedJedis(provider)) { AbstractTransaction tx = client.multi(); @@ -100,7 +93,7 @@ public void commandFailover() { int slidingWindowSize = 10; MultiClusterClientConfig.Builder builder = new MultiClusterClientConfig.Builder( - getClusterConfigs(clientConfig, hostPort_1, hostPort_2)) + getClusterConfigs(clientConfig, hostPortWithFailure, workingEndpoint.getHostAndPort())) .circuitBreakerSlidingWindowMinCalls(slidingWindowMinCalls) .circuitBreakerSlidingWindowSize(slidingWindowSize); @@ -138,7 +131,7 @@ public void pipelineFailover() { int slidingWindowSize = 10; MultiClusterClientConfig.Builder builder = new MultiClusterClientConfig.Builder( - getClusterConfigs(clientConfig, hostPort_1, hostPort_2)) + getClusterConfigs(clientConfig, hostPortWithFailure, workingEndpoint.getHostAndPort())) .circuitBreakerSlidingWindowMinCalls(slidingWindowMinCalls) .circuitBreakerSlidingWindowSize(slidingWindowSize) .fallbackExceptionList(Arrays.asList(JedisConnectionException.class)); @@ -171,7 +164,7 @@ public void failoverFromAuthError() { int slidingWindowSize = 10; MultiClusterClientConfig.Builder builder = new MultiClusterClientConfig.Builder( - getClusterConfigs(clientConfig, hostPort_1_2, hostPort_2)) + getClusterConfigs(clientConfig, endpointForAuthFailure.getHostAndPort(), workingEndpoint.getHostAndPort())) .circuitBreakerSlidingWindowMinCalls(slidingWindowMinCalls) .circuitBreakerSlidingWindowSize(slidingWindowSize) .fallbackExceptionList(Arrays.asList(JedisAccessControlException.class)); diff --git a/src/test/java/redis/clients/jedis/misc/ClusterInitErrorTest.java b/src/test/java/redis/clients/jedis/misc/ClusterInitErrorTest.java index 619c08cead..76998dc808 100644 --- a/src/test/java/redis/clients/jedis/misc/ClusterInitErrorTest.java +++ b/src/test/java/redis/clients/jedis/misc/ClusterInitErrorTest.java @@ -4,7 +4,7 @@ import org.junit.After; import org.junit.Assert; import org.junit.Test; -import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.EndpointConfig; import redis.clients.jedis.HostAndPorts; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.exceptions.JedisClusterOperationException; @@ -21,9 +21,10 @@ public void cleanUp() { @Test(expected = JedisClusterOperationException.class) public void initError() { Assert.assertNull(System.getProperty(INIT_NO_ERROR_PROPERTY)); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); try (JedisCluster cluster = new JedisCluster( - Collections.singleton(HostAndPorts.getRedisServers().get(0)), - DefaultJedisClientConfig.builder().password("foobared").build())) { + Collections.singleton(endpoint.getHostAndPort()), + endpoint.getClientConfigBuilder().build())) { throw new IllegalStateException("should not reach here"); } } @@ -31,9 +32,10 @@ public void initError() { @Test public void initNoError() { System.setProperty(INIT_NO_ERROR_PROPERTY, ""); + EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0"); try (JedisCluster cluster = new JedisCluster( - Collections.singleton(HostAndPorts.getRedisServers().get(0)), - DefaultJedisClientConfig.builder().password("foobared").build())) { + Collections.singleton(endpoint.getHostAndPort()), + endpoint.getClientConfigBuilder().build())) { Assert.assertThrows(JedisClusterOperationException.class, () -> cluster.get("foo")); } } diff --git a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java index d094d26b4f..1f2c7576f6 100644 --- a/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java +++ b/src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java @@ -17,8 +17,8 @@ */ public class MultiClusterPooledConnectionProviderTest { - private final HostAndPort hostAndPort1 = HostAndPorts.getRedisServers().get(0); - private final HostAndPort hostAndPort2 = HostAndPorts.getRedisServers().get(1); + private final EndpointConfig endpointStandalone0 = HostAndPorts.getRedisEndpoint("standalone0"); + private final EndpointConfig endpointStandalone1 = HostAndPorts.getRedisEndpoint("standalone1"); private MultiClusterPooledConnectionProvider provider; @@ -26,8 +26,8 @@ public class MultiClusterPooledConnectionProviderTest { public void setUp() { ClusterConfig[] clusterConfigs = new ClusterConfig[2]; - clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build()); - clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build()); + clusterConfigs[0] = new ClusterConfig(endpointStandalone0.getHostAndPort(), endpointStandalone0.getClientConfigBuilder().build()); + clusterConfigs[1] = new ClusterConfig(endpointStandalone1.getHostAndPort(), endpointStandalone0.getClientConfigBuilder().build()); provider = new MultiClusterPooledConnectionProvider(new MultiClusterClientConfig.Builder(clusterConfigs).build()); } @@ -138,8 +138,8 @@ public void testConnectionPoolConfigApplied() { poolConfig.setMaxIdle(4); poolConfig.setMinIdle(1); ClusterConfig[] clusterConfigs = new ClusterConfig[2]; - clusterConfigs[0] = new ClusterConfig(hostAndPort1, DefaultJedisClientConfig.builder().build(), poolConfig); - clusterConfigs[1] = new ClusterConfig(hostAndPort2, DefaultJedisClientConfig.builder().build(), poolConfig); + clusterConfigs[0] = new ClusterConfig(endpointStandalone0.getHostAndPort(), endpointStandalone0.getClientConfigBuilder().build(), poolConfig); + clusterConfigs[1] = new ClusterConfig(endpointStandalone1.getHostAndPort(), endpointStandalone0.getClientConfigBuilder().build(), poolConfig); try (MultiClusterPooledConnectionProvider customProvider = new MultiClusterPooledConnectionProvider( new MultiClusterClientConfig.Builder(clusterConfigs).build())) { MultiClusterPooledConnectionProvider.Cluster activeCluster = customProvider.getCluster(); diff --git a/src/test/java/redis/clients/jedis/util/RedisVersionUtil.java b/src/test/java/redis/clients/jedis/util/RedisVersionUtil.java index 8b125ca7d3..fed3055b4f 100644 --- a/src/test/java/redis/clients/jedis/util/RedisVersionUtil.java +++ b/src/test/java/redis/clients/jedis/util/RedisVersionUtil.java @@ -1,14 +1,15 @@ package redis.clients.jedis.util; +import redis.clients.jedis.EndpointConfig; import redis.clients.jedis.Jedis; public class RedisVersionUtil { - public static Integer getRedisMajorVersionNumber() { + public static Integer getRedisMajorVersionNumber(EndpointConfig endpoint) { String completeVersion = null; - try (Jedis jedis = new Jedis()) { - jedis.auth("foobared"); + try (Jedis jedis = new Jedis(endpoint.getHostAndPort(), + endpoint.getClientConfigBuilder().build())) { String info = jedis.info("server"); String[] splitted = info.split("\\s+|:"); for (int i = 0; i < splitted.length; i++) { @@ -25,7 +26,7 @@ public static Integer getRedisMajorVersionNumber() { return Integer.parseInt(completeVersion.substring(0, completeVersion.indexOf("."))); } - public static boolean checkRedisMajorVersionNumber(int minVersion) { - return getRedisMajorVersionNumber() >= minVersion; + public static boolean checkRedisMajorVersionNumber(int minVersion, EndpointConfig endpoint) { + return getRedisMajorVersionNumber(endpoint) >= minVersion; } } diff --git a/src/test/resources/endpoints.json b/src/test/resources/endpoints.json new file mode 100644 index 0000000000..c1d905bfb8 --- /dev/null +++ b/src/test/resources/endpoints.json @@ -0,0 +1,107 @@ +{ + "standalone0": { + "password": "foobared", + "tls": false, + "endpoints": [ + "redis://localhost:6379" + ] + }, + "standalone0-tls": { + "username": "default", + "password": "foobared", + "tls": true, + "endpoints": [ + "rediss://localhost:6390" + ] + }, + "standalone0-acl": { + "username": "acljedis", + "password": "fizzbuzz", + "tls": false, + "endpoints": [ + "redis://localhost:6379" + ] + }, + "standalone0-acl-tls": { + "username": "acljedis", + "password": "fizzbuzz", + "tls": true, + "endpoints": [ + "rediss://localhost:6390" + ] + }, + "standalone1": { + "username": "default", + "password": "foobared", + "tls": false, + "endpoints": [ + "redis://localhost:6380" + ] + }, + "standalone2-primary": { + "username": "default", + "password": "foobared", + "tls": false, + "endpoints": [ + "redis://localhost:6381" + ] + }, + "standalone3-replica-of-standalone2": { + "username": "default", + "password": "foobared", + "tls": false, + "endpoints": [ + "redis://localhost:6382" + ] + }, + "standalone4-replica-of-standalone1": { + "username": "default", + "password": "foobared", + "tls": false, + "endpoints": [ + "redis://localhost:6383" + ] + }, + "standalone5-primary": { + "username": "default", + "password": "foobared", + "tls": false, + "endpoints": [ + "redis://localhost:6384" + ] + }, + "standalone6-replica-of-standalone5": { + "username": "default", + "password": "foobared", + "tls": false, + "endpoints": [ + "redis://localhost:6385" + ] + }, + "standalone7-with-lfu-policy": { + "username": "default", + "password": "foobared", + "tls": false, + "endpoints": [ + "redis://localhost:6386" + ] + }, + "standalone9": { + "tls": false, + "endpoints": [ + "redis://localhost:6388" + ] + }, + "standalone10-replica-of-standalone9": { + "tls": false, + "endpoints": [ + "redis://localhost:6389" + ] + }, + "modules-docker": { + "tls": false, + "endpoints": [ + "redis://localhost:6479" + ] + } +} \ No newline at end of file