Skip to content

Commit

Permalink
Update Migrate tests for Redis 6 and Support MIGRATE with AUTH (#2006)
Browse files Browse the repository at this point in the history
Add support for passing AUTH on migrate
  • Loading branch information
sazzad16 authored and gkorland committed Jul 4, 2019
1 parent d5f7f20 commit 5479c27
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 38 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ appendonly no
slaveof localhost 6384
endef

define REDIS8_CONF
daemonize yes
protected-mode no
port 6386
pidfile /tmp/redis8.pid
logfile /tmp/redis8.log
save ""
appendonly no
endef

# SENTINELS
define REDIS_SENTINEL1
port 26379
Expand Down Expand Up @@ -242,6 +252,7 @@ export REDIS4_CONF
export REDIS5_CONF
export REDIS6_CONF
export REDIS7_CONF
export REDIS8_CONF
export REDIS_SENTINEL1
export REDIS_SENTINEL2
export REDIS_SENTINEL3
Expand All @@ -268,6 +279,7 @@ start: stunnel cleanup
echo "$$REDIS5_CONF" | redis-server -
echo "$$REDIS6_CONF" | redis-server -
echo "$$REDIS7_CONF" | redis-server -
echo "$$REDIS8_CONF" | redis-server -
echo "$$REDIS_SENTINEL1" > /tmp/sentinel1.conf && redis-server /tmp/sentinel1.conf --sentinel
@sleep 0.5
echo "$$REDIS_SENTINEL2" > /tmp/sentinel2.conf && redis-server /tmp/sentinel2.conf --sentinel
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/redis/clients/jedis/params/MigrateParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class MigrateParams extends Params {

private static final String COPY = "COPY";
private static final String REPLACE = "REPLACE";
private static final String AUTH = "AUTH";

public MigrateParams() {
}
Expand All @@ -21,4 +22,9 @@ public MigrateParams replace() {
addParam(REPLACE);
return this;
}

public MigrateParams auth(String password) {
addParam(AUTH, password);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private HostAndPortUtil(){
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));

sentinelHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT));
sentinelHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import redis.clients.jedis.tests.HostAndPortUtil;

public abstract class JedisCommandTestBase {
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
protected static final HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);

protected Jedis jedis;

Expand Down
144 changes: 107 additions & 37 deletions src/test/java/redis/clients/jedis/tests/commands/MigrateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,67 +27,152 @@ public class MigrateTest extends JedisCommandTestBase {
private static final byte[] bbar3 = {0x09, 0x00, 0x03};

private Jedis dest;
private String host;
private int port;
private final int destDB = 2;
private final int timeout = Protocol.DEFAULT_TIMEOUT;
private String pass;
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 int db = 2;
private static final int dbAuth = 3;
private static final int timeout = Protocol.DEFAULT_TIMEOUT;

@Before
@Override
public void setUp() throws Exception {
super.setUp();
host = jedis.getClient().getHost();
port = 6380;

dest = new Jedis(host, port, 500);
dest.auth("foobared");
dest.flushAll();
dest.select(db);

dest.select(destDB);
pass = dest.configGet("requirepass").get(1);
dest.configSet("requirepass", "");
destAuth = new Jedis(host, portAuth, 500);
destAuth.auth("foobared");
destAuth.flushAll();
destAuth.select(dbAuth);
}

@After
@Override
public void tearDown() {
dest.configSet("requirepass", pass);
dest.close();
destAuth.close();
super.tearDown();
}

@Test
public void nokey() {
assertEquals("NOKEY", jedis.migrate(host, port, "foo", destDB, timeout));
assertEquals("NOKEY", jedis.migrate(host, port, bfoo, destDB, timeout));
assertEquals("NOKEY", jedis.migrate(host, port, destDB, timeout, new MigrateParams(), "foo1", "foo2", "foo3"));
assertEquals("NOKEY", jedis.migrate(host, port, destDB, timeout, new MigrateParams(), bfoo1, bfoo2, bfoo3));
assertEquals("NOKEY", jedis.migrate(host, port, "foo", db, timeout));
assertEquals("NOKEY", jedis.migrate(host, port, bfoo, db, timeout));
assertEquals("NOKEY", jedis.migrate(host, port, db, timeout, new MigrateParams(), "foo1", "foo2", "foo3"));
assertEquals("NOKEY", jedis.migrate(host, port, db, timeout, new MigrateParams(), bfoo1, bfoo2, bfoo3));
}

@Test
public void migrate() {
jedis.set("foo", "bar");
assertEquals("OK", jedis.migrate(host, port, "foo", destDB, timeout));
assertEquals("OK", jedis.migrate(host, port, "foo", db, timeout));
assertEquals("bar", dest.get("foo"));
assertNull(jedis.get("foo"));

jedis.set(bfoo, bbar);
assertEquals("OK", jedis.migrate(host, port, bfoo, destDB, timeout));
assertEquals("OK", jedis.migrate(host, port, bfoo, db, timeout));
assertArrayEquals(bbar, dest.get(bfoo));
assertNull(jedis.get(bfoo));
}

@Test
public void migrateEmptyParams() {
jedis.set("foo", "bar");
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams(), "foo"));
assertEquals("bar", dest.get("foo"));
assertNull(jedis.get("foo"));

jedis.set(bfoo, bbar);
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams(), bfoo));
assertArrayEquals(bbar, dest.get(bfoo));
assertNull(jedis.get(bfoo));
}

@Test
public void migrateCopy() {
jedis.set("foo", "bar");
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams().copy(), "foo"));
assertEquals("bar", dest.get("foo"));
assertEquals("bar", jedis.get("foo"));

jedis.set(bfoo, bbar);
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams().copy(), bfoo));
assertArrayEquals(bbar, dest.get(bfoo));
assertArrayEquals(bbar, jedis.get(bfoo));
}

@Test
public void migrateReplace() {
jedis.set("foo", "bar1");
dest.set("foo", "bar2");
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams().replace(), "foo"));
assertEquals("bar1", dest.get("foo"));
assertNull(jedis.get("foo"));

jedis.set(bfoo, bbar1);
dest.set(bfoo, bbar2);
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams().replace(), bfoo));
assertArrayEquals(bbar1, dest.get(bfoo));
assertNull(jedis.get(bfoo));
}

@Test
public void migrateCopyReplace() {
jedis.set("foo", "bar1");
dest.set("foo", "bar2");
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams().copy().replace(), "foo"));
assertEquals("bar1", dest.get("foo"));
assertEquals("bar1", jedis.get("foo"));

jedis.set(bfoo, bbar1);
dest.set(bfoo, bbar2);
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams().copy().replace(), bfoo));
assertArrayEquals(bbar1, dest.get(bfoo));
assertArrayEquals(bbar1, jedis.get(bfoo));
}

@Test
public void migrateAuth() {
jedis.set("foo", "bar");
assertEquals("OK", jedis.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), "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));
assertArrayEquals(bbar, destAuth.get(bfoo));
assertNull(jedis.get(bfoo));
}

@Test
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("bar1", destAuth.get("foo"));
assertEquals("bar1", jedis.get("foo"));

jedis.set(bfoo, bbar1);
destAuth.set(bfoo, bbar2);
assertEquals("OK", jedis.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().copy().replace().auth("foobared"), bfoo));
assertArrayEquals(bbar1, destAuth.get(bfoo));
assertArrayEquals(bbar1, jedis.get(bfoo));
}

@Test
public void migrateMulti() {
jedis.mset("foo1", "bar1", "foo2", "bar2", "foo3", "bar3");
assertEquals("OK", jedis.migrate(host, port, destDB, timeout, new MigrateParams(), "foo1", "foo2", "foo3"));
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams(), "foo1", "foo2", "foo3"));
assertEquals("bar1", dest.get("foo1"));
assertEquals("bar2", dest.get("foo2"));
assertEquals("bar3", dest.get("foo3"));

jedis.mset(bfoo1, bbar1, bfoo2, bbar2, bfoo3, bbar3);
assertEquals("OK", jedis.migrate(host, port, destDB, timeout, new MigrateParams(), bfoo1, bfoo2, bfoo3));
assertEquals("OK", jedis.migrate(host, port, db, timeout, new MigrateParams(), bfoo1, bfoo2, bfoo3));
assertArrayEquals(bbar1, dest.get(bfoo1));
assertArrayEquals(bbar2, dest.get(bfoo2));
assertArrayEquals(bbar3, dest.get(bfoo3));
Expand All @@ -98,7 +183,7 @@ public void migrateConflict() {
jedis.mset("foo1", "bar1", "foo2", "bar2", "foo3", "bar3");
dest.set("foo2", "bar");
try {
jedis.migrate(host, port, destDB, timeout, new MigrateParams(), "foo1", "foo2", "foo3");
jedis.migrate(host, port, db, timeout, new MigrateParams(), "foo1", "foo2", "foo3");
fail("Should get BUSYKEY error");
} catch(JedisDataException jde) {
assertTrue(jde.getMessage().contains("BUSYKEY"));
Expand All @@ -110,7 +195,7 @@ public void migrateConflict() {
jedis.mset(bfoo1, bbar1, bfoo2, bbar2, bfoo3, bbar3);
dest.set(bfoo2, bbar);
try {
jedis.migrate(host, port, destDB, timeout, new MigrateParams(), bfoo1, bfoo2, bfoo3);
jedis.migrate(host, port, db, timeout, new MigrateParams(), bfoo1, bfoo2, bfoo3);
fail("Should get BUSYKEY error");
} catch(JedisDataException jde) {
assertTrue(jde.getMessage().contains("BUSYKEY"));
Expand All @@ -120,19 +205,4 @@ public void migrateConflict() {
assertArrayEquals(bbar3, dest.get(bfoo3));
}

@Test
public void migrateCopyReplace() {
jedis.set("foo", "bar1");
dest.set("foo", "bar2");
assertEquals("OK", jedis.migrate(host, port, destDB, timeout, new MigrateParams().copy().replace(), "foo"));
assertEquals("bar1", jedis.get("foo"));
assertEquals("bar1", dest.get("foo"));

jedis.set(bfoo, bbar1);
dest.set(bfoo, bbar2);
assertEquals("OK", jedis.migrate(host, port, destDB, timeout, new MigrateParams().copy().replace(), bfoo));
assertArrayEquals(bbar1, jedis.get(bfoo));
assertArrayEquals(bbar1, dest.get(bfoo));
}

}

0 comments on commit 5479c27

Please sign in to comment.