Skip to content

[#115534183]: Scenario Tests in VS does not show any logs in Test/Build/Output window #2056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
<HintPath>..\..\packages\Microsoft.Azure.Management.Resources.2.20.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Test.Framework">
<HintPath>..\..\packages\Microsoft.Azure.Test.Framework.1.0.5896.19355-prerelease\lib\net45\Microsoft.Azure.Test.Framework.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Test.Framework.1.0.5945.28173-prerelease\lib\net45\Microsoft.Azure.Test.Framework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Test.HttpRecorder">
<HintPath>..\..\packages\Microsoft.Azure.Test.HttpRecorder.1.0.5896.19355-prerelease\lib\net45\Microsoft.Azure.Test.HttpRecorder.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Test.HttpRecorder.1.0.5945.28173-prerelease\lib\net45\Microsoft.Azure.Test.HttpRecorder.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -149,6 +149,7 @@
<Compile Include="PowerShellExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SMTestBase.cs" />
<Compile Include="XunitTracingInterceptor.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Assert.ps1">
Expand Down
14 changes: 10 additions & 4 deletions src/Common/Commands.ScenarioTests.Common/EnvironmentSetupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.ServiceManagemenet.Common;
using System.Text;
using Microsoft.WindowsAzure.ServiceManagemenet.Common.Models;

namespace Microsoft.WindowsAzure.Commands.ScenarioTest
{
Expand All @@ -46,6 +47,8 @@ public class EnvironmentSetupHelper

protected List<string> modules;

public XunitTracingInterceptor TracingInterceptor { get; set; }

protected ProfileClient ProfileClient { get; set; }

public EnvironmentSetupHelper()
Expand Down Expand Up @@ -299,7 +302,10 @@ private Collection<PSObject> ExecuteShellTest(

foreach (var script in scripts)
{
Console.WriteLine(script);
if (TracingInterceptor != null)
{
TracingInterceptor.Information(script);
}
powershell.AddScript(script);
}
try
Expand All @@ -326,12 +332,12 @@ private Collection<PSObject> ExecuteShellTest(
}
catch (Exception psException)
{
powershell.LogPowerShellException(psException);
powershell.LogPowerShellException(psException, TracingInterceptor);
throw;
}
finally
{
powershell.LogPowerShellResults(output);
powershell.LogPowerShellResults(output, TracingInterceptor);
powershell.Streams.Error.Clear();
}
}
Expand All @@ -354,7 +360,7 @@ private void SetupPowerShellModules(System.Management.Automation.PowerShell powe
}

powershell.AddScript(
string.Format(@"set-location {0}", AppDomain.CurrentDomain.BaseDirectory));
string.Format("set-location \"{0}\"", AppDomain.CurrentDomain.BaseDirectory));
powershell.AddScript(string.Format(@"$TestOutputRoot='{0}'", AppDomain.CurrentDomain.BaseDirectory));
powershell.AddScript("$VerbosePreference='Continue'");
powershell.AddScript("$DebugPreference='Continue'");
Expand Down
82 changes: 55 additions & 27 deletions src/Common/Commands.ScenarioTests.Common/PowerShellExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.ServiceManagemenet.Common.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -95,59 +96,83 @@ public static void SetVariable(this System.Management.Automation.PowerShell powe
/// PowerShell error record if available
/// </summary>
/// <param name="runtimeException">The exception to parse</param>
public static void LogPowerShellException(this System.Management.Automation.PowerShell powershell, Exception runtimeException)
public static void LogPowerShellException(
this System.Management.Automation.PowerShell powershell,
Exception runtimeException,
XunitTracingInterceptor xunitLogger)
{
Console.WriteLine("Caught Exception: {0}\n", runtimeException);
Console.WriteLine("Message: {0}\n", runtimeException.Message);
if (xunitLogger != null)
{
xunitLogger.Information(string.Format("Caught Exception: {0}", runtimeException));
xunitLogger.Information(string.Format("Message: {0}", runtimeException.Message));
}

IContainsErrorRecord recordContainer = runtimeException as IContainsErrorRecord;
if (recordContainer != null)
{
ErrorRecord record = recordContainer.ErrorRecord;
Console.WriteLine("PowerShell Error Record: {0}\nException:{1}\nDetails:{2}\nScript Stack Trace: {3}\n: Target: {4}\n", record, record.Exception, record.ErrorDetails, record.ScriptStackTrace, record.TargetObject);

if (xunitLogger != null)
{
xunitLogger.Information(string.Format(
"PowerShell Error Record: {0}\nException:{1}\nDetails:{2}\nScript Stack Trace: {3}\n: Target: {4}\n",
record,
record.Exception,
record.ErrorDetails,
record.ScriptStackTrace,
record.TargetObject));
}
}

if (runtimeException.InnerException != null)
{
powershell.LogPowerShellException(runtimeException.InnerException);
powershell.LogPowerShellException(runtimeException.InnerException, xunitLogger);
}
}

/// <summary>
/// Log the PowerShell Streams from a PowerShell invocation
/// </summary>
/// <param name="powershell">The PowerShell instance to log</param>
public static void LogPowerShellResults(this System.Management.Automation.PowerShell powershell)
public static void LogPowerShellResults(
this System.Management.Automation.PowerShell powershell,
XunitTracingInterceptor xunitLogger)
{
powershell.LogPowerShellResults(null);
powershell.LogPowerShellResults(null, xunitLogger);
}

/// <summary>
/// Log the PowerShell Streams from a PowerShell invocation
/// </summary>
/// <param name="powershell">The PowerShell instance to log</param>
public static void LogPowerShellResults(this System.Management.Automation.PowerShell powershell, Collection<PSObject> output)
public static void LogPowerShellResults(
this System.Management.Automation.PowerShell powershell,
Collection<PSObject> output,
XunitTracingInterceptor xunitLogger)
{
if (output != null)
{
LogPowerShellStream<PSObject>(output, "OUTPUT");
LogPowerShellStream<PSObject>(xunitLogger, output, "OUTPUT");
}
if (powershell.Commands != null && powershell.Commands.Commands != null &&
if (xunitLogger != null &&
powershell.Commands != null &&
powershell.Commands.Commands != null &&
powershell.Commands.Commands.Count > 0)
{
Console.WriteLine("================== COMMANDS =======================\n");
xunitLogger.Information("================== COMMANDS =======================\n");
foreach (Command command in powershell.Commands.Commands)
{
Console.WriteLine("{0}\n", command.CommandText);
xunitLogger.Information(string.Format("{0}\n", command.CommandText));
}

Console.WriteLine("===================================================\n");
xunitLogger.Information("===================================================\n");
}

LogPowerShellStream<DebugRecord>(powershell.Streams.Debug, "DEBUG");
LogPowerShellStream<ErrorRecord>(powershell.Streams.Error, "ERROR");
LogPowerShellStream<ProgressRecord>(powershell.Streams.Progress, "PROGRESS");
LogPowerShellStream<VerboseRecord>(powershell.Streams.Verbose, "VERBOSE");
LogPowerShellStream<WarningRecord>(powershell.Streams.Warning, "WARNING");
LogPowerShellStream<DebugRecord>(xunitLogger, powershell.Streams.Debug, "DEBUG");
LogPowerShellStream<ErrorRecord>(xunitLogger, powershell.Streams.Error, "ERROR");
LogPowerShellStream<ProgressRecord>(xunitLogger, powershell.Streams.Progress, "PROGRESS");
LogPowerShellStream<VerboseRecord>(xunitLogger, powershell.Streams.Verbose, "VERBOSE");
LogPowerShellStream<WarningRecord>(xunitLogger, powershell.Streams.Warning, "WARNING");
}

/// <summary>
Expand Down Expand Up @@ -196,23 +221,26 @@ public static void RemoveCredentials(this System.Management.Automation.PowerShel
/// <typeparam name="T">The type of the internal data record (different for every stream)</typeparam>
/// <param name="stream">The stream to log</param>
/// <param name="name">The name of the stream to print in the log</param>
private static void LogPowerShellStream<T>(ICollection<T> stream, string name)
private static void LogPowerShellStream<T>(
XunitTracingInterceptor xunitLogger,
ICollection<T> stream,
string name)
{
if (stream != null && stream.Count > 0)
if (xunitLogger !=null && stream != null && stream.Count > 0)
{

Console.WriteLine("---------------------------------------------------------------\n");
Console.WriteLine("{0} STREAM\n", name);
Console.WriteLine("---------------------------------------------------------------\n");
xunitLogger.Information("---------------------------------------------------------------\n");
xunitLogger.Information(string.Format("{0} STREAM\n", name));
xunitLogger.Information("---------------------------------------------------------------\n");
foreach (T item in stream)
{
if(item != null)
{
Console.WriteLine("{0}\n", item.ToString());
xunitLogger.Information(string.Format("{0}\n", item.ToString()));
}
}
Console.WriteLine("---------------------------------------------------------------\n");
Console.WriteLine("");

xunitLogger.Information("---------------------------------------------------------------\n");
xunitLogger.Information("");
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/Common/Commands.ScenarioTests.Common/SMTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ public void Log(string format, params object[] args)
{
TestContext.WriteLine(format, args);
}
else
{
Console.WriteLine(format, args);
}
}
}
}
106 changes: 106 additions & 0 deletions src/Common/Commands.ScenarioTests.Common/XunitTracingInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Http;
using Hyak.Common;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.WindowsAzure.Commands.Common;
using Xunit.Abstractions;
using System.IO;
using System.Reflection;

namespace Microsoft.WindowsAzure.ServiceManagemenet.Common.Models
{
public class XunitTracingInterceptor : Hyak.Common.ICloudTracingInterceptor
{
private readonly string callingAssembly;

public XunitTracingInterceptor(ITestOutputHelper output)
{
traceOutput = output;
callingAssembly = Assembly.GetCallingAssembly().FullName.Split(new[] { ',' })[0];
}

public ITestOutputHelper traceOutput;

private void Write(string message, params object[] arguments)
{
try
{
traceOutput.WriteLine(string.Format(message, arguments));
using (StreamWriter file = new StreamWriter(string.Format("{0}.test.log", callingAssembly).AsAbsoluteLocation(), true))
{
file.WriteLine(string.Format(message, arguments));
}
}
catch {}
}

public void Information(string message)
{
Write(message);
}

public void Configuration(string source, string name, string value)
{
// Ignore
}

public void Enter(string invocationId, object instance, string method, IDictionary<string, object> parameters)
{
// Ignore
}

public void SendRequest(string invocationId, HttpRequestMessage request)
{
Write(GeneralUtilities.GetLog(request));
}

public void ReceiveResponse(string invocationId, HttpResponseMessage response)
{
Write(GeneralUtilities.GetLog(response));
}

public void Error(string invocationId, Exception ex)
{
// Ignore
}

public void Exit(string invocationId, object result)
{
// Ignore
}

public static void AddToContext(XunitTracingInterceptor interceptor)
{
RemoveFromContext(interceptor);
TracingAdapter.AddTracingInterceptor(interceptor);
}

public static void RemoveFromContext(XunitTracingInterceptor interceptor)
{
try
{
TracingAdapter.RemoveTracingInterceptor(interceptor);
}
catch
{
// Ignore
}
}
}
}
4 changes: 2 additions & 2 deletions src/Common/Commands.ScenarioTests.Common/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.11-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Test.Framework" version="1.0.5896.19355-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5896.19355-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.Framework" version="1.0.5945.28173-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5945.28173-prerelease" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@
</Reference>
<Reference Include="Microsoft.Azure.Test.Framework, Version=1.0.5885.21631, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Test.Framework.1.0.5896.19355-prerelease\lib\net45\Microsoft.Azure.Test.Framework.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Test.Framework.1.0.5945.28173-prerelease\lib\net45\Microsoft.Azure.Test.Framework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Test.HttpRecorder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Test.HttpRecorder.1.0.5896.19355-prerelease\lib\net45\Microsoft.Azure.Test.HttpRecorder.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Test.HttpRecorder.1.0.5945.28173-prerelease\lib\net45\Microsoft.Azure.Test.HttpRecorder.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.14.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public class ApiManagementTests : RMTestBase
{
private readonly EnvironmentSetupHelper _helper;

public ApiManagementTests()
public ApiManagementTests(Xunit.Abstractions.ITestOutputHelper output)
{
ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
_helper = new EnvironmentSetupHelper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace Microsoft.Azure.Commands.ApiManagement.Test.UnitTests
{
public class PsApiManagementTests
{
public PsApiManagementTests(Xunit.Abstractions.ITestOutputHelper output)
{
ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestCtor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<package id="Microsoft.Azure.Management.Authorization" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.20.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Storage" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Test.Framework" version="1.0.5896.19355-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5896.19355-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.Framework" version="1.0.5945.28173-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5945.28173-prerelease" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
Expand Down
Loading