Hi, I’m just getting started on a pactflow poc. I...
# pactflow
l
Hi, I’m just getting started on a pactflow poc. I have a simple contract (messaging) published on the consumer side (java), but when I try to run the provider test (node) I am unable to download that contract from the broker. Any help would be appreciated. Thanks! Pasting log in a thread
Copy code
RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
[12:18:55.275] INFO (86088): pact@11.0.2: Verifying message
[12:18:55.299] INFO (86088): pact-core@13.13.8: Verifying Pacts.
 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
2023-05-23T16:18:57.198115Z  WARN ThreadId(20) pact_models::pact: No metadata found in pact file "<https://linostest.pactflow.io>", assuming V3 specification
2023-05-23T16:18:57.199289Z  WARN ThreadId(20) pact_models::pact: No metadata found in pact file "<https://linostest.pactflow.io>", assuming V
 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
2023-05-23T16:18:59.216868Z  INFO ThreadId(20) pact_verifier::pact_broker: Fetching path '/pacts/provider/linos-backend/for-verification' fr
 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
2023-05-23T16:19:00.123939Z ERROR ThreadId(20) pact_verifier: Failed to load pact - Could not load pacts from the pact broker '<https://linostest.pactflow.io>'
2023-05-23T16:19:00.124025Z  WARN ThreadId(20) pact_matching::metrics:

Please note:
We are tracking events anonymously to gather important usage statistics like Pact version and operating system. To disable tracking, set the 'PACT_DO_NOT_TRACK' environment variable to 'true'.
 FAIL  apps/core-service/src/tests/Contract/aapiContract2.spec.ts (13.017s)
  message provider tests
    send an event
      ✕ Damaged flowrack inventory exception (5024ms)

  ● message provider tests › send an event › Damaged flowrack inventory exception

    : Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error:

      69 |
      70 |   describe('send an event', () => {
    > 71 |     it('Damaged flowrack inventory exception', () => {
         |     ^
      72 |       return p.verify();
      73 |     });
      74 |   });

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Suite.<anonymous> (apps/core-service/src/tests/Contract/aapiContract2.spec.ts:71:5)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        13.097s
Ran all test suites matching /.\/apps\/core-service.+\aapiContract2.spec.ts/i.

Verifying a pact between consumer and provider
WARNING: Pact file has no interactions

Failures:

1) Failed to load pact - Could not load pacts from the pact broker '<https://linostest.pactflow.io>'


There were 1 pact failures

[12:19:00.544] ERROR (86088): pact-core@13.13.8: Verification unsuccessful
/Users/m_884005/Documents/Lineage/repos/linos-backend/nestjs-monorepo/node_modules/@pact-foundation/pact-core/src/verifier/nativeVerifier.js:62
                        reject(new Error('Verfication failed'));
                               ^

Error: Caught error after test environment was torn down

Verfication failed
    at /Users/m_884005/Documents/Lineage/repos/linos-backend/nestjs-monorepo/node_modules/@pact-foundation/pact-core/src/verifier/nativeVerifier.ts:52:20
