Add authentication to Lettuce conn factory and pool

DATAREDIS-202
This commit is contained in:
Jennifer Hickey
2013-06-28 16:50:14 -07:00
parent 29b68e2808
commit 7b75924c40
7 changed files with 392 additions and 113 deletions

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import com.lambdaworks.redis.RedisAsyncConnection;
import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisConnection;
import com.lambdaworks.redis.RedisException;
import com.lambdaworks.redis.pubsub.RedisPubSubConnection;
/**
* Integration test of {@link AuthenticatingRedisClient}. Enable requirepass and
* comment out the @Ignore to run.
*
* @author Jennifer Hickey
*
*/
@Ignore("Redis must have requirepass set to run this test")
public class AuthenticatingRedisClientTests {
private RedisClient client;
@Before
public void setUp() {
client = new AuthenticatingRedisClient("localhost", "foo");
}
@Test
public void connect() {
RedisConnection<String, String> conn = client.connect();
conn.ping();
}
@Test(expected = RedisException.class)
public void connectWithInvalidPassword() {
RedisClient badClient = new AuthenticatingRedisClient("localhost", "notthepassword");
badClient.connect();
}
@Test
public void codecConnect() {
RedisConnection<byte[], byte[]> conn = client.connect(LettuceUtils.CODEC);
conn.ping();
}
@Test
public void connectAsync() {
RedisAsyncConnection<String, String> conn = client.connectAsync();
conn.ping();
}
@Test
public void codecConnectAsync() {
RedisAsyncConnection<byte[], byte[]> conn = client.connectAsync(LettuceUtils.CODEC);
conn.ping();
}
@Test
public void connectPubSub() {
RedisPubSubConnection<String, String> conn = client.connectPubSub();
conn.ping();
}
@Test
public void codecConnectPubSub() {
RedisPubSubConnection<byte[], byte[]> conn = client.connectPubSub(LettuceUtils.CODEC);
conn.ping();
}
}

View File

@@ -21,14 +21,13 @@ import static org.junit.Assert.fail;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.connection.PoolConfig;
import org.springframework.data.redis.connection.PoolException;
import com.lambdaworks.redis.RedisAsyncConnection;
import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisException;
/**
@@ -39,25 +38,19 @@ import com.lambdaworks.redis.RedisException;
*/
public class DefaultLettucePoolTests {
private RedisClient client;
private DefaultLettucePool pool;
@Before
public void setUp() {
this.client = new RedisClient(SettingsUtils.getHost(), SettingsUtils.getPort());
}
@After
public void tearDown() {
if (this.pool != null) {
if(this.pool != null) {
this.pool.destroy();
}
}
@Test
public void testGetResource() {
this.pool = new DefaultLettucePool(client);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
client.ping();
@@ -68,7 +61,8 @@ public class DefaultLettucePoolTests {
Config poolConfig = new Config();
poolConfig.maxActive = 1;
poolConfig.maxWait = 1;
this.pool = new DefaultLettucePool(client, poolConfig, 0);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
try {
@@ -82,14 +76,16 @@ public class DefaultLettucePoolTests {
public void testGetResourceValidate() {
PoolConfig poolConfig = new PoolConfig();
poolConfig.setTestOnBorrow(true);
this.pool = new DefaultLettucePool(client, poolConfig, 0);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
}
@Test(expected = PoolException.class)
public void testGetResourceCreationUnsuccessful() {
this.pool = new DefaultLettucePool(new RedisClient(SettingsUtils.getHost(), 3333));
public void testGetResourceCreationUnsuccessful() throws Exception {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), 3333);
pool.afterPropertiesSet();
pool.getResource();
}
@@ -98,7 +94,8 @@ public class DefaultLettucePoolTests {
Config poolConfig = new Config();
poolConfig.maxActive = 1;
poolConfig.maxWait = 1;
this.pool = new DefaultLettucePool(client);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
pool.returnResource(client);
@@ -110,7 +107,8 @@ public class DefaultLettucePoolTests {
Config poolConfig = new Config();
poolConfig.maxActive = 1;
poolConfig.maxWait = 1;
this.pool = new DefaultLettucePool(client, poolConfig, 0);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
pool.returnBrokenResource(client);
@@ -123,21 +121,46 @@ public class DefaultLettucePoolTests {
}
}
@Test
public void testCreateWithHostAndPort() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
assertNotNull(pool.getResource());
}
@Test
public void testCreateWithDbIndex() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), 1, 65000);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setDatabase(1);
pool.afterPropertiesSet();
assertNotNull(pool.getResource());
}
@Test(expected = PoolException.class)
public void testCreateWithDbIndexInvalid() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), 17, 65000);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setDatabase(17);
pool.afterPropertiesSet();
pool.getResource();
}
@Test(expected = PoolException.class)
public void testCreateWithPasswordNoPassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setPassword("notthepassword");
pool.afterPropertiesSet();
pool.getResource();
}
@Ignore("Redis must have requirepass set to run this test")
@Test
public void testCreatePassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setPassword("foo");
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> conn = pool.getResource();
conn.ping();
}
@Ignore("Redis must have requirepass set to run this test")
@Test(expected = PoolException.class)
public void testCreateInvalidPassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setPassword("bad");
pool.afterPropertiesSet();
pool.getResource();
}
}

