Skip to content

Commit

Permalink
Make packBroker client timeout configurable (#181)
Browse files Browse the repository at this point in the history
* add new setting keys for `pactBrokerClientTimeout` and `sslContextName`

* add missing descriptions for sbt setting keys

* verifier uses separate clients for broker and running verification against
  • Loading branch information
jbwheatley authored Oct 19, 2020
1 parent 160ad01 commit d02df52
Show file tree
Hide file tree
Showing 45 changed files with 881 additions and 887 deletions.
5 changes: 4 additions & 1 deletion example/consumer/pact.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//For publishing to pact-broker test server (these credentials are public knowledge)

import scala.concurrent.duration._

pactBrokerAddress := "https://test.pact.dius.com.au"
pactBrokerCredentials := ("dXfltyFMgNOFZAxr8io9wJ37iUpY42M", "O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1")
pactContractVersion := "0.2.0"
pactContractTags := Seq("example")
pactContractTags := Seq("example")
pactBrokerClientTimeout := 5.seconds
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
"name" : "scala-pact-consumer"
},
"interactions" : [
{
"providerState" : "Results: Bob, Fred, Harry",
"description" : "Fetching results",
"request" : {
"method" : "GET",
"path" : "/results"
},
"response" : {
"status" : 200,
"headers" : {
"Pact" : "modifiedRequest"
},
"body" : {
"count" : 3,
"results" : [
"Bob",
"Fred",
"Harry"
]
}
}
},
{
"description" : "Fetching least secure auth token ever",
"request" : {
Expand Down Expand Up @@ -37,28 +59,6 @@
}
}
}
},
{
"providerState" : "Results: Bob, Fred, Harry",
"description" : "Fetching results",
"request" : {
"method" : "GET",
"path" : "/results"
},
"response" : {
"status" : 200,
"headers" : {
"Pact" : "modifiedRequest"
},
"body" : {
"count" : 3,
"results" : [
"Bob",
"Fred",
"Harry"
]
}
}
}
],
"metadata" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class VerifyContractsSpec extends FunSpec with Matchers with BeforeAndAfterAll {
List(),
None,
//again, these are publicly known creds for a test pact-broker
PactBrokerAuthorization(pactBrokerCredentials = ("dXfltyFMgNOFZAxr8io9wJ37iUpY42M", "O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1"), "")
PactBrokerAuthorization(pactBrokerCredentials = ("dXfltyFMgNOFZAxr8io9wJ37iUpY42M", "O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1"), ""),
None
)
)
.setupProviderState("given") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
"name" : "scala-pact-consumer"
},
"interactions" : [
{
"providerState" : "Results: Bob, Fred, Harry",
"description" : "Fetching results",
"request" : {
"method" : "GET",
"path" : "/results"
},
"response" : {
"status" : 200,
"headers" : {
"Pact" : "modifiedRequest"
},
"body" : {
"count" : 3,
"results" : [
"Bob",
"Fred",
"Harry"
]
}
}
},
{
"description" : "Fetching least secure auth token ever",
"request" : {
Expand Down Expand Up @@ -37,28 +59,6 @@
}
}
}
},
{
"providerState" : "Results: Bob, Fred, Harry",
"description" : "Fetching results",
"request" : {
"method" : "GET",
"path" : "/results"
},
"response" : {
"status" : 200,
"headers" : {
"Pact" : "modifiedRequest"
},
"body" : {
"count" : 3,
"results" : [
"Bob",
"Fred",
"Harry"
]
}
}
}
],
"metadata" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.itv.scalapact.plugin

import cats.effect.IO
import com.itv.scalapact.http._
import com.itv.scalapact.json._
import com.itv.scalapact.plugin.shared._
import com.itv.scalapact.shared.{PactBrokerAuthorization, ProviderStateResult, ScalaPactSettings}
import com.itv.scalapact.shared.{ConsumerVersionSelector, PactBrokerAuthorization, ProviderStateResult, ScalaPactSettings}
import com.itv.scalapact.shared.ProviderStateResult.SetupProviderState
import com.itv.scalapactcore.verifier.Verifier
import sbt.Keys._
import sbt.plugins.JvmPlugin
import sbt.{Def, _}
import complete.DefaultParsers._

import scala.concurrent.duration._
import scala.language.implicitConversions

