Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote pact mock server #152

Open
catalinzanfir-am opened this issue Jun 16, 2020 · 9 comments
Open

Remote pact mock server #152

catalinzanfir-am opened this issue Jun 16, 2020 · 9 comments

Comments

@catalinzanfir-am
Copy link

Hello,
I have microservices that are running in a slim php image inside docker, therefore I am unable to install ruby on them, so my tests fail to connect to the internal mock pact server, since ruby isn't(and can't be) installed.

  • is it possible to run the pact tests in a docker image? If yes, how am i supposed to do it?

What are my options in order to have a integrated running pact environment within kubernetes?
Thanks.

@mefellows
Copy link
Member

This should help https://docs.pact.io/docker

Also, you don't need to install Ruby. The standalone package it downloads already includes a Ruby runtime.

@bethesque
Copy link
Member

The problem is, Pact PHP might not support providing a standalone mock server. The Ruby impl does. You might need to raise a feature request.

@mefellows mefellows reopened this Jun 16, 2020
@mefellows
Copy link
Member

I guess this can be that feature request.

@catalinzanfir-am is your problem that you can't run any pact tests within a Docker environment? Or is it that you want to deploy the pact mock server on a separate docker container to the PHP service and that Pact PHP doesn't support that model?

(or something else).

@catalinzanfir-am
Copy link
Author

catalinzanfir-am commented Jun 17, 2020

Hello @mefellows,

Let me describe once again my situation:
I have the microservices running in docker orchestrated by kubernetes. When i run the pact tests from inside docker,

  • the consumer tests report that:
    -- Fatal error: Uncaught PhpPact\Standalone\Exception\HealthCheckFailedException: Failed to make connection to Mock Server in 10 attempts. in /vendor/pact-foundation/pact-php/src/PhpPact/Standalone/MockService/MockServer.php:153
  • the provider tests report that:
    -- console.error: err > /var/www/vendor/pact-foundation/pact-php/pact/lib/ruby/bin/ruby: line 11: /var/www/vendor/pact-foundation/pact-php/pact/lib/ruby/bin.real/ruby: No such file or directory
    -- Tests\Pact\Provider\ProviderTest::testCustomerProvider Exception: PactPHP Process returned non-zero exit code: 127
    -- both files exist

Well.. I'm not really sure what's happening. I'm guessing the ruby exec isnt able to start due to something missing from my docker images.. (basically the images only have php and nginx)

Back to your questions,
Q: is your problem that you can't run any pact tests within a Docker environment
A: yes

but most importantly i wanted to obtain this:
I want to deploy the pact mock server on a separate docker container to the PHP service. so i could keep my docker images unchanged, so i could the same one mock server per cluster , as i do with the pact broker.

thank you

ps: i dont have glibc installed, i have musl .. i'm guessing this is the cause?

@mefellows
Copy link
Member

OK, as for running the mock service in docker - please see my comment above. In fact, we already have a docker container you can use. You can test this in isolation of the second point.

You'll need to verify this yourself, but looking at the way the docs specify it, you could just not start/stop the server. This is more or less consistent with the way we allow it in Pact JS:

<?php
    use PhpPact\Standalone\MockService\MockServer;
    use PhpPact\Standalone\MockService\MockServerConfig;

    // Create your basic configuration. The host and port will need to match
    // whatever your Http Service will be using to access the providers data.
    $config = new MockServerConfig();
    $config->setHost('localhost');
    $config->setPort(7200);
    $config->setConsumer('someConsumer');
    $config->setProvider('someProvider');
    $config->setCors(true);

    // Instantiate the mock server object with the config. This can be any
    // instance of MockServerConfigInterface.
    $server = new MockServer($config);

    // Create the process -> Don't call this!
    $server->start();

    // Stop the process -> Don't call this!
    $server->stop();

There may be some state within the MockServer that won't let you do things if not started, so there may still be a need to raise a feature request.

@catalinzanfir-am
Copy link
Author

Alright, I will test the consumer tests with this docker image using:

docker run -dit \
  --rm \
  --name pact-mock-service \
  -p 1234:1234 \
  -v ${HOST_PACT_DIRECTORY}:/tmp/pacts \
  pactfoundation/pact-cli:latest \
  mock-service \
  -p 1234 \
  --host 0.0.0.0 \
  --pact-dir /tmp/pacts

.

However, i thought, since the following variables are available in the phpunit.xml file:

<env name="PACT_MOCK_SERVER_HOST" value="localhost"/>
<env name="PACT_MOCK_SERVER_PORT" value="7201"/>

you could just provide an url to a pact mock server (one that is hosted on in docker, started, and listening to inputs)

@mefellows
Copy link
Member

I don't know much about that, but give it a go!

@catalinzanfir-am
Copy link
Author

Hello again.
I've managed to run the pact-mock-server with:

docker run -dit \
  --rm \
  --name pact-mock-service \
  -p 1234:1234 \
  -v /Users/catalinzanfir-am/work/app-name/pact/output:/example/output \
  pactfoundation/pact-cli:latest \
  mock-service \
  --port 1234 \
  --host 0.0.0.0 \
  --pact-file-write-mode=overwrite \
  --consumer=android-consumer \
  --provider=ms-provider \
  --pact-dir /example/output \
  --log /example/output/log.log

So i guess it solves the issue, but it's a bit tricky to use because:

  • you have to start the mock server before your tests, because the test doesn't know how to start the remote docker vm (obviously)
  • and because you start it manually, you can't rely on the parameters set in phpunit.xml and you have to set the parameters as in the example above

@mefellows / @bethesque i have some other questions, but i don't wanna spam the issues tab. are you interested to discuss on some platform of your choice(slack, discord, skype etc)?

@mefellows
Copy link
Member

Good to hear it works - that all sounds about right to me.

Sure, please join us at slack.pact.io and chat in the #pact-php channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants