Skip to content

Commit

Permalink
Merge pull request #212 from pact-foundation/fix/verify_in_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottmurray authored Mar 27, 2021
2 parents 2c8779b + ea0b64a commit c778c71
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
27 changes: 19 additions & 8 deletions examples/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def pytest_addoption(parser):
help="The url to our provider."
)

parser.addoption(
"--run-broker", type=bool, action="store",
help="Whether to run broker in this test or not."
)

# This fixture is to simulate a managed Pact Broker or Pactflow account
# Do not do this yourself but setup one of the above
# https://github.com/pact-foundation/pact_broker
Expand All @@ -27,13 +32,19 @@ def broker(request):
yield
return

print('Starting broker')
with DockerCompose("../broker",
compose_file_name=["docker-compose.yml"],
pull=True) as compose:
run_broker = request.config.getoption('--run-broker')

stdout, stderr = compose.get_logs()
if stderr:
print("Errors\\n:{}".format(stderr))
print(stdout)
if not run_broker:
yield
return
else:
print('Starting broker')
with DockerCompose("../broker",
compose_file_name=["docker-compose.yml"],
pull=True) as compose:

stdout, stderr = compose.get_logs()
if stderr:
print("Errors\\n:{}".format(stderr))
print(stdout)
yield
2 changes: 1 addition & 1 deletion examples/e2e/run_pytest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trap teardown EXIT

sleep 3

pytest contract_tests --publish-pact 1
pytest tests --run-broker True --publish-pact 1

teardown

4 changes: 3 additions & 1 deletion examples/e2e/tests/provider/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def default_opts():
return {
'broker_username': PACT_BROKER_USERNAME,
'broker_password': PACT_BROKER_PASSWORD,
'broker_url': PACT_BROKER_URL
'broker_url': PACT_BROKER_URL,
'publish_version': '3',
'publish_verification_results': True
}


Expand Down
4 changes: 2 additions & 2 deletions pact/cli/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def main(pacts, base_url, pact_url, pact_urls, states_url, states_setup_url,
raise click.Abort()

if publish_verification_results:
publish_results(error, provider_app_version)
validate_publish(error, provider_app_version)

options = {
'broker_password': password,
Expand Down Expand Up @@ -196,7 +196,7 @@ def main(pacts, base_url, pact_url, pact_urls, states_url, states_setup_url,
sys.exit(success)


def publish_results(error, provider_app_version):
def validate_publish(error, provider_app_version):
"""Publish results to broker."""
if not provider_app_version:
click.echo(
Expand Down
10 changes: 6 additions & 4 deletions pact/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def verify_with_broker(self, enable_pending=False, include_wip_pacts_since=None,
broker_url ([String]): url of broker
enable_pending ([Boolean])
include_wip_pacts_since ([String])
publish_version ([String])
"""
broker_username = kwargs.get('broker_username', None)
Expand Down Expand Up @@ -103,8 +104,8 @@ def extract_params(self, **kwargs):
provider_tags = kwargs.get('provider_tags', [])
states_setup_url = kwargs.get('provider_states_setup_url', None)
verbose = kwargs.get('verbose', False)
publish_version = kwargs.get('publish_version', None)

provider_app_version = kwargs.get('publish_version', None)
publish_verification_results = kwargs.get('publish_verification_results', None)
raw_consumer_selectors = kwargs.get('consumer_version_selectors', [])
consumer_selectors = self._build_consumer_selectors(raw_consumer_selectors)

Expand All @@ -118,8 +119,9 @@ def extract_params(self, **kwargs):
'provider_tags': list(provider_tags),
'provider_states_setup_url': states_setup_url,
'verbose': verbose,
'publish_version': publish_version,
'consumer_selectors': consumer_selectors
'provider_app_version': provider_app_version,
'consumer_selectors': consumer_selectors,
'publish_verification_results': publish_verification_results
}
return self.filter_empty_options(**options)

Expand Down
26 changes: 24 additions & 2 deletions tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_publish_on_success(self, mock_path_exists, mock_wrapper):
provider_base_url='http://localhost:8888',
log_level='INFO',
verbose=False,
publish_version='1.0.0',
provider_app_version='1.0.0',
enable_pending=False,
include_wip_pacts_since=None)

Expand Down Expand Up @@ -171,6 +171,28 @@ def test_verifier_with_broker(self, mock_wrapper):
enable_pending=False,
include_wip_pacts_since=None)

@patch("pact.verify_wrapper.VerifyWrapper.call_verify")
def test_verifier_and_pubish_with_broker(self, mock_wrapper):

mock_wrapper.return_value = (True, 'some value')

self.default_opts['publish_verification_results'] = True
output, _ = self.verifier.verify_with_broker(**self.default_opts)

self.assertTrue(output)
assertVerifyCalled(mock_wrapper,
provider='test_provider',
provider_base_url='http://localhost:8888',
broker_password=self.broker_password,
broker_username=self.broker_username,
broker_token='token',
broker_url=self.broker_url,
log_level='INFO',
verbose=False,
enable_pending=False,
include_wip_pacts_since=None,
publish_verification_results=True)

@patch("pact.verify_wrapper.VerifyWrapper.call_verify")
def test_verifier_with_broker_passes_consumer_selctors(self, mock_wrapper):

Expand Down Expand Up @@ -216,7 +238,7 @@ def test_publish_on_success(self, mock_path_exists, mock_wrapper):
broker_url=self.broker_url,
log_level='INFO',
verbose=False,
publish_version='1.0.0',
provider_app_version='1.0.0',
enable_pending=False,
include_wip_pacts_since=None)

Expand Down

0 comments on commit c778c71

Please sign in to comment.