You Would Never Know Where The Bug Is

Screenshot from 2016-07-05 13-21-26

If you can’t reproduce the bug you would never know where the bug is!

I realized exactly the same when I have been working on a bug of Bodhi. We were pretty clueless about why the bug has arrived. After working on it for long I now believe that I am able to figure out where the bug is, Though the work is still going on. But we are pretty closer to it now. The current state is https://github.com/fedora-infra/bodhi/issues/855.

If we were not be able to reproduce to the bug we would never know why the bug had arrived and believe me it was not really identifiable this time! So before working on patch for a bug we should always try to reproduce it first.

Why Reproduce Bugs?

If you can’t reproduce the bug you would never know where the bug is. Patch doesn’t work just on guess. You are expected to be failed to create a patch if you fail to reproduce the bug.

If you don’t go through the steps when the bug is arrived it is technically impossible to create a patch. After you know why the bug has arrived it is possible for you to fix it as well. If you can’t reproduce the bug you need to create a patch just guessing the issue and you are not sure whether it is going to work or not! So it is not considered as a good practice.  Make sure you are not wasting your time on creating patch about which you are not sure.

Good Bug Reporting: Generally a good bug report contains all required info like when the bug has arrived, what was expected to happen and what has actually happened.

There are bugs about what we have no idea why they have occurred neither the users nor the developers. But the developers can always figure it out by reproducing the bug i.e, creating the same situation(In non-technical word). We will now see How Can We Reproduce Bugs.

How To Reproduce Bugs?

Unittest!

Unittest is one of the certain ways in order to reproduce bugs. I will show a simple demo.

We will write a simple program to check whether a number is odd or even.

Write the code:

Screenshot from 2016-07-05 20-57-56

Write the test:

Screenshot from 2016-07-05 20-58-08

Test is supposed to fail since 6 is an even number. Now we will see what happens:

Screenshot from 2016-07-05 20-54-52

Now we know why the error has occurred. In order to reproduce bugs we always want our test to fail.

This is just the fundamental of unitesting on reproducing bugs. You will know more when working on real world projects.

We can use Pdb also. Check this to see how you can use it.

For Bodhi I have used unittest and mock testing. Here is the testcase I wrote to reproduce the bug I mentioned above: https://paste.fedoraproject.org/386958.

Why is Testing important?

Why is writing tests for your code as important as writing code for your product?

We write Tests to check/test:

  • If the functionalities of the code you wrote or added are working properly.
  • If the new code you added is not breaking the old existing one.

So it’s better to catch the bugs of your code before it reaches to others 😉 .

I was working on fedora-bodhi for last few days and I chose an issue from there to work on. Though it looked simple enough but it really needed some tricks to solve the bug. The interesting thing is that it was not clearly visible that we have to apply some tricks to solve the issue. And I came to know that I had to apply some tricks after I wrote tests for the code I added.

So the solution for the issue has the following conditions:

  1. When an update of a build in bodhi is ‘pending’ and it requests for ‘testing’ and if that request is revoked, the status of that build should be set to ‘unpushed’.
  2. When an update of a build in bodhi is ‘testing’ and it requests for ‘stable’ and if that request is revoked, the status of that build should be set to ‘testing’.

I pushed a patch for it accordingly and received positive comment from threebean 🙂 . Then threebean and lmacken told me to write test for it, because “Sometimes what we see is not the truth!!” . If you look at the patch it really seemed to work. But when I wrote test for the code I added, the test was failing. As pingou suggested I tried printing some debugging statements to make sure if the test is exercising the code I added. Hence I came to know that the test is not going through the code :|. After struggling for a day I catched that my code was conflicting here which also has action ‘revoke’.

Then I modified my code to solve that conflict and received positive review from pingou 😉 .

Now I strongly feel why writing test is as important as writing code and testing makes you write better code as well 🙂 . The earlier patch was not behaving the way it was expected to behave. I wouldn’t realize that my earlier patch would fall if I didn’t write tests for it and if that patch would be merged the bug would be realized after it reached to masses. So isn’t it a smart practice to catch bug of your code by writing tests before it reaches to masses? 😉

The PR can be found here which is finally merged. Thanks to pingou for continuously reviewing my patches and thanks to threebean and lmacken for encouraging and helping me to write tests 🙂 .

 

How I got started with Tunir: Automating tests

This post not only explains how I got started with Tunir but also how I got started with unittest. My first unittest cases started with fedora cloud images.

It was a nice evening before Diwali. Kushal told on #dgplug that he needed new volunteers for Fedora Cloud SIG. Though I was totally clueless about cloud but I showed interest to join in because I would be able to learn something new and exciting. Next day, I and Farhaan had discussion with Kushal and he showed us how to write Python 3 unittest for Fedora cloud images (including atomic images).

Let me tell you about Tunir first. Tunir is a simple continuous integration system that helps us run automated test for cloud images. I would want you to visit Life Of Tunir which amazingly explains why we need Tunir and how it works.

So the aim is to convert manual testing into automated testing. Kushal gave us some shell commands and we convert them into Python 3 unittests. I have currently worked on NonGatingtests for cloud images.  And this way I learned how to write unittest. I can run test for qcow2 cloud base image and atomic image. I have the images in my local machine. I just have to start the  local server and follow the required steps to test the images. Whenever I got stuck at any test case Kushal and Rtnpro helped me a lot, Thanks to them 🙂 .

Now I am learning about mock. If test resources are not available mock helps us to replace it creating mock object. I am waiting for Kushal to give me the next task where I have to use mock.

If you also want to join as a volunteer for fedora cloud testing visit Need help to test Fedora Cloud images and Tunirtests/wiki 🙂 .