Fault Injection to Web application or Windows service

I have been busy since this march when i moved to Azure team. since then, a newer version of TestAPI (v0.5) was released. we take a few bugs fix in this release. one of them is .net 4.0 compatible. in my previous post, i mentioned that you need to set the enviornment varible for .net 4.0 application. in v0.5, we fix this, so if you use v0.5 onward version, you can ignore that manual workaround. we also add another important feature called "global mode"  which enable you to inject fault into web applications or window service.

as I pointed out in prior post that to use fault injection API, you have to use the provided API to launch your application that you want to inject fault. The problem comes when you don't have the executable or you can't launch the executable, one of obvious example is web application or windows service application which are launched by windows system or IIS automatically. this is exactly the reason why we provided 'global mode'. in global mode, the fault injection will take effect on all processes running on this machine, so you don't have to launch your test application. however, you still need to enable the fault injection BEFORE web application or windows service starts. in this post, I will show you how to use global mode to inject fault into a web application step by step.

step 1: download the TestAPI v0.5  from https://www.codeplex.com/testapi

Step2: create you web application for test ( i will create a simple calculator )

            a. in visual studio, create a new web application project

            b. in the web page design view, add two testbox and one button and one label

            c. double click the button to open the code view. in the code, add the following codes:

 protected void Button1_Click(object sender, EventArgs e)

{

   Label1.Text = Add(Int32.Parse(TextBox1.Text),Int32.Parse(TextBox2.Text)).ToString ();

}

private int Add(int x, int y)

{

     return x + y;

}

            d. Ctr+F5 to run the application, in the web page, input two integers and click button, the correct result displayed.

 

Step3: inject fault using testapi

         a. in visual studio, create a console project and add the testapicore.dll to the reference

         b. define the fault injection rules/sessions and start global mode. since i don't have test automation for my web application, i just let it pause before press any key, and then manully test my web application

         static void Main(string[] args)

        {

            // Define rules

            string method = "WebApplication1.MyTest.Add(int,int)";

            ICondition condition = BuiltInConditions.TriggerOnEveryCall;

            IFault fault = BuiltInFaults.ReturnValueFault(0);

            FaultRule rule = new FaultRule(method, condition, fault);





            // Establish a session, injecting the faults defined by the fault rule(s)

            FaultSession session = new FaultSession(rule);



            // Enable Global mode

            FaultSession.SetGlobalFault(session);



            // Run your test automation here

            Console.WriteLine("press any key to exit....");

            Console.ReadLine();



            // After test automation complete, disble global mode

            FaultSession.ClearGlobalFault();



        }

         c. shift+F5 to build, saying the executable is named: mytestapitest.exe

           d.  copy mytestapitest.exe to the webapplication binary folder (in my case it's d:\webapplication1\bin). AND copy "FaultInjectionEngine" folder from your testapi download/unzipped folder to the webapplication binary folder too.

           e. run your test at administrator privilege: mytestapitest.exe          

           f. start a new visual studio and open your web application project and run it. in the web page, input two integers, you can see it always return 0 regardless what two integers you input.

 

Step4: press a key to exit your test. and start a new visual studio and run your web application again, the global mode shold be disabled and the application should run as normal.

 

a few things you need to be aware to make sure you can get this working in your own machine:

1. make sure your test running on elevated privilege

2. copy test executable and "FaultInjectionEngine" folder to your web application binary folder due to the 'one-place' issue (ya, still not fixed in v0.5, hopefully i can fix this in next release)

3. make sure to restart you web application or windows servcie AFTER enable global mode. in my case, i restart the visual stuido. if you run IIS on your test machine, you can run "iisreset" to restart the IIS. this is true to all other application under global mode. you have to restart the application/service or command window to let global mode take effect.

4. after test complete, do not forget to disable global mode. again, after disabling global mode, you have to restart the application/servie or command window to let global mode truely disabled.

 

with all of these, you should be able to use global mode to inject fault into web application/window service or in any situation that you can't launch test application.