View File

@@ -221,8 +221,9 @@ public class LettuceConnectionFactoryTests {
Config poolConfig = new Config();
poolConfig.maxActive = 1;
poolConfig.maxWait = 1;
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(),
poolConfig));
DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.afterPropertiesSet();
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
factory2.afterPropertiesSet();
factory2.createLettuceConnector(false);
try {
@@ -250,4 +251,15 @@ public class LettuceConnectionFactoryTests {
}
Thread.sleep(234234234);
}
@Ignore("Redis must have requirepass set to run this test")
@Test
public void testConnectWithPassword() {
factory.setPassword("foo");
factory.afterPropertiesSet();
RedisConnection conn = factory.getConnection();
// Test shared and dedicated conns
conn.ping();
conn.bLPop(1, "key".getBytes());
}
}

View File

@@ -127,8 +127,10 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra
@Test
public void testClosePooledConnectionWithShared() {
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(new DefaultLettucePool(SettingsUtils.getHost(),
SettingsUtils.getPort()));
DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(),
SettingsUtils.getPort());
pool.afterPropertiesSet();
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
factory2.afterPropertiesSet();
RedisConnection connection = factory2.getConnection();
// Use the connection to make sure the channel is initialized, else nothing happens on close
@@ -144,8 +146,10 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra
@Test
public void testClosePooledConnectionNotShared() {
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(new DefaultLettucePool(SettingsUtils.getHost(),
SettingsUtils.getPort()));
DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(),
SettingsUtils.getPort());
pool.afterPropertiesSet();
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
factory2.setShareNativeConnection(false);
factory2.afterPropertiesSet();
RedisConnection connection = factory2.getConnection();
@@ -178,8 +182,10 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra
@SuppressWarnings("rawtypes")
@Test
public void testCloseReturnBrokenResourceToPool() {
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(new DefaultLettucePool(SettingsUtils.getHost(),
SettingsUtils.getPort()));
DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(),
SettingsUtils.getPort());
pool.afterPropertiesSet();
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
factory2.setShareNativeConnection(false);
factory2.afterPropertiesSet();
RedisConnection connection = factory2.getConnection();
@@ -202,8 +208,10 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra
@Test
public void testSelectNotShared() {
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(new DefaultLettucePool(SettingsUtils.getHost(),
SettingsUtils.getPort()));
DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(),
SettingsUtils.getPort());
pool.afterPropertiesSet();
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
factory2.setShareNativeConnection(false);
factory2.afterPropertiesSet();
RedisConnection connection = factory2.getConnection();