Rechercher dans ce blog

Affichage des articles dont le libellé est jenkins. Afficher tous les articles
Affichage des articles dont le libellé est jenkins. Afficher tous les articles

dimanche 29 avril 2012

9. Unit-test and EasyMock

Now that the first functions have been implemented. It is key to verify they are working properly.

In this post, I will describe one unit test and explain how to use EasyMock to make sure that the functions are called the appropriate number of times and that the arguments are the one which are expected.

Maven

Let's start to include EasyMock and Junit in the maven configuration. In the project pom.xml, we have to add the following lines:
  
37       <dependencies> 
38           <dependency> 
39               <groupId>junit</groupId> 
40               <artifactId>junit</artifactId> 
41               <version>4.8.2</version> 
42           </dependency> 
43           <dependency> 
44               <groupId>org.easymock</groupId> 
45               <artifactId>easymock</artifactId> 
46               <version>2.0</version> 
47           </dependency> 
48       </dependencies>

Now that it is done, we can use junit and easy mock in our modules.

Unit test sample

Here is a simple example showing how to verify that the YoctoHub object successfully refresh its content.
 
39    
40       private YoctoTemplate yoctoTemplate; 
41    
42       @Before 
43       public void setUp() throws IOException { 
44           yoctoTemplate = EasyMock.createMock(YoctoTemplate.class); 
45    
46    
47           YoctoMap content = RestYoctoMock.getMap(RestYoctoMock.MAIN_JSON); 
48           EasyMock.expect(yoctoTemplate.query("/api.json")).andReturn( 
49                   content).once(); 
50    
51           content = RestYoctoMock.getMap(RestYoctoMock.COLOR_JSON); 
52           EasyMock.expect(yoctoTemplate.query("/bySerial/YRGBLED1-01934/api.json")).andReturn( 
53                   content).anyTimes(); 
54    
55    
56       } 
57    
58    
59       @Test 
60       public void testRefresh() throws Exception { 
61    
62           EasyMock.replay(yoctoTemplate); 
63           YoctoHub hub = new YoctoHub(yoctoTemplate); 
64           hub.refresh(); 
65           hub.findAll(); 
66    
67           Collection<YoctoObject> list = hub.findAll(YoctoProduct.YOCTO_METEO); 
68           for (YoctoObject object : list) 
69               System.out.println(object); 
70    
71           EasyMock.verify(yoctoTemplate); 
72       }

First, let me describe the overall structure of the junit tests:

  • At line 42, @Before means that this function must be called before each test to make sure tho initialize the objects. There is usually a misconception that it is called only once.
  • At line 59, @Test informs the junit framework that this function must be tested. Please note that we can add parameters to the function like expectation of exception.


In this example, we introduce a few items:
  • At line 44, we initialize a mock object: that means it is an object that will share the same API but that will return the results we expect
  • At line 48: we declare that the function query with parameter "/api.json" will be called exactly once and that the return of this function will be a constant.
  • At line 62, we inform the mock object that it must be ready to be called
  • At line 71, we verify that all the objects have been called the expected number of times
What are the advantages of EasyMock against a built-in mockup object. Well, they are numerous:
  • Only the expected function are mocked up, you do not end up with thousands of empty functions that are not needed for the test
  • You can specify the exact number of times a function will be called
  • You can reuse the mockup at many place and only need to called replay to reset the object.
  • The errors are pretty well described and it is easy to understand the issue. 
As we have already configured jenkins, we should already be able to see the results of the tests in an automated way:


lundi 23 avril 2012

5. Jenkins (2/2)

Jenkins is now installed and configured. We will now configure it to work with Github and build the yocto-meteo project.

You should be able to access Jenkins through the previous URL on port 8080. For my case it is: http://192.158.1.59:8080


  1. Go in the plugins tab and select github plugin.
  2. Select install and restart jenkins afterward
Jenkins is now up and running but not configured yet. 

  1. Go in the configuration tab
  2. Jenkins virtual appliance is shipped with Sun java 1.6. Let's set it in the JDK section:
    • Select a name for the jdk
    • Unset the "Install automatically" flag
    • Set the JAVA_HOME path with /usr/lib/jvm/java
  3. Maven is available by default but set it to install it automatically in the Maven section
  4. In GitHub plugin:
    1. Set "Let Jenkins auto-manage hook URLs"
    2. Set your credentials to github
  5. Select Save. 
Now, we have a jenkins virtual appliance fully functional but we still need to configure the project... We will now create a build and junit job


  1. Select "New Job". 
  2. Set the name "yocto-meteo" and set the radio button: "Maven project". You should then see a big page where you can configure the job
  3. Select Git as the source code repository
  4. Enter the Git URL: https://github.com/jfontignie/yocto-meteo
  5. Select "Build when a change is pushed to GitHub. The build will be automatically performed every time submit something. It is very useful if you send an email when an issue is found. 
  6. Select "Build periodically" and enter "0 3 * * *". It will perform a build every night at 3 AM.
  7. Select "Check the versioning tool" and enter: "*/5 * * * *" in order to check for new changes every 5 minutes

Congratulations, you have now a build environment and a source code repository. We can now really start the project.



4. Jenkins (1/2)

Now, that we have the source code repository. I would like to put in place a continuous integration environment. An environment that I like and which is pretty easy to use is Jenkins. It provides automatic build verification, junit tests, etc.

In this post, I will explain:


  1. How to install Jenkins
  2. How to configure Jenkins
  3. How to use Jenkins

If you already know how it works, forget this post and read the next one!

Jenkins installation

Normally, we should install a new OS, configure the OS, install the Jenkins dependencies, configure the Jenkins dependencies and so on, and so on... It would take ages to do it and it is absolutely not interesting. In order to go faster, we will install a virtual appliance and configure only the minimal...

A virtual appliance is a virtual machine (VM) already configured that we can boot in an hypervisor (in my case VMWare workstation). The first boot will configure a few things on the VM and we can use it afterward.

Download the Jenkins virtual appliance

The Jenkins virtual appliance can be downloaded here: http://susestudio.com/a/u1OC8F/jenkins. There are a few login steps to perform but eventually you should get the VM. In my case, it was called: Jenkins-i686-0.0.5.vmx.tar.gz.

  1. Download the virtual appliance
  2. Decompress the file, you should get a folder containing two files:
    • vmx: is the configuration of the VM.
    • vmdk: the VM's virtual disk
  3. Let's copy the folder in the VMWare "Virtual Machines" folder

Start Jenkins virtual appliance

Now that we have downloaded, decompressed and moved the folder at the correct location, let's start the virtual appliance for the first time...

  1. Start VMWare workstation
  2. Go in File->Open
  3. Select the vmx file in the folder
  4. Normally, you should have a new VM in your VMs list.
  5. Let's start the virtual appliance.

Configure the virtual appliance

The first boot is very important in virtual appliances as it will configure the virtual machine and if things go wrong, it may be pretty complex to reset some settings. 

  1. The first time you start, you should be asked to upgrade the virtual machine: Select "Upgrade". Usually, you do not have to bother about this option as it may be useful only if you plan to downgrade to an older VMWare version which is rarely the case.
  2. When starting, you should get the following screen:
  3. If you did not press any key, the configuration should start. If it does not, press <enter>
  4. After a couple of seconds, you should get the Timezone configuration panel. Select your timezone and select "Next". Please note that the mouse should not work and you will have to play with the tab button to select the proper timezone...
  5. After a couple of more seconds, you should get the following screen. Congratulations, your VM is up and running.
Now that the virtual appliance is running, you should be able to configure the virtual machine using your browser which will be easier...


  1. On the VM, you should see a green line. Go  on this URL with your browser... The username is "root" and password "ch@ngeme"
  2. You should get an error message related to Java, do not bother about it for the moment... Configure the password, the users, etc.
  3. Now, let's go to the "Software Repositories" panel and disable webyast and java. It seems that there was a bug in webyast and upgrading it was causing it to crash.... 
  4. It will take a very long time...
  5. After a while, everything should be installed, reboot the VM now.
This concludes the install of the Jenkins virtual appliance. In next post, we will see how to configure Jenkins.

jeudi 12 avril 2012

1. Introduction

When I look for projects, I usually found some code samples on a particular technology but it is quite rare to find:
  • How to install the test framework
  • How to put the project in production
  • How to use the VCS
The goal of this blog is to introduce a small project and explain the different steps of development. I will not go in details and will only provide a small overview of the project.

Overview

The goal of the project is to provide a map where you can see all the yoctopuce devices from the people who have come on the page. Yoctopuce provide a meteo sensor device that can be plugged through USB. In this project, we would like to have the following flow:


  1. An user goes on the web page with his browser
  2. The web page is loaded and tries to contact known yoctopuce device URL: http://127.0.0.1:4444
  3. If it is successful
    1. The Web page analyzes which yoctopuce devices are plugged
    2. The Web page stores the data in the database with the current location
  4. The webpage loads and displays the world map with all the yoctopuce devices stored in the database




In this blog, I will describe:
  1. Prepare GIT to store the source code
  2. Configure the maven project
  3. Configure and install a virtual appliance with Jenkins
  4. Run a code coverage with Cobertura in Jenkins
  5. Use yoctopuce devices
  6. Basic introduction of java API for yoctopuce
  7. GWT introduction
  8. Put in production with google app engine