Rohit Gupta

Engaging talk on Microsoft Technologies ....My Resume

  Home  |   Contact  |   Syndication    |   Login
  41 Posts | 0 Stories | 52 Comments | 0 Trackbacks

News



Twitter












Archives

Image Galleries

Personal

Thursday, August 18, 2011 #

While developing applications for Windows Azure, we are all aware that we need to create Web and Worker Roles and use the Azure Compute Emulator (previously known as the Development Fabric) for running and testing applications in the local IIS server.

However to run unit tests against libraries that are being consumed from your web/worker roles we need to perform some preliminary steps before we can start using the CloudStorageAccount class within our Unit Tests.

The First step is to start the Storage Service if it is not already started which can be done in a method decorated with the AssemblyInitialze attribute.
The Second step is to call the CloudStorageAccount.SetConfigurationSettingPublisher method from a method decorated with the ClassInitialize attribute, so that we can then successfully use the CloudStorageAccount.FromConfigurationSetting("DataConnectionString") call from within any of the TestMethods.

The code to start the Storage Service can be something like the following:

   1: [AssemblyInitialize]
   2: public static void AssemblyInitialize(TestContext context)
   3: {
   4:     var count = Process.GetProcessesByName("DSService").Length;
   5:     if (count == 0)
   6:     {
   7:         var start = new ProcessStartInfo
   8:         {
   9:             Arguments = "/devstore:start",
  10:             FileName = @"C:\Program Files\Windows Azure SDK\v1.4\bin\csrun.exe"
  11:         };
  12:  
  13:         var proc = new Process { StartInfo = start };
  14:         proc.Start();
  15:         proc.WaitForExit();
  16:     }
  17: }

Before we can start using the _storageAccount from within our unit tests we need the following code to be configured and setup:

First in the app.config file in the AppSettings section we need to add the following entry:

   1: <configuration>
   2:   <appSettings>
   3:     <add key="DataConnectionString" value="UseDevelopmentStorage=true" />
   4:   </appSettings>
   5: </configuration>

Then in the ClassInitialize static method we need the following code:

   1: [ClassInitialize()]
   2: public static void Init(TestContext testContext)
   3: {
   4:     CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
   5:     {
   6:         var connectionString = ConfigurationManager.AppSettings[configName];
   7:         configSetter(connectionString);
   8:     });
   9: }

Finally in the TestMethod() we can use the _storageAccount and work directly against the Azure table/blob/queue storage as shown below:

   1: [TestMethod]
   2: public void FileExists(string pathWithFileName)
   3: {
   4:     pathWithFileName = pathWithFileName.Replace("\\", "/");
   5:     var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
   6:     _blobClient = storageAccount.CreateCloudBlobClient();
   7:     _blobClient.Timeout = new TimeSpan(1, 0, 0);
   8:     _blobClient.ParallelOperationThreadCount = 2;
   9:  
  10:     // Get the container
  11:     _blobContainer = _blobClient.GetContainerReference(_blobContainerName);
  12:     //Get the file from the blob container
  13:     var blob = _blobContainer.GetBlobReference(pathWithFileName);
  14:     Assert.Istrue(blob.Exists());
  15: }