I have a solution in .NET 5 with some Unit Tests in NUnit that required some secrets to work correctly. Testing in Visual Studio using local user secrets works fine but now I want to try to integrate using Azure Key Vault and run the Unit Tests inside a Pipeline.
I added the Task AzureKeyVault@2
and according to the log the secrets are downloaded just fine but in the next steps, the Unit Tests fail because it doesn't find some keys when try to access from IConfiguration
.
This is my azure-pipelines.yml
(only the relevant parts).
- job: Init
displayName: Unit Testing
variables:
solutionToBuild: 'Solution.sln'
unitTestsProject: 'UnitTests\UnitTests.csproj'
buildConfiguration: 'Release'
steps:
- task: DotNetCoreCLI@2
displayName: 'Restore Packages'
inputs:
command: 'restore'
projects: '$(solutionToBuild)'
verbosityRestore: minimal
feedsToUse: 'select'
- task: DotNetCoreCLI@2
displayName: 'Build Solution'
inputs:
command: 'build'
projects: '$(solutionToBuild)'
arguments: '-c $(buildConfiguration)'
- task: AzureKeyVault@2
displayName: 'Configure Key Vault'
inputs:
azureSubscription: '**-etc-**'
KeyVaultName: '**key-vault-name**'
- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests'
inputs:
command: 'test'
projects: '$(unitTestsProject)'
arguments: '-c $(buildConfiguration) --collect:"XPlat Code Coverage"'
publishTestResults: true
I created this basic tests that are currently failing in the Pipeline. I'm using the Host.CreateDefaultBuilder()
method to initialize all the unit tests.
[Test]
[TestCase("secret-key-1")]
[TestCase("secret-key-1")]
public void TestSecrets(string key)
{
Assert.NotNull(_config[key]);
Assert.IsNotEmpty(_config[key]);
}
Does the AzureKeyVault@2
pass the secrets keys and values for the DotNetCoreCLI@2
task or do I need an additional step?. I read that you can play with the FileTransform@1 step using an empty appsettings.json
but I couldn't make it work. Is there a correct method to achieve this?