Pages

Integration Test Strategy for REST Services - Part II - Final


This is the second and final part of how to use a strategy of tests for your REST services.


Make things a little more beautiful

In the previous post we got a report for our tests:


Nothing wrong about it, but let show the results in a more beautiful way, using the Pipeline Plugin:

Go to Jenkins > Manage Jenkins > Manage Plugins > Add the plugin Build Pipeline Plugin.

Now, with the plugin installed, add a New View > Build Pipeline View:




It's important to configure the section Pipeline Flow with the initial job that will trigger this pipeline (in our case, Customers API): 


Now, our pipeline is configured to run based on the specific job:



At this point we have our pipeline configured to run our tests against the API. But, how can I run automatically the tests, every time some new code is pushed into my repository? Let's learn how to do this.


Add Git Support

To add support for git, go to Jenkins > Manage Jenkins > Manage Plugins > Add the plugin Git Plugin.

With this plugin installed, go to Jenkins > Customers API job > Configure > in the section Source Code Management, select the Git option:

Here, you can add your repository in GitHub. 


It's necessary to add a Credential to connect. Click Add > Jenkins:

Add you credentials (Username and Password) from GitHub, and confirm the creation of this Credential. 


Now you can select this credential to the respective branch you had configured. 

Just one observation, they are other ways to register the credentials like, creating a SSH key and adding in GitHub. I followed this easier and straightforward possible, but feel free to add your approach.


Webhooks?

Remember that, even when you push in GitHub, there is no way your container knows that some code was pushed. We can solve this using webhooks in GitHub, a way that allows us to configure some repository to trigger an event, each time the code is pushed.

Go to you repository in GitHub > Settings > Webhooks > Add Webhook:


You should add a Payload URL, corresponding to the address of you Jenkins container running. Now we have a problem, because how Github can communicate with a local container that it's running in you local machine?

ngrok

Ngrok is a multiplatform tunnelling, reverse proxy software that establishes secure tunnels from a public endpoint such as internet to a locally running network service while capturing all traffic for detailed inspection and replay. Basically we will expose an address that will be achievable by Github and our local container. The use is pretty simple.

  1. Go to http://ngrok.com and create and account;
  2. Extract the ngrok package in your machine;
  3. Each account will create an authorization token. You can see here;
  4. Go to the directory you added ngrok, and run the command: ./ngrok http 8080
Now the tunnel was created and an external address is available at this moment, pointing to our local Jenkins container:


Put all the things together

Now the tunnel was created and an external address is available at this moment, pointing to our local Jenkins container, you must configure two things to provide this communication.

With this plugin installed, go to Jenkins > Customers API job > Configure > in the section Build Triggers, select the GitHub hook trigger for GITScm pooling option:


Lets go back to GitHub. In the Manage webhook, add the generated ngrok.io address in the PayloadURL and confirm:



To confirm the the communication is occurring, you will see, just bellow this configuration, you can see the health of this routine, where you have the vision of the payloads, with the option to Redeliver the POST:


ngrok provides a way to verify this POST locally, so you can ensure everything occurred all right:


With these latest configurations, every time you push some code in your remote branch, a new job will be executed, generating the results in a beautiful pipeline :-)

---------------------------------------------------------------------------------

The source code is available here.

I hope you have enjoyed this small adventure and thank you for your time. Have an awesome day ;-)


Fabio Ono

No comments:

Post a Comment