Skip to content

Commit

Permalink
Update signedAssertion test to avoid resource contention (#2263)
Browse files Browse the repository at this point in the history
* Update signedAssertion test to avoid resource contention
* remove sonarcloud
* add yaml
* update test
* use msbuild
* Fixing flaky tests
* Update tests/Microsoft.Identity.Web.Test/AzureIdentityForKubernetesClientAssertionTests.cs
* Update template-run-unit-tests.yaml to exclude test apps

---------

Co-authored-by: Jean-Marc Prieur <jmprieur@microsoft.com>
  • Loading branch information
jennyf19 and jmprieur authored Jun 4, 2023
1 parent a2f68f9 commit 2892d55
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 77 deletions.
30 changes: 2 additions & 28 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,12 @@ pr:
- master

jobs:
- job: "Sonar"
- job: "PR_build"
pool:
vmImage: 'windows-latest'
demands:
- msbuild
steps:
- template: build/template-install-dotnet-core.yaml
- template: build/template-install-keyvault-secrets.yaml
- task: SonarCloudPrepare@1
inputs:
SonarCloud: 'SonarCloud-microsoft_identity_web'
organization: 'azuread'
scannerMode: 'MSBuild'
projectKey: 'AzureAD_microsoft-identity-web'
projectName: 'microsoft-identity-web'
extraProperties: |
sonar.exclusions=tests\**
- task: DotNetCoreCLI@2
inputs:
projects: 'Microsoft.Identity.Web.sln'
command: 'restore'
- task: VSBuild@1
inputs:
solution: Microsoft.Identity.Web.sln
vsVersion: '16.0'
- task: DotNetCoreCLI@2
displayName: 'Run unit tests'
inputs:
command: test
projects: 'tests/**/Microsoft.Identity.Web.Test*.csproj'
arguments: '--collect "Code coverage"'
- task: SonarCloudAnalyze@1
- task: SonarCloudPublish@1
inputs:
pollingTimeoutSec: '300'
- template: build/template-run-unit-tests.yaml
12 changes: 12 additions & 0 deletions build/template-run-unit-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# template-run-unit-tests.yaml
# Run all unit tests across the IdWeb project

steps:

- task: DotNetCoreCLI@2
inputs:
command: 'test'
projects: |
Tests/**/*.csproj
!Tests/DevApps/**/*.csproj
arguments: '--collect "Code coverage"'
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Microsoft.Identity.Web.Tests.Certificateless
public class AzureIdentityForKubernetesClientAssertionTests
{
string token;
private const string FilePath = "signedAssertion.txt";
private const string FilePath2 = "signedAssertion2.txt";

public AzureIdentityForKubernetesClientAssertionTests()
{
Expand All @@ -25,25 +27,22 @@ public AzureIdentityForKubernetesClientAssertionTests()
public async Task GetAksClientAssertion_WhenSpecifiedSignedAssertionFileExists_ReturnsClientAssertion()
{
// Arrange
File.WriteAllText(TestConstants.signedAssertionFilePath, token.ToString());
AzureIdentityForKubernetesClientAssertion azureIdentityForKubernetesClientAssertion = new AzureIdentityForKubernetesClientAssertion(TestConstants.signedAssertionFilePath);
File.WriteAllText(FilePath, token.ToString());
AzureIdentityForKubernetesClientAssertion azureIdentityForKubernetesClientAssertion = new AzureIdentityForKubernetesClientAssertion(FilePath);

// Act
string signedAssertion = await azureIdentityForKubernetesClientAssertion.GetSignedAssertion(CancellationToken.None);

// Assert
Assert.NotNull(signedAssertion);

// Delete the signed assertion file.
File.Delete(TestConstants.signedAssertionFilePath);
}

[Fact]
public async Task GetAksClientAssertion_WhenEnvironmentVariablePointsToSignedAssertionFileExists_ReturnsClientAssertion()
{
// Arrange
File.WriteAllText(TestConstants.signedAssertionFilePath, token.ToString());
Environment.SetEnvironmentVariable("AZURE_FEDERATED_TOKEN_FILE", TestConstants.signedAssertionFilePath);
File.WriteAllText(FilePath2, token.ToString());
Environment.SetEnvironmentVariable("AZURE_FEDERATED_TOKEN_FILE", FilePath2);
AzureIdentityForKubernetesClientAssertion azureIdentityForKubernetesClientAssertion = new AzureIdentityForKubernetesClientAssertion();

// Act
Expand All @@ -53,7 +52,6 @@ public async Task GetAksClientAssertion_WhenEnvironmentVariablePointsToSignedAss
Assert.NotNull(signedAssertion);

// Delete the signed assertion file and remove the environment variable.
File.Delete(TestConstants.signedAssertionFilePath);
Environment.SetEnvironmentVariable("AZURE_FEDERATED_TOKEN_FILE", null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,75 @@

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Identity.Abstractions;
using Microsoft.Identity.Web.Test.Common;
using Microsoft.IdentityModel.JsonWebTokens;
using Xunit;

namespace Microsoft.Identity.Web.Tests.Certificateless
{
public class SignedAssertionFilePathCredentialsLoaderTests
{
const string filePath = "signedAssertion.txt";
const string aksEnvironmentVariableName = "AZURE_FEDERATED_TOKEN_FILE";
string token;
SignedAssertionFilePathCredentialsLoader signedAssertionFilePathCredentialsLoader = new SignedAssertionFilePathCredentialsLoader(null);

private const string FilePath = "signedAssertion.txt";
private const string FilePath2 = "signedAssertion2.txt";
private const string AksEnvironmentVariableName = "AZURE_FEDERATED_TOKEN_FILE";
private readonly string _token;
private readonly SignedAssertionFilePathCredentialsLoader _signedAssertionFilePathCredentialsLoader = new(null);

public SignedAssertionFilePathCredentialsLoaderTests()
{
JsonWebTokenHandler handler = new JsonWebTokenHandler();
token = handler.CreateToken("{}");
JsonWebTokenHandler handler = new();
_token = handler.CreateToken("{}");
}

[Fact]
public async Task GetClientAssertion_WhenSpecifiedSignedAssertionFileExists_ReturnsClientAssertion()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task GetClientAssertion_WhenSpecifiedSignedAssertionFileExists_ReturnsClientAssertion(bool withEnvVariable)
{
// Arrange
File.WriteAllText(filePath, token.ToString());
CredentialDescription credentialDescription = new CredentialDescription
var filePath = withEnvVariable ? FilePath : FilePath2;
File.WriteAllText(filePath, _token.ToString());
CredentialDescription credentialDescription;
if (withEnvVariable)
{
SourceType = CredentialSource.SignedAssertionFilePath,
SignedAssertionFileDiskPath = filePath
};
Environment.SetEnvironmentVariable(AksEnvironmentVariableName, filePath);
credentialDescription = new()
{
SourceType = CredentialSource.SignedAssertionFilePath,
};
}
else
{
credentialDescription = new()
{
SourceType = CredentialSource.SignedAssertionFilePath,
SignedAssertionFileDiskPath = filePath
};
}

// Act
await signedAssertionFilePathCredentialsLoader.LoadIfNeededAsync(credentialDescription, null);
await _signedAssertionFilePathCredentialsLoader.LoadIfNeededAsync(credentialDescription, null);

// Assert
Assert.NotNull(credentialDescription.CachedValue);

// Delete the signed assertion file.
File.Delete(filePath);
}

[Fact]
public async Task GetClientAssertion_WhenEnvironmentVariablePointsToSignedAssertionFileExists_ReturnsClientAssertion()
{
// Arrange
File.WriteAllText(filePath, token.ToString());
Environment.SetEnvironmentVariable(aksEnvironmentVariableName, filePath);
CredentialDescription credentialDescription = new CredentialDescription
if (withEnvVariable)
{
SourceType = CredentialSource.SignedAssertionFilePath,
};

// Act
await signedAssertionFilePathCredentialsLoader.LoadIfNeededAsync(credentialDescription, null);

// Assert
Assert.NotNull(credentialDescription.CachedValue);

// Delete the signed assertion file and remove the environment variable.
File.Delete(filePath);
Environment.SetEnvironmentVariable(aksEnvironmentVariableName, null);
Environment.SetEnvironmentVariable(AksEnvironmentVariableName, null);
}
}

[Fact]
public async Task GetClientAssertion_WhenSignedAssertionFileDoesNotExist_ThrowsFileNotFoundException()
{
// Act
CredentialDescription credentialDescription = new CredentialDescription
CredentialDescription credentialDescription = new()
{
SourceType = CredentialSource.SignedAssertionFilePath,
};
await signedAssertionFilePathCredentialsLoader.LoadIfNeededAsync(credentialDescription, null);
await _signedAssertionFilePathCredentialsLoader.LoadIfNeededAsync(credentialDescription, null);

// Act & Assert
Assert.Null(credentialDescription.CachedValue);
Expand Down

0 comments on commit 2892d55

Please sign in to comment.