Using Console logger to print logs instead of using Console.WriteLine

This commit is contained in:
Praburaj 2015-01-09 16:44:08 -08:00
parent a6bf0e16aa
commit fdfe88f8e0
12 changed files with 428 additions and 389 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Data.SqlClient;
using Microsoft.Framework.Logging;
namespace E2ETests
{
@ -8,11 +9,11 @@ namespace E2ETests
/// </summary>
public class DbUtils
{
public static void DropDatabase(string databaseName)
public static void DropDatabase(string databaseName, ILogger logger)
{
try
{
Console.WriteLine("Trying to drop database '{0}'", databaseName);
logger.WriteInformation("Trying to drop database '{0}'", databaseName);
using (var conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB;Database=master;Trusted_Connection=True;"))
{
conn.Open();
@ -25,13 +26,13 @@ namespace E2ETests
END", databaseName);
cmd.ExecuteNonQuery();
Console.WriteLine("Successfully dropped database {0}", databaseName);
logger.WriteInformation("Successfully dropped database {0}", databaseName);
}
}
catch (Exception exception)
{
//Ignore if there is failure in cleanup.
Console.WriteLine("Error occured while dropping database {0}. Exception : {1}", databaseName, exception);
logger.WriteWarning("Error occured while dropping database {0}. Exception : {1}", databaseName, exception.ToString());
}
}
}

View File

@ -7,6 +7,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Runtime;
using Microsoft.Framework.Runtime.Infrastructure;
@ -56,7 +57,7 @@ namespace E2ETests
private static string APP_RELATIVE_PATH = Path.Combine("..", "..", "src", "MusicStore");
public static Process StartApplication(StartParameters startParameters, string identityDbName)
public static Process StartApplication(StartParameters startParameters, string identityDbName, ILogger logger)
{
startParameters.ApplicationPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, APP_RELATIVE_PATH));
@ -83,12 +84,12 @@ namespace E2ETests
if (startParameters.KreFlavor == KreFlavor.Mono)
{
hostProcess = StartMonoHost(startParameters);
hostProcess = StartMonoHost(startParameters, logger);
}
else
{
//Tweak the %PATH% to the point to the right KREFLAVOR
startParameters.Kre = SwitchPathToKreFlavor(startParameters.KreFlavor, startParameters.KreArchitecture);
startParameters.Kre = SwitchPathToKreFlavor(startParameters.KreFlavor, startParameters.KreArchitecture, logger);
//Reason to do pack here instead of in a common place is use the right KRE to do the packing. Previous line switches to use the right KRE.
if (startParameters.PackApplicationBeforeStart)
@ -97,22 +98,22 @@ namespace E2ETests
startParameters.ServerType == ServerType.IIS)
{
// Pack to IIS root\application folder.
KpmPack(startParameters, Path.Combine(Environment.GetEnvironmentVariable("SystemDrive") + @"\", @"inetpub\wwwroot"));
KpmPack(startParameters, logger, Path.Combine(Environment.GetEnvironmentVariable("SystemDrive") + @"\", @"inetpub\wwwroot"));
// Drop a Microsoft.AspNet.Hosting.ini with ASPNET_ENV information.
Console.WriteLine("Creating Microsoft.AspNet.Hosting.ini file with ASPNET_ENV.");
logger.WriteInformation("Creating Microsoft.AspNet.Hosting.ini file with ASPNET_ENV.");
var iniFile = Path.Combine(startParameters.ApplicationPath, "Microsoft.AspNet.Hosting.ini");
File.WriteAllText(iniFile, string.Format("ASPNET_ENV={0}", startParameters.EnvironmentName));
// Can't use localdb with IIS. Setting an override to use InMemoryStore.
Console.WriteLine("Creating configoverride.json file to override default config.");
logger.WriteInformation("Creating configoverride.json file to override default config.");
var overrideConfig = Path.Combine(startParameters.ApplicationPath, "..", "approot", "src", "MusicStore", "configoverride.json");
overrideConfig = Path.GetFullPath(overrideConfig);
File.WriteAllText(overrideConfig, "{\"UseInMemoryStore\": \"true\"}");
if (startParameters.ServerType == ServerType.IISNativeModule)
{
Console.WriteLine("Turning runAllManagedModulesForAllRequests=true in web.config.");
logger.WriteInformation("Turning runAllManagedModulesForAllRequests=true in web.config.");
// Set runAllManagedModulesForAllRequests=true
var webConfig = Path.Combine(startParameters.ApplicationPath, "web.config");
var configuration = new XmlDocument();
@ -129,29 +130,29 @@ namespace E2ETests
configuration.Save(webConfig);
}
Console.WriteLine("Successfully finished IIS application directory setup.");
logger.WriteInformation("Successfully finished IIS application directory setup.");
Thread.Sleep(1 * 1000);
}
else
{
KpmPack(startParameters);
KpmPack(startParameters, logger);
}
}
if (startParameters.ServerType == ServerType.IISNativeModule ||
startParameters.ServerType == ServerType.IIS)
{
startParameters.IISApplication = new IISApplication(startParameters);
startParameters.IISApplication = new IISApplication(startParameters, logger);
startParameters.IISApplication.SetupApplication();
}
else if (startParameters.ServerType == ServerType.IISExpress)
{
hostProcess = StartHeliosHost(startParameters);
hostProcess = StartHeliosHost(startParameters, logger);
}
else
{
hostProcess = StartSelfHost(startParameters, identityDbName);
hostProcess = StartSelfHost(startParameters, identityDbName, logger);
}
}
@ -161,7 +162,7 @@ namespace E2ETests
return hostProcess;
}
private static Process StartMonoHost(StartParameters startParameters)
private static Process StartMonoHost(StartParameters startParameters, ILogger logger)
{
var path = Environment.GetEnvironmentVariable("PATH");
var kreBin = path.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Where(c => c.Contains("KRE-Mono")).FirstOrDefault();
@ -175,19 +176,19 @@ namespace E2ETests
{
// We use full path to KRE to pack.
startParameters.Kre = new DirectoryInfo(kreBin).Parent.FullName;
KpmPack(startParameters);
KpmPack(startParameters, logger);
}
//Mono does not have a way to pass in a --appbase switch. So it will be an environment variable.
Environment.SetEnvironmentVariable("KRE_APPBASE", startParameters.ApplicationPath);
Console.WriteLine("Setting the KRE_APPBASE to {0}", startParameters.ApplicationPath);
logger.WriteInformation("Setting the KRE_APPBASE to {0}", startParameters.ApplicationPath);
var monoPath = "mono";
var klrMonoManaged = Path.Combine(kreBin, "klr.mono.managed.dll");
var applicationHost = Path.Combine(kreBin, "Microsoft.Framework.ApplicationHost");
var commandName = startParameters.ServerType == ServerType.Kestrel ? "kestrel" : string.Empty;
Console.WriteLine(string.Format("Executing command: {0} {1} {2} {3}", monoPath, klrMonoManaged, applicationHost, commandName));
logger.WriteInformation(string.Format("Executing command: {0} {1} {2} {3}", monoPath, klrMonoManaged, applicationHost, commandName));
var startInfo = new ProcessStartInfo
{
@ -199,7 +200,7 @@ namespace E2ETests
};
var hostProcess = Process.Start(startInfo);
Console.WriteLine("Started {0}. Process Id : {1}", hostProcess.MainModule.FileName, hostProcess.Id);
logger.WriteInformation("Started {0}. Process Id : {1}", hostProcess.MainModule.FileName, hostProcess.Id.ToString());
Thread.Sleep(5 * 1000);
//Clear the appbase so that it does not create issues with successive runs
@ -207,7 +208,7 @@ namespace E2ETests
return hostProcess;
}
private static Process StartHeliosHost(StartParameters startParameters)
private static Process StartHeliosHost(StartParameters startParameters, ILogger logger)
{
if (!string.IsNullOrWhiteSpace(startParameters.ApplicationHostConfigTemplateContent))
{
@ -233,7 +234,7 @@ namespace E2ETests
var iisExpressPath = GetIISExpressPath(startParameters.KreArchitecture);
Console.WriteLine("Executing command : {0} {1}", iisExpressPath, parameters);
logger.WriteInformation("Executing command : {0} {1}", iisExpressPath, parameters);
var startInfo = new ProcessStartInfo
{
@ -244,15 +245,15 @@ namespace E2ETests
};
var hostProcess = Process.Start(startInfo);
Console.WriteLine("Started iisexpress. Process Id : {0}", hostProcess.Id);
logger.WriteInformation("Started iisexpress. Process Id : {0}", hostProcess.Id.ToString());
return hostProcess;
}
private static Process StartSelfHost(StartParameters startParameters, string identityDbName)
private static Process StartSelfHost(StartParameters startParameters, string identityDbName, ILogger logger)
{
var commandName = startParameters.ServerType == ServerType.WebListener ? "web" : "kestrel";
Console.WriteLine(string.Format("Executing klr.exe --appbase {0} \"Microsoft.Framework.ApplicationHost\" {1}", startParameters.ApplicationPath, commandName));
logger.WriteInformation("Executing klr.exe --appbase {0} \"Microsoft.Framework.ApplicationHost\" {1}", startParameters.ApplicationPath, commandName);
var startInfo = new ProcessStartInfo
{
@ -268,23 +269,23 @@ namespace E2ETests
try
{
Console.WriteLine("Started {0}. Process Id : {1}", hostProcess.MainModule.FileName, hostProcess.Id);
logger.WriteInformation("Started {0}. Process Id : {1}", hostProcess.MainModule.FileName, hostProcess.Id.ToString());
}
catch (Win32Exception win32Exception)
{
Console.WriteLine("Cannot access 64 bit modules from a 32 bit process. Failed with following message : {0}", win32Exception.Message);
logger.WriteWarning("Cannot access 64 bit modules from a 32 bit process. Failed with following message.", win32Exception);
}
WaitTillDbCreated(identityDbName);
WaitTillDbCreated(identityDbName, logger);
return hostProcess;
}
private static string SwitchPathToKreFlavor(KreFlavor kreFlavor, KreArchitecture kreArchitecture)
private static string SwitchPathToKreFlavor(KreFlavor kreFlavor, KreArchitecture kreArchitecture, ILogger logger)
{
var pathValue = Environment.GetEnvironmentVariable("PATH");
Console.WriteLine();
Console.WriteLine("Current %PATH% value : {0}", pathValue);
logger.WriteInformation(string.Empty);
logger.WriteInformation("Current %PATH% value : {0}", pathValue);
var replaceStr = new StringBuilder().
Append("KRE").
@ -301,17 +302,17 @@ namespace E2ETests
// Tweak the %PATH% to the point to the right KREFLAVOR.
Environment.SetEnvironmentVariable("PATH", pathValue);
Console.WriteLine();
Console.WriteLine("Changing to use KRE : {0}", kreName);
logger.WriteInformation(string.Empty);
logger.WriteInformation("Changing to use KRE : {0}", kreName);
return kreName;
}
private static void KpmPack(StartParameters startParameters, string packRoot = null)
private static void KpmPack(StartParameters startParameters, ILogger logger, string packRoot = null)
{
startParameters.PackedApplicationRootPath = Path.Combine(packRoot ?? Path.GetTempPath(), Guid.NewGuid().ToString());
var parameters = string.Format("pack {0} -o {1} --runtime {2}", startParameters.ApplicationPath, startParameters.PackedApplicationRootPath, startParameters.Kre);
Console.WriteLine(string.Format("Executing command kpm {0}", parameters));
logger.WriteInformation("Executing command kpm {0}", parameters);
var startInfo = new ProcessStartInfo
{
@ -331,22 +332,22 @@ namespace E2ETests
Path.Combine(startParameters.PackedApplicationRootPath, "wwwroot") :
Path.Combine(startParameters.PackedApplicationRootPath, "approot", "src", "MusicStore");
Console.WriteLine("kpm pack finished with exit code : {0}", hostProcess.ExitCode);
logger.WriteInformation("kpm pack finished with exit code : {0}", hostProcess.ExitCode.ToString());
}
//In case of self-host application activation happens immediately unlike iis where activation happens on first request.
//So in self-host case, we need a way to block the first request until the application is initialized. In MusicStore application's case,
//identity DB creation is pretty much the last step of application setup. So waiting on this event will help us wait efficiently.
private static void WaitTillDbCreated(string identityDbName)
private static void WaitTillDbCreated(string identityDbName, ILogger logger)
{
var identityDBFullPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), identityDbName + ".mdf");
if (File.Exists(identityDBFullPath))
{
Console.WriteLine("Database file '{0}' exists. Proceeding with the tests.", identityDBFullPath);
logger.WriteWarning("Database file '{0}' exists. Proceeding with the tests.", identityDBFullPath);
return;
}
Console.WriteLine("Watching for the DB file '{0}'", identityDBFullPath);
logger.WriteInformation("Watching for the DB file '{0}'", identityDBFullPath);
var dbWatch = new FileSystemWatcher(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), identityDbName + ".mdf");
dbWatch.EnableRaisingEvents = true;
@ -360,17 +361,17 @@ namespace E2ETests
{
//This event is fired immediately after the localdb file is created. Give it a while to finish populating data and start the application.
Thread.Sleep(5 * 1000);
Console.WriteLine("Database file created '{0}'. Proceeding with the tests.", identityDBFullPath);
logger.WriteInformation("Database file created '{0}'. Proceeding with the tests.", identityDBFullPath);
}
else
{
Console.WriteLine("Database file '{0}' not created", identityDBFullPath);
logger.WriteWarning("Database file '{0}' not created", identityDBFullPath);
}
}
}
catch (Exception exception)
{
Console.WriteLine("Received this exception while watching for Database file {0}", exception);
logger.WriteWarning("Received this exception while watching for Database file", exception);
}
finally
{
@ -378,7 +379,7 @@ namespace E2ETests
}
}
public static void CleanUpApplication(StartParameters startParameters, Process hostProcess, string musicStoreDbName)
public static void CleanUpApplication(StartParameters startParameters, Process hostProcess, string musicStoreDbName, ILogger logger)
{
if (startParameters.ServerType == ServerType.IISNativeModule ||
startParameters.ServerType == ServerType.IIS)
@ -396,22 +397,22 @@ namespace E2ETests
hostProcess.WaitForExit(5 * 1000);
if (!hostProcess.HasExited)
{
Console.WriteLine("Unable to terminate the host process with process Id '{0}", hostProcess.Id);
logger.WriteWarning("Unable to terminate the host process with process Id '{0}", hostProcess.Id.ToString());
}
else
{
Console.WriteLine("Successfully terminated host process with process Id '{0}'", hostProcess.Id);
logger.WriteInformation("Successfully terminated host process with process Id '{0}'", hostProcess.Id.ToString());
}
}
else
{
Console.WriteLine("Host process already exited or never started successfully.");
logger.WriteWarning("Host process already exited or never started successfully.");
}
if (!Helpers.RunningOnMono)
{
//Mono uses InMemoryStore
DbUtils.DropDatabase(musicStoreDbName);
DbUtils.DropDatabase(musicStoreDbName, logger);
}
if (!string.IsNullOrWhiteSpace(startParameters.ApplicationHostConfigLocation))
@ -426,7 +427,7 @@ namespace E2ETests
catch (Exception exception)
{
//Ignore delete failures - just write a log
Console.WriteLine("Failed to delete '{0}'. Exception : {1}", startParameters.ApplicationHostConfigLocation, exception.Message);
logger.WriteWarning("Failed to delete '{0}'. Exception : {1}", startParameters.ApplicationHostConfigLocation, exception.Message);
}
}
}
@ -440,7 +441,7 @@ namespace E2ETests
}
catch (Exception exception)
{
Console.WriteLine("Failed to delete directory '{0}'. Exception message: {1}", startParameters.PackedApplicationRootPath, exception.Message);
logger.WriteWarning("Failed to delete directory.", exception);
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.Framework.Logging;
using Microsoft.Web.Administration;
namespace E2ETests
@ -12,14 +13,16 @@ namespace E2ETests
private readonly ServerManager _serverManager = new ServerManager();
private readonly StartParameters _startParameters;
private readonly ILogger _logger;
private ApplicationPool _applicationPool;
private Application _application;
public string VirtualDirectoryName { get; set; }
public IISApplication(StartParameters startParameters)
public IISApplication(StartParameters startParameters, ILogger logger)
{
_startParameters = startParameters;
_logger = logger;
}
public void SetupApplication()
@ -55,20 +58,22 @@ namespace E2ETests
applicationPool.ManagedRuntimeVersion = NATIVE_MODULE_MANAGED_RUNTIME_VERSION;
}
applicationPool.Enable32BitAppOnWin64 = (_startParameters.KreArchitecture == KreArchitecture.x86);
Console.WriteLine("Created {0} application pool '{1}' with runtime version '{2}'.", _startParameters.KreArchitecture, applicationPool.Name, applicationPool.ManagedRuntimeVersion ?? "default");
_logger.WriteInformation("Created {0} application pool '{1}' with runtime version '{2}'.",
_startParameters.KreArchitecture.ToString(), applicationPool.Name,
applicationPool.ManagedRuntimeVersion ?? "default");
return applicationPool;
}
public void StopAndDeleteAppPool()
{
Console.WriteLine("Stopping application pool '{0}' and deleting application.", _applicationPool.Name);
_logger.WriteInformation("Stopping application pool '{0}' and deleting application.", _applicationPool.Name);
_applicationPool.Stop();
// Remove the application from website.
_application = Website.Applications.Where(a => a.Path == _application.Path).FirstOrDefault();
Website.Applications.Remove(_application);
_serverManager.ApplicationPools.Remove(_serverManager.ApplicationPools[_applicationPool.Name]);
_serverManager.CommitChanges();
Console.WriteLine("Successfully stopped application pool '{0}' and deleted application from IIS.", _applicationPool.Name);
_logger.WriteInformation("Successfully stopped application pool '{0}' and deleted application from IIS.", _applicationPool.Name);
}
}
}

