From af17d83457a5c0db376df7687027738c35915c39 Mon Sep 17 00:00:00 2001 From: Kyle Florence Date: Tue, 9 Feb 2021 16:27:35 -0600 Subject: [PATCH] Allow configuring of ports in ServiceTest.startServer #3168 #1584 --- .../testkit/CassandraTestServer.scala | 4 +- .../internal/testkit/TestkitSslSetup.scala | 10 ++- .../lagom/scaladsl/testkit/ServiceTest.scala | 75 ++++++++++++++++--- 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/testkit/core/src/main/scala/com/lightbend/lagom/internal/testkit/CassandraTestServer.scala b/testkit/core/src/main/scala/com/lightbend/lagom/internal/testkit/CassandraTestServer.scala index 1999926b99..ac63350c61 100644 --- a/testkit/core/src/main/scala/com/lightbend/lagom/internal/testkit/CassandraTestServer.scala +++ b/testkit/core/src/main/scala/com/lightbend/lagom/internal/testkit/CassandraTestServer.scala @@ -21,7 +21,7 @@ private[lagom] object CassandraTestServer { private lazy val log = Logger(getClass) - def run(cassandraDirectoryPrefix: String, lifecycle: ApplicationLifecycle): Int = { + def run(cassandraDirectoryPrefix: String, lifecycle: ApplicationLifecycle, port: Int): Int = { val cassandraDirectory = Files.createTempDirectory(cassandraDirectoryPrefix) @@ -40,7 +40,7 @@ private[lagom] object CassandraTestServer { cassandraDirectory.toFile, LagomTestConfigResource, clean = false, - port = 0, + port = port, CassandraLauncher.classpathForResources(LagomTestConfigResource) ) diff --git a/testkit/core/src/main/scala/com/lightbend/lagom/internal/testkit/TestkitSslSetup.scala b/testkit/core/src/main/scala/com/lightbend/lagom/internal/testkit/TestkitSslSetup.scala index bb2fc689e3..1aa93a5986 100644 --- a/testkit/core/src/main/scala/com/lightbend/lagom/internal/testkit/TestkitSslSetup.scala +++ b/testkit/core/src/main/scala/com/lightbend/lagom/internal/testkit/TestkitSslSetup.scala @@ -32,15 +32,17 @@ private[lagom] object TestkitSslSetup { /** * - * @param serverKeyStoreFile keyStore to setup the server - * @param trustStoreFile trustStore for the clients + * @param keyStoreMetadata keyStore to setup the server + * @param trustStoreMetadata trustStore for the clients * @param clientSslContext SSLContext to create SSL clients + * @param sslPort The port to bind to. * @return */ def enabled( keyStoreMetadata: KeyStoreMetadata, trustStoreMetadata: KeyStoreMetadata, - clientSslContext: SSLContext + clientSslContext: SSLContext, + sslPort: Option[Int] ): TestkitSslSetup = { val sslSettings: Map[String, AnyRef] = Map( // See also play/core/server/devmode/LagomDevModeReloadableServer.scala @@ -54,6 +56,6 @@ private[lagom] object TestkitSslSetup { "ssl-config.trustManager.stores.0.type" -> trustStoreMetadata.storeType, "ssl-config.trustManager.stores.0.password" -> String.valueOf(trustStoreMetadata.storePassword) ) - Enabled(Some(0), sslSettings, Some(clientSslContext)) + Enabled(sslPort, sslSettings, Some(clientSslContext)) } } diff --git a/testkit/scaladsl/src/main/scala/com/lightbend/lagom/scaladsl/testkit/ServiceTest.scala b/testkit/scaladsl/src/main/scala/com/lightbend/lagom/scaladsl/testkit/ServiceTest.scala index 7e9d986b2a..ec96c5be2e 100644 --- a/testkit/scaladsl/src/main/scala/com/lightbend/lagom/scaladsl/testkit/ServiceTest.scala +++ b/testkit/scaladsl/src/main/scala/com/lightbend/lagom/scaladsl/testkit/ServiceTest.scala @@ -4,7 +4,6 @@ package com.lightbend.lagom.scaladsl.testkit -import java.io.File import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -56,6 +55,16 @@ object ServiceTest { */ def withCassandra(enabled: Boolean): Setup + /** + * Enable or disable Cassandra on the specified port. + * + * @param enabled True if Cassandra should be enabled, or false if disabled. Enabling Cassandra will also enable the + * cluster. + * @param port The port to assign the Cassandra server to. + * @return A copy of this setup. + */ + def withCassandra(enabled: Boolean, port: Int): Setup + /** * Enable Cassandra. * @@ -114,6 +123,15 @@ object ServiceTest { @ApiMayChange def withSsl(enabled: Boolean): Setup + /** + * Enable or disable SSL on the specified port. + * @param enabled True if the server should bind an HTTP+TLS port, or false if only HTTP should be bound. + * @param port The port to bind to. + * @return A copy of this setup. + */ + @ApiMayChange + def withSsl(enabled: Boolean, port: Int): Setup + /** * Enable the SSL port. * @@ -122,11 +140,31 @@ object ServiceTest { @ApiMayChange def withSsl(): Setup = withSsl(true) + /** + * Sets the HTTP port the server should run on. + * + * By default, a dynamically assigned port will be used. + * + * @param port The port to assign the server to. + * @return A copy of this setup. + */ + def withPort(port: Int): Setup + + /** + * The server HTTP port. + */ + def port: Int + /** * Whether Cassandra is enabled. */ def cassandra: Boolean + /** + * The Cassandra server port. + */ + def cassandraPort: Int + /** * Whether JDBC is enabled. */ @@ -141,17 +179,26 @@ object ServiceTest { * Whether SSL is enabled. */ def ssl: Boolean + + /** + * The server HTTPS port. + */ + def sslPort: Int } private case class SetupImpl( + port: Int = 0, cassandra: Boolean = false, + cassandraPort: Int = 0, jdbc: Boolean = false, cluster: Boolean = false, - ssl: Boolean = false + ssl: Boolean = false, + sslPort: Int = 0 ) extends Setup { - override def withCassandra(enabled: Boolean): Setup = { + override def withCassandra(enabled: Boolean): Setup = withCassandra(enabled, port = 0) + override def withCassandra(enabled: Boolean, port: Int): Setup = { if (enabled) { - copy(cassandra = true, cluster = true) + copy(cassandra = true, cassandraPort = port, cluster = true) } else { copy(cassandra = false) } @@ -172,8 +219,13 @@ object ServiceTest { } } - override def withSsl(enabled: Boolean): Setup = { - copy(ssl = enabled) + override def withSsl(enabled: Boolean): Setup = withSsl(enabled, port = 0) + override def withSsl(enabled: Boolean, port: Int): Setup = { + copy(ssl = enabled, sslPort = port) + } + + override def withPort(port: Int): Setup = { + copy(port = port) } } @@ -287,7 +339,7 @@ object ServiceTest { val now = DateTimeFormatter.ofPattern("yyMMddHHmmssSSS").format(LocalDateTime.now()) val testName = s"ServiceTest_$now" - val cassandraPort = CassandraTestServer.run(testName, lifecycle) + val cassandraPort = CassandraTestServer.run(testName, lifecycle, setup.cassandraPort) cassandraConfigMap(testName, cassandraPort) } else if (setup.jdbc) { @@ -317,7 +369,12 @@ object ServiceTest { val clientSslContext: SSLContext = sslHolder.sslContext // In tests we're using a self-signed certificate so we use the same keyStore for both // the server and the client trustStore. - TestkitSslSetup.enabled(sslHolder.keyStoreMetadata, sslHolder.trustStoreMetadata, clientSslContext) + TestkitSslSetup.enabled( + sslHolder.keyStoreMetadata, + sslHolder.trustStoreMetadata, + clientSslContext, + Some(setup.sslPort) + ) } else { Disabled } @@ -327,7 +384,7 @@ object ServiceTest { Configuration.load(this.getClass.getClassLoader, props, sslSetup.sslSettings, allowMissingApplicationConf = true) val serverConfig: ServerConfig = new ServerConfig( - port = Some(0), + port = Some(setup.port), sslPort = sslSetup.sslPort, mode = lagomApplication.environment.mode, configuration = sslConfig,