Skip to content

Commit

Permalink
Merge pull request #427 from Microsoft/tomlm/serverStorageTests
Browse files Browse the repository at this point in the history
change unit tests to run unit tests if environment variable or emulat…
  • Loading branch information
cleemullins authored Apr 14, 2018
2 parents 52e5f67 + fc84514 commit 1ee10d7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
using Microsoft.Bot.Builder.Core.Extensions;
using Microsoft.Bot.Builder.Core.Extensions.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage;

namespace Microsoft.Bot.Builder.Azure.Tests
{
[TestClass]
[TestCategory("Storage")]
[TestCategory("Storage - Azure Blob")]
public class BlobtorageTests : StorageBaseTests
public class BlobStorageTests : StorageBaseTests
{
private IStorage storage;

Expand All @@ -24,8 +24,8 @@ public class BlobtorageTests : StorageBaseTests
private static TestContext _testContext;

private static string emulatorPath = Environment.ExpandEnvironmentVariables(@"%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\azurestorageemulator.exe");
private const string noEmulatorMessage = "This test requires Azure Storage Emulator! go to https://go.microsoft.com/fwlink/?LinkId=717179 to download and install.";
private const string DataConnectionString = "UseDevelopmentStorage=true";
private const string noEmulatorMessage = "This test requires Azure Storage Emulator! go to https://go.microsoft.com/fwlink/?LinkId=717179 to download and install.";
private string connectionString = null;
private static Lazy<bool> hasStorageEmulator = new Lazy<bool>(() =>
{
if (File.Exists(emulatorPath))
Expand All @@ -51,81 +51,81 @@ public static void SetupTests(TestContext testContext)

private string _containerName;

private const string emulatorConnectionString = "UseDevelopmentStorage=true";

[TestInitialize]
public void TestInit()
{
if (hasStorageEmulator.Value)
connectionString = Environment.GetEnvironmentVariable("STORAGECONNECTIONSTRING") ?? emulatorConnectionString;

if (connectionString != emulatorConnectionString || hasStorageEmulator.Value)
{
_containerName = TestContext.TestName.ToLowerInvariant().Replace("_", "") + TestContext.GetHashCode().ToString("x");
storage = new AzureBlobStorage(DataConnectionString, _containerName);
_containerName = TestContext.TestName.ToLowerInvariant().Replace("_", "") + TestContext.GetHashCode().ToString("x");
storage = new AzureBlobStorage(connectionString, _containerName);
}
}

[TestCleanup]
public async Task BlobStorage_TestCleanUp()
{
if (storage != null)
{
var storageAccount = CloudStorageAccount.Parse(DataConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(_containerName);
await container.DeleteIfExistsAsync();
if (storage != null)
{
var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(_containerName);
await container.DeleteIfExistsAsync();
}
}

public bool CheckStorageEmulator()
public bool HasStorage()
{
if (!hasStorageEmulator.Value)
Debug.WriteLine(noEmulatorMessage);
if (Debugger.IsAttached)
Assert.IsTrue(hasStorageEmulator.Value, noEmulatorMessage);
return hasStorageEmulator.Value;
return storage != null;
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task BlobStorage_CreateObjectTest()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._createObjectTest(storage);
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task BlobStorage_ReadUnknownTest()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._readUnknownTest(storage);
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task BlobStorage_UpdateObjectTest()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._updateObjectTest(storage);
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task BlobStorage_DeleteObjectTest()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._deleteObjectTest(storage);
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task BlobStorage_HandleCrazyKeys()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._handleCrazyKeys(storage);
}

[TestMethod]
public async Task BlobStorage_TypedSerialization()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._typedSerialization(this.storage);
}
}
Expand Down
39 changes: 20 additions & 19 deletions tests/Microsoft.Bot.Builder.Azure.Tests/BlobTranscriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,78 +40,79 @@ public class BlobTranscriptTests : TranscriptBaseTests
return false;
});

public static bool CheckStorageEmulator()
public bool HasStorage()
{
if (!hasStorageEmulator.Value)
System.Diagnostics.Debug.WriteLine(noEmulatorMessage);
if (System.Diagnostics.Debugger.IsAttached)
Assert.IsTrue(hasStorageEmulator.Value, noEmulatorMessage);
return hasStorageEmulator.Value;
return this.store != null;
}

public static CloudStorageAccount cloudStorageAccount;

[ClassInitialize]
public static void Initialize(TestContext context)
{
if (CheckStorageEmulator())
cloudStorageAccount = (hasStorageEmulator.Value) ? CloudStorageAccount.DevelopmentStorageAccount : null;
var connectionString = Environment.GetEnvironmentVariable("STORAGECONNECTIONSTRING");
if (!String.IsNullOrEmpty(connectionString))
cloudStorageAccount = CloudStorageAccount.Parse(connectionString);

if (cloudStorageAccount != null)
{
var containerName = "BlobTranscriptTests".ToLower();
var blobClient = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
container.DeleteIfExistsAsync().Wait();
cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(nameof(BlobTranscriptTests).ToLower()).DeleteIfExistsAsync().Wait();
}
}

public BlobTranscriptTests() : base()
{
this.store = new AzureBlobTranscriptStore(CloudStorageAccount.DevelopmentStorageAccount, "BlobTranscriptTests");
if (cloudStorageAccount != null)
this.store = new AzureBlobTranscriptStore(cloudStorageAccount, nameof(BlobTranscriptTests));
}

[TestMethod]
public async Task BlobTranscript_BadArgs()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._BadArgs();
}

[TestMethod]
public async Task BlobTranscript_LogActivity()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._LogActivity();
}

