Fix aspnet/Home#3379 - add a functional test for Redis storage provider to ensure keys round-trip (#321)

This commit is contained in:
Nate McMaster 2018-08-10 15:20:09 -07:00 committed by GitHub
parent 9534c08142
commit e0235b1e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 129 additions and 13 deletions

View File

@ -1,13 +0,0 @@
trigger:
- master
- release/*
resources:
repositories:
- repository: buildtools
type: git
name: aspnet-BuildTools
ref: refs/heads/release/2.2
phases:
- template: .vsts-pipelines/templates/project-ci.yml@buildtools

View File

@ -13,3 +13,19 @@ resources:
phases:
- template: .vsts-pipelines/templates/project-ci.yml@buildtools
- template: .vsts-pipelines/templates/phases/default-build.yml@buildtools
parameters:
phaseName: Linux_RedisTests
queueName: DotNetCore-Docker
agentOs: Linux
demands:
- docker
variables:
Test__Redis__Server: localhost:6379,127.0.0.1:6379
beforeBuild:
- script: docker run --rm -d --name test-redis-server -p 6379:6379 redis
displayName: Start Redis in Docker
afterBuild:
- script: docker stop test-redis-server
displayName: Stop Redis in Docker
condition: always()

View File

@ -8,6 +8,7 @@
<MicrosoftAspNetCoreHostingPackageVersion>2.2.0-preview1-34896</MicrosoftAspNetCoreHostingPackageVersion>
<MicrosoftAspNetCoreTestingPackageVersion>2.2.0-preview1-34896</MicrosoftAspNetCoreTestingPackageVersion>
<MicrosoftAzureKeyVaultPackageVersion>2.3.2</MicrosoftAzureKeyVaultPackageVersion>
<MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>2.2.0-preview1-34896</MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>
<MicrosoftExtensionsConfigurationJsonPackageVersion>2.2.0-preview1-34896</MicrosoftExtensionsConfigurationJsonPackageVersion>
<MicrosoftExtensionsConfigurationPackageVersion>2.2.0-preview1-34896</MicrosoftExtensionsConfigurationPackageVersion>
<MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion>2.2.0-preview1-34896</MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion>

View File

@ -1,17 +1,29 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Moq;
using StackExchange.Redis;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.DataProtection
{
public class DataProtectionRedisTests
{
private readonly ITestOutputHelper _output;
public DataProtectionRedisTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void GetAllElements_ReturnsAllXmlValuesForGivenKey()
{
@ -55,5 +67,44 @@ namespace Microsoft.AspNetCore.DataProtection
database.Verify();
}
[ConditionalFact]
[TestRedisServerIsAvailable]
public async Task XmlRoundTripsToActualRedisServer()
{
var connStr = TestRedisServer.GetConnectionString();
_output.WriteLine("Attempting to connect to " + connStr);
var guid = Guid.NewGuid().ToString();
RedisKey key = "Test:DP:Key" + guid;
try
{
using (var redis = await ConnectionMultiplexer.ConnectAsync(connStr).TimeoutAfter(TimeSpan.FromMinutes(1)))
{
var repo = new RedisXmlRepository(() => redis.GetDatabase(), key);
var element = new XElement("HelloRedis", guid);
repo.StoreElement(element, guid);
}
using (var redis = await ConnectionMultiplexer.ConnectAsync(connStr).TimeoutAfter(TimeSpan.FromMinutes(1)))
{
var repo = new RedisXmlRepository(() => redis.GetDatabase(), key);
var elements = repo.GetAllElements();
Assert.Contains(elements, e => e.Name == "HelloRedis" && e.Value == guid);
}
}
finally
{
// cleanup
using (var redis = await ConnectionMultiplexer.ConnectAsync(connStr).TimeoutAfter(TimeSpan.FromMinutes(1)))
{
await redis.GetDatabase().KeyDeleteAsync(key);
}
}
}
}
}

View File

@ -8,12 +8,20 @@
<Compile Include="..\common\**\*.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="testconfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.DataProtection.Abstractions\Microsoft.AspNetCore.DataProtection.Abstractions.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.DataProtection.Redis\Microsoft.AspNetCore.DataProtection.Redis.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsConfigurationJsonPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsDependencyInjectionPackageVersion)" />
</ItemGroup>

View File

@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Extensions.Configuration;
using System;
namespace Microsoft.AspNetCore.DataProtection
{
internal class TestRedisServer
{
public const string ConnectionStringKeyName = "Test:Redis:Server";
private static readonly IConfigurationRoot _config;
static TestRedisServer()
{
_config = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("testconfig.json")
.AddEnvironmentVariables()
.Build();
}
internal static string GetConnectionString()
{
return _config[ConnectionStringKeyName];
}
}
}

View File

@ -0,0 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Testing.xunit;
using System;
namespace Microsoft.AspNetCore.DataProtection
{
internal class TestRedisServerIsAvailableAttribute : Attribute, ITestCondition
{
public bool IsMet => !string.IsNullOrEmpty(TestRedisServer.GetConnectionString());
public string SkipReason => $"A test redis server must be configured to run. Set the connection string as an environment variable as {TestRedisServer.ConnectionStringKeyName.Replace(":", "__")} or in testconfig.json";
}
}

View File

@ -0,0 +1,10 @@
{
"Test": {
"Redis": {
// You can setup a local Redis server easily with Docker by running
// docker run --rm -it -p 6379:6379 redis
// Then uncomment this config below
// "Server": "localhost:6379,127.0.0.1:6379"
}
}
}