1
0
Форкнуть 0

Functional tests infrastracture

This commit is contained in:
Sergey Kanzhelev 2015-03-27 09:37:46 -07:00
Родитель 2503d0e245
Коммит 7f57e63a16
12 изменённых файлов: 284 добавлений и 4 удалений

Просмотреть файл

@ -18,6 +18,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApplicationInsights.AspNet.
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SampleWebAppIntegration", "test\SampleWebAppIntegration\SampleWebAppIntegration.kproj", "{330152EC-1092-41F0-9F96-C3438E07FCC2}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "FunctionalTestUtils", "test\FunctionalTestUtils\FunctionalTestUtils.kproj", "{B7217A00-66FA-49A8-8EF3-39C07E1F7E33}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -36,6 +38,10 @@ Global
{330152EC-1092-41F0-9F96-C3438E07FCC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{330152EC-1092-41F0-9F96-C3438E07FCC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{330152EC-1092-41F0-9F96-C3438E07FCC2}.Release|Any CPU.Build.0 = Release|Any CPU
{B7217A00-66FA-49A8-8EF3-39C07E1F7E33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B7217A00-66FA-49A8-8EF3-39C07E1F7E33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7217A00-66FA-49A8-8EF3-39C07E1F7E33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7217A00-66FA-49A8-8EF3-39C07E1F7E33}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -44,5 +50,6 @@ Global
{95EC3635-22E4-4C3A-A066-F5823A0648DA} = {2E6DDE9E-8C75-4F9C-8906-08EBDD6E73EF}
{2766D8AF-C20B-4F3A-8260-6C2D39B7A8A0} = {8B5230E5-8138-44D6-839F-DF9248F195EE}
{330152EC-1092-41F0-9F96-C3438E07FCC2} = {8B5230E5-8138-44D6-839F-DF9248F195EE}
{B7217A00-66FA-49A8-8EF3-39C07E1F7E33} = {8B5230E5-8138-44D6-839F-DF9248F195EE}
EndGlobalSection
EndGlobal

Просмотреть файл

@ -13,7 +13,7 @@
"dependencies": {
"Microsoft.ApplicationInsights": "0.13.4-build00460"
}
},
},
"aspnetcore50": {
"dependencies": {
"System.Runtime": "4.0.20-*",

Просмотреть файл

@ -11,5 +11,5 @@
"frameworks": {
"aspnet50": { },
"aspnetcore50": { }
},
}
}

Просмотреть файл

@ -0,0 +1,27 @@

namespace FunctionalTestUtils
{
using Microsoft.ApplicationInsights.Channel;
using System.Collections.Generic;
internal class BackTelemetryChannel : ITelemetryChannel
{
IList<ITelemetry> buffer;
public BackTelemetryChannel(IList<ITelemetry> buffer)
{
this.buffer = buffer;
}
public bool DeveloperMode { get; set; }
public void Dispose()
{
}
public void Send(ITelemetry item)
{
this.buffer.Add(item);
}
}
}

Просмотреть файл

@ -0,0 +1,40 @@
namespace FunctionalTestUtils
{
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Framework.DependencyInjection;
using System;
using System.Collections.Generic;
public static class BackTelemetryChannelExtensions
{
/// <summary>
/// I know, statics are bad. Let's refactor when it will become a problem
/// </summary>
private static BackTelemetryChannel channel;
/// <summary>
/// If applciaiton is running under functional tests - in process channel will be used.
/// Otherwise - regular channel will be used
/// </summary>
/// <param name="services"></param>
public static void AddFunctionalTestTelemetryChannel(this IServiceCollection services)
{
if (channel != null)
{
TelemetryConfiguration.Active.TelemetryChannel = channel;
}
}
/// <summary>
/// I haven't implemented Reset method to delete channel. Since an applicaiton will either be used by
/// unit tests or started directly - it should not be a big problem, every unit test will override
/// channel with it's own version.
/// </summary>
/// <param name="buffer"></param>
public static void InitializeFunctionalTestTelemetryChannel(IList<ITelemetry> buffer)
{
channel = new BackTelemetryChannel(buffer);
}
}
}

Просмотреть файл

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>b7217a00-66fa-49a8-8ef3-39c07e1f7e33</ProjectGuid>
<RootNamespace>FunctionalTestUtils</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

Просмотреть файл

@ -0,0 +1,55 @@
namespace FunctionalTestUtils
{
using Microsoft.AspNet.Hosting;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback;
using System;
public class InProcessServer
{
private string url = "http://localhost:" + (new Random(239).Next(5000, 8000)).ToString();
private string assemblyName = "ConsoleTest";
public InProcessServer(string currentAssemblyName)
{
this.assemblyName = currentAssemblyName;
}
public string BaseHost
{
get
{
return this.url;
}
}
public IDisposable Start()
{
var customConfig = new NameValueConfigurationSource();
customConfig.Set("server.urls", this.BaseHost);
var config = new Configuration();
config.Add(customConfig);
var serviceCollection = HostingServices.Create(null);
var services = serviceCollection.BuildServiceProvider();
var context = new HostingContext()
{
Services = services,
Configuration = config,
ServerName = "Microsoft.AspNet.Server.WebListener",
ApplicationName = this.assemblyName,
EnvironmentName = "Production"
};
var hostingEngine = services.GetService<IHostingEngine>();
if (hostingEngine == null)
{
throw new Exception("TODO: IHostingEngine service not available exception");
}
return hostingEngine.Start(context);
}
}
}

