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

pact verification issue #38

Closed
pawelszk opened this issue Jul 14, 2017 · 7 comments
Closed

pact verification issue #38

pawelszk opened this issue Jul 14, 2017 · 7 comments

Comments

@pawelszk
Copy link

Hi,
I am new to contract testing and I am trying to find out how it works.
I have copied animal example and started animal service:

Animal Profile Service listening on http://localhost:8081
Animal Matching Service listening on http://localhots:8080

I have started also pact mock service locally

My code looks like that:

pact = Consumer('Consumer').has_pact_with(Provider('Provider'), host_name='localhost', port=1234)  

class ContractTest(unittest.TestCase):

    def test_first(self):
        url = 'http://localhost:8081/animals/1'
        response = requests.get(url)
        print(response.status_code)
        print(response.json())
        expected = {'first_name': 'Billy', 'last_name': 'Goat', 'animal': 'goat', 'age': 21, 'gender': 'M', 'location': {'description': 'Melbourne Zoo', 'country': 'Australia', 'post_code': 3000},
                    'eligibility': {'available': True, 'previously_married': False}, 'interests': ['walks in the garden/meadow', 'munching on a paddock bomb', 'parkour'], 'id': 1}

        (pact
         .given('UserA exists and is not an administrator')
         .upon_receiving('a request for UserA')
         .with_request('get', '/animals/1')
         .will_respond_with(200, body=expected))

        with pact:
            result = requests.get(url)
        self.assertEqual(result, expected)
        # self.assertEqual(response.json(), expected)

When I am running my test I am getting Missing Request warning and tests fails
Here is mock service console output:

D:\Pact_Mock_Service\pact-mock-service-2.1.0-1-win32>.\bin\pact-mock-service.bat -p 1234
[2017-07-14 13:35:17] INFO  WEBrick 1.3.1
[2017-07-14 13:35:17] INFO  ruby 2.2.2 (2015-04-13) [i386-mingw32]
[2017-07-14 13:35:17] INFO  WEBrick::HTTPServer#start: pid=13608 port=1234
I, [2017-07-14T13:35:28.695586 #13608]  INFO -- : Cleared interactions before example ""
I, [2017-07-14T13:35:28.732112 #13608]  INFO -- : Cleared interactions before example ""
I, [2017-07-14T13:35:28.740117 #13608]  INFO -- : Registered expected interaction GET /animals/1
D, [2017-07-14T13:35:28.740618 #13608] DEBUG -- : {
  "description": "a request for UserA",
  "providerState": "UserA exists and is not an administrator",
  "request": {
    "method": "get",
    "path": "/animals/1"
  },
  "response": {
    "status": 200,
    "headers": {
    },
    "body": {
      "first_name": "Billy",
      "last_name": "Goat",
      "animal": "goat",
      "age": 21,
      "gender": "M",
      "location": {
        "description": "Melbourne Zoo",
        "country": "Australia",
        "post_code": 3000
      },
      "eligibility": {
        "available": true,
        "previously_married": false
      },
      "interests": [
        "walks in the garden/meadow",
        "munching on a paddock bomb",
        "parkour"
      ],
      "id": 1
    }
  }
}
W, [2017-07-14T13:35:28.761633 #13608]  WARN -- : Verifying - actual interactions do not match expected interactions for example "".
Missing requests:
        GET /animals/1


W, [2017-07-14T13:35:28.761633 #13608]  WARN -- : Missing requests:
        GET /animals/1

Is this a verification issue or is my code wrong? (if my code is wrong then how can I fix it?)

@matthewbalvanz-wf
Copy link
Contributor

@pawelszk I think what's happening is when the contract runs its hitting your locally running animal service instead of the mock service. At the top when you create the mock service:

pact = Consumer('Consumer').has_pact_with(
    Provider('Provider'), host_name='localhost', port=1234)  
# Don't forget to start the service and register the method to stop it when Python exits
pact.start_service()
import atexit
atexit.register(pact.stop_service)

It will start on port 1234. Later on in your test you create the URL:

url = 'http://localhost:8081/animals/1'

Which is used for the request before you enter the pact context and inside it. You would need to adjust the URL in the second request to point it to the running mock service instead:

url = 'http://localhost:1234/animals/1'
with pact:
    result = requests.get(url)

That should connect to the mock service instead of the animal service and it will then register the missing request.

@pawelszk
Copy link
Author

Thanks 👍
After changing the port number to 1234 test passes -> pact file is being generated correctly.

The main issue was in my understanding of how contract tests are working. That is why in the second request I was hitting the animal service instead of pact mock service.

Regarding starting and stopping mock service:
I am getting errors while execution of pact.start()
the process.returncode is 1 that is probably because I am using Windows and I have python installed under C:\\Program Files (x86)\\Python36-32\\lib\\site-packages\\pact\\bin\\pact\\bin\\pact-mock-service.bat
So right now I am using standalone pact mock service which I have downloaded from here: https://github.com/pact-foundation/pact-mock_service/releases

@matthewbalvanz-wf
Copy link
Contributor

@pawelszk It sounds like there might be an issue in the Ruby application that pact-python is calling when running on Windows. #40 was opened to track the details of that.

@bethesque
Copy link
Member

Yup, I'm trying to fix this. Really hoping a windows expert can give me a hand with working out how to shut down a process gracefully (sigterm) rather than using sigint.

@pawelszk
Copy link
Author

@matthewbalvanz-wf @bethesque
Yes, that is right I am receiving:
'fork': fork() function is unimplemented on this machine (NotImplementedError)
while I am trying to run (on Windows) following code:

pact = Consumer('Consumer').has_pact_with(Provider('Provider'), host_name='localhost', port=1234)  
pact.start_service()

I was trying to run the same code on ubuntu and I was not observing this issue.

@bethesque
Copy link
Member

I'm pretty close to getting some very ugly code working on windows.

@bethesque
Copy link
Member

This will be closed by #41

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