object ScalaPactPlugin extends AutoPlugin {
Expand Down Expand Up @@ -70,11 +69,17 @@ object ScalaPactPlugin extends AutoPlugin {
"The name and list of tags of the services that consume the service to verify"
)

val consumerVersionSelectors: SettingKey[Seq[(String, Option[String], Option[String], Option[Boolean])]] =
SettingKey[Seq[(String, Option[String], Option[String], Option[Boolean])]]("consumerVersionSelectors", "")
val consumerVersionSelectors: SettingKey[Seq[ConsumerVersionSelector]] =
SettingKey[Seq[ConsumerVersionSelector]](
"consumerVersionSelectors",
"the consumer version selectors to fetch pacts using the `pacts-for-verification` endpoint"
)

val providerVersionTags: SettingKey[Seq[String]] =
SettingKey[Seq[String]]("providerVersionTags", "")
SettingKey[Seq[String]](
"providerVersionTags",
"the tag name(s) for the provider application version that will be published with the verification results"
)

val pactContractVersion: SettingKey[String] =
SettingKey[String](
Expand All @@ -94,6 +99,18 @@ object ScalaPactPlugin extends AutoPlugin {
"Flag to permit publishing of snapshot pact files to pact broker. Default is false."
)

val pactBrokerClientTimeout: SettingKey[Duration] =
SettingKey[Duration](
"pactBrokerClientTimeout",
"The timeout for requests when communicating with the pact broker"
)

val sslContextName: SettingKey[Option[String]] =
SettingKey[Option[String]](
"sslContextName",
"The ssl context to extract from the defined `SslContextMap`"
)

val scalaPactEnv: SettingKey[ScalaPactEnv] =
SettingKey[ScalaPactEnv]("scalaPactEnv", "Settings used to config the running of tasks and commands")

Expand Down Expand Up @@ -124,14 +141,16 @@ object ScalaPactPlugin extends AutoPlugin {
consumerNames := Seq.empty[String],
versionedConsumerNames := Seq.empty[(String, String)],
taggedConsumerNames := Seq.empty[(String, Seq[String])],
consumerVersionSelectors := Seq.empty[(String, Option[String], Option[String], Option[Boolean])],
consumerVersionSelectors := Seq.empty[ConsumerVersionSelector],
providerVersionTags := Seq.empty[String],
pactContractVersion := "",
pactContractTags := Seq.empty[String],
allowSnapshotPublish := false,
scalaPactEnv := ScalaPactEnv.empty,
pactBrokerCredentials := (("", ""): (String, String)),
pactBrokerToken := ""
pactBrokerToken := "",
pactBrokerClientTimeout := 2.seconds,
sslContextName := None
)

@SuppressWarnings(Array("org.wartremover.warts.Any"))
Expand Down Expand Up @@ -167,14 +186,16 @@ object ScalaPactPlugin extends AutoPlugin {
pactContractVersion.value,
allowSnapshotPublish.value,
pactContractTags.value,
PactBrokerAuthorization(pactBrokerCredentials.value, pactBrokerToken.value)
PactBrokerAuthorization(pactBrokerCredentials.value, pactBrokerToken.value),
pactBrokerClientTimeout.value,
sslContextName.value
)
}

@SuppressWarnings(Array("org.wartremover.warts.Any"))
def pactCheckTask: Def.Initialize[InputTask[Unit]] =
Def.inputTask {
ScalaPactVerifyCommand.doPactVerify(Verifier[IO])(
ScalaPactVerifyCommand.doPactVerify(
scalaPactEnv.value.toSettings + ScalaPactSettings.parseArguments(spaceDelimited("<arg>").parsed),
providerStates.value,
providerStateMatcher.value,
Expand All @@ -186,7 +207,9 @@ object ScalaPactPlugin extends AutoPlugin {
taggedConsumerNames.value,
consumerVersionSelectors.value,
providerVersionTags.value,
PactBrokerAuthorization(pactBrokerCredentials.value, pactBrokerToken.value)
PactBrokerAuthorization(pactBrokerCredentials.value, pactBrokerToken.value),
pactBrokerClientTimeout.value,
sslContextName.value
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.itv.scalapact.plugin.shared

import com.itv.scalapact.shared.ColourOutput._
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IScalaPactHttpClient}
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared._
import com.itv.scalapactcore.common.LocalPactFileLoader

import scala.concurrent.duration._

object ScalaPactPublishCommand {

def doPactPublish[F[_]](
Expand All @@ -15,10 +17,14 @@ object ScalaPactPublishCommand {
pactContractVersion: String,
allowSnapshotPublish: Boolean,
tagsToPublishWith: Seq[String],
pactBrokerAuthorization: Option[PactBrokerAuthorization]
)(implicit pactReader: IPactReader, pactWriter: IPactWriter, httpClient: IScalaPactHttpClient[F]): Unit = {
pactBrokerAuthorization: Option[PactBrokerAuthorization],
pactBrokerClientTimeout: Duration,
sslContextName: Option[String]
)(implicit pactReader: IPactReader, pactWriter: IPactWriter, httpClientBuilder: IScalaPactHttpClientBuilder[F]): Unit = {
import Publisher._

val httpClient = httpClientBuilder.build(pactBrokerClientTimeout, sslContextName)

PactLogger.message("*************************************".white.bold)
PactLogger.message("** ScalaPact: Publishing Contracts **".white.bold)
PactLogger.message("*************************************".white.bold)
Expand All @@ -36,7 +42,7 @@ object ScalaPactPublishCommand {

// Publish all to main broker
val mainPublishResults: List[PublishResult] = publishToBroker(
httpClient.doRequestSync(_, scalaPactSettings.giveClientTimeout),
httpClient.doRequestSync,
pactBrokerAddress,
versionToPublishAs,
tagsToPublishWith,
Expand All @@ -48,7 +54,7 @@ object ScalaPactPublishCommand {
pactContract <- configAndPactFiles
broker <- providerBrokerPublishMap.get(pactContract.provider.name).toList
publishResult <- publishToBroker(
httpClient.doRequestSync(_, scalaPactSettings.giveClientTimeout),
httpClient.doRequestSync,
broker,
versionToPublishAs,
tagsToPublishWith,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.itv.scalapact.plugin.shared
import com.itv.scalapact.shared.ColourOutput._
import com.itv.scalapact.shared._
import com.itv.scalapactcore.common.LocalPactFileLoader
import com.itv.scalapact.shared.typeclasses.IPactReader
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.ProviderStateResult.SetupProviderState
import com.itv.scalapactcore.verifier.Verifier

import scala.concurrent.duration._

object ScalaPactVerifyCommand {

def doPactVerify[F[_]](verifier: Verifier[F])(
def doPactVerify[F[_]](
scalaPactSettings: ScalaPactSettings,
providerStates: Seq[(String, SetupProviderState)],
providerStateMatcher: PartialFunction[String, ProviderStateResult],
Expand All @@ -19,10 +21,15 @@ object ScalaPactVerifyCommand {
consumerNames: Seq[String],
versionedConsumerNames: Seq[(String, String)],
taggedConsumerNames: Seq[(String, Seq[String])],
consumerVersionSelectors: Seq[(String, Option[String], Option[String], Option[Boolean])],
consumerVersionSelectors: Seq[ConsumerVersionSelector],
providerVersionTags: Seq[String],
pactBrokerAuthorization: Option[PactBrokerAuthorization]
)(implicit pactReader: IPactReader): Unit = {
pactBrokerAuthorization: Option[PactBrokerAuthorization],
pactBrokerClientTimeout: Duration,
sslContextName: Option[String]
)(implicit pactReader: IPactReader,
pactWriter: IPactWriter,
httpClientBuilder: IScalaPactHttpClientBuilder[F],
publisher: IResultPublisher): Unit = {
PactLogger.message("*************************************".white.bold)
PactLogger.message("** ScalaPact: Running Verifier **".white.bold)
PactLogger.message("*************************************".white.bold)
Expand All @@ -39,13 +46,15 @@ object ScalaPactVerifyCommand {
.map(t => TaggedConsumer(t._1, t._2.toList)),
versionedConsumerNames = versionedConsumerNames.toList
.map(t => VersionedConsumer(t._1, t._2)),
consumerVersionSelectors.toList.map(v => ConsumerVersionSelector(v._1, v._2, v._3, v._4)),
consumerVersionSelectors.toList,
providerVersionTags.toList,
pactBrokerAuthorization
pactBrokerAuthorization,
Some(pactBrokerClientTimeout),
sslContextName
)

val stringToSettingsToPacts = LocalPactFileLoader.loadPactFiles(pactReader)(true)
val successfullyVerified = verifier.verify(stringToSettingsToPacts, pactVerifySettings)(scalaPactSettings)
val successfullyVerified = Verifier[F].verify(stringToSettingsToPacts, pactVerifySettings)(scalaPactSettings)

if (successfullyVerified) sys.exit(0) else sys.exit(1)

Expand Down
Loading

0 comments on commit d02df52

Please sign in to comment.