Changing System Time in Xen VM

Yesterday I wanted to change the system time in one of our test servers. Server was a Xen VM running redhat linux.  I used date --set="20 Dec 2012 00:30:00" command, but when I checked the date again it showed the previous date not the date I've set using date command. 

After doing some searching I found out that by default VM clock is synchronized with its host Xen Server; so changing date is not possible. We need to disable this time synchronization before updating date in the VM. To do that I had to execute following command in VM linux console.
/sbin/sysctl -e xen.independent_wallclock=1

After that I was able to change the system time using date command.

 

Stubbing Asynchronous Http Services using Wiremock

Wiremock is simple library which can be used to stub Http Services. It is a handy tool when you want to write unit tests against some Http service(eg: Http REST web service). It has direct support for JUnit using @Rule feature. We can write unit tests to stub different http requests and verify those requests and responses very easily.

One weakness of wiremock was it only supported synchronous  http request mocking(i.e: You send the request and stubbed wiremock server send the response). But I wanted to mock Asynchronous REST server. In asynchronous scenario in addition direct http response, server is initiating another call back request to the server. This method is commonly used by web service operations which take lots of time to process. Flow of such operation would look like follows.

When a client want to get some operation done;
  1) Client sends the request to the server
  2) Server accept that request and give that accepted response as direct http response
  3) Server do the processing and send the actual result as a new http request to the client

This feature is implemented in my wiremock forked repository. Still this is not being merged to main wiremock repository. So if you are interested in this you'll have to clone my forked repo, build it locally and publish the jar file to your local maven repository.

Stubbing asynchronous request is easy.


In most asynchronous http services, some kind of Id is being used to map original request with the asynchronous response. There can be two ways of doing this.
  1) Server generates transactionId and sends it with the immediate response
  2) Client sends the transactionId and server echo that when it sends back asynchronous response

If you want to use the second method with wiremock, you can use setEchoField method.

At the moment there is a one limitation with this. You can only use this facility with JSON requests and responses.

You can find the complete test class at github.
 

Useful GIT Commands

This is collection of useful GIT commands I use in my day today work. Just posting here to keep as my own reference.

Revert changes made to your working copy:

git checkout .

Revert changes made to the index (i.e., that you have added):

git reset

Revert a change that you have committed, do this:

git revert ...

View commits not yet pushed to remote:

git log --branches --not --remotes

Not pushed most recent commit with branch name:

git log --branches --not --remotes --simplify-by-decoration --decorate --oneline

Difference between two branches:

git diff --stat --color master..branch 

Here the order of branch name is merge-target..merge-source.

Create remote tracking branch:

git branch --track feature1 origin/my-remote-branch

Here git fetch/pull from feature1 branch will get the updates from my-remote-branch

Create new local branch from remote branch:

git branch --no-track feature2 origin/master

Push local branch to remote repository:

git push origing feature2

Checkout specific file from differrent branch:

git checkout remote/branch path/to/file

 

Exclude resource files from jar file created with SBT

I use SBT to build my scala projects. In addition to scala source files I have some resource files in my project. Those are some configuration files and localization resource files.

When I build the jar file using SBT package command all of those resource files also get included in the jar file. When those resource files are included inside the jar, modifying them becomes difficult. I have to extract the jar file, edit them and create the jar file again.

Ideal way of handling resource files are not to include them in the jar file, rather keep them separately and include them in the class path when running the application. How to exclude them from SBT package task was the problem I had. After some googling I found out that this can be achieved by including mapping task in the build. Following is sample build file which uses mapping to exclude various types of resource files.


val excludeFileRegx = """(.*?)\.(properties|props|conf|dsl|txt|xml)$""".r

  lazy val myapp = Project(id = "myapp", base = file("myapp"),
    settings = baseSettings ++ Seq(
      name := "My App",
      mappings in (Compile, packageBin) ~= { (ms: Seq[(File, String)]) =>
        ms filter {
          case (file, toPath =>{
            val shouldExclude = excludeFileRegx.pattern.matcher(file.getName).matches
           // println("===========" + file + "  " + shouldExclude)
            !shouldExclude
          }
        }
      },
        libraryDependencies ++= Seq (dispatch),
      libraryDependencies ++= testDependencies))

 

Solving no-interface issue in Wireshark on Ubuntu 11.10

Recently I switched to Ubuntu 11.10 as OS in my development machine. Previously I was using Fedora 12. When I installed Wireshark in ubuntu and run it, wireshark didn't show any interfaces which can capture packets. I think it is because wireshark was not run as root user. In fedora when I start wireshark it  ask to run as root and get the password. In ubuntu it was not like that.
So after doing some googleing found a solution at a blog post by tavshed.

sudo groupadd wireshark
sudo usermod -a -G wireshark YOUR_USER_NAME
sudo chgrp wireshark /usr/bin/dumpcap
sudo chmod 750 /usr/bin/dumpcap
sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/dumpcap
sudo getcap /usr/bin/dumpcap
sudo chmod +xs /usr/bin/dumpcap