Skip to content

Commit

Permalink
Ignore live-only tests when in Playback or Record modes
Browse files Browse the repository at this point in the history
Partially fixes Azure#7416
  • Loading branch information
heaths committed Aug 29, 2019
1 parent 8d6d454 commit 83fe1f6
Show file tree
Hide file tree
Showing 27 changed files with 119 additions and 62 deletions.
7 changes: 7 additions & 0 deletions sdk/core/Azure.Core/tests/LiveOnly.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project ToolsVersion="15.0">
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)\TestFramework\LiveOnlyAttribute.cs" Link="TestFramework\LiveOnlyAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)\TestFramework\RecordedTestMode.cs" Link="TestFramework\RecordedTestMode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)\TestFramework\RecordedTestUtilities.cs" Link="TestFramework\RecordedTestUtilities.cs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions sdk/core/Azure.Core/tests/TestFramework.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup Condition="'$(ExcludeRecordingFramework)' == 'true'">
<Compile Remove="$(MSBuildThisFileDirectory)\TestFramework\LiveOnlyAttribute.cs" />
<Compile Remove="$(MSBuildThisFileDirectory)\TestFramework\PlaybackTransport.cs" />
<Compile Remove="$(MSBuildThisFileDirectory)\TestFramework\*Record*.cs" />
</ItemGroup>
Expand Down
36 changes: 36 additions & 0 deletions sdk/core/Azure.Core/tests/TestFramework/LiveOnlyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;

namespace Azure.Core.Testing
{
/// <summary>
/// Attribute on test assemblies, classes, or methods that run only against live resources.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = true)]
public class LiveOnlyAttribute : NUnitAttribute, IApplyToTest
{
/// <summary>
/// Modifies the <paramref name="test"/> by adding categories to it and changing the run state as needed.
/// </summary>
/// <param name="test">The <see cref="Test"/> to modify.</param>
public void ApplyToTest(Test test)
{
test.Properties.Add("Category", "Live");

if (test.RunState != RunState.NotRunnable)
{
RecordedTestMode mode = RecordedTestUtilities.GetModeFromEnvironment();
if (mode != RecordedTestMode.Live)
{
test.RunState = RunState.Ignored;
test.Properties.Set("_SKIPREASON", $"Live tests will not run when AZURE_TEST_MODE is {mode}");
}
}
}
}
}
18 changes: 1 addition & 17 deletions sdk/core/Azure.Core/tests/TestFramework/RecordedTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.IO;
using NUnit.Framework;

Expand All @@ -10,8 +9,6 @@ namespace Azure.Core.Testing
[Category("Recorded")]
public abstract class RecordedTestBase: ClientTestBase
{
private const string ModeEnvironmentVariableName = "AZURE_TEST_MODE";

protected RecordedTestSanitizer Sanitizer { get; set; }

protected RecordMatcher Matcher { get; set; }
Expand All @@ -20,7 +17,7 @@ public abstract class RecordedTestBase: ClientTestBase

protected RecordedTestMode Mode { get; }

protected RecordedTestBase(bool isAsync) : this(isAsync, GetModeFromEnvironment())
protected RecordedTestBase(bool isAsync) : this(isAsync, RecordedTestUtilities.GetModeFromEnvironment())
{
}

Expand All @@ -31,19 +28,6 @@ protected RecordedTestBase(bool isAsync, RecordedTestMode mode) : base(isAsync)
Mode = mode;
}

internal static RecordedTestMode GetModeFromEnvironment()
{
string modeString = Environment.GetEnvironmentVariable(ModeEnvironmentVariableName);

RecordedTestMode mode = RecordedTestMode.Playback;
if (!string.IsNullOrEmpty(modeString))
{
mode = (RecordedTestMode)Enum.Parse(typeof(RecordedTestMode), modeString, true);
}

return mode;
}

