Skip to content

Commit

Permalink
Allow configuring of ports in ServiceTest.startServer #3168 #1584
Browse files Browse the repository at this point in the history
  • Loading branch information
kflorence committed Feb 9, 2021
1 parent e642617 commit af17d83
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -40,7 +40,7 @@ private[lagom] object CassandraTestServer {
cassandraDirectory.toFile,
LagomTestConfigResource,
clean = false,
port = 0,
port = port,
CassandraLauncher.classpathForResources(LagomTestConfigResource)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package com.lightbend.lagom.scaladsl.testkit

import java.io.File
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*/
Expand All @@ -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)
}
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down

0 comments on commit af17d83

Please sign in to comment.