diff --git a/src/test/scala/com/redis/PipelineSpec.scala b/src/test/scala/com/redis/PipelineSpec.scala index b8a4c87..6d98325 100644 --- a/src/test/scala/com/redis/PipelineSpec.scala +++ b/src/test/scala/com/redis/PipelineSpec.scala @@ -364,3 +364,55 @@ class PipelineSpec extends AnyFunSpec } } } + +class SecureBatchedPipelineSpec extends AnyFunSpec + with Matchers + with IntSpec + with Inside { + + override protected lazy val r: RedisClient = + new RedisClient(redisContainerHost, redisContainerPort) + + override val redisPassword = Some("antirez") + + private def shutdown(client: RedisClient) { + client.disconnect + client.close() + } + + describe("a redis client in batch mode connecting to a password-protected redis instance") { + it("should be able to connect to the instance") { + val client = new RedisClient(redisContainerHost, redisContainerPort, secret = redisPassword, + batch = RedisClient.BATCH) + + noException should be thrownBy { + val res = client.batchedPipeline(List(() => { client.ping })) + res.get should contain only ("PONG") + } + + shutdown(client) + } + + it("should be rejected when it is not initialized with a password") { + val client = new RedisClient(redisContainerHost, redisContainerPort, secret = None, + batch = RedisClient.BATCH) + + an[Exception] shouldBe thrownBy { + client.batchedPipeline(List(() => { client.ping })) + } + + shutdown(client) + } + + it("should be rejected when it is initialized with an incorrect password") { + val client = new RedisClient(redisContainerHost, redisContainerPort, secret = Some("WRONG"), + batch = RedisClient.BATCH) + + an[Exception] shouldBe thrownBy { + client.batchedPipeline(List(() => { client.ping })) + } + + shutdown(client) + } + } +} diff --git a/src/test/scala/com/redis/PoolSpec.scala b/src/test/scala/com/redis/PoolSpec.scala index fa51190..e885c0c 100644 --- a/src/test/scala/com/redis/PoolSpec.scala +++ b/src/test/scala/com/redis/PoolSpec.scala @@ -8,12 +8,13 @@ import org.scalatest.matchers.should.Matchers import scala.concurrent._ import scala.concurrent.duration._ -class PoolSpec extends AnyFunSpec +abstract class BasePoolSpec extends AnyFunSpec with Matchers with BeforeAndAfterEach with RedisDocker { - implicit lazy val clients: RedisClientPool = new RedisClientPool(redisContainerHost, redisContainerPort) + implicit lazy val clients: RedisClientPool = + new RedisClientPool(redisContainerHost, redisContainerPort, secret = redisPassword) override protected def beforeEach(): Unit = { super.beforeEach() @@ -32,6 +33,10 @@ class PoolSpec extends AnyFunSpec clients.close() super.afterAll() } +} + +trait PoolLoadTesting { + self: BasePoolSpec => def lp(msgs: List[String]) = { clients.withClient { @@ -63,6 +68,9 @@ class PoolSpec extends AnyFunSpec } } } +} + +class PoolSpec extends BasePoolSpec with PoolLoadTesting { describe("pool test") { it("should distribute work amongst the clients") { @@ -127,3 +135,38 @@ class PoolSpec extends AnyFunSpec } } } + +class SecurePoolSpec extends BasePoolSpec { + + override val redisPassword = Option("mayonaise") + + describe("redis pools with authentication") { + it("should be able to connect as normal") { + val response = clients.withClient { client => + client.ping + } + response should equal(Some("PONG")) + } + } + + private def shutdown(client: RedisClient) = { + client.disconnect + client.close() + } + + // sanity check that the password set in the whisk docker container is working + describe("ad-hoc clients connecting to a secure redis server") { + it("should be rejected for no password") { + val client = new RedisClient(redisContainerHost, redisContainerPort) + an[Exception] shouldBe thrownBy(client.ping) + shutdown(client) + } + + it("should be rejected for wrong password") { + val client = new RedisClient(redisContainerHost, redisContainerPort, secret = Some("WRONG")) + an[Exception] shouldBe thrownBy(client.ping) + shutdown(client) + } + } +} + diff --git a/src/test/scala/com/redis/common/RedisDocker.scala b/src/test/scala/com/redis/common/RedisDocker.scala index e03df81..6ed414b 100644 --- a/src/test/scala/com/redis/common/RedisDocker.scala +++ b/src/test/scala/com/redis/common/RedisDocker.scala @@ -54,8 +54,12 @@ trait RedisContainer extends DockerKit with DockerTestKit with DockerKitDockerJa protected val redisContainerHost: String = "localhost" protected val redisPort: Int = 6379 + protected val redisPassword: Option[String] = None - protected def baseContainer(name: Option[String]) = DockerContainer("redis:latest", name=name) + // just password supported for now + private def args = redisPassword.map(password => Seq("--requirepass", password)) + + protected def baseContainer(name: Option[String]) = DockerContainer("redis:latest", name=name, command=args) protected def createContainer(name: Option[String] = Some(RandomStringUtils.randomAlphabetic(10)), ports: Map[Int, Int] = Map.empty): DockerContainer = {