View File

@ -4,6 +4,7 @@ using System.Net;
using System.Net.Http;
using Microsoft.AspNet.PipelineCore.Collections;
using Microsoft.AspNet.WebUtilities;
using Microsoft.Framework.Logging;
using Xunit;
namespace E2ETests
@ -18,7 +19,7 @@ namespace E2ETests
var response = _httpClient.GetAsync("Account/Login").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Signing in with Facebook account");
_logger.WriteInformation("Signing in with Facebook account");
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("provider", "Facebook"),
@ -84,14 +85,14 @@ namespace E2ETests
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.ExternalLogin"));
Console.WriteLine("Successfully signed in with user '{0}'", "AspnetvnextTest@test.com");
_logger.WriteInformation("Successfully signed in with user '{0}'", "AspnetvnextTest@test.com");
Console.WriteLine("Verifying if the middleware notifications were fired");
_logger.WriteInformation("Verifying if the middleware notifications were fired");
//Check for a non existing item
response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", "123")).Result;
//This action requires admin permissions. If notifications are fired this permission is granted
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Console.WriteLine("Middleware notifications were fired successfully");
_logger.WriteInformation("Middleware notifications were fired successfully");
}
}
}

View File

@ -4,6 +4,7 @@ using System.Net;
using System.Net.Http;
using Microsoft.AspNet.PipelineCore.Collections;
using Microsoft.AspNet.WebUtilities;
using Microsoft.Framework.Logging;
using Xunit;
namespace E2ETests
@ -18,7 +19,7 @@ namespace E2ETests
var response = _httpClient.GetAsync("Account/Login").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Signing in with Google account");
_logger.WriteInformation("Signing in with Google account");
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("provider", "Google"),
@ -64,7 +65,7 @@ namespace E2ETests
//Correlation cookie not getting cleared after successful signin?
if (!Helpers.RunningOnMono)
{
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Correlation.Google"));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Correlation.Google"));
}
Assert.Equal(_applicationBaseUrl + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri);
Assert.Contains("AspnetvnextTest@gmail.com", responseContent, StringComparison.OrdinalIgnoreCase);
@ -85,15 +86,15 @@ namespace E2ETests
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.ExternalLogin"));
Console.WriteLine("Successfully signed in with user '{0}'", "AspnetvnextTest@gmail.com");
_logger.WriteInformation("Successfully signed in with user '{0}'", "AspnetvnextTest@gmail.com");
Console.WriteLine("Verifying if the middleware notifications were fired");
_logger.WriteInformation("Verifying if the middleware notifications were fired");
//Check for a non existing item
response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", "123")).Result;
//This action requires admin permissions. If notifications are fired this permission is granted
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
_logger.WriteVerbose(response.Content.ReadAsStringAsync().Result);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Console.WriteLine("Middleware notifications were fired successfully");
_logger.WriteInformation("Middleware notifications were fired successfully");
}
}
}

