Skip to content

Commit

Permalink
Unit test refactoring (#44)
Browse files Browse the repository at this point in the history
* Refactored unit tests:
* Disabled parallelization, and moved logging functionality to TestUtility.
* Added a test attribute that will log the begining/end of each test along with the current platform
* Added permutations for authenticated/unauthenticated entities
* Updating connection string environment variable name
* Added back base test class, and added new class for raw web socket tests
* Removing unnecessary constructors, and adding comment to explain the compiler directive
  • Loading branch information
jtaubensee authored Jan 24, 2017
1 parent 1583957 commit b95dae6
Show file tree
Hide file tree
Showing 13 changed files with 661 additions and 660 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ In order to run the unit tests complete the following:

1. Click below to deploy the ARM template that is a part of this repository.

2. Add an Environment Variable called `RELAYCONNECTIONSTRING` with the resulting namespace connection string.
2. Add an Environment Variable called `azure-relay-dotnet/connectionstring` with the resulting namespace connection string.

<a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-relay-dotnet%2Fmaster%2Ftemplates%2Fazuredeploy.json" target="_blank">
<img src="http://azuredeploy.net/deploybutton.png"/>
Expand Down
4 changes: 2 additions & 2 deletions build/AppveyorBuildFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function Deploy-AzureResources
Write-Host "Relay namespace: $NamespaceName"

$ConnectionString = $settings.Outputs.Get_Item("namespaceConnectionString").Value
[Environment]::SetEnvironmentVariable('RELAYCONNECTIONSTRING', $ConnectionString)
[Environment]::SetEnvironmentVariable('azure-relay-dotnet/connectionstring', $ConnectionString)

Write-Host "Completed creating Azure resources"
}
Expand All @@ -89,7 +89,7 @@ function Deploy-AzureResources

function Run-UnitTests
{
if ([bool][Environment]::GetEnvironmentVariable('RELAYCONNECTIONSTRING'))
if ([bool][Environment]::GetEnvironmentVariable('azure-relay-dotnet/connectionstring'))
{
Write-Host "Running unit tests."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ public class ConnectionStringBuilderTests
readonly string sasKeyName;
readonly string entityPath;
readonly string sasKeyValue;
Logger logger;

public ConnectionStringBuilderTests(ITestOutputHelper output)
{
this.logger = new Logger(output);
this.endpoint = new Uri("sb://contoso.servicebus.windows.net/");
this.sasKeyName = "RootManageSharedAccessKey";
this.entityPath = "hc1";
this.sasKeyValue = SasKeyGenerator.GenerateRandomKey();
this.sasKeyValue = TestUtility.GenerateRandomSasKey();
}

[Fact]
[DisplayTestMethodName]
public async Task ConnectionStringBuilderOperationValidation()
{
this.logger.Log("Create a new connection string using RelayConnectionStringBuilder properties.");
TestUtility.Log("Create a new connection string using RelayConnectionStringBuilder properties.");
var connectionStringBuilder = new RelayConnectionStringBuilder()
{
Endpoint = this.endpoint,
Expand All @@ -54,58 +53,58 @@ public async Task ConnectionStringBuilderOperationValidation()
// SharedAccessSignature should be omitted since it is not specified.
Assert.DoesNotContain("SharedAccessSignature", connectionString);

this.logger.Log("Try to set the timeout to a negative number");
TestUtility.Log("Try to set the timeout to a negative number");
Assert.Throws<ArgumentOutOfRangeException>(() => connectionStringBuilder.OperationTimeout = TimeSpan.FromMinutes(-1));

this.logger.Log("Try to create a connection string with SAS KeyName but no KeyValue");
TestUtility.Log("Try to create a connection string with SAS KeyName but no KeyValue");
connectionStringBuilder.SharedAccessKeyName = this.sasKeyName;
connectionStringBuilder.SharedAccessKey = null;
Assert.Throws<ArgumentException>(() => connectionStringBuilder.ToString());

this.logger.Log("Try to create a connection string with SAS KeyValue but no KeyName");
TestUtility.Log("Try to create a connection string with SAS KeyValue but no KeyName");
connectionStringBuilder.SharedAccessKeyName = null;
connectionStringBuilder.SharedAccessKey = this.sasKeyValue;
Assert.Throws<ArgumentException>(() => connectionStringBuilder.ToString());

var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(this.sasKeyName, this.sasKeyValue);
var sasToken = await tokenProvider.GetTokenAsync(this.endpoint.AbsoluteUri, TimeSpan.FromMinutes(5));

this.logger.Log("Create a connection string with SAS token only");
TestUtility.Log("Create a connection string with SAS token only");
connectionStringBuilder.SharedAccessSignature = sasToken.TokenString;
connectionStringBuilder.SharedAccessKeyName = null;
connectionStringBuilder.SharedAccessKey = null;
connectionString = connectionStringBuilder.ToString();

this.logger.Log("Parse a connection string with SAS token only");
TestUtility.Log("Parse a connection string with SAS token only");
connectionStringBuilder = new RelayConnectionStringBuilder(connectionString);
Assert.Equal(sasToken.TokenString, connectionStringBuilder.SharedAccessSignature);
Assert.Null(connectionStringBuilder.SharedAccessKeyName);
Assert.Null(connectionStringBuilder.SharedAccessKey);

this.logger.Log("Create a connection string with SAS token and SAS KeyName");
TestUtility.Log("Create a connection string with SAS token and SAS KeyName");
connectionStringBuilder.SharedAccessSignature = sasToken.TokenString;
connectionStringBuilder.SharedAccessKeyName = this.sasKeyName;
connectionStringBuilder.SharedAccessKey = null;
Assert.Throws<ArgumentException>(() => connectionStringBuilder.ToString());

this.logger.Log("Create a connection string with SAS token and SAS Key value");
TestUtility.Log("Create a connection string with SAS token and SAS Key value");
connectionStringBuilder.SharedAccessSignature = sasToken.TokenString;
connectionStringBuilder.SharedAccessKeyName = null;
connectionStringBuilder.SharedAccessKey = this.sasKeyValue;
Assert.Throws<ArgumentException>(() => connectionStringBuilder.ToString());

this.logger.Log("Create a new ConnectionStringBuilder, set no properties, and call ToString()");
TestUtility.Log("Create a new ConnectionStringBuilder, set no properties, and call ToString()");
connectionStringBuilder = new RelayConnectionStringBuilder();
Assert.Throws<ArgumentNullException>(() => connectionStringBuilder.ToString());

this.logger.Log("Set only Endpoint, call ToString()");
TestUtility.Log("Set only Endpoint, call ToString()");
connectionStringBuilder = new RelayConnectionStringBuilder()
{
Endpoint = this.endpoint
};
connectionString = connectionStringBuilder.ToString();

this.logger.Log("Set OperationTimeout using connectionString");
TestUtility.Log("Set OperationTimeout using connectionString");
TimeSpan operationTimeout = TimeSpan.FromSeconds(90);
connectionString += $"OperationTimeout={operationTimeout.ToString(null, CultureInfo.InvariantCulture)}";
connectionStringBuilder = new RelayConnectionStringBuilder(connectionString);
Expand All @@ -117,6 +116,7 @@ public async Task ConnectionStringBuilderOperationValidation()
}

[Fact]
[DisplayTestMethodName]
public void CreateConnectionStringBuilderFromConnectionString()
{
var connectionStringBuilder = new RelayConnectionStringBuilder()
Expand All @@ -128,7 +128,7 @@ public void CreateConnectionStringBuilderFromConnectionString()
};
var connectionString = connectionStringBuilder.ToString();

this.logger.Log("Use ConnectionStringBuilder..ctor(string) to parse the created connectionString");
TestUtility.Log("Use ConnectionStringBuilder..ctor(string) to parse the created connectionString");
connectionStringBuilder = new RelayConnectionStringBuilder(connectionString);
Assert.Equal(this.endpoint, connectionStringBuilder.Endpoint);
Assert.Equal(this.entityPath, connectionStringBuilder.EntityPath);
Expand Down
12 changes: 12 additions & 0 deletions test/Microsoft.Azure.Relay.UnitTests/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Azure.Relay.UnitTests
{
static class Constants
{
internal const string ConnectionStringEnvironmentVariable = "azure-relay-dotnet/connectionstring";
internal const string AuthenticatedEntityPath = "authenticated";
internal const string UnauthenticatedEntityPath = "unauthenticated";
}
}
24 changes: 24 additions & 0 deletions test/Microsoft.Azure.Relay.UnitTests/DisplayTestNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.Azure.Relay.UnitTests
{
using System.Reflection;
using Microsoft.Extensions.PlatformAbstractions;
using Xunit.Sdk;

public class DisplayTestMethodNameAttribute : BeforeAfterTestAttribute
{
public override void Before(MethodInfo methodUnderTest)
{
TestUtility.Log($"Begin {methodUnderTest.DeclaringType}.{methodUnderTest.Name} on {PlatformServices.Default.Application.RuntimeFramework.ToString()}");
base.Before(methodUnderTest);
}

public override void After(MethodInfo methodUnderTest)
{
TestUtility.Log($"End {methodUnderTest.DeclaringType}.{methodUnderTest.Name}");
base.After(methodUnderTest);
}
}
}
Loading

0 comments on commit b95dae6

Please sign in to comment.