Tuesday, October 10, 2006

Network Load Balancing

Setting up Network Load balancing using window 2003 Enterprise servers
In this example we will use two cluster hosts which will be using IIS to publish some web pages. Host1 IP address 192.168.1.197 And Host2 IP address is 192.168.1.198. We will use one virtual IP address for this cluster 192.168.1.191.

•    Open up Network Load Balancing Manager
•    Right Click -> ‘New Cluster’ or Menu ‘Cluster’ -> ‘New’
•    Enter clustering IP address, subnet mask and full Internet Name
•    Select the cluster operation Mode. This can be Multicast with IGMP support
•    Enter any additional IP address this cluster needs to manage.

•    Connect to host. Pass the Host’s address and press connect. Upon pressing connect; it will list all the interfaces available for host. Choose interface to connect to.
Note: if you get ‘No interfaces available for installing a new cluster’ error and if you are using a imaged/cloned servers then error could be because the servers that are created by using an image may have the same Network GUIDs in their registry if the network adapter is installed as part of the image.
Resolution: re-install the network adapter for subsequent nodes on servers created using Image/Clone.
Kb 828258
•    Set the priority, Ip address and subnet mask for this host.
•    Once submitted it will take few moments to update this to relevant adapters.

•    Once cluster is created you can add more hosts to it by right-click on cluster -> ‘Add Host to Cluster’. Follow the same step as from ‘Connect to host’
•    From the client’s machines you should be able to access host machines using ‘Cluster IP’.

Wednesday, September 20, 2006

Mock Object for Testing

 
    Mock: an imitation, pretend, mimics somebody

    I have been using unit testing for some time now, but never really bothered about mock objects. Over time when projects grow, so do unit tests and then unit tests began to look more like integration tests. Objects would call other objects, which in turn calls others all the way to database. Hence after most of the tests, there is a needed to reset database. So to do this we would put some script/db calls in test’s setup and it all works fine like a well oiled machine. Sometimes when we see an occasional red in tests, we can fire up my debugger attach it to NUnit and find the problem.
    So what's the problem? Well unit tests are ‘unit’ tests not integration tests or uat. They should be testing only a unit of code not the whole end to end application. When we see red in NUnit we should be able to know which object’s which function is acting up from tests itself. Hence there is a need to isolate each object and test it separately; here is where Mock object comes in. Mock object is a runtime object which can imitate as some object in a controlled way. It also allows setting mock responses. With mock objects we would be testing only single unit and not whole application. 
   There are lot of frameworks which provides mock object functionality Rhino-mocks , EasyMock.Net, NMock, NUnit.Mocks TypeMock.Net etc, most of them offer similar functionalities
Here is a sample using Nunit.Mocks

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NUnit.Mocks;
namespace TestMockObjects
{
    public interface ISomeInterface
    {
       string SomeFunction();
    }

    [TestFixture]
    public class SomeTestClass
    {
        DynamicMock mockObject;
        ISomeInterface someObject;
        [SetUp]
        public void SetUp()
        {
            mockObject = new DynamicMock(typeof(ISomeInterface));
            someObject = (ISomeInterface)mockObject.MockInstance;
            mockObject.SetReturnValue("SomeFunction", "I am Mock");
        }

        [Test]
        public void TestMock()
        {
            Assert.IsTrue("I am Mock" == someObject.SomeFunction());
        }
   }
}

Saturday, September 16, 2006

Working with Directory Services using .Net

DirectoryEntry and DirectorySearcher are major classes and most of time only these two are needed to work with Directory Service. Use following steps to retrieve user list from Directory Service
  • Use DirectoryEntry and bind using a query. Domain user’s credential is required only when user account running this query is not a domain user.
    DirectoryEntry root = new DirectoryEntry("LDAP://<domainName>/CN=Users,DC=<DomainName>", "<DomainName>\\<domainUser>", "<Password>");
  • Create DirectorySearcher and pass the DirectoryEntry earlier binded to this object. We need this so that we can use a filter to get only users and not groups.
    DirectorySearcher searcher = new DirectorySearcher(root);
  • Set a filter.
    searcher.Filter = "(objectClass=user)";
  • Fire a query.
    SearchResultCollection userList = searcher.FindAll();
  • Retrive the user information from the result collection.
    foreach (SearchResult result in userList)
    {
    DirectoryEntry userEntry = result.GetDirectoryEntry();
    string loginID = (string)userEntry.InvokeGet("samAccountName");
    string userName = (string)userEntry.InvokeGet("displayName");
    string emailID = (string)userEntry.InvokeGet("mail");

    }