Implemented AzureBlobStorageProvider

Added unit tests
Created a specific solution with Azure dependencies

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-04-23 17:39:23 -07:00
parent 152d8dea15
commit 9efcf65058
11 changed files with 807 additions and 4 deletions

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AzureSDK" value="C:\Program Files\Windows Azure SDK\v1.1\"/>
</appSettings>
</configuration>

View File

@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{1CC62F45-E6FF-43D5-84BF-509A1085D994}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Orchard.Azure.Tests</RootNamespace>
<AssemblyName>Orchard.Azure.Tests</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="Microsoft.WindowsAzure.ServiceRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="Microsoft.WindowsAzure.StorageClient, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="nunit.framework, Version=2.5.2.9222, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\nunit\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Storage\AzureBlobStorageProviderTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Orchard.Azure\Orchard.Azure.csproj">
<Project>{2505AA84-65A6-43D0-9C27-4F44FD576284}</Project>
<Name>Orchard.Azure</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard\Orchard.Framework.csproj">
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
<Name>Orchard.Framework</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Orchard.Azure.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Orchard.Azure.Tests")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("21e6aa35-d13d-495b-af35-bc066a1a8bf2")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,190 @@
using System;
using System.Configuration;
using System.IO;
using NUnit.Framework;
using System.Diagnostics;
using Orchard.Azure.Storage;
using Microsoft.WindowsAzure;
using System.Linq;
using Microsoft.WindowsAzure.StorageClient;
namespace Orchard.Azure.Tests.Storage {
[TestFixture]
public class AzureBlobStorageProviderTests {
#region Azure Environment initialization
private Process _dsService;
[TestFixtureSetUp]
public void FixtureSetup() {
var count = Process.GetProcessesByName("DSService").Length;
if (count == 0)
{
var start = new ProcessStartInfo
{
Arguments = "/devstore:start",
FileName =
Path.Combine(ConfigurationManager.AppSettings["AzureSDK"], @"bin\csrun.exe")
};
_dsService = new Process { StartInfo = start };
_dsService.Start();
_dsService.WaitForExit();
}
CloudStorageAccount devAccount;
CloudStorageAccount.TryParse("UseDevelopmentStorage=true", out devAccount);
_azureBlobStorageProvider = new AzureBlobStorageProvider("default", false, devAccount);
}
[TestFixtureTearDown]
public void FixtureTearDown() {
if(_dsService != null)
_dsService.Close();
}
[SetUp]
public void Setup() {
// ensure default container is empty before running any test
DeleteAllBlobs();
}
#endregion
private AzureBlobStorageProvider _azureBlobStorageProvider;
private void DeleteAllBlobs() {
foreach(var blob in _azureBlobStorageProvider.Container.ListBlobs()) {
if(blob is CloudBlob) {
((CloudBlob) blob).DeleteIfExists();
}
if (blob is CloudBlobDirectory) {
DeleteAllBlobs((CloudBlobDirectory)blob);
}
}
}
private static void DeleteAllBlobs(CloudBlobDirectory cloudBlobDirectory) {
foreach (var blob in cloudBlobDirectory.ListBlobs()) {
if (blob is CloudBlob) {
((CloudBlob)blob).DeleteIfExists();
}
if (blob is CloudBlobDirectory) {
DeleteAllBlobs((CloudBlobDirectory)blob);
}
}
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void GetFileShouldOnlyAcceptRelativePath() {
_azureBlobStorageProvider.CreateFile("foo.txt");
_azureBlobStorageProvider.GetFile("/foot.txt");
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void GetFileThatDoesNotExistShouldThrow() {
_azureBlobStorageProvider.GetFile("notexisting");
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void DeleteFileThatDoesNotExistShouldThrow() {
_azureBlobStorageProvider.DeleteFile("notexisting");
}
[Test]
public void CreateFileShouldReturnCorrectStorageFile() {
var storageFile = _azureBlobStorageProvider.CreateFile("foo.txt");
Assert.AreEqual(".txt", storageFile.GetFileType());
Assert.AreEqual("foo.txt", storageFile.GetName());
Assert.That(storageFile.GetPath().EndsWith("/default/foo.txt"), Is.True);
Assert.AreEqual(0, storageFile.GetSize());
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void CreateFileShouldThrowAnExceptionIfAlreadyExisting() {
var storageFile = _azureBlobStorageProvider.CreateFile("foo.txt");
Assert.AreEqual(storageFile.GetSize(), 0);
_azureBlobStorageProvider.CreateFile("foo.txt");
}
[Test]
public void RenameFileShouldCreateANewFileAndRemoveTheOld() {
_azureBlobStorageProvider.CreateFile("foo1.txt");
_azureBlobStorageProvider.RenameFile("foo1.txt", "foo2.txt");
var files = _azureBlobStorageProvider.ListFiles("");
Assert.AreEqual(1, files.Count());
Assert.That(files.First().GetPath().EndsWith("foo2.txt"), Is.True);
}
[Test]
public void CreateFileShouldBeFolderAgnostic()
{
_azureBlobStorageProvider.CreateFile("foo.txt");
_azureBlobStorageProvider.CreateFile("folder/foo.txt");
_azureBlobStorageProvider.CreateFile("folder/folder/foo.txt");
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("").Count());
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder").Count());
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder/folder").Count());
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void CreateFolderThatExistsShouldThrow() {
// sebros: In Azure, the folder concept is just about checking files prefix. So until a file exists, a folder is nothing
_azureBlobStorageProvider.CreateFile("folder/foo.txt");
_azureBlobStorageProvider.CreateFolder("folder");
}
[Test]
public void DeleteFolderShouldDeleteFilesAlso() {
_azureBlobStorageProvider.CreateFile("folder/foo1.txt");
_azureBlobStorageProvider.CreateFile("folder/foo2.txt");
_azureBlobStorageProvider.CreateFile("folder/folder/foo1.txt");
_azureBlobStorageProvider.CreateFile("folder/folder/foo2.txt");
Assert.AreEqual(2, _azureBlobStorageProvider.ListFiles("folder").Count());
Assert.AreEqual(2, _azureBlobStorageProvider.ListFiles("folder/folder").Count());
_azureBlobStorageProvider.DeleteFolder("folder");
Assert.AreEqual(0, _azureBlobStorageProvider.ListFiles("folder").Count());
Assert.AreEqual(0, _azureBlobStorageProvider.ListFiles("folder/folder").Count());
}
[Test]
public void ShouldRenameFolders() {
_azureBlobStorageProvider.CreateFile("folder1/foo.txt");
_azureBlobStorageProvider.CreateFile("folder2/foo.txt");
_azureBlobStorageProvider.CreateFile("folder1/folder2/foo.txt");
_azureBlobStorageProvider.CreateFile("folder1/folder2/folder3/foo.txt");
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder1").Count());
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder2").Count());
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder1/folder2").Count());
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder1/folder2/folder3").Count());
_azureBlobStorageProvider.RenameFolder("folder1/folder2", "folder1/folder4");
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder1").Count());
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder2").Count());
Assert.AreEqual(0, _azureBlobStorageProvider.ListFiles("folder1/folder2").Count());
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder1/folder4").Count());
Assert.AreEqual(1, _azureBlobStorageProvider.ListFiles("folder1/folder4/folder3").Count());
}
}
}