Просмотреть файл

@ -0,0 +1,35 @@
namespace FunctionalTestUtils
{
using Microsoft.Framework.ConfigurationModel;
using System;
using System.Collections.Generic;
public class NameValueConfigurationSource : IConfigurationSource
{
private Dictionary<string, string> _config = new Dictionary<string, string>();
public NameValueConfigurationSource()
{
}
public void Load()
{
//should already be loaded
}
public IEnumerable<string> ProduceSubKeys(IEnumerable<string> earlierKeys, string prefix, string delimiter)
{
throw new NotImplementedException();
}
public void Set(string key, string value)
{
_config.Add(key, value);
}
public bool TryGet(string key, out string value)
{
return _config.TryGetValue(key, out value);
}
}
}

Просмотреть файл

@ -0,0 +1,27 @@
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-beta3",
"Microsoft.AspNet.Hosting": "1.0.0-beta3",
"Microsoft.AspNet.Mvc": "6.0.0-beta3",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta3",
"xunit": "2.1.0.0-beta1-build2945",
"xunit.runner.aspnet": "2.1.0.0-beta1-build60",
"xunit.runner.visualstudio": "2.1.0.0-beta1-build1051",
"Microsoft.Net.Http.Client": "1.0.0-*"
},
"frameworks": {
"aspnet50": {
"dependencies": {
"Microsoft.ApplicationInsights": "0.13.4-build00460"
}
},
"aspnetcore50": {
"dependencies": {
"System.Runtime": "4.0.20-beta-22523",
"Microsoft.ApplicationInsights.Portable": "0.30.0.0-beta"
}
}
}
}

Просмотреть файл

@ -0,0 +1,61 @@
namespace SampleWebAppIntegration.FunctionalTest
{
using Xunit;
using System.Net.Http;
using System.Net;
using FunctionalTestUtils;
using System.Collections.Generic;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
public class SuccessScenario
{
[Fact]
public async void ServerReturns200WithMonitoring()
{
InProcessServer server = new InProcessServer("SampleWebAppIntegration");
List<ITelemetry> buffer = new List<ITelemetry>();
BackTelemetryChannelExtensions.InitializeFunctionalTestTelemetryChannel(buffer);
using (server.Start())
{
HttpClient client = new HttpClient();
var result = await client.GetAsync(server.BaseHost);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal(1, buffer.Count);
ITelemetry telemetry = buffer[0];
Assert.IsAssignableFrom(typeof(RequestTelemetry), telemetry);
RequestTelemetry request = (RequestTelemetry)telemetry;
Assert.Equal("200", request.ResponseCode);
Assert.Equal("GET /", request.Name);
}
}
[Fact]
public async void ServerReturns404ForNotExistingController()
{
InProcessServer server = new InProcessServer("SampleWebAppIntegration");
List<ITelemetry> buffer = new List<ITelemetry>();
BackTelemetryChannelExtensions.InitializeFunctionalTestTelemetryChannel(buffer);
using (server.Start())
{
HttpClient client = new HttpClient();
var result = await client.GetAsync(server.BaseHost + "/not/existing/controller");
Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
Assert.Equal(1, buffer.Count);
ITelemetry telemetry = buffer[0];
Assert.IsAssignableFrom(typeof(RequestTelemetry), telemetry);
RequestTelemetry request = (RequestTelemetry)telemetry;
Assert.Equal("404", request.ResponseCode);
}
}
}
}

Просмотреть файл

@ -14,6 +14,7 @@ using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;
using SampleWebAppIntegration.Models;
using FunctionalTestUtils;
namespace SampleWebAppIntegration
{
@ -35,6 +36,8 @@ namespace SampleWebAppIntegration
// Add Application Insights services to the services container.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddFunctionalTestTelemetryChannel();
// Add EF services to the services container.
services.AddEntityFramework(Configuration)
.AddSqlServer()

Просмотреть файл

@ -19,13 +19,18 @@
"Microsoft.Framework.Logging": "1.0.0-beta3",
"Microsoft.Framework.Logging.Console": "1.0.0-beta3",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta3",
"ApplicationInsights.AspNet": "0.30.0.1-beta"
"ApplicationInsights.AspNet": "0.30.0.1-beta",
"FunctionalTestUtils": "1.0.0-*",
"xunit": "2.1.0.0-beta1-build2945",
"xunit.runner.aspnet": "2.1.0.0-beta1-build60",
"xunit.runner.visualstudio": "2.1.0.0-beta1-build1051",
},
"commands": {
/* Change the port number when you are self hosting this application */
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
"gen": "Microsoft.Framework.CodeGeneration",
"ef": "EntityFramework.Commands"
"ef": "EntityFramework.Commands",
"test": "xunit.runner.aspnet"
},
"frameworks": {
"aspnet50": { },