View File

@ -4,6 +4,7 @@ using System.Net;
using System.Net.Http;
using Microsoft.AspNet.PipelineCore.Collections;
using Microsoft.AspNet.WebUtilities;
using Microsoft.Framework.Logging;
using Xunit;
namespace E2ETests
@ -18,7 +19,7 @@ namespace E2ETests
var response = _httpClient.GetAsync("Account/Login").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Signing in with Microsoft account");
_logger.WriteInformation("Signing in with Microsoft account");
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("provider", "Microsoft"),
@ -84,15 +85,15 @@ namespace E2ETests
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.ExternalLogin"));
Console.WriteLine("Successfully signed in with user '{0}'", "microsoft@test.com");
_logger.WriteInformation("Successfully signed in with user '{0}'", "microsoft@test.com");
Console.WriteLine("Verifying if the middleware notifications were fired");
_logger.WriteInformation("Verifying if the middleware notifications were fired");
//Check for a non existing item
response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", "123")).Result;
//This action requires admin permissions. If notifications are fired this permission is granted
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
_logger.WriteInformation(response.Content.ReadAsStringAsync().Result);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Console.WriteLine("Middleware notifications were fired successfully");
_logger.WriteInformation("Middleware notifications were fired successfully");
}
}
}

View File

@ -4,6 +4,7 @@ using System.Net;
using System.Net.Http;
using System.Threading;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.Framework.Logging;
using Xunit;
namespace E2ETests
@ -12,29 +13,29 @@ namespace E2ETests
{
private void VerifyStaticContentServed()
{
Console.WriteLine("Validating if static contents are served..");
Console.WriteLine("Fetching favicon.ico..");
_logger.WriteInformation("Validating if static contents are served..");
_logger.WriteInformation("Fetching favicon.ico..");
var response = _httpClient.GetAsync("favicon.ico").Result;
ThrowIfResponseStatusNotOk(response);
Console.WriteLine("Etag received: {0}", response.Headers.ETag.Tag);
_logger.WriteInformation("Etag received: {0}", response.Headers.ETag.Tag);
//Check if you receive a NotModified on sending an etag
Console.WriteLine("Sending an IfNoneMatch header with e-tag");
_logger.WriteInformation("Sending an IfNoneMatch header with e-tag");
_httpClient.DefaultRequestHeaders.IfNoneMatch.Add(response.Headers.ETag);
response = _httpClient.GetAsync("favicon.ico").Result;
Assert.Equal(HttpStatusCode.NotModified, response.StatusCode);
_httpClient.DefaultRequestHeaders.IfNoneMatch.Clear();
Console.WriteLine("Successfully received a NotModified status");
_logger.WriteInformation("Successfully received a NotModified status");
Console.WriteLine("Fetching /Content/bootstrap.css..");
_logger.WriteInformation("Fetching /Content/bootstrap.css..");
response = _httpClient.GetAsync("Content/bootstrap.css").Result;
ThrowIfResponseStatusNotOk(response);
Console.WriteLine("Verified static contents are served successfully");
_logger.WriteInformation("Verified static contents are served successfully");
}
private void VerifyHomePage(HttpResponseMessage response, string responseContent, bool useNtlmAuthentication = false)
{
Console.WriteLine("Home page content : {0}", responseContent);
_logger.WriteVerbose("Home page content : {0}", responseContent);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
ValidateLayoutPage(responseContent);
Assert.Contains(PrefixBaseAddress("<a href=\"/{0}/Store/Details/"), responseContent, StringComparison.OrdinalIgnoreCase);
@ -49,7 +50,7 @@ namespace E2ETests
Assert.Contains("mvcmusicstore.codeplex.com", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("/Images/home-showcase.png", responseContent, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Application initialization successful.");
_logger.WriteInformation("Application initialization successful.");
}
private string PrefixBaseAddress(string url)
@ -73,7 +74,7 @@ namespace E2ETests
private void AccessStoreWithoutPermissions(string email = null)
{
Console.WriteLine("Trying to access StoreManager that needs ManageStore claim with the current user : {0}", email ?? "Anonymous");
_logger.WriteInformation("Trying to access StoreManager that needs ManageStore claim with the current user : {0}", email ?? "Anonymous");
var response = _httpClient.GetAsync("Admin/StoreManager/").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
@ -81,30 +82,29 @@ namespace E2ETests
Assert.Contains("<title>Log in MVC Music Store</title>", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("<h4>Use a local account to log in.</h4>", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Equal<string>(_applicationBaseUrl + PrefixBaseAddress("Account/Login?ReturnUrl=%2F{0}%2FAdmin%2FStoreManager%2F"), response.RequestMessage.RequestUri.AbsoluteUri);
Console.WriteLine("Redirected to login page as expected.");
_logger.WriteInformation("Redirected to login page as expected.");
}
private void AccessStoreWithPermissions()
{
Console.WriteLine("Trying to access the store inventory..");
_logger.WriteInformation("Trying to access the store inventory..");
var response = _httpClient.GetAsync("Admin/StoreManager/").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Equal<string>(_applicationBaseUrl + "Admin/StoreManager/", response.RequestMessage.RequestUri.AbsoluteUri);
Console.WriteLine("Successfully acccessed the store inventory");
_logger.WriteInformation("Successfully acccessed the store inventory");
}
private void RegisterUserWithNonMatchingPasswords()
{
Console.WriteLine("Trying to create user with not matching password and confirm password");
_logger.WriteInformation("Trying to create user with not matching password and confirm password");
var response = _httpClient.GetAsync("Account/Register").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
ValidateLayoutPage(responseContent);
var generatedEmail = Guid.NewGuid().ToString().Replace("-", string.Empty) + "@test.com";
Console.WriteLine("Creating a new user with name '{0}'", generatedEmail);
_logger.WriteInformation("Creating a new user with name '{0}'", generatedEmail);
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", generatedEmail),
@ -118,7 +118,7 @@ namespace E2ETests
responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Assert.Contains("<div class=\"validation-summary-errors text-danger\" data-valmsg-summary=\"true\"><ul><li>The password and confirmation password do not match.</li>", responseContent, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Server side model validator rejected the user '{0}''s registration as passwords do not match.", generatedEmail);
_logger.WriteInformation("Server side model validator rejected the user '{0}''s registration as passwords do not match.", generatedEmail);
}
private string RegisterValidUser()
@ -129,7 +129,7 @@ namespace E2ETests
ValidateLayoutPage(responseContent);
var generatedEmail = Guid.NewGuid().ToString().Replace("-", string.Empty) + "@test.com";
Console.WriteLine("Creating a new user with name '{0}'", generatedEmail);
_logger.WriteInformation("Creating a new user with name '{0}'", generatedEmail);
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", generatedEmail),
@ -158,11 +158,11 @@ namespace E2ETests
private void RegisterExistingUser(string email)
{
Console.WriteLine("Trying to register a user with name '{0}' again", email);
_logger.WriteInformation("Trying to register a user with name '{0}' again", email);
var response = _httpClient.GetAsync("Account/Register").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Creating a new user with name '{0}'", email);
_logger.WriteInformation("Creating a new user with name '{0}'", email);
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", email),
@ -175,12 +175,12 @@ namespace E2ETests
response = _httpClient.PostAsync("Account/Register", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains(string.Format("UserName &#39;{0}&#39; is already taken.", email), responseContent, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Identity threw a valid exception that user '{0}' already exists in the system", email);
_logger.WriteInformation("Identity threw a valid exception that user '{0}' already exists in the system", email);
}
private void SignOutUser(string email)
{
Console.WriteLine("Signing out from '{0}''s session", email);
_logger.WriteInformation("Signing out from '{0}''s session", email);
var response = _httpClient.GetAsync(string.Empty).Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
@ -203,7 +203,7 @@ namespace E2ETests
Assert.Contains("/Images/home-showcase.png", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie cleared on logout
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Console.WriteLine("Successfully signed out of '{0}''s session", email);
_logger.WriteInformation("Successfully signed out of '{0}''s session", email);
}
else
{
@ -218,7 +218,7 @@ namespace E2ETests
var response = _httpClient.GetAsync("Account/Login").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Signing in with user '{0}'", email);
_logger.WriteInformation("Signing in with user '{0}'", email);
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", email),
@ -232,7 +232,7 @@ namespace E2ETests
Assert.Contains("<div class=\"validation-summary-errors text-danger\"><ul><li>Invalid login attempt.</li>", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie not sent
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Console.WriteLine("Identity successfully prevented an invalid user login.");
_logger.WriteInformation("Identity successfully prevented an invalid user login.");
}
private void SignInWithUser(string email, string password)
@ -240,7 +240,7 @@ namespace E2ETests
var response = _httpClient.GetAsync("Account/Login").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Signing in with user '{0}'", email);
_logger.WriteInformation("Signing in with user '{0}'", email);
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", email),
@ -255,7 +255,7 @@ namespace E2ETests
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Console.WriteLine("Successfully signed in with user '{0}'", email);
_logger.WriteInformation("Successfully signed in with user '{0}'", email);
}
private void ChangePassword(string email)
@ -276,7 +276,7 @@ namespace E2ETests
responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains("Your password has been changed.", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Console.WriteLine("Successfully changed the password for user '{0}'", email);
_logger.WriteInformation("Successfully changed the password for user '{0}'", email);
}
private string CreateAlbum()
@ -287,7 +287,7 @@ namespace E2ETests
var hubConnection = new HubConnection(_applicationBaseUrl + "SignalR");
hubConnection.Received += (data) =>
{
Console.WriteLine("Data received by SignalR client: {0}", data);
_logger.WriteVerbose("Data received by SignalR client: {0}", data);
dataFromHub = data;
OnReceivedEvent.Set();
};
@ -295,7 +295,7 @@ namespace E2ETests
IHubProxy proxy = hubConnection.CreateHubProxy("Announcement");
hubConnection.Start().Wait();
Console.WriteLine("Trying to create an album with name '{0}'", albumName);
_logger.WriteInformation("Trying to create an album with name '{0}'", albumName);
var response = _httpClient.GetAsync("Admin/StoreManager/create").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
@ -315,27 +315,27 @@ namespace E2ETests
Assert.Equal<string>(_applicationBaseUrl + "Admin/StoreManager", response.RequestMessage.RequestUri.AbsoluteUri);
Assert.Contains(albumName, responseContent);
Console.WriteLine("Waiting for the SignalR client to receive album created announcement");
_logger.WriteInformation("Waiting for the SignalR client to receive album created announcement");
OnReceivedEvent.WaitOne(TimeSpan.FromSeconds(10));
dataFromHub = dataFromHub ?? "No relevant data received from Hub";
Assert.Contains(albumName, dataFromHub);
Console.WriteLine("Successfully created an album with name '{0}' in the store", albumName);
_logger.WriteInformation("Successfully created an album with name '{0}' in the store", albumName);
return albumName;
}
private string FetchAlbumIdFromName(string albumName)
{
Console.WriteLine("Fetching the album id of '{0}'", albumName);
_logger.WriteInformation("Fetching the album id of '{0}'", albumName);
var response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result;
ThrowIfResponseStatusNotOk(response);
var albumId = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Album id for album '{0}' is '{1}'", albumName, albumId);
_logger.WriteInformation("Album id for album '{0}' is '{1}'", albumName, albumId);
return albumId;
}
private void VerifyAlbumDetails(string albumId, string albumName)
{
Console.WriteLine("Getting details of album with Id '{0}'", albumId);
_logger.WriteInformation("Getting details of album with Id '{0}'", albumId);
var response = _httpClient.GetAsync(string.Format("Admin/StoreManager/Details?id={0}", albumId)).Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
@ -348,7 +348,7 @@ namespace E2ETests
// This gets the view that non-admin users get to see.
private void GetAlbumDetailsFromStore(string albumId, string albumName)
{
Console.WriteLine("Getting details of album with Id '{0}'", albumId);
_logger.WriteInformation("Getting details of album with Id '{0}'", albumId);
var response = _httpClient.GetAsync(string.Format("Store/Details/{0}", albumId)).Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
@ -357,18 +357,18 @@ namespace E2ETests
private void AddAlbumToCart(string albumId, string albumName)
{
Console.WriteLine("Adding album id '{0}' to the cart", albumId);
_logger.WriteInformation("Adding album id '{0}' to the cart", albumId);
var response = _httpClient.GetAsync(string.Format("ShoppingCart/AddToCart?id={0}", albumId)).Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Assert.Contains(albumName, responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("<span class=\"glyphicon glyphicon glyphicon-shopping-cart\"></span>", responseContent, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("Verified that album is added to cart");
_logger.WriteInformation("Verified that album is added to cart");
}
private void CheckOutCartItems()
{
Console.WriteLine("Checking out the cart contents...");
_logger.WriteInformation("Checking out the cart contents...");
var response = _httpClient.GetAsync("Checkout/AddressAndPayment").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
@ -397,7 +397,7 @@ namespace E2ETests
private void DeleteAlbum(string albumId, string albumName)
{
Console.WriteLine("Deleting album '{0}' from the store..", albumName);
_logger.WriteInformation("Deleting album '{0}' from the store..", albumName);
var formParameters = new List<KeyValuePair<string, string>>
{
@ -408,17 +408,17 @@ namespace E2ETests
var response = _httpClient.PostAsync("Admin/StoreManager/RemoveAlbum", content).Result;
ThrowIfResponseStatusNotOk(response);
Console.WriteLine("Verifying if the album '{0}' is deleted from store", albumName);
_logger.WriteInformation("Verifying if the album '{0}' is deleted from store", albumName);
response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result;
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Console.WriteLine("Album is successfully deleted from the store.", albumName, albumId);
_logger.WriteInformation("Album is successfully deleted from the store.", albumName, albumId);
}
private void ThrowIfResponseStatusNotOk(HttpResponseMessage response)
{
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
_logger.WriteError(response.Content.ReadAsStringAsync().Result);
throw new Exception(string.Format("Received the above response with status code : {0}", response.StatusCode.ToString()));
}
}

View File

@ -4,6 +4,7 @@ using System.Net;
using System.Net.Http;
using Microsoft.AspNet.PipelineCore.Collections;
using Microsoft.AspNet.WebUtilities;
using Microsoft.Framework.Logging;
using Xunit;
namespace E2ETests
@ -21,7 +22,7 @@ namespace E2ETests
var response = _httpClient.GetAsync("Account/Login").Result;
ThrowIfResponseStatusNotOk(response);
var responseContent = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Signing in with Twitter account");
_logger.WriteInformation("Signing in with Twitter account");
var formParameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("provider", "Twitter"),
@ -84,14 +85,14 @@ namespace E2ETests
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application"));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.ExternalLogin"));
Console.WriteLine("Successfully signed in with user '{0}'", "twitter@test.com");
_logger.WriteInformation("Successfully signed in with user '{0}'", "twitter@test.com");
Console.WriteLine("Verifying if the middleware notifications were fired");
_logger.WriteInformation("Verifying if the middleware notifications were fired");
//Check for a non existing item
response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", "123")).Result;
//This action requires admin permissions. If notifications are fired this permission is granted
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Console.WriteLine("Middleware notifications were fired successfully");
_logger.WriteInformation("Middleware notifications were fired successfully");
}
}
}

View File

@ -5,6 +5,7 @@ using System.Net;
using System.Net.Http;
using System.Threading;
using Microsoft.AspNet.Testing.xunit;
using Microsoft.Framework.Logging;
using Xunit;
namespace E2ETests
@ -18,84 +19,89 @@ namespace E2ETests
[InlineData(ServerType.WebListener, KreFlavor.CoreClr, KreArchitecture.amd64, "http://localhost:5002/")]
public void NtlmAuthenticationTest(ServerType serverType, KreFlavor kreFlavor, KreArchitecture architecture, string applicationBaseUrl)
{
Console.WriteLine("Variation Details : HostType = {0}, KreFlavor = {1}, Architecture = {2}, applicationBaseUrl = {3}", serverType, kreFlavor, architecture, applicationBaseUrl);
_startParameters = new StartParameters
using (_logger.BeginScope("NtlmAuthenticationTest"))
{
ServerType = serverType,
KreFlavor = kreFlavor,
KreArchitecture = architecture,
EnvironmentName = "NtlmAuthentication", //Will pick the Start class named 'StartupNtlmAuthentication'
ApplicationHostConfigTemplateContent = (serverType == ServerType.IISExpress) ? File.ReadAllText("NtlmAuthentation.config") : null,
SiteName = "MusicStoreNtlmAuthentication" //This is configured in the NtlmAuthentication.config
};
_logger.WriteInformation("Variation Details : HostType = {0}, KreFlavor = {1}, Architecture = {2}, applicationBaseUrl = {3}",
serverType.ToString(), kreFlavor.ToString(), architecture.ToString(), applicationBaseUrl);
var testStartTime = DateTime.Now;
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
Console.WriteLine("Pointing MusicStore DB to '{0}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
//Override the connection strings using environment based configuration
Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
_applicationBaseUrl = applicationBaseUrl;
Process hostProcess = null;
bool testSuccessful = false;
try
{
hostProcess = DeploymentUtility.StartApplication(_startParameters, musicStoreDbName);
_httpClientHandler = new HttpClientHandler() { UseDefaultCredentials = true };
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) };
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
for (int retryCount = 0; retryCount < 3; retryCount++)
_startParameters = new StartParameters
{
try
ServerType = serverType,
KreFlavor = kreFlavor,
KreArchitecture = architecture,
EnvironmentName = "NtlmAuthentication", //Will pick the Start class named 'StartupNtlmAuthentication'
ApplicationHostConfigTemplateContent = (serverType == ServerType.IISExpress) ? File.ReadAllText("NtlmAuthentation.config") : null,
SiteName = "MusicStoreNtlmAuthentication" //This is configured in the NtlmAuthentication.config
};
var testStartTime = DateTime.Now;
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
_logger.WriteInformation("Pointing MusicStore DB to '{0}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
//Override the connection strings using environment based configuration
Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
_applicationBaseUrl = applicationBaseUrl;
Process hostProcess = null;
bool testSuccessful = false;
try
{
hostProcess = DeploymentUtility.StartApplication(_startParameters, musicStoreDbName, _logger);
_httpClientHandler = new HttpClientHandler() { UseDefaultCredentials = true };
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) };
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
for (int retryCount = 0; retryCount < 3; retryCount++)
{
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
Console.WriteLine("[Time]: Approximate time taken for application initialization : '{0}' seconds", (initializationCompleteTime - testStartTime).TotalSeconds);
break; //Went through successfully
}
catch (AggregateException exception)
{
if (exception.InnerException is HttpRequestException || exception.InnerException is WebException)
try
{
Console.WriteLine("Failed to complete the request with error: {0}", exception.ToString());
Console.WriteLine("Retrying request..");
Thread.Sleep(1 * 1000); //Wait for a second before retry
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
(initializationCompleteTime - testStartTime).TotalSeconds.ToString());
break; //Went through successfully
}
catch (AggregateException exception)
{
if (exception.InnerException is HttpRequestException || exception.InnerException is WebException)
{
_logger.WriteWarning("Failed to complete the request.", exception);
_logger.WriteWarning("Retrying request..");
Thread.Sleep(1 * 1000); //Wait for a second before retry
}
}
}
VerifyHomePage(response, responseContent, true);
//Check if the user name appears in the page
Assert.Contains(string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName), responseContent, StringComparison.OrdinalIgnoreCase);
//Should be able to access the store as the Startup adds necessary permissions for the current user
AccessStoreWithPermissions();
var testCompletionTime = DateTime.Now;
_logger.WriteInformation("[Time]: All tests completed in '{0}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds.ToString());
_logger.WriteInformation("[Time]: Total time taken for this test variation '{0}' seconds", (testCompletionTime - testStartTime).TotalSeconds.ToString());
testSuccessful = true;
}
VerifyHomePage(response, responseContent, true);
//Check if the user name appears in the page
Assert.Contains(string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName), responseContent, StringComparison.OrdinalIgnoreCase);
//Should be able to access the store as the Startup adds necessary permissions for the current user
AccessStoreWithPermissions();
var testCompletionTime = DateTime.Now;
Console.WriteLine("[Time]: All tests completed in '{0}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds);
Console.WriteLine("[Time]: Total time taken for this test variation '{0}' seconds", (testCompletionTime - testStartTime).TotalSeconds);
testSuccessful = true;
}
finally
{
if (!testSuccessful)
finally
{
Console.WriteLine("Some tests failed. Proceeding with cleanup.");
}
if (!testSuccessful)
{
_logger.WriteError("Some tests failed. Proceeding with cleanup.");
}
DeploymentUtility.CleanUpApplication(_startParameters, hostProcess, musicStoreDbName);
DeploymentUtility.CleanUpApplication(_startParameters, hostProcess, musicStoreDbName, _logger);
}
}
}
}

View File

@ -5,6 +5,7 @@ using System.Net;
using System.Net.Http;
using System.Threading;
using Microsoft.AspNet.Testing.xunit;
using Microsoft.Framework.Logging;
using Xunit;
namespace E2ETests
@ -39,89 +40,94 @@ namespace E2ETests
private void Publish_And_Run_Tests(ServerType serverType, KreFlavor kreFlavor, KreArchitecture architecture, string applicationBaseUrl)
{
Console.WriteLine("Variation Details : HostType = {0}, KreFlavor = {1}, Architecture = {2}, applicationBaseUrl = {3}", serverType, kreFlavor, architecture, applicationBaseUrl);
_startParameters = new StartParameters
using (_logger.BeginScope("Publish_And_Run_Tests"))
{
ServerType = serverType,
KreFlavor = kreFlavor,
KreArchitecture = architecture,
PackApplicationBeforeStart = true
};
_logger.WriteInformation("Variation Details : HostType = {0}, KreFlavor = {1}, Architecture = {2}, applicationBaseUrl = {3}",
serverType.ToString(), kreFlavor.ToString(), architecture.ToString(), applicationBaseUrl);
var testStartTime = DateTime.Now;
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
Console.WriteLine("Pointing MusicStore DB to '{0}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
//Override the connection strings using environment based configuration
Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
_applicationBaseUrl = applicationBaseUrl;
Process hostProcess = null;
bool testSuccessful = false;
try
{
hostProcess = DeploymentUtility.StartApplication(_startParameters, musicStoreDbName);
_httpClientHandler = new HttpClientHandler() { UseDefaultCredentials = true };
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) };
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
//Add retry logic since tests are flaky on mono due to connection issues
for (int retryCount = 0; retryCount < 3; retryCount++)
_startParameters = new StartParameters
{
try
ServerType = serverType,
KreFlavor = kreFlavor,
KreArchitecture = architecture,
PackApplicationBeforeStart = true
};
var testStartTime = DateTime.Now;
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
_logger.WriteInformation("Pointing MusicStore DB to '{0}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
//Override the connection strings using environment based configuration
Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
_applicationBaseUrl = applicationBaseUrl;
Process hostProcess = null;
bool testSuccessful = false;
try
{
hostProcess = DeploymentUtility.StartApplication(_startParameters, musicStoreDbName, _logger);
_httpClientHandler = new HttpClientHandler() { UseDefaultCredentials = true };
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) };
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
//Add retry logic since tests are flaky on mono due to connection issues
for (int retryCount = 0; retryCount < 3; retryCount++)
{
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
Console.WriteLine("[Time]: Approximate time taken for application initialization : '{0}' seconds", (initializationCompleteTime - testStartTime).TotalSeconds);
break; //Went through successfully
}
catch (AggregateException exception)
{
// Both type exceptions thrown by Mono which are resolved by retry logic
if (exception.InnerException is HttpRequestException || exception.InnerException is WebException)
try
{
Console.WriteLine("Failed to complete the request with error: {0}", exception.ToString());
Console.WriteLine("Retrying request..");
Thread.Sleep(1 * 1000); //Wait for a second before retry
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
(initializationCompleteTime - testStartTime).TotalSeconds.ToString());
break; //Went through successfully
}
catch (AggregateException exception)
{
// Both type exceptions thrown by Mono which are resolved by retry logic
if (exception.InnerException is HttpRequestException || exception.InnerException is WebException)
{
_logger.WriteWarning("Failed to complete the request.", exception);
_logger.WriteWarning("Retrying request..");
Thread.Sleep(1 * 1000); //Wait for a second before retry
}
}
}
}
VerifyHomePage(response, responseContent, true);
VerifyHomePage(response, responseContent, true);
//Static files are served?
VerifyStaticContentServed();
//Static files are served?
VerifyStaticContentServed();
if (serverType != ServerType.IISExpress)
{
if (Directory.GetFiles(_startParameters.ApplicationPath, "*.cmd", SearchOption.TopDirectoryOnly).Length > 0)
if (serverType != ServerType.IISExpress)
{
throw new Exception("packExclude parameter values are not honored");
if (Directory.GetFiles(_startParameters.ApplicationPath, "*.cmd", SearchOption.TopDirectoryOnly).Length > 0)
{
throw new Exception("packExclude parameter values are not honored.");
}
}
}
var testCompletionTime = DateTime.Now;
Console.WriteLine("[Time]: All tests completed in '{0}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds);
Console.WriteLine("[Time]: Total time taken for this test variation '{0}' seconds", (testCompletionTime - testStartTime).TotalSeconds);
testSuccessful = true;
}
finally
{
if (!testSuccessful)
var testCompletionTime = DateTime.Now;
_logger.WriteInformation("[Time]: All tests completed in '{0}' seconds.", (testCompletionTime - initializationCompleteTime).TotalSeconds.ToString());
_logger.WriteInformation("[Time]: Total time taken for this test variation '{0}' seconds.", (testCompletionTime - testStartTime).TotalSeconds.ToString());
testSuccessful = true;
}
finally
{
Console.WriteLine("Some tests failed. Proceeding with cleanup.");
}
if (!testSuccessful)
{
_logger.WriteError("Some tests failed. Proceeding with cleanup.");
}
DeploymentUtility.CleanUpApplication(_startParameters, hostProcess, musicStoreDbName);
DeploymentUtility.CleanUpApplication(_startParameters, hostProcess, musicStoreDbName, _logger);
}
}
}
}