private string GetSessionFilePath(string name = null)
{
TestContext.TestAdapter testAdapter = TestContext.CurrentContext.Test;
Expand Down
26 changes: 26 additions & 0 deletions sdk/core/Azure.Core/tests/TestFramework/RecordedTestUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.Core.Testing
{
internal static class RecordedTestUtilities
{
private const string ModeEnvironmentVariableName = "AZURE_TEST_MODE";

internal static RecordedTestMode GetModeFromEnvironment()
{
string modeString = Environment.GetEnvironmentVariable(ModeEnvironmentVariableName);

RecordedTestMode mode = RecordedTestMode.Playback;
if (!string.IsNullOrEmpty(modeString))
{
mode = (RecordedTestMode)Enum.Parse(typeof(RecordedTestMode), modeString, true);
}

return mode;
}

}
}
3 changes: 2 additions & 1 deletion sdk/core/Azure.Core/tests/samples/Sample1_HelloWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
// license information.

using Azure.Core.Pipeline;
using Azure.Core.Testing;
using NUnit.Framework;
using System;
using System.IO;
using System.Threading.Tasks;

namespace Azure.Core.Samples
{
[Category("Live")]
[LiveOnly]
public partial class BaseSamples
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
</PropertyGroup>
<Import Project="..\..\..\core\Azure.Core\tests\LiveOnly.props" />
<ItemGroup>
<PackageReference Include="nunit" />
<PackageReference Include="NUnit3TestAdapter" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
// Licensed under the MIT License. See License.txt in the project root for
// license information.

using Azure.Core.Testing;
using Azure.Identity;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;

namespace Azure.Security.KeyVault.Keys.Samples
{
/// <summary>
/// Sample demonstrates how to set, get, update and delete a key using the synchronous methods of the KeyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class HelloWorld
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
// Licensed under the MIT License. See License.txt in the project root for
// license information.

using Azure.Core.Testing;
using Azure.Identity;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.Security.KeyVault.Keys.Samples
{
/// <summary>
/// Sample demonstrates how to set, get, update and delete a key using the asynchronous methods of the KeyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class HelloWorld
{
[Test]
public async Task HelloWorldASync()
public async Task HelloWorldAsync()
{
// Environment variable with the Key Vault endpoint.
string keyVaultUrl = Environment.GetEnvironmentVariable("AZURE_KEYVAULT_URL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for
// license information.

using Azure.Core.Testing;
using Azure.Identity;
using NUnit.Framework;
using System;
Expand All @@ -14,7 +15,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// Sample demonstrates how to backup and restore keys in the Key Vault
/// using the synchronous methods of the KeyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class BackupAndRestore
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Licensed under the MIT License. See License.txt in the project root for
// license information.

using Azure.Core.Testing;
using Azure.Identity;
using NUnit.Framework;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.Security.KeyVault.Keys.Samples
Expand All @@ -15,7 +15,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// Sample demonstrates how to backup and restore keys in the Key Vault
/// using the asynchronous methods of the KeyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class BackupAndRestore
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the MIT License. See License.txt in the project root for
// license information.

using Azure.Core.Testing;
using Azure.Identity;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;

namespace Azure.Security.KeyVault.Keys.Samples
Expand All @@ -17,7 +17,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// and list deleted keys in a soft-delete enabled Key Vault
/// using the synchronous methods of the KeyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class GetKeys
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Licensed under the MIT License. See License.txt in the project root for
// license information.

using Azure.Core.Testing;
using Azure.Identity;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.Security.KeyVault.Keys.Samples
Expand All @@ -17,7 +16,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// and list deleted keys in a soft-delete enabled Key Vault
/// using the asynchronous methods of the KeyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class GetKeys
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Azure.Identity;
using Azure.Security.KeyVault.Keys;
using Azure.Core.Testing;
using Azure.Identity;
using Azure.Security.KeyVault.Keys.Cryptography;
using NUnit.Framework;
using System;
Expand All @@ -13,7 +13,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// <summary>
/// Sample demonstrates how to encrypt and decrypt a single block of plain text with an RSA key using the synchronous methods of the CryptographyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class Sample4_EncryptDecypt
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Azure.Identity;
using Azure.Core.Testing;
using Azure.Identity;
using Azure.Security.KeyVault.Keys.Cryptography;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -13,7 +13,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// <summary>
/// Sample demonstrates how to encrypt and decrypt a single block of plain text with an RSA key using the asynchronous methods of the CryptographyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class Sample4_EncryptDecypt
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Azure.Identity;
using Azure.Security.KeyVault.Keys;
using Azure.Core.Testing;
using Azure.Identity;
using Azure.Security.KeyVault.Keys.Cryptography;
using NUnit.Framework;
using System;
Expand All @@ -14,7 +14,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// <summary>
/// Sample demonstrates how to sign data with both a RSA key and an EC key using the synchronous methods of the CryptographyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class Sample5_SignVerify
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Azure.Identity;
using Azure.Security.KeyVault.Keys;
using Azure.Core.Testing;
using Azure.Identity;
using Azure.Security.KeyVault.Keys.Cryptography;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.Security.KeyVault.Keys.Samples
Expand All @@ -15,7 +14,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// <summary>
/// Sample demonstrates how to sign data with both a RSA key and an EC key using the synchronous methods of the CryptographyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class Sample5_SignVerify
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Azure.Identity;
using Azure.Security.KeyVault.Keys;
using Azure.Core.Testing;
using Azure.Identity;
using Azure.Security.KeyVault.Keys.Cryptography;
using NUnit.Framework;
using System;
Expand All @@ -13,7 +13,7 @@ namespace Azure.Security.KeyVault.Keys.Samples
/// <summary>
/// Sample demonstrates how to wrap and unwrap a symmetric key with an RSA key using the synchronous methods of the CryptographyClient.
/// </summary>
[Category("Live")]
[LiveOnly]
public partial class Sample6_WrapUnwrap
{
[Test]
Expand Down
Loading

0 comments on commit 83fe1f6

Please sign in to comment.