From 32d22e7c78c8e5c039c746018fb235dac9e4fc50 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Sat, 13 Mar 2021 14:56:52 +1100 Subject: [PATCH] chore: add some tests around handling IO errors from Pact Broker #1322 --- .../pact/core/pactbroker/PactBrokerClient.kt | 2 ++ .../pactbroker/PactBrokerClientSpec.groovy | 25 +++++++++++++++++++ .../junit/loader/PactBrokerLoaderSpec.groovy | 14 +++++++++++ .../junit5/PactJUnit5VerificationProvider.kt | 4 ++- .../junitsupport/loader/PactBrokerLoader.kt | 1 + 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt b/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt index 30a85faf7f..46d56bb610 100644 --- a/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt +++ b/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt @@ -18,6 +18,7 @@ import com.github.michaelbull.result.unwrap import com.google.common.net.UrlEscapers.urlPathSegmentEscaper import mu.KLogging import java.io.File +import java.io.IOException import java.net.URLDecoder import java.util.function.Consumer @@ -114,6 +115,7 @@ interface IPactBrokerClient { /** * Fetches all consumers for the given provider and selectors */ + @Throws(IOException::class) fun fetchConsumersWithSelectors( providerName: String, selectors: List, diff --git a/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy b/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy index 4cf569d07a..9c768b557c 100644 --- a/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy +++ b/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy @@ -11,6 +11,8 @@ import spock.lang.Issue import spock.lang.Specification import spock.lang.Unroll +import javax.net.ssl.SSLHandshakeException + @SuppressWarnings('UnnecessaryGetter') class PactBrokerClientSpec extends Specification { @@ -587,4 +589,27 @@ class PactBrokerClientSpec extends Specification { 1 * client.fetchConsumersWithTag('provider', 'MASTER') >> [] result instanceof Ok } + + @Issue('#1322') + def 'when fetching pacts fails with a certificate error'() { + given: + def halClient = Mock(IHalClient) + PactBrokerClient client = Spy(PactBrokerClient, constructorArgs: ['baseUrl']) { + newHalClient() >> halClient + } + def selectors = [ + new ConsumerVersionSelector('DEV', true, null, 'MASTER') + ] + + when: + def result = client.fetchConsumersWithSelectors('provider', selectors, [], false, '') + + then: + 1 * halClient.navigate() >> { + throw new InvalidNavigationRequest('PKIX path building failed', new SSLHandshakeException('PKIX path building failed')) + } + notThrown(SSLHandshakeException) + result instanceof Err + result.component2() instanceof InvalidNavigationRequest + } } diff --git a/provider/junit/src/test/groovy/au/com/dius/pact/provider/junit/loader/PactBrokerLoaderSpec.groovy b/provider/junit/src/test/groovy/au/com/dius/pact/provider/junit/loader/PactBrokerLoaderSpec.groovy index b353a76e23..539d3d56cc 100644 --- a/provider/junit/src/test/groovy/au/com/dius/pact/provider/junit/loader/PactBrokerLoaderSpec.groovy +++ b/provider/junit/src/test/groovy/au/com/dius/pact/provider/junit/loader/PactBrokerLoaderSpec.groovy @@ -6,6 +6,7 @@ import au.com.dius.pact.core.model.PactReader import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector import au.com.dius.pact.core.pactbroker.IPactBrokerClient import au.com.dius.pact.core.pactbroker.InvalidHalResponse +import au.com.dius.pact.core.pactbroker.InvalidNavigationRequest import au.com.dius.pact.core.pactbroker.PactBrokerResult import au.com.dius.pact.core.support.expressions.SystemPropertyResolver import au.com.dius.pact.core.support.expressions.ValueResolver @@ -21,6 +22,7 @@ import spock.lang.Specification import spock.lang.Unroll import spock.util.environment.RestoreSystemProperties +import javax.net.ssl.SSLHandshakeException import java.lang.annotation.Annotation import static au.com.dius.pact.core.support.expressions.ExpressionParser.VALUES_SEPARATOR @@ -1247,6 +1249,18 @@ class PactBrokerLoaderSpec extends Specification { 'localhost' | '1234' | 'https' | null | 'https://localhost:1234' } + @Issue('#1322') + def 'Throws an Exception if there is a certificate error'() { + when: + pactBrokerLoader().load('test') + + then: + 1 * brokerClient.fetchConsumersWithSelectors('test', [], [], false, '') >> { + throw new InvalidNavigationRequest('PKIX path building failed', new SSLHandshakeException('PKIX path building failed')) + } + thrown(InvalidNavigationRequest) + } + private static VersionSelector createVersionSelector(Map args = [:]) { new VersionSelector() { @Override diff --git a/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt b/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt index d02cc0b9c3..e9f1ddedcb 100644 --- a/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt +++ b/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactJUnit5VerificationProvider.kt @@ -78,7 +78,9 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon description += "\nSource: ${loader.description()}" val pacts = handleWith> { loader.load(serviceName) }.getOrElse { val ignoreAnnotation = AnnotationSupport.findAnnotation(context.requiredTestClass, IgnoreNoPactsToVerify::class.java) - if (ignoreAnnotation.isPresent && ignoreAnnotation.get().ignoreIoErrors == "true") { + if (ignoreAnnotation.isPresent && + (valueResolver != null && valueResolver.resolveValue(ignoreAnnotation.get().ignoreIoErrors) == "true") || + ignoreAnnotation.get().ignoreIoErrors == "true") { emptyList() } else { throw it diff --git a/provider/src/main/kotlin/au/com/dius/pact/provider/junitsupport/loader/PactBrokerLoader.kt b/provider/src/main/kotlin/au/com/dius/pact/provider/junitsupport/loader/PactBrokerLoader.kt index 4245b3efa7..443920f8bd 100644 --- a/provider/src/main/kotlin/au/com/dius/pact/provider/junitsupport/loader/PactBrokerLoader.kt +++ b/provider/src/main/kotlin/au/com/dius/pact/provider/junitsupport/loader/PactBrokerLoader.kt @@ -100,6 +100,7 @@ open class PactBrokerLoader( overriddenConsumer = consumer } + @Throws(IOException::class) override fun load(providerName: String): List { val resolver = setupValueResolver() return when {