private void LogIfNotNull(string message) { if (this.TracingInterceptor != null) { TracingInterceptor.Information($"[EnvironmentSetupHelper]: {message}"); } }
public virtual Collection <PSObject> RunPowerShellTest(params string[] scripts) { // If a test customizes the reecord matcher, use the cutomized version otherwise use the default // permissive record matcher. if (HttpMockServer.Matcher == null || HttpMockServer.Matcher.GetType() == typeof(SimpleRecordMatcher)) { Dictionary <string, string> d = new Dictionary <string, string>(); d.Add("Microsoft.Resources", null); d.Add("Microsoft.Features", null); d.Add("Microsoft.Authorization", null); d.Add("Microsoft.Compute", null); d.Add("Microsoft.KeyVault", null); var providersToIgnore = new Dictionary <string, string>(); providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"); HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore); } using (var powershell = System.Management.Automation.PowerShell.Create(RunspaceMode.NewRunspace)) { SetupPowerShellModules(powershell); Collection <PSObject> output = null; for (int i = 0; i < scripts.Length; ++i) { if (TracingInterceptor != null) { TracingInterceptor.Information(scripts[i]); } powershell.AddScript(scripts[i]); } try { output = powershell.Invoke(); if (powershell.Streams.Error.Count > 0) { throw new RuntimeException( string.Format( "Test failed due to a non-empty error stream. First error: {0}{1}", PowerShellExtensions.FormatErrorRecord(powershell.Streams.Error[0]), powershell.Streams.Error.Count > 0 ? "Check the error stream in the test log for additional errors." : "")); } return(output); } catch (Exception psException) { powershell.LogPowerShellException(psException, TracingInterceptor); powershell.LogPowerShellResults(output, TracingInterceptor); TracingInterceptor?.Flush(); throw; } finally { powershell.Streams.Error.Clear(); } } }
private Collection <PSObject> ExecuteShellTest( System.Management.Automation.PowerShell powershell, IEnumerable <string> setupScripts, IEnumerable <string> scripts) { SetupPowerShellModules(powershell, setupScripts); Collection <PSObject> output = null; foreach (var script in scripts) { if (TracingInterceptor != null) { TracingInterceptor.Information(script); } powershell.AddScript(script); } try { powershell.Runspace.Events.Subscribers.Clear(); powershell.Streams.Error.Clear(); output = powershell.Invoke(); if (powershell.Streams.Error.Count > 0) { var sb = new StringBuilder(); sb.AppendLine("Test failed due to a non-empty error stream, check the error stream in the test log for more details."); sb.AppendLine(string.Format("{0} total Errors", powershell.Streams.Error.Count)); foreach (var error in powershell.Streams.Error) { sb.AppendLine(error.Exception.ToString()); } throw new RuntimeException(sb.ToString()); } return(output); } catch (Exception psException) { powershell.LogPowerShellException(psException, TracingInterceptor); throw; } finally { powershell.LogPowerShellResults(output, TracingInterceptor); powershell.Streams.Error.Clear(); } }
public virtual Collection <PSObject> RunPowerShellTest(params string[] scripts) { Dictionary <string, string> d = new Dictionary <string, string>(); d.Add("Microsoft.Resources", null); d.Add("Microsoft.Features", null); d.Add("Microsoft.Authorization", null); d.Add("Microsoft.Compute", null); var providersToIgnore = new Dictionary <string, string>(); providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"); HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore); using (var powershell = System.Management.Automation.PowerShell.Create(RunspaceMode.NewRunspace)) { SetupPowerShellModules(powershell); Collection <PSObject> output = null; for (int i = 0; i < scripts.Length; ++i) { if (TracingInterceptor != null) { TracingInterceptor.Information(scripts[i]); } powershell.AddScript(scripts[i]); } try { powershell.Runspace.Events.Subscribers.Clear(); output = powershell.Invoke(); if (powershell.Streams.Error.Count > 0) { throw new RuntimeException( "Test failed due to a non-empty error stream, check the error stream in the test log for more details."); } return(output); } catch (Exception psException) { powershell.LogPowerShellException(psException, TracingInterceptor); throw; } finally { powershell.LogPowerShellResults(output, TracingInterceptor); powershell.Streams.Error.Clear(); } } }