pact-foundation / pact-reference

Reference implementations for the pact specifications
https://pact.io
MIT License
91 stars 46 forks source link

IPactVerifier.Verify() does not work if .FromPactUri() is used without .FromPactBroker() #169

Closed cyrusdevnomad closed 2 years ago

cyrusdevnomad commented 2 years ago

Hi In the following code, the if path does not work:

            IPactVerifier tmpPactVerifier;

            if (tmpPactUrl != null)
            {
                // For builds triggered by a 'contract content changed' webhook,
                // just verify the changed pact. The URL will bave been passed in
                // from the webhook to the CI job.
                Console.WriteLine("PACT_URL != null");
                Console.WriteLine($"tmpPactUrl: {tmpPactUrl}");
                Uri tmpPactUri = new Uri(tmpPactUrl, UriKind.Absolute);
                tmpPactVerifier = pactVerifier.FromPactUri(tmpPactUri);
            }
            else
            {
                // For 'normal' provider builds, fetch `master` and `prod` pacts for this provider
                Console.WriteLine("PACT_URL: == null");
                Console.WriteLine($"brokerBaseUri: {brokerBaseUri}");
                IEnumerable<string> consumerVersionTags = tmpVersionTags;
                string includeWipPactSince = "2020-01-01";

                tmpPactVerifier = pactVerifier.FromPactBroker(brokerBaseUri, uriOptions, enablePending, consumerVersionTags, includeWipPactSince);
            }

            tmpPactVerifier = tmpPactVerifier
                .WithProviderStateUrl(new Uri($"{_pactServiceUri}/provider-states"))
                .ServiceProvider("ProductService", new Uri(_pactServiceUri))
                .HonoursPactWith("ApiClient")
                .WithPublishedResults(tmpProviderVersion, tmpProviderVersionTags);
            tmpPactVerifier.Verify();

In the code above if the verify pact changed pipeline is triggered from PactFlow webhook and the if path with pactVerifier.FromPactUri() is executed the following URL is passed to Uri tmpPactUri = new Uri(tmpPactUrl, UriKind.Absolute) and I get the error message further down bellow: https://xxxxx.pactflow.io/pacts/provider/ProductService/consumer/ApiClient/pact-version/xxxxxxxxxx/metadata/xxxxxxxxxxxxxxxxxxxxxx

I get the following error message:

PactNet.PactFailureException : Invalid arguments were provided to the verification process Stack Trace: at PactNet.Native.NativePactVerifier.Verify(String args) in /Users/erikdanielsen/work/dius/pact-net/src/PactNet.Native/NativePactVerifier.cs:line 42 at PactNet.Native.PactVerifier.Verify() in /Users/erikdanielsen/work/dius/pact-net/src/PactNet.Native/PactVerifier.cs:line 240 at tests.ProductTest.EnsureProviderApiHonoursPactWithConsumer() in D:\a\1\s\tests\ProductTest.cs:line 98 Standard Output Messages: Invoking the pact verifier with args: --url https://xxxxxx.pactflow.io/pacts/provider/ProductService/consumer/ApiClient/pact-version/xxxxxxxxxxxxxxxxxxxxxxxxx/metadata/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx --state-change-url http://127.0.0.1:9001/provider-states --provider-name ProductService --hostname 127.0.0.1 --port 9001 --filter-consumer ApiClient --publish --provider-version xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx --provider-tags master --loglevel trace

The exception above is not thrown if the method .FromPactBroker() is added to the if path as follows:

             if (tmpPactUrl != null)
            {
                // For builds triggered by a 'contract content changed' webhook,
                // just verify the changed pact. The URL will bave been passed in
                // from the webhook to the CI job.
                Console.WriteLine("PACT_URL != null");
                Console.WriteLine($"tmpPactUrl: {tmpPactUrl}");
                Uri tmpPactUri = new Uri(tmpPactUrl, UriKind.Absolute);
                tmpPactVerifier = pactVerifier.FromPactUri(tmpPactUri);

                **tmpPactVerifier = pactVerifier.FromPactBroker(brokerBaseUri, uriOptions);**
            }

Thanks

mefellows commented 2 years ago

Thank you.

As discussed in slack, the issue seems to be that you can't pass --uri without also passing --broker-url which seems incorrect at first glance and results in awkward use of the .NET interface at least.

We'll take a look.

adamrodger commented 2 years ago

This is probably not an issue after PactNet moved to a handles-based approach for verification instead of this older CLI args style.

adamrodger commented 2 years ago

The PactNet API has moved on quite significantly from this both internally and externally so this is no longer applicable and can be closed I think.