class MockApiServer: def __init__(self, service, log_dir='contract_logs/consumer'): config = SERVICES[service] self.pact = Consumer(BASE_CONFIG['consumer']).has_pact_with( Provider(service), host_name=BASE_CONFIG['host'], port=config['port'], pact_dir=BASE_CONFIG['path_to_pacts'], log_dir=log_dir) for interaction in config['interactions']: (self.pact.given(interaction['GIVEN']).upon_receiving( interaction['UPON_RECEIVING']).with_request( **interaction['REQUEST']).will_respond_with( **interaction['RESPONSE'])) def stop(self): self.pact.stop_service() return self def start(self): self.pact.start_service() self.pact.setup() return self def safe_start(self): self.start() atexit.register(self.stop) return self.pact
def pact(): pact = Consumer(CONSUMER_NAME, tags=['master', 'consumer-py'], version='1.0.0') \ .has_pact_with(Provider(PROVIDER_NAME), # pact_dir=PACT_DIR, //https://github.com/pact-foundation/pact-python/issues/128 version='2.0.0', publish_to_broker=True, broker_base_url=PACT_BROKER_URL) try: pact.start_service() yield pact finally: pact.stop_service()
def pact(request): pact = Consumer('UserServiceClient').has_pact_with( Provider('UserService'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir=PACT_DIR) pact.start_service() yield pact pact.stop_service() version = request.config.getoption('--publish-pact') if not request.node.testsfailed and version: push_to_broker(version)
def pact(request): pact = Consumer('consumer-in-python').has_pact_with( Provider('provider-in-dotnet'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir="./pacts", log_dir="./logs") try: print('start service') pact.start_service() yield pact finally: print('stop service') pact.stop_service()
def pact(request): version = request.config.getoption('--publish-pact') publish = True if version else False pact = Consumer('UserServiceClient', version=version).has_pact_with( Provider('UserService'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir=PACT_DIR, publish_to_broker=publish, broker_base_url=PACT_BROKER_URL, broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_PASSWORD) print('start service') pact.start_service() yield pact print('stop service') pact.stop_service()
def pact(request): pact = Consumer('UserServiceClient').has_pact_with( Provider('UserService'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir=PACT_DIR, publish_to_broker=False, broker_base_url=PACT_BROKER_URL, broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_USERNAME) pact.start_service() atexit.register(pact.stop_service) yield pact pact.stop_service() version = request.config.getoption('--publish-pact') if not request.node.testsfailed and version: push_to_broker(version) print('not handcraft')
def pact(request): pact = Consumer('PythonConzoomer').has_pact_with( Provider('AccountJSService'), host_name=PROVIDER_HOST, port=PROVIDER_PORT, pact_dir=PACT_DIR) # V: Provider port must align with inner Consumer call setup! (Might be python thing) # V: Therefore I'm pulling definition for local development from Consumer directly try: print('start service') pact.start_service() yield pact finally: print('stop service') pact.stop_service() # version = request.config.getoption('--publish-pact') version = CONSUMER_VERSION # version = False if not request.node.testsfailed and version: push_to_broker(version)
def pact(request): """Setup a Pact Consumer, which provides the Provider mock service. This will generate and optionally publish Pacts to the Pact Broker""" # When publishing a Pact to the Pact Broker, a version number of the Consumer # is required, to be able to construct the compatability matrix between the # Consumer versions and Provider versions version = request.config.getoption("--publish-pact") publish = True if version else False pact = Consumer("UserServiceClient", version=version).has_pact_with( Provider("UserService"), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir=PACT_DIR, publish_to_broker=publish, broker_base_url=PACT_BROKER_URL, broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_PASSWORD, ) pact.start_service() # Make sure the Pact mocked provider is stopped when we finish, otherwise # port 1234 may become blocked atexit.register(pact.stop_service) yield pact # This will stop the Pact mock server, and if publish is True, submit Pacts # to the Pact Broker pact.stop_service() # Given we have cleanly stopped the service, we do not want to re-submit the # Pacts to the Pact Broker again atexit, since the Broker may no longer be # available if it has been started using the --run-broker option, as it will # have been torn down at that point pact.publish_to_broker = False