[TestMethod]
public async Task BlobTranscript_LogMultipleActivities()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._LogMultipleActivities();
}

[TestMethod]
public async Task BlobTranscript_GetConversationActivities()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._GetTranscriptActivities();
}

[TestMethod]
public async Task BlobTranscript_GetConversationActivitiesStartDate()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._GetTranscriptActivitiesStartDate();
}

[TestMethod]
public async Task BlobTranscript_ListConversations()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._ListTranscripts();
}

[TestMethod]
public async Task BlobTranscript_DeleteConversation()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._DeleteTranscript();
}

Expand Down
30 changes: 16 additions & 14 deletions tests/Microsoft.Bot.Builder.Azure.Tests/TableStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Bot.Builder.Core.Extensions;
using Microsoft.Bot.Builder.Core.Extensions.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Storage;

namespace Microsoft.Bot.Builder.Azure.Tests
{
Expand Down Expand Up @@ -51,9 +52,14 @@ public static void SetupTests(TestContext testContext)
[TestInitialize]
public void TestInit()
{
if (hasStorageEmulator.Value)
var cloudStorageAccount = (hasStorageEmulator.Value) ? CloudStorageAccount.DevelopmentStorageAccount : null;
var connectionString = Environment.GetEnvironmentVariable("STORAGECONNECTIONSTRING") ;
if (!String.IsNullOrEmpty(connectionString))
cloudStorageAccount = CloudStorageAccount.Parse(connectionString);

if (cloudStorageAccount != null)
{
storage = new AzureTableStorage("UseDevelopmentStorage=true", TestContext.TestName.Replace("_","") + TestContext.GetHashCode().ToString("x"));
storage = new AzureTableStorage(cloudStorageAccount, TestContext.TestName.Replace("_","") + TestContext.GetHashCode().ToString("x"));
}
}

Expand All @@ -67,59 +73,55 @@ public async Task TableStorage_TestCleanUp()
}
}

public bool CheckStorageEmulator()
public bool HasStorage()
{
if (!hasStorageEmulator.Value)
Debug.WriteLine(noEmulatorMessage);
if (Debugger.IsAttached)
Assert.IsTrue(hasStorageEmulator.Value, noEmulatorMessage);
return hasStorageEmulator.Value;
return storage != null;
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task TableStorage_CreateObjectTest()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._createObjectTest(storage);
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task TableStorage_ReadUnknownTest()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._readUnknownTest(storage);
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task TableStorage_UpdateObjectTest()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._updateObjectTest(storage);
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task TableStorage_DeleteObjectTest()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._deleteObjectTest(storage);
}

// NOTE: THESE TESTS REQUIRE THAT THE AZURE STORAGE EMULATOR IS INSTALLED AND STARTED !!!!!!!!!!!!!!!!!
[TestMethod]
public async Task TableStorage_HandleCrazyKeys()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._handleCrazyKeys(storage);
}

[TestMethod]
public async Task TableStorage_TypedSerialization()
{
if (CheckStorageEmulator())
if (HasStorage())
await base._typedSerialization(this.storage);
}
}
Expand Down

0 comments on commit 1ee10d7

Please sign in to comment.