Skip to content

Asynchronous/Messages metadata validation fail #543

Open
@flipcc

Description

@flipcc

Description

I was trying to provider-side verify a customer pact file, when noticing that my current local testing setup must fail, because it does not include meta attributes in contrast to the actual pub-sub resources. But it did not fail, it just did not verify them.

The verification is important, as faning out by SNS->multiple SQS is often realized by using filters on meta attributes.

To reproduce my error I adopted your examples provided in this repo (a big thank you at this point for providing examples!).

Steps to reproduce

(1) consumer test:

"""pact test for a message consumer"""

import logging
import time
from os import remove
from os.path import isfile

import pytest
from pact import MessageConsumer
from pact import Provider

log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

PACT_DIR = "pacts"

CONSUMER_NAME = "DetectContentLambda"
PROVIDER_NAME = "ContentProvider"
PACT_FILE = (
    f"{PACT_DIR}/{CONSUMER_NAME.lower().replace(' ', '_')}-"
    + f"{PROVIDER_NAME.lower().replace(' ', '_')}.json"
)


@pytest.fixture(scope="session")
def pact(request):
    pact = MessageConsumer(CONSUMER_NAME).has_pact_with(
        Provider(PROVIDER_NAME),
        publish_to_broker=False,
        pact_dir=PACT_DIR,
    )

    yield pact


def cleanup_json(file):
    """
    Remove existing json file before test if any
    """
    if isfile(f"{file}"):
        remove(f"{file}")


def progressive_delay(file, time_to_wait=10, second_interval=0.5, verbose=False):
    """
    progressive delay
    defaults to wait up to 5 seconds with 0.5 second intervals
    """
    time_counter = 0
    while not isfile(file):
        time.sleep(second_interval)
        time_counter += 1
        if verbose:
            print(f"Trying for {time_counter * second_interval} seconds")
        if time_counter > time_to_wait:
            if verbose:
                print(f"Already waited {time_counter * second_interval} seconds")
            break


def test_put_file(pact):
    cleanup_json(PACT_FILE)

    expected_event = {
        "event": "ObjectCreated:Put",
        "documentName": "document.doc",
        "creator": "TP",
        "documentType": "microsoft-word",
    }

    (
        pact.given("A document created successfully")
        .expects_to_receive("Description")
        .with_content(expected_event)
        .with_metadata(
            {"Content-Type": "application/json", "Additional-Key": "to test with"}
        )
    )

    with pact:
        print("\nRunning test.")

    progressive_delay(f"{PACT_FILE}")
    assert isfile(f"{PACT_FILE}") == 1

(2) subsequently provider test:

from pact import MessageProvider

PACT_DIR = "pacts"


def document_created_handler():
    return {
        "event": "ObjectCreated:Put",
        "documentName": "document.doc",
        "creator": "TP",
        "documentType": "microsoft-word",
    }


def test_verify_success():
    provider = MessageProvider(
        message_providers={
            "A document created successfully": document_created_handler,
        },
        provider="ContentProvider",
        consumer="DetectContentLambda",
        pact_dir="pacts",
    )
    with provider:
        provider.verify()

results in:

test_msg_provider.py::test_verify_success PASSED                         

[100%]pact WARN: Please note: we are tracking events anonymously to gather important usage statistics like Pact-Ruby version and operating system. To disable tracking, set the 'PACT_DO_NOT_TRACK' environment variable to 'true'.
INFO: Reading pact at pacts/detectcontentlambda-contentprovider.json

Verifying a pact between DetectContentLambda and ContentProvider
  Given A document created successfully
    Description
WARN: Skipping set up for provider state 'A document created successfully' for consumer 'DetectContentLambda' as there is no --provider-states-setup-url specified.
      has matching content

1 interaction, 0 failures

Expected behavior

A failure is expected in the form of:

Verifying a pact between DetectContentLambda and ContentProvider
  Given A document created successfully
    has matching metadata
        Expected message metada "Additional-Key" to have value  "to test with" but was ""

There were 1 pact failures

Actual behavior

The test succeeds without verifying the metadata attributes.

Your environment

Adapted the examples from examples/message/tests.

Self-service

  • I'd be willing to fix this bug myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions