How to automate test cases need reboot: a simple script solution

July 1, 2011 by · Comments Off on How to automate test cases need reboot: a simple script solution
Filed under: automation, test 

During the first 2 years in my career as a tester, I learned that for a tester, everything should be automated if possible. 2 years later I beginning my programming life and found that automation is a must skill for programmers too. There is lots of interesting things on my testings experience, here’s one of them.

The background: Sometimes we need to run some test cases, and each case must be run in a newly booted environment. Or when case B is depended on case A which need reboot after run. And we want automation of the process. (IT software installation, Antivirus performance test, etc is some practice requirement like this).

A simple way to automate such test case is : write the test cases in scripts, define all the
test cases script file name in an array scriptlist,  then run the script files whose name
is defined in the elements of the array one by one.  We can define an index for which script
is to be running in the array.  And each time we run a script file, we save the index, lets
say idx to a file on local disk.

Assum you have 4 scripted test cases to run in order:,,, (the script
language doesn’t matter, you can choose whatever you love). and after running of each case, need a system reboot.

We can write a new script called, and put it to the auto start. Then
run the script manually as a trigger for the starting of all these 4 cases. Then each one
will run and reboot, and continue to run the next one.

Here’s the 4 simple things should be contained in the, I’ll
demonstrate it in perl on windows, for linux some command must the changed, you know).

1).Initialize the index(idx) value
If next-file-torun.txt exist, try to read idx value from next-file-torun.txt,  else set $idx  value to zero.

my $idx = 0;
my @scriptlist=  ("","","","");

2). Save the idx value to a local file called next-file-torun.txt
The idx value indicates the file whose name is equal to scriptlist[idx] will be run.
echo $idx >next-file-torun.txt

3). Run the file indicated by scriptlist[idx] , and then increase idx by 1

my $filetorun = $scriptlist[$idx];

4). Reboot by restart command

system("shutdown -r -t 01");

Then script will be run, then reboot, then script will be run, then reboot and

This method is really useful on automation lots of software installation for IT if the
software need reboot. Also useful when we need to test some software performance  on a newly booted system, especially for antivirus performance test.

Here’s a sample script for running:

# The script would be run after system reboot

my @filelist =  ("","","");

my $idfile="next-file-torun.txt";
open READIN,"< $idfile" or die "can not open file:$!";
my $id = <READIN>;
close (READIN);
print "id=$id \n";

	print "all files has been run!\n";
	print "\x07\x07"; # ring the bell

# sleep some seconds to run the following scripts:
print "sleeping...\n ";

my $filetorun = $filelist[$id];
print "to run file:  $filetorun \n";

open OUT,"> $idfile" or die "can not open file:$!";
print OUT $id;
close (OUT);

system("$filetorun");  ### call the script
system("shutdown -r -t 01"); ### reboot the system
Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

How to automate virtual machine creation and runing on virtualbox by command line

June 29, 2011 by · 2 Comments
Filed under: linux, virtualization 

Many times we need to create a linux virtual machine by command line.
It is very useful for batch vm operation, automation, regression test, integration test…
Here’s the steps to create a functional linux virtual machine (works on windows host && linux host).

These steps has been tested on Debian squeeze x64 with kernel version of 2.6.32, and windows server 2003 x64 .
Note for windows users: Make sure you have put virtualbox directory to PATH variable. (Press Win+Break , click “Advanced”-> “Environment Variables”, find the PATH variable, and append virtualbox path to the variable.)

1.Create a virtualbox vm and register the vm

vboxmanage createvm -name testvm -register
vboxmanage showvminfo testvm

The showvminfo command shows information about created virtual machine.

2.Specifiy network adapter and setting boot order

vboxmanage modifyvm “testvm” –memory 768 –vram 64 –acpi on –boot1 dvd –nic1 bridged –bridgeadapter1 eth0

vboxmanage list ostypes

vboxmanage modifyvm “testvm” –ostype “linux26”

The list command shows all available system types. If you don’t want your vm connect to network directly by bridged network,
then change the arguments after –nicX, see manual:

3.Create virtual disk for file storage with specified size(2G)

vboxmanage createvdi –filename “/data/vm/testvm-disk01.vdi” –size 2048 –remember

4.Set boot up order and add an IDE controller
vboxmanage storagectl testvm –name “IDE Controller” –add ide
vboxmanage modifyvm “testvm” –boot1 dvd –hda “/data/vm/testvm-disk01.vdi” –sata on

Now a virtual machine has been created and register, let’s go to step 5 specify the booting iso file,
and boot into linux.

5.Specify ISO file as DVD (the installation iso or live iso)
I use tinycore, and put the iso at /data/vm.

Attach virtual disk testos.vdi to the 0th port at device 0:
vboxmanage storageattach testvm –storagectl “IDE Controller” –port 0 –device 0 –type hdd –medium “/data/vm/testvm-disk01.vdi”

Attach the ISO at the 1th port at device 0:
vboxmanage storageattach testvm –storagectl “IDE Controller” –port 1 –device 0 –type dvddrive –medium /data/vm/tinycore_3.6.iso

vboxmanage modifyvm “testvm” –dvd /data/vm/tinycore_3.6.iso

6、Boot the virtual machine

vboxmanage startvm “testvm”

For linux users , if error like “The character device /dev/vboxdrv does not exist, lease install the virtualbox-ose-dkms package and the appropriate headers, most likely linux-headers” occured.

Then you probably run the following command by root:
modprobe vboxdrv
modprobe vboxnetflt

To poweroff the virtual machine, just run the command:
vboxmanage controlvm testvm poweroff

To boot the virtual machine in headless, so it starts without virtual box gui, it is useful for virtual server.
And you can put the command to start up script, it works on windows and linux:
vboxmanage startvm “testvm” –headless

To show detail information of the virtual machine:
vboxmanage showvminfo testvm –details

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)