m
Can you please share your provider code setup? Looks like you're passing the Pactflow host as a pact file. You probably want to look at the pact broker / token options as well as selectors. See the examples (howtoexamples)
s
l
Hi Matt, Sure…
Copy code
const baseOpts = {
    // logLevel: 'DEBUG',
    provider: 'linos-backend',
    providerVersion: '1.0', // process.env.GIT_COMMIT,
    pactBroker: '<https://linostest.pactflow.io>',
    consumerVersionTags: [],
    // providerVersionTags: process.env.GIT_BRANCH ? [process.env.GIT_BRANCH] : [],
    // pactBrokerUrl: ,
    pactBrokerToken: 'ogmEqX8PNkmfHnOJ4W16TQ',
  };

  // 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.
  const pactChangedOpts = {
    pactUrls: ['<https://linostest.pactflow.io>'],
  };

  // For 'normal' provider builds, fetch `master` and `prod` pacts for this provider
  const fetchPactsDynamicallyOpts = {
    provider: 'linos-backend',
    consumerVersionTags: ['master', 'prod', 'main'], // the old way of specifying which pacts to verify
    // consumerVersionSelectors: [{ tag: 'master', latest: true }, { tag: 'prod', latest: true } ], // the new way of specifying which pacts to verify
    pactUrls: ['<https://linostest.pactflow.io>'],
    pactBrokerUrl: '<https://linostest.pactflow.io>',
    pactBrokerToken: 'ogmEqX8PNkmfHnOJ4W16TQ',
    enablePending: false,
  };

  const opts = {
    ...baseOpts,
    ...(process.env.PACT_URL ? pactChangedOpts : fetchPactsDynamicallyOpts),
    messageProviders: {
      'Damaged flowrack inventory exception': () =>
        // messageService.sendMissingFlowrackInventoryRequest('46',{
        messageService.buildAAPIMessage({
          type: 'MissingFlowrackInventoryRequest',
          input,
          facilityId: 'facilityId',
        }),
    },
  };
  it('Damaged flowrack inventory exception - message provider pact', () => {
    const p = new MessageProviderPact(opts);
    // let msg = messageService.buildAAPIMessage({
    //   type: 'MissingFlowrackInventoryRequest',
    //   input,
    //   facilityId: 'facilityId',
    // })
    // console.log("Message: " + msg)
    console.log(`opts: ${JSON.stringify(opts, undefined, 2)}`);
    return p.verify();
  });
here are what the options look like when running:
Copy code
console.log
    opts: {
      "provider": "linos-backend",
      "providerVersion": "1.0",
      "pactBroker": "<https://linostest.pactflow.io>",
      "consumerVersionTags": [
        "master",
        "prod",
        "main"
      ],
      "pactBrokerToken": "ogmEqX8PNkmfHnOJ4W16TQ",
      "pactUrls": [
        "<https://linostest.pactflow.io>"
      ],
      "pactBrokerUrl": "<https://linostest.pactflow.io>",
      "enablePending": false,
      "messageProviders": {}
    }
I did try to remove the pactUrls (see options below), but got the same result - could not load pacts
Copy code
opts: {
      "provider": "linos-backend",
      "providerVersion": "1.0",
      "pactBroker": "<https://linostest.pactflow.io>",
      "consumerVersionTags": [
        "master",
        "prod",
        "main"
      ],
      "pactBrokerToken": "ogmEqX8PNkmfHnOJ4W16TQ",
      "pactBrokerUrl": "<https://linostest.pactflow.io>",
      "enablePending": false,
      "messageProviders": {}
    }
m
Can you please share the error/logs when you have removed the
pactUrls
item? That should be populated by values from a webhook, or if you have a direct URL to a pact you want to verify
If the second options can’t find a pact file, it’s probably because the tags don’t match what’s been published
But also, you shouldn’t be using tags - you should be using branches. See the CI/CD workshop here: https://docs.pactflow.io/docs/workshops/ci-cd/ (or copy how the examples do it) Our killercoda workshops should also be using branches and environments - may I ask where you’re getting the tags example from?
Lastly, please regenerate your API token (you just published it in a public forum)
😱 2
1
l
Good Morning, Matt. The example code I’m following is here: https://github.com/pactflow/example-provider-js-sns/blob/master/src/product/product.pact.test.js. I’ll be the first person to admit I don’t know what I’m doing, so I’m sure some of it seems crazy to you - I’ve been changing up options hoping something will work. Logs from the run without the pactUrls:
Copy code
> nestjs-monorepo@0.0.1 test:contract2
> jest --testTimeout 30000 --no-cache './apps/core-service.+\aapiContract2.spec.ts' --maxWorkers=4

jest-haste-map: Watchman crawl failed. Retrying once with node crawler.
  Usually this happens when watchman isn't running. Create an empty `.watchmanconfig` file in your project's root folder or initialize a git or hg repository in your project.
  Error: Watchman error: std::__1::system_error: open: /Users/m_884005/Documents/Lineage/repos/linos-backend/nestjs-monorepo/apps: Operation not permitted. Make sure watchman is running for this project. See <https://facebook.github.io/watchman/docs/troubleshooting>.

 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
[08:25:37.718] INFO (10083): pact@11.0.2: Verifying message
  console.log
    opts: {
      "provider": "linos-backend",
      "providerVersion": "1.0",
      "pactBroker": "<https://linostest.pactflow.io>",
      "consumerVersionTags": [
        "master",
        "prod",
        "main"
      ],
      "pactBrokerToken": "xxxxxxx",
      "pactBrokerUrl": "<https://linostest.pactflow.io>",
      "enablePending": false,
      "messageProviders": {}
    }

      at Object.<anonymous> (apps/core-service/src/tests/Contract/aapiContract2.spec.ts:76:13)


 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
2023-05-24T12:25:39.785510Z  INFO ThreadId(17) pact_verifier::pact_broker: Fetching path '/pacts/provider/linos-backend/for-verification' from pact
 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts
2023-05-24T12:25:40.553318Z ERROR ThreadId(17) pact_verifier: Failed to load pact - Could not load pacts from the pact broker '<https://linostest.pactflow.io>'
2023-05-24T12:25:40.554256Z  WARN ThreadId(17) pact_matching::metrics:

Please note:
We are tracking events anonymously to gather important usage statistics like Pact version and operating system. To disable tracking, set the 'PACT_DO_NOT_TRACK' environment variable to 'true'.

 RUNS  apps/core-service/src/tests/Contract/aapiContract2.spec.ts

Failures:

1) Failed to load pact - Could not load pacts from the pact broker '<https://linostest.pactflow.io>'


There were 1 pact failures
 FAIL  apps/core-service/src/tests/Contract/aapiContract2.spec.ts (11.558s)
  message provider tests
    ✕ Damaged flowrack inventory exception - message provider pact (3600ms)

  ● message provider tests › Damaged flowrack inventory exception - message provider pact

    Verfication failed

      at node_modules/@pact-foundation/pact-core/src/verifier/nativeVerifier.ts:52:20

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        11.636s
Ran all test suites matching /.\/apps\/core-service.+\aapiContract2.spec.ts/i.
(base) m_884005@AMBUS000029 nestjs-monorepo %
I’m not triggering any of this with CICD - I’m just trying to get the tests to run locally at this point - I’m at the beginning of this journey. The contract was published without any tags, or branches - just a vanilla publish from the maven plugin on the consumer side.
m
It’s because you’re telling Pact to find contracts by tag (
master
,
prod
and
main
) but none of the contracts you have published have those tags - hence it can’t find them
to fix this, publish your contract and set a tag - e.g.
main
I’d suggest starting here: https://docs.pactflow.io/docs/tutorials#getting-started-with-pactflow You can do a full end-to-end in the browser and then once you’ve got the gist of it, translate to code on your desktop
Writing pact tests is one part, and then being able to link them to both sides (consumer/provider) is the other key bit. We use things called “consumer version selectors” on the provider tests to discover the pacts (the relevant integration points in different states of development/deployments) we care about. In your case, the tags don’t line up so it’s not finding anything to verify. I think you could also just drop the
consumerVersionTags
property and it would just find the latest, which is probably fine for a POC
l
Thanks, Matt. I’ve already done the in-browser tutorials in both Java and Node - they were very helpful. Now I’m trying to get this to work on our own code base, which is where I’ve hit a couple of hiccups:
🙌 1
I have published with tags and can now pull the contract on the provider side - so that is resolved.
🎉 2
m
Ah, rightyo. Glad to hear you’re back on track!!