View File

@ -4,6 +4,8 @@ using System.Net;
using System.Net.Http;
using System.Threading;
using Microsoft.AspNet.Testing.xunit;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Console;
using Xunit;
namespace E2ETests
@ -16,6 +18,14 @@ namespace E2ETests
private HttpClient _httpClient;
private HttpClientHandler _httpClientHandler;
private StartParameters _startParameters;
private readonly ILogger _logger;
public SmokeTests()
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole();
_logger = loggerFactory.Create<SmokeTests>();
}
[ConditionalTheory]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
@ -81,159 +91,164 @@ namespace E2ETests
private void SmokeTestSuite(ServerType serverType, KreFlavor kreFlavor, KreArchitecture architecture, string applicationBaseUrl)
{
Console.WriteLine("Variation Details : HostType = {0}, KreFlavor = {1}, Architecture = {2}, applicationBaseUrl = {3}", serverType, kreFlavor, architecture, applicationBaseUrl);
_startParameters = new StartParameters
using (_logger.BeginScope("SmokeTestSuite"))
{
ServerType = serverType,
KreFlavor = kreFlavor,
KreArchitecture = architecture,
EnvironmentName = "SocialTesting"
};
_logger.WriteInformation("Variation Details : HostType = {0}, KreFlavor = {1}, Architecture = {2}, applicationBaseUrl = {3}",
serverType.ToString(), kreFlavor.ToString(), architecture.ToString(), applicationBaseUrl);
var testStartTime = DateTime.Now;
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
Console.WriteLine("Pointing MusicStore DB to '{0}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
//Override the connection strings using environment based configuration
Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
_applicationBaseUrl = applicationBaseUrl;
Process hostProcess = null;
bool testSuccessful = false;
try
{
hostProcess = DeploymentUtility.StartApplication(_startParameters, musicStoreDbName);
if (serverType == ServerType.IISNativeModule || serverType == ServerType.IIS)
_startParameters = new StartParameters
{
// Accomodate the vdir name.
_applicationBaseUrl += _startParameters.IISApplication.VirtualDirectoryName + "/";
}
ServerType = serverType,
KreFlavor = kreFlavor,
KreArchitecture = architecture,
EnvironmentName = "SocialTesting"
};
_httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_applicationBaseUrl) };
var testStartTime = DateTime.Now;
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
_logger.WriteInformation("Pointing MusicStore DB to '{0}'", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
for (int retryCount = 0; retryCount < 3; retryCount++)
//Override the connection strings using environment based configuration
Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(CONNECTION_STRING_FORMAT, musicStoreDbName));
_applicationBaseUrl = applicationBaseUrl;
Process hostProcess = null;
bool testSuccessful = false;
try
{
try
hostProcess = DeploymentUtility.StartApplication(_startParameters, musicStoreDbName, _logger);
if (serverType == ServerType.IISNativeModule || serverType == ServerType.IIS)
{
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
Console.WriteLine("[Time]: Approximate time taken for application initialization : '{0}' seconds", (initializationCompleteTime - testStartTime).TotalSeconds);
break; //Went through successfully
// Accomodate the vdir name.
_applicationBaseUrl += _startParameters.IISApplication.VirtualDirectoryName + "/";
}
catch (AggregateException exception)
_httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_applicationBaseUrl) };
HttpResponseMessage response = null;
string responseContent = null;
var initializationCompleteTime = DateTime.MinValue;
//Request to base address and check if various parts of the body are rendered & measure the cold startup time.
for (int retryCount = 0; retryCount < 3; retryCount++)
{
if (exception.InnerException is HttpRequestException || exception.InnerException is WebException)
try
{
Console.WriteLine("Failed to complete the request with error: {0}", exception.ToString());
Console.WriteLine("Retrying request..");
Thread.Sleep(1 * 1000); //Wait for a second before retry
response = _httpClient.GetAsync(string.Empty).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
initializationCompleteTime = DateTime.Now;
_logger.WriteInformation("[Time]: Approximate time taken for application initialization : '{0}' seconds",
(initializationCompleteTime - testStartTime).TotalSeconds.ToString());
break; //Went through successfully
}
catch (AggregateException exception)
{
if (exception.InnerException is HttpRequestException || exception.InnerException is WebException)
{
_logger.WriteWarning("Failed to complete the request.", exception);
_logger.WriteWarning("Retrying request..");
Thread.Sleep(1 * 1000); //Wait for a second before retry
}
}
}
VerifyHomePage(response, responseContent);
//Verify the static file middleware can serve static content
VerifyStaticContentServed();
//Making a request to a protected resource should automatically redirect to login page
AccessStoreWithoutPermissions();
//Register a user - Negative scenario where the Password & ConfirmPassword do not match
RegisterUserWithNonMatchingPasswords();
//Register a valid user
var generatedEmail = RegisterValidUser();
SignInWithUser(generatedEmail, "Password~1");
//Register a user - Negative scenario : Trying to register a user name that's already registered.
RegisterExistingUser(generatedEmail);
//Logout from this user session - This should take back to the home page
SignOutUser(generatedEmail);
//Sign in scenarios: Invalid password - Expected an invalid user name password error.
SignInWithInvalidPassword(generatedEmail, "InvalidPassword~1");
//Sign in scenarios: Valid user name & password.
SignInWithUser(generatedEmail, "Password~1");
//Change password scenario
ChangePassword(generatedEmail);
//SignIn with old password and verify old password is not allowed and new password is allowed
SignOutUser(generatedEmail);
SignInWithInvalidPassword(generatedEmail, "Password~1");
SignInWithUser(generatedEmail, "Password~2");
//Making a request to a protected resource that this user does not have access to - should automatically redirect to login page again
AccessStoreWithoutPermissions(generatedEmail);
//Logout from this user session - This should take back to the home page
SignOutUser(generatedEmail);
//Login as an admin user
SignInWithUser("Administrator@test.com", "YouShouldChangeThisPassword1!");
//Now navigating to the store manager should work fine as this user has the necessary permission to administer the store.
AccessStoreWithPermissions();
//Create an album
var albumName = CreateAlbum();
var albumId = FetchAlbumIdFromName(albumName);
//Get details of the album
VerifyAlbumDetails(albumId, albumName);
//Get the non-admin view of the album.
GetAlbumDetailsFromStore(albumId, albumName);
//Add an album to cart and checkout the same
AddAlbumToCart(albumId, albumName);
CheckOutCartItems();
//Delete the album from store
DeleteAlbum(albumId, albumName);
//Logout from this user session - This should take back to the home page
SignOutUser("Administrator");
//Google login
LoginWithGoogle();
//Facebook login
LoginWithFacebook();
//Twitter login
LoginWithTwitter();
//MicrosoftAccountLogin
LoginWithMicrosoftAccount();
var testCompletionTime = DateTime.Now;
_logger.WriteInformation("[Time]: All tests completed in '{0}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds.ToString());
_logger.WriteInformation("[Time]: Total time taken for this test variation '{0}' seconds", (testCompletionTime - testStartTime).TotalSeconds.ToString());
testSuccessful = true;
}
VerifyHomePage(response, responseContent);
//Verify the static file middleware can serve static content
VerifyStaticContentServed();
//Making a request to a protected resource should automatically redirect to login page
AccessStoreWithoutPermissions();
//Register a user - Negative scenario where the Password & ConfirmPassword do not match
RegisterUserWithNonMatchingPasswords();
//Register a valid user
var generatedEmail = RegisterValidUser();
SignInWithUser(generatedEmail, "Password~1");
//Register a user - Negative scenario : Trying to register a user name that's already registered.
RegisterExistingUser(generatedEmail);
//Logout from this user session - This should take back to the home page
SignOutUser(generatedEmail);
//Sign in scenarios: Invalid password - Expected an invalid user name password error.
SignInWithInvalidPassword(generatedEmail, "InvalidPassword~1");
//Sign in scenarios: Valid user name & password.
SignInWithUser(generatedEmail, "Password~1");
//Change password scenario
ChangePassword(generatedEmail);
//SignIn with old password and verify old password is not allowed and new password is allowed
SignOutUser(generatedEmail);
SignInWithInvalidPassword(generatedEmail, "Password~1");
SignInWithUser(generatedEmail, "Password~2");
//Making a request to a protected resource that this user does not have access to - should automatically redirect to login page again
AccessStoreWithoutPermissions(generatedEmail);
//Logout from this user session - This should take back to the home page
SignOutUser(generatedEmail);
//Login as an admin user
SignInWithUser("Administrator@test.com", "YouShouldChangeThisPassword1!");
//Now navigating to the store manager should work fine as this user has the necessary permission to administer the store.
AccessStoreWithPermissions();
//Create an album
var albumName = CreateAlbum();
var albumId = FetchAlbumIdFromName(albumName);
//Get details of the album
VerifyAlbumDetails(albumId, albumName);
//Get the non-admin view of the album.
GetAlbumDetailsFromStore(albumId, albumName);
//Add an album to cart and checkout the same
AddAlbumToCart(albumId, albumName);
CheckOutCartItems();
//Delete the album from store
DeleteAlbum(albumId, albumName);
//Logout from this user session - This should take back to the home page
SignOutUser("Administrator");
//Google login
LoginWithGoogle();
//Facebook login
LoginWithFacebook();
//Twitter login
LoginWithTwitter();
//MicrosoftAccountLogin
LoginWithMicrosoftAccount();
var testCompletionTime = DateTime.Now;
Console.WriteLine("[Time]: All tests completed in '{0}' seconds", (testCompletionTime - initializationCompleteTime).TotalSeconds);
Console.WriteLine("[Time]: Total time taken for this test variation '{0}' seconds", (testCompletionTime - testStartTime).TotalSeconds);
testSuccessful = true;
}
finally
{
if (!testSuccessful)
finally
{
Console.WriteLine("Some tests failed. Proceeding with cleanup.");
}
if (!testSuccessful)
{
_logger.WriteError("Some tests failed. Proceeding with cleanup.");
}
DeploymentUtility.CleanUpApplication(_startParameters, hostProcess, musicStoreDbName);
DeploymentUtility.CleanUpApplication(_startParameters, hostProcess, musicStoreDbName, _logger);
}
}
}
}

View File

@ -8,6 +8,7 @@
"Microsoft.AspNet.SignalR.Client": "2.1.1",
"Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.AspNet.WebUtilities": "1.0.0-*",
"Microsoft.Framework.Logging.Console": "1.0.0-*",
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*",
"Microsoft.Web.Administration": "7.0.0",
"xunit.runner.kre": "1.0.0-*"