Sunday 11 May 2014

configure CruiseControl.NET(ccnet) and integrate with mstests using msbuild

Intro:

To setup a continuous integration process for a sample project and integrate mstest into the build.

Continuous integration:
Automated process for preparing your code for deployment generally involves running build
Scripts and unit tests, staging a release, tracking changes, and notifying dev. team of problems.

CruiseControl.NET:
(CCNet) is an Open source continuous integration (CI) server.it automatically build with every commit,
run unit tests, record results and track build history.

Build process:
Developer commits code and build server retrieves from source control, compiles code, run unit tests.

Build script:
Script whose primary purpose is to compile your main project and prepare it for release
ex:msbuild

Integrating MSTest into your build

You can use MSTest.exe to execute your MSTest unit tests from the command-line. You can find a list of the MSTest.exe command-line options here. You will need to using the /resultsfile: command-line option in order to get MSTest to log the test results to file so that they can be included in the build report.
MSTest.exe can be invoked from your MSBuild script.
Installation:
Let's try to set up a CCNet server to continuously integrate our project.
·         Go to http://sourceforge.net/projects/ccnet/ and Download CruiseControl.Net-1.8.4.0-Setup.exe
·         Run this exe with default options, after that a service will be created with name “CruiseControl.NET Server” and also a folder “CruiseControl.NET” will be created in program files(in general C:\Program Files (x86)\).
·         CCNet is able to work standalone or as a Windows service. If you plan to use CCNet as a production CI server, it surely should run as a Windows service. Standalone mode is very helpful while configuring and troubleshooting the server. Note that the CCNet Windows service will not be started automatically after the installation. for this demo we are running ccnet as windows service.
·         Go to services.msc and click start on CruiseControl.NET Server service.

After running the setup, a site named ccnet will be created in iis. If that doesn’t happen follow the below steps:
·         Open inetmgr(make sure iis installed in your machine) from run and go to “Sites” and right click on it,Select Add Web Site
·         Give “ccnet” against Site name field,
·         Give “C:\Program Files (x86)\CruiseControl.NET\webdashboard” against Physical Path field,
·         Give ”523” against Port field
·         Leave remaining with defaults and click on Ok. Now a site with name “CCNet” will be created in IIS.
Now the CCNet installer is able to prepare everything on the IIS for the Web Dashboard. This way, you will only have to configure your CCNet instance to make it work.
·         Expand ccnet and right click on ccnet site and select “Switch to content view”
·         Now files will appear on right panel, among them select default.aspx and right click on it and select browse.
·         Browser will open a site http://localhost:523/ViewFarmReport.aspx

If error shows up like : This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false". 

Follow the below steps to resolve:
  • Click "Start button"
  • in the search box, enter "Turn windows features on or off"
  • in the features window, Click: "Internet Information Services"
  • Click: "World Wide Web Services"
  • Click: "Application Development Features"
  • Check (enable) the features. check all but CGI
We have not configured project for cruise control yet. So far we have installed cruise control.net as windows service and created ccnet site in IIS.

Working environment with a repository:

Creating a project that contains unit tests

·         Create a unit test project from visual studio and created a class with simple assert(not a real scenario, its only a sample to test) as below:

[TestClass]
    public class Test
    {
        [TestMethod]
        public void Test1()
        {
            Assert.AreEqual(“test”, “test”);
        }
    }
Solution and project name is “sampletest”.

Choosing source control and creating repository and pulling data from source control to cruise control:

I have chosen tortoise svn as my source control.Follw the below steps to run it
·         Go to http://tortoisesvn.net/downloads.html and download TortoiseSVN 1.8.6.exe
·         Run the exe with default options, a folder with name “svn_repos” will be created in the respective directory (in general C:\)

Some basics on tortoise svn:

SVN Usage Diagram:

·         This diagram illustrates the basic usage of an SVN repository.  Developers commit changes to the SVN and generally one person would be responsible for exporting to production at regular intervals.
SVN repo vocab:
·         Import: To copy files into a repository.
·         Checkout: To copy files out of a repository with version data. The inital checkout creates your working copy. This is to copy the code from the repository to the computer you use for developing the code.
·         Commit: To copy files that have a modified date more recent than what is in the repository. Commit also perform actions previously indicated by various function like add, delete, move, rename.
·         Update: To copy files from that repository that have more recent changes than your local version.

Importing your Code:
·         Select a project and right click on it and select import and give file:///C:/svn_repos/trunk/sampletestrepo against URL of repository field and click on ok.( Navigate to the sampletest project we have created and right click on it and select import.here am using my local system as SVN repository(web server) so file:///)
Checkout:
·         Check out in D: drive by right click and select SVN Checkout from context menu. Give “D:\Machine1Repo” against Checkout directory and don’t change the URL of repository field (it should be filled with “file:///C:/svn_repos/trunk/sampletestrepo”).click ok.
·         Here using Machine1Repo as one of the dev. machines.
·         Check out in E: drive by right click and select SVN Checkout from context menu. Give “E:\Machine2Repo” against Checkout directory and don’t change the URL of repository field (it should be filled with “file:///C:/svn_repos/trunk/sampletestrepo”).click ok.
·         Here using Machine2Repo as ccnet server.

Configure ccnet:

·         Basically tortoise svn is a shell extension. It cannot be called from command prompt. So it should be downloaded manually.
·         To run svn.exe from command prompt you have to install subversion from http://www.collab.net/downloads/subversion, download Subversion 1.8.8 (Windows 64-bit)

All the configuration files we’re going to talk about are placed or are to be placed in C:\%ProgramFiles%\CruiseControl.NET\server.

Open ccnet.config and modify it as shown in below:

ccnet.config is an xml file with a root element named <cruisecontrol> and a child element, named <project>, for each set of activities that we want CruiseControl.NET to execute, as shown in the following example:
<cruisecontrol>
<project name="project1">
    ...
  </project>
<project name="project2">
    ...
  </project>
</cruisecontrol>
Source Control Block:
Source Control configuration block tells CruiseControl.NET that the project named “CruiseControlSampleTestDemo” is bound to a Subversion repository.
This means that the task performed when executing this project depends on the status of that particular Subversion repository.As soon as CruiseControl.NET detects a new revision in the repository it updates its working copy and executes the tasks related to the current project.
Here we are getting data from svn repository(file:///C:/svn_repos/trunk/sampletestrepo) and pushing the it to ccnet server(E:\Machine2Repo).we are using (D:\Machine1Repo) as dev. machine. So if I change code in D:\Machine1Repo\ sampletestrepo project and commit to the svn repository and force a build. Ccnet config will get the latest changes from svn repository(file:///C:/svn_repos/trunk/sampletestrepo) and pushes them to (E:\Machine2Repo\ sampletestrepo).

<sourcecontrol type="svn">
   <executable>C:\Program Files\CollabNet\Subversion Client\svn.exe</executable>
    <trunkUrl>file:///C:/svn_repos/trunk/bankrepo</trunkUrl>
    <workingDirectory>E:\Machine2Repo</workingDirectory>
  </sourcecontrol>

Bat file:
batch file is the name given to a type of script file, a text file containing a series ofcommands to be executed by the command interpreter.contains list of commands like del,start.

Include the below code in deltrx.bat:
del E:\Machine2Repo\ sampletestrepo \test.trx

Tasks Block:
The tasks block represents how the build of the project actually takes place.In our example we will use an MsBuild Task to accomplish the main purpose of our project, which is to compile the versioned Visual Studio solution.
After that we will use an
 Executable Task to run our unit tests, if the build succeeds.

MsBuild Task:
Let’s have a look at the meaning of the xml nodes children of the <msbuild> node:
<executable>: contains the path to the msbuild executable file. You don’t really need to set it because the default value is the standard installation path:C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe.
<projectFile>: is the name of the project to build. MsBuild accepts a Visual Studio solution file as the project file to build. Obviously the MsBuild Task accepts it as well.
<buildArgs> This row provides additional command line arguments to MsBuild.


ccnet.config
--------------------------------------------------------------------------------------------------------------------------------------
<cruisecontrol xmlns:cb="urn:ccnet.config.builder">

<!—this name appears in web dashboard -->
   <project name="CruiseControlSampleTestDemo">
   <artifactDirectory>E:\Machine2Repo</artifactDirectory>
    <triggers>
     <intervalTrigger name="continuous" seconds="30" buildCondition="ForceBuild"   initialSeconds="30" />
    </triggers>
   <sourcecontrol type="svn">
   <executable>C:\Program Files\CollabNet\Subversion Client\svn.exe</executable>
    <trunkUrl>file:///C:/svn_repos/trunk/sampletestrepo</trunkUrl>
    <workingDirectory>E:\Machine2Repo</workingDirectory>
  </sourcecontrol> 
    <tasks>
<msbuild>
        <executable>C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe</executable>       
        <projectFile>E:\Machine2Repo\sampletestrepo.sln</projectFile>       
        <timeout>900</timeout>
        <logger>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll</logger>
      </msbuild>
<exec>
            <executable>E:\Machine2Repo\sampletestrepo\deltrx.bat</executable>
            <buildArgs></buildArgs>
            <buildTimeoutSeconds>30</buildTimeoutSeconds>
      </exec>
      <exec>           
      <executable>C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\MSTest.exe</executable>
            <buildArgs>/testcontainer:E:\Machine2Repo\sampletestrepo\bin\Debug\sampletestrepo.dll /resultsfile:E:\Machine2Repo\sampletestrepo\test.trx</buildArgs>
            <buildTimeoutSeconds>30</buildTimeoutSeconds>
      </exec>     
    </tasks>
   <publishers>
         <!--to get the test results in the dashboard we have to merge the results XML file -->
         <merge>
               <files>
                     <file>E:\Machine2Repo\sampletestrepo\test.trx</file>
               </files>
         </merge>
         <xmllogger />
</publishers>
  </project>
</cruisecontrol>


Save this file and go to ccnet in IIS. Refresh on ccnet site and click browse from default.aspx and it opens as shown in below.

The build fails because of the (failing test case) below code as

[TestClass]
    public class Test
    {
        [TestMethod]
        public void Test1()
        {
            Assert.AreEqual(“test”, “test1”);
        }
    }
Now, change that code to

[TestClass]
    public class Test
    {
        [TestMethod]
        public void Test1()
        {
            Assert.AreEqual(“test”, “test”);
        }
    }
Commit this change from  D:\Machine2Repo\sampletestrepo and click on “Force” from dashboard



Now the build succeeds as shown below:

Click on the record Last Build Time, and see the code change committed to the svn repository under Modifications since last build.