public void HandleLogMessage(TestMessageLevel level, string message)
        {
            switch ((TestMessageLevel)level)
            {
            case TestMessageLevel.Informational:
                EqtTrace.Info(message);
                break;

            case TestMessageLevel.Warning:
                EqtTrace.Warning(message);
                break;

            case TestMessageLevel.Error:
                this.errors.Add(message);
                EqtTrace.Error(message);
                break;

            default:
                EqtTrace.Info(message);
                break;
            }
        }
Exemple #2
0
        /// <summary>
        /// Helper to get the Uri from the ExtensionUriAttribute on logger plugin.
        /// </summary>
        /// <param name="testLoggerType">Data type of the test logger</param>
        /// <returns>Uri identifying the test logger</returns>
        private static string GetExtensionUri(Type testLoggerType)
        {
            string extensionUri = string.Empty;

            object[] attributes = testLoggerType.GetTypeInfo().GetCustomAttributes(typeof(ExtensionUriAttribute), false).ToArray();
            if (attributes != null && attributes.Length > 0)
            {
                ExtensionUriAttribute extensionUriAttribute = (ExtensionUriAttribute)attributes[0];

                if (!string.IsNullOrEmpty(extensionUriAttribute.ExtensionUri))
                {
                    extensionUri = extensionUriAttribute.ExtensionUri;
                }
            }

            if (EqtTrace.IsErrorEnabled && string.IsNullOrEmpty(extensionUri))
            {
                EqtTrace.Error("The type \"{0}\" defined in \"{1}\" does not have ExtensionUri attribute.", testLoggerType.ToString(), testLoggerType.Module.Name);
            }

            return(extensionUri);
        }
Exemple #3
0
        /// <summary>
        /// Gets the test results directory.
        /// </summary>
        /// <param name="runSettings">Test run settings.</param>
        /// <returns>Test results directory</returns>
        internal string GetResultsDirectory(string runSettings)
        {
            string resultsDirectory = null;

            if (runSettings != null)
            {
                try
                {
                    RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings);
                    resultsDirectory = RunSettingsUtilities.GetTestResultsDirectory(runConfiguration);
                }
                catch (SettingsException se)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error("TestLoggerManager.GetResultsDirectory: Unable to get the test results directory: Error {0}", se);
                    }
                }
            }

            return(resultsDirectory);
        }
        /// <inheritdoc/>
        public void AttachToTargetProcess(int processId, string outputFile, DumpTypeOption dumpType)
        {
            EqtTrace.Info($"ProcDumpCrashDumper.AttachToTargetProcess: Attaching to process '{processId}' to dump into '{outputFile}'.");

            // Procdump will append .dmp at the end of the dump file. We generate this internally so it is rather a safety check.
            if (!outputFile.EndsWith(".dmp", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("Procdump crash dump file must end with .dmp extension.");
            }

            if (!this.TryGetProcDumpExecutable(processId, out var procDumpPath))
            {
                var err = $"{procDumpPath} could not be found, please set PROCDUMP_PATH environment variable to a directory that contains {procDumpPath} executable, or make sure that the executable is available on PATH.";
                ConsoleOutput.Instance.Warning(false, err);
                EqtTrace.Error($"ProcDumpCrashDumper.AttachToTargetProcess: {err}");
                return;
            }

            this.tempDirectory = Path.GetDirectoryName(outputFile);
            this.dumpFileName  = Path.GetFileNameWithoutExtension(outputFile);

            string procDumpArgs = new ProcDumpArgsBuilder().BuildTriggerBasedProcDumpArgs(
                processId,
                this.dumpFileName,
                ProcDumpExceptionsList,
                isFullDump: dumpType == DumpTypeOption.Full);

            EqtTrace.Info($"ProcDumpCrashDumper.AttachToTargetProcess: Running ProcDump with arguments: '{procDumpArgs}'.");
            this.procDumpProcess = this.processHelper.LaunchProcess(
                procDumpPath,
                procDumpArgs,
                this.tempDirectory,
                null,
                null,
                null,
                this.OutputReceivedCallback) as Process;

            EqtTrace.Info($"ProcDumpCrashDumper.AttachToTargetProcess: ProcDump started as process with id '{this.procDumpProcess.Id}'.");
        }
Exemple #5
0
        /// <summary>
        /// Verifies Parallel Setting and returns parallel level to use based on the run criteria
        /// </summary>
        /// <param name="sourceCount">
        /// The source Count.
        /// </param>
        /// <param name="runSettings">
        /// The run Settings.
        /// </param>
        /// <returns>
        /// Parallel Level to use
        /// </returns>
        private int VerifyParallelSettingAndCalculateParallelLevel(int sourceCount, string runSettings)
        {
            // Default is 1
            int parallelLevelToUse = 1;

            try
            {
                // Check the User Parallel Setting
                int userParallelSetting = RunSettingsUtilities.GetMaxCpuCount(runSettings);
                parallelLevelToUse = userParallelSetting == 0 ? Environment.ProcessorCount : userParallelSetting;
                var enableParallel = parallelLevelToUse > 1;

                EqtTrace.Verbose("TestEngine: Initializing Parallel Execution as MaxCpuCount is set to: {0}", parallelLevelToUse);

                // Verify if the number of Sources is less than user setting of parallel
                // we should use number of sources as the parallel level, if sources count is less than parallel level
                if (enableParallel)
                {
                    parallelLevelToUse = Math.Min(sourceCount, parallelLevelToUse);

                    // If only one source, no need to use parallel service client
                    enableParallel = parallelLevelToUse > 1;

                    if (EqtTrace.IsInfoEnabled)
                    {
                        EqtTrace.Verbose("TestEngine: ParallelExecution set to '{0}' as the parallel level is adjusted to '{1}' based on number of sources", enableParallel, parallelLevelToUse);
                    }
                }
            }
            catch (Exception ex)
            {
                EqtTrace.Error("TestEngine: Error occured while initializing ParallelExecution: {0}", ex);
                EqtTrace.Warning("TestEngine: Defaulting to Sequential Execution");

                parallelLevelToUse = 1;
            }

            return(parallelLevelToUse);
        }
        /// <summary>
        /// Gets the value of FailWhenNoTestsFound parameter from runsettings file
        /// </summary>
        /// <param name="runSettings">Runsetting string value</param>
        /// <returns>The value of FailWhenNoTestsFound</returns>
        public static bool GetTreatNoTestsAsError(string runSettings)
        {
            bool failWhenNoTestFound = false;

            if (runSettings != null)
            {
                try
                {
                    RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings);
                    failWhenNoTestFound = GetTreatNoTestsAsError(runConfiguration);
                }
                catch (SettingsException se)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error("RunSettingsUtilities.GetTreatNoTestsAsError: Unable to get the value of TreatNoTestsAsError from runsettings: Error {0}", se);
                    }
                }
            }

            return(failWhenNoTestFound);
        }
        public static void Setup()
        {
            EqtTrace.Info("Setting up debug trace listener.");
            // in the majority of cases there will be only a single DefaultTraceListener in this collection
            // and we will replace that with our listener, in case there are listeners of different types we keep
            // them as is
            for (var i = 0; i < Trace.Listeners.Count; i++)
            {
                var listener = Trace.Listeners[i];
                if (listener is DefaultTraceListener)
                {
                    EqtTrace.Verbose($"TestPlatformTraceListener.Setup: Replacing listener {0} with { nameof(TestHostTraceListener) }.", Trace.Listeners[i]);
                    Trace.Listeners[i] = new TestHostTraceListener();
                }
            }

            EqtTrace.Verbose("TestPlatformTraceListener.Setup: Added test platform trace listener.");

            // this is a netcoreapp2.1 only fix, but because we always compile against netcoreapp2.1
            // and upgrade the executable as necessary this needs to be a runtime check and not a compile time
            // check. This call returns ".NET Core 4.6.xxx" on netcore 2.1 and older, and ".NET Core 3.1.xxx"
            // or the respective version on the newer runtimes
            if (System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith(".NET Core 4.6"))
            {
                try
                {
                    // workaround for netcoreapp2.1 where the trace listener api is not called when
                    // Debug.Assert fails. This method is internal, but the class is on purpose keeping the
                    // callback settable so tests can set the callback
                    var field = typeof(Debug).GetField("s_ShowDialog", BindingFlags.Static | BindingFlags.NonPublic);
                    var value = field.GetValue(null);
                    field.SetValue(null, (Action <string, string, string, string>)ShowDialog);
                }
                catch (Exception ex)
                {
                    EqtTrace.Error("TestPlatformTraceListener.Setup: Failed to replace inner callback to ShowDialog in Debug.Assert. Calls to Debug.Assert with crash the test host process. {0}", ex);
                }
            }
        }
Exemple #8
0
        public IList <String> ToResultFiles(IEnumerable <ObjectModel.AttachmentSet> attachmentSets, TestRun testRun, string trxFileDirectory,
                                            List <string> errorMessages)
        {
            List <String> resultFiles = new List <string>();

            if (attachmentSets == null)
            {
                return(resultFiles);
            }

            foreach (var attachmentSet in attachmentSets)
            {
                if (!attachmentSet.Uri.AbsoluteUri.StartsWith(Constants.DataCollectorUriPrefix, StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        IList <string> testResultFiles = ToResultFiles(attachmentSet, Guid.Empty, testRun, trxFileDirectory);
                        resultFiles.AddRange(testResultFiles);
                    }
                    catch (Exception e)
                    {
                        string errorMsg = string.Format(
                            CultureInfo.CurrentCulture,
                            TrxLoggerResources.FailureToAttach,
                            attachmentSet.DisplayName,
                            e.GetType().ToString(),
                            e);

                        if (EqtTrace.IsErrorEnabled)
                        {
                            EqtTrace.Error("Converter: ToResultFiles: " + errorMsg);
                        }

                        errorMessages.Add(errorMsg);
                    }
                }
            }
            return(resultFiles);
        }
Exemple #9
0
        /// <summary>
        /// Gets the target framework of the test run.
        /// </summary>
        /// <param name="runSettings">Test run settings.</param>
        /// <returns>Target framework</returns>
        internal Framework GetTargetFramework(string runSettings)
        {
            Framework targetFramework = null;

            if (runSettings != null)
            {
                try
                {
                    RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings);
                    targetFramework = RunSettingsUtilities.GetTargetFramework(runConfiguration);
                }
                catch (SettingsException se)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error("TestLoggerManager.GetResultsDirectory: Unable to get the target framework: Error {0}", se);
                    }
                }
            }

            return(targetFramework);
        }
        /// <summary>
        /// Get results directory.
        /// </summary>
        /// <param name="settings">Settings xml.</param>
        /// <returns>Results directory.</returns>
        private string GetResultsDirectory(string settings)
        {
            string resultsDirectory = null;

            if (settings != null)
            {
                try
                {
                    RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(settings);
                    resultsDirectory = RunSettingsUtilities.GetTestResultsDirectory(runConfiguration);
                }
                catch (SettingsException se)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error("EnableBlameArgumentProcessor: Unable to get the test results directory: Error {0}", se);
                    }
                }
            }

            return(resultsDirectory);
        }
        /// <summary>
        /// Returns a dictionary of environment variables given in run settings
        /// </summary>
        /// <param name="runsettingsXml">The run settings xml string</param>
        /// <returns>Environment Variables Dictionary</returns>
        public static Dictionary <string, string> GetEnvironmentVariables(string runSettings)
        {
            Dictionary <string, string> environmentVariables = null;

            try
            {
                using (var stream = new StringReader(runSettings))
                    using (var reader = XmlReader.Create(stream, XmlRunSettingsUtilities.ReaderSettings))
                    {
                        var document = new XmlDocument();
                        document.Load(reader);
                        var runSettingsNavigator = document.CreateNavigator();

                        var node = runSettingsNavigator.SelectSingleNode(EnvironmentVariablesNodePath);
                        if (node == null)
                        {
                            return(null);
                        }

                        environmentVariables = new Dictionary <string, string>();
                        var childNodes = node.SelectChildren(XPathNodeType.Element);

                        while (childNodes.MoveNext())
                        {
                            if (!environmentVariables.ContainsKey(childNodes.Current.Name))
                            {
                                environmentVariables.Add(childNodes.Current.Name, childNodes.Current?.Value);
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                EqtTrace.Error("Error while trying to read environment variables settings. Message: {0}", ex.ToString());
                return(null);
            }

            return(environmentVariables);
        }
Exemple #12
0
        private void OnDiscoveryAbort(ITestDiscoveryEventsHandler eventHandler, Exception exception)
        {
            try
            {
                EqtTrace.Error("Server: TestExecution: Aborting test discovery because {0}", exception);

                var reason = string.Format(CommonResources.AbortedTestDiscovery, exception?.Message);

                // Log to vstest console
                eventHandler.HandleLogMessage(TestMessageLevel.Error, reason);

                // Log to vs ide test output
                var testMessagePayload = new TestMessagePayload {
                    MessageLevel = TestMessageLevel.Error, Message = reason
                };
                var rawMessage = this.dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload);
                eventHandler.HandleRawMessage(rawMessage);

                // Notify discovery abort to IDE test output
                var payload = new DiscoveryCompletePayload()
                {
                    IsAborted           = true,
                    LastDiscoveredTests = null,
                    TotalTests          = -1
                };
                rawMessage = this.dataSerializer.SerializePayload(MessageType.DiscoveryComplete, payload);
                eventHandler.HandleRawMessage(rawMessage);

                // Complete discovery
                eventHandler.HandleDiscoveryComplete(-1, null, true);

                this.CleanupCommunicationIfProcessExit();
            }
            catch (Exception ex)
            {
                EqtTrace.Error(ex);
                throw ex;
            }
        }
        /// <inheritdoc/>
        public override void LogError(DataCollectionContext context, string text, Exception exception)
        {
            ValidateArg.NotNull(context, "context");
            ValidateArg.NotNull(text, "text");
            ValidateArg.NotNull(exception, "exception");

            // Make sure the data collection context is not a derived data collection context.  This
            // is done to safeguard from 3rd parties creating their own data collection contexts.
            if (context.GetType() != typeof(DataCollectionContext))
            {
                throw new InvalidOperationException(Resources.Resources.WrongDataCollectionContextType);
            }

            if (EqtTrace.IsErrorEnabled)
            {
                EqtTrace.Error(
                    "Data collector '{0}' logged the following error:" + Environment.NewLine +
                    "Description:            {1}" + Environment.NewLine +
                    "Exception type:         {2}" + Environment.NewLine + "Exception message:      {3}"
                    + Environment.NewLine + "Exception stack trace:  {4}",
                    this.dataCollectorConfig.TypeUri,
                    text,
                    exception.GetType(),
                    exception.Message,
                    exception.StackTrace);
            }

            // Currently there is one type of DataCollectionMessage sent accross client for all message kind.
            // If required new type can be created for different message type.
            var message = string.Format(
                CultureInfo.CurrentCulture,
                Resources.Resources.ReportDataCollectorException,
                exception.GetType(),
                exception.Message,
                text);

            this.SendTextMessage(context, message, TestMessageLevel.Error);
        }
        /// <summary>
        /// Discovers tests
        /// </summary>
        /// <param name="discoveryCriteria">Settings, parameters for the discovery request</param>
        /// <param name="eventHandler">EventHandler for handling discovery events from Engine</param>
        public void DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)
        {
            this.baseTestDiscoveryEventsHandler = eventHandler;
            try
            {
                this.isCommunicationEstablished = this.SetupChannel(discoveryCriteria.Sources, this.cancellationTokenSource.Token);

                if (this.isCommunicationEstablished)
                {
                    this.InitializeExtensions(discoveryCriteria.Sources);
                    discoveryCriteria.UpdateDiscoveryCriteria(testHostManager);

                    this.RequestSender.DiscoverTests(discoveryCriteria, this);
                }
            }
            catch (Exception exception)
            {
                EqtTrace.Error("ProxyDiscoveryManager.DiscoverTests: Failed to discover tests: {0}", exception);

                // Log to vs ide test output
                var testMessagePayload = new TestMessagePayload {
                    MessageLevel = TestMessageLevel.Error, Message = exception.Message
                };
                var rawMessage = this.dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload);
                this.HandleRawMessage(rawMessage);

                // Log to vstest.console
                // Send a discovery complete to caller. Similar logic is also used in ParallelProxyDiscoveryManager.DiscoverTestsOnConcurrentManager
                // Aborted is `true`: in case of parallel discovery (or non shared host), an aborted message ensures another discovery manager
                // created to replace the current one. This will help if the current discovery manager is aborted due to irreparable error
                // and the test host is lost as well.
                this.HandleLogMessage(TestMessageLevel.Error, exception.Message);

                var discoveryCompleteEventsArgs = new DiscoveryCompleteEventArgs(-1, true);

                this.HandleDiscoveryComplete(discoveryCompleteEventsArgs, new List <ObjectModel.TestCase>());
            }
        }
        /// <summary>
        /// Start vanguard
        /// </summary>
        /// <param name="context">Context</param>
        protected void StartVanguard(DataCollectionContext context)
        {
            if (this.Vanguard != null)
            {
                string outputCoverageFolder = Path.Combine(this.sessionDirectory, Guid.NewGuid().ToString());
                this.CreateDirectory(context, outputCoverageFolder);

                this.coverageFilePath = Path.Combine(outputCoverageFolder, this.coverageFileName);
                try
                {
                    this.Vanguard.Start(this.coverageFilePath, context);
                }
                catch (Exception ex)
                {
                    EqtTrace.Error(
                        "DynamicCoverageDataCollectorImpl.StartVanguard: Failed to start Vanguard for datacollection context sessionID: {0}, with exception: {1}",
                        context.SessionId,
                        ex);
                    this.logger.LogError(context, ex);
                    throw;
                }
            }
        }
        private static bool TryToLoadDiscoverer(LazyExtension <ITestDiscoverer, ITestDiscovererCapabilities> discoverer, IMessageLogger logger, out Type discovererType)
        {
            discovererType = null;

            // See if discoverer can be instantiated successfully else move next.
            try
            {
                discovererType = discoverer.Value.GetType();
            }
            catch (Exception e)
            {
                var mesage = string.Format(
                    CultureInfo.CurrentUICulture,
                    CrossPlatEngineResources.DiscovererInstantiationException,
                    e.Message);
                logger.SendMessage(TestMessageLevel.Warning, mesage);
                EqtTrace.Error("DiscovererEnumerator.LoadTestsFromAnExtension: {0} ", e);

                return(false);
            }

            return(true);
        }
        /// <summary>
        /// The handle discovery message.
        /// </summary>
        /// <param name="level"> Logging level. </param>
        /// <param name="message"> Logging message. </param>
        public void HandleLogMessage(TestMessageLevel level, string message)
        {
            switch ((TestMessageLevel)level)
            {
            case TestMessageLevel.Informational:
                EqtTrace.Info(message);
                break;

            case TestMessageLevel.Warning:
                EqtTrace.Warning(message);
                break;

            case TestMessageLevel.Error:
                EqtTrace.Error(message);
                break;

            default:
                EqtTrace.Info(message);
                break;
            }

            this.requestHandler.SendLog(level, message);
        }
Exemple #18
0
        /// <inheritdoc/>
        public void Initialize(TestLoggerEvents events, Dictionary <string, string> parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (parameters.Count == 0)
            {
                throw new ArgumentException("No default parameters added", nameof(parameters));
            }

            parametersDictionary = parameters;

            if (parameters.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && parameters.TryGetValue(HtmlLoggerConstants.LogFileNameKey, out string logFileNameValue))
            {
                var htmlParameterErrorMsg = string.Format(CultureInfo.CurrentCulture, HtmlResource.PrefixAndNameProvidedError);
                EqtTrace.Error(htmlParameterErrorMsg);
                throw new ArgumentException(htmlParameterErrorMsg);
            }

            this.Initialize(events, parameters[DefaultLoggerParameterNames.TestRunDirectory]);
        }
Exemple #19
0
 internal static void SetNETFrameworkCompatiblityMode(AppDomainSetup setup, IRunContext runContext)
 {
     try
     {
         RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runContext.RunSettings.SettingsXml);
         if (null != runConfiguration && (Enum.Equals(runConfiguration.TargetFramework, FrameworkVersion.Framework40) ||
                                          string.Equals(runConfiguration.TargetFramework.ToString(), Constants.DotNetFramework40, StringComparison.OrdinalIgnoreCase)))
         {
             if (EqtTrace.IsVerboseEnabled)
             {
                 EqtTrace.Verbose("AssemblyHelper.SetNETFrameworkCompatiblityMode: setting .NetFramework,Version=v4.0 compatiblity mode.");
             }
             setup.TargetFrameworkName = Constants.DotNetFramework40;
         }
     }
     catch (Exception e)
     {
         if (EqtTrace.IsErrorEnabled)
         {
             EqtTrace.Error("AssemblyHelper:SetNETFrameworkCompatiblityMode:  Caught an exception:{0}", e);
         }
     }
 }
Exemple #20
0
        /// <summary>
        /// Attempts to find a test extension from given type.
        /// </summary>
        /// <typeparam name="TPluginInfo">Data type of the test plugin information</typeparam>
        /// <param name="type">Type to inspect for being test extension</param>
        /// <param name="extensionType">Test extension type to look for.</param>
        /// <param name="extensionCollection">Test extensions collection to add to.</param>
        /// <returns>True if test extension is found, false otherwise.</returns>
        private void GetTestExtensionFromType <TPluginInfo>(
            Type type,
            Type extensionType,
            Dictionary <string, TPluginInfo> extensionCollection)
            where TPluginInfo : TestPluginInformation
        {
            if (extensionType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
            {
                var dataObject = Activator.CreateInstance(typeof(TPluginInfo), type);
                var pluginInfo = (TPluginInfo)dataObject;

                if (extensionCollection.ContainsKey(pluginInfo.IdentifierData))
                {
                    EqtTrace.Error(
                        "TryGetTestExtensionFromType: Discovered multiple test extensions with identifier data '{0}'; keeping the first one.",
                        pluginInfo.IdentifierData);
                }
                else
                {
                    extensionCollection.Add(pluginInfo.IdentifierData, pluginInfo);
                }
            }
        }
Exemple #21
0
        private void GetDependentAssembliesInternal(string assemblyString, IList <string> result, ISet <string> visitedAssemblies, IList <string> warnings)
        {
            Debug.Assert(!string.IsNullOrEmpty(assemblyString), "assemblyString");

            if (!visitedAssemblies.Add(assemblyString))
            {
                // The assembly was already in the hashset, so we already visited it.
                return;
            }

            Assembly assembly = null;

            try
            {
                EqtTrace.Verbose($"AssemblyLoadWorker.GetDependentAssembliesInternal: Reflection loading {assemblyString}.");

                string postPolicyAssembly = AppDomain.CurrentDomain.ApplyPolicy(assemblyString);
                Debug.Assert(!string.IsNullOrEmpty(postPolicyAssembly), "postPolicyAssembly");

                assembly = this.assemblyUtility.ReflectionOnlyLoad(postPolicyAssembly);
                visitedAssemblies.Add(assembly.FullName);   // Just in case.
            }
            catch (Exception ex)
            {
                EqtTrace.Error($"AssemblyLoadWorker.GetDependentAssembliesInternal: Reflection loading {assemblyString} failed:.");
                EqtTrace.Error(ex);

                string warning = string.Format(CultureInfo.CurrentCulture, Resource.MissingDeploymentDependency, assemblyString, ex.Message);
                warnings.Add(warning);
                return;
            }

            EqtTrace.Verbose($"AssemblyLoadWorker.GetDependentAssembliesInternal: Assembly {assemblyString} was added as dependency.");
            result.Add(assembly.Location);

            this.ProcessChildren(assembly, result, visitedAssemblies, warnings);
        }
        /// <summary>
        /// Initializes Communication with vstest.console.exe
        /// Hosts a communication channel and asynchronously connects to vstest.console.exe
        /// </summary>
        /// <returns>Port Number of hosted server on this side</returns>
        public int InitializeCommunication()
        {
            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info("VsTestConsoleRequestSender.InitializeCommunication: Started.");
            }

            this.processExitCancellationTokenSource = new CancellationTokenSource();
            this.handShakeSuccessful = false;
            this.handShakeComplete.Reset();
            int port = -1;

            try
            {
                port = this.communicationManager.HostServer(new IPEndPoint(IPAddress.Loopback, 0)).Port;
                this.communicationManager.AcceptClientAsync();

                Task.Run(() =>
                {
                    this.communicationManager.WaitForClientConnection(Timeout.Infinite);
                    this.handShakeSuccessful = this.HandShakeWithVsTestConsole();
                    this.handShakeComplete.Set();
                });
            }
            catch (Exception ex)
            {
                EqtTrace.Error("VsTestConsoleRequestSender.InitializeCommunication: Error initializing communication with VstestConsole: {0}", ex);
                this.handShakeComplete.Set();
            }

            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info("VsTestConsoleRequestSender.InitializeCommunication: Ended.");
            }

            return(port);
        }
Exemple #23
0
        /// <summary>
        /// Launches the process with the arguments provided.
        /// </summary>
        /// <param name="processPath">The path to the process.</param>
        /// <param name="arguments">Process arguments.</param>
        /// <param name="workingDirectory">Working directory of the process.</param>
        /// <param name="errorCallback"></param>
        /// <returns>The process spawned.</returns>
        /// <exception cref="Exception">Throws any exception that could result as part of the launch.</exception>
        public Process LaunchProcess(string processPath, string arguments, string workingDirectory, Action <Process, string> errorCallback)
        {
            var process = new Process();

            try
            {
                process.StartInfo.UseShellExecute  = false;
                process.StartInfo.CreateNoWindow   = true;
                process.StartInfo.WorkingDirectory = workingDirectory;

                process.StartInfo.FileName              = processPath;
                process.StartInfo.Arguments             = arguments;
                process.StartInfo.RedirectStandardError = true;
                process.EnableRaisingEvents             = true;

                if (errorCallback != null)
                {
                    process.ErrorDataReceived += (sender, args) => errorCallback(sender as Process, args.Data);
                }

                EqtTrace.Verbose("ProcessHelper: Starting process '{0}' with command line '{1}'", processPath, arguments);
                process.Start();

                process.BeginErrorReadLine();
            }
            catch (Exception exception)
            {
                process.Dispose();
                process = null;

                EqtTrace.Error("TestHost Process {0} failed to launch with the following exception: {1}", processPath, exception.Message);

                throw;
            }

            return(process);
        }
Exemple #24
0
        /// <summary>
        /// The get environment variables.
        /// </summary>
        /// <param name="unloadedAnyCollector">
        /// The unloaded any collector.
        /// </param>
        /// <returns>
        /// Dictionary of variable name as key and collector requested environment variable as value.
        /// </returns>
        private Dictionary <string, DataCollectionEnvironmentVariable> GetEnvironmentVariables(out bool unloadedAnyCollector)
        {
            var failedCollectors = new List <DataCollectorInformation>();

            unloadedAnyCollector = false;
            var dataCollectorEnvironmentVariable = new Dictionary <string, DataCollectionEnvironmentVariable>(StringComparer.OrdinalIgnoreCase);

            // Ordering here is temporary to enable Fakes + Code Coverage integration in scenarios when Fakes decides to instrument code using
            // CLR Instrumentation Engine. This code will be cleaned when both Fakes and Code Coverage will fully switch to CLR Instrumentation Engine.
            foreach (var dataCollectorInfo in this.RunDataCollectors.Values.
                     OrderBy(rdc => rdc.DataCollectorConfig.FriendlyName.Equals(CodeCoverageFriendlyName, StringComparison.OrdinalIgnoreCase) ? 1 : 0))
            {
                try
                {
                    dataCollectorInfo.SetTestExecutionEnvironmentVariables();
                    this.AddCollectorEnvironmentVariables(dataCollectorInfo, dataCollectorEnvironmentVariable);
                }
                catch (Exception ex)
                {
                    unloadedAnyCollector = true;

                    var friendlyName = dataCollectorInfo.DataCollectorConfig.FriendlyName;
                    failedCollectors.Add(dataCollectorInfo);
                    dataCollectorInfo.Logger.LogError(
                        this.dataCollectionEnvironmentContext.SessionDataCollectionContext,
                        string.Format(CultureInfo.CurrentCulture, Resources.Resources.DataCollectorErrorOnGetVariable, friendlyName, ex));

                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error("DataCollectionManager.GetEnvironmentVariables: Failed to get variable for Collector '{0}': {1}", friendlyName, ex);
                    }
                }
            }

            this.RemoveDataCollectors(failedCollectors);
            return(dataCollectorEnvironmentVariable);
        }
        /// <summary>
        /// Loads all the inproc data collector dlls
        /// </summary>
        /// <param name="runSettings">
        /// The run Settings.
        /// </param>
        private void InitializeInProcDataCollectors(string runSettings)
        {
            try
            {
                // Check if runsettings contains in-proc datacollector element
                var inProcDataCollectionRunSettings = XmlRunSettingsUtilities.GetInProcDataCollectionRunSettings(runSettings);
                var inProcDataCollectionSettingsPresentInRunSettings = inProcDataCollectionRunSettings?.IsCollectionEnabled ?? false;

                // Verify if it has any valid in-proc datacollectors or just a dummy element
                inProcDataCollectionSettingsPresentInRunSettings = inProcDataCollectionSettingsPresentInRunSettings &&
                                                                   inProcDataCollectionRunSettings.DataCollectorSettingsList.Any();

                // Initialize if we have atleast one
                if (inProcDataCollectionSettingsPresentInRunSettings)
                {
                    this.inProcDataCollectorSettingsCollection = inProcDataCollectionRunSettings.DataCollectorSettingsList;

                    var interfaceTypeInfo = typeof(InProcDataCollection).GetTypeInfo();
                    foreach (var inProcDc in this.inProcDataCollectorSettingsCollection)
                    {
                        var codeBase = this.GetCodebase(inProcDc.CodeBase);
                        var assemblyQualifiedName = inProcDc.AssemblyQualifiedName;
                        var configuration         = inProcDc.Configuration;
                        var inProcDataCollector   = this.CreateDataCollector(assemblyQualifiedName, codeBase, configuration, interfaceTypeInfo);
                        this.InProcDataCollectors[inProcDataCollector.AssemblyQualifiedName] = inProcDataCollector;
                    }
                }
            }
            catch (Exception ex)
            {
                EqtTrace.Error("InProcDataCollectionExtensionManager: Error occured while Initializing the datacollectors : {0}", ex);
            }
            finally
            {
                this.IsInProcDataCollectionEnabled = this.InProcDataCollectors.Any();
            }
        }
        public TestRequestHandlerTests()
        {
            this.mockCommunicationClient          = new Mock <ICommunicationEndPoint>();
            this.mockCommunicationEndpointFactory = new Mock <ICommunicationEndpointFactory>();
            this.mockChannel            = new Mock <ICommunicationChannel>();
            this.dataSerializer         = JsonDataSerializer.Instance;
            this.testHostConnectionInfo = new TestHostConnectionInfo
            {
                Endpoint = IPAddress.Loopback + ":123",
                Role     = ConnectionRole.Client
            };

            this.jobQueue = new JobQueue <Action>(
                action => { action(); },
                "TestHostOperationQueue",
                500,
                25000000,
                true,
                message => EqtTrace.Error(message));

            // Setup mock discovery and execution managers
            this.mockTestHostManagerFactory = new Mock <ITestHostManagerFactory>();
            this.mockDiscoveryManager       = new Mock <IDiscoveryManager>();
            this.mockExecutionManager       = new Mock <IExecutionManager>();
            this.mockTestHostManagerFactory.Setup(mf => mf.GetDiscoveryManager()).Returns(this.mockDiscoveryManager.Object);
            this.mockTestHostManagerFactory.Setup(mf => mf.GetExecutionManager()).Returns(this.mockExecutionManager.Object);
            this.mockCommunicationEndpointFactory.Setup(f => f.Create(ConnectionRole.Client))
            .Returns(this.mockCommunicationClient.Object);

            this.requestHandler = new TestableTestRequestHandler(
                this.testHostConnectionInfo,
                this.mockCommunicationEndpointFactory.Object,
                JsonDataSerializer.Instance,
                jobQueue);
            this.requestHandler.InitializeCommunication();
            this.mockCommunicationClient.Raise(e => e.Connected += null, new ConnectedEventArgs(this.mockChannel.Object));
        }
Exemple #27
0
        private void OnTestRunAbort(ITestRunEventsHandler testRunEventsHandler, Exception exception)
        {
            try
            {
                EqtTrace.Error("Server: TestExecution: Aborting test run because {0}", exception);

                var reason = string.Format(CommonResources.AbortedTestRun, exception?.Message);

                // log console message to vstest console
                testRunEventsHandler.HandleLogMessage(TestMessageLevel.Error, reason);

                // log console message to vstest console wrapper
                var testMessagePayload = new TestMessagePayload {
                    MessageLevel = TestMessageLevel.Error, Message = reason
                };
                var rawMessage = this.dataSerializer.SerializePayload(MessageType.TestMessage, testMessagePayload);
                testRunEventsHandler.HandleRawMessage(rawMessage);

                // notify test run abort to vstest console wrapper.
                var completeArgs = new TestRunCompleteEventArgs(null, false, true, exception, null, TimeSpan.Zero, null);
                var payload      = new TestRunCompletePayload {
                    TestRunCompleteArgs = completeArgs
                };
                rawMessage = this.dataSerializer.SerializePayload(MessageType.ExecutionComplete, payload);
                testRunEventsHandler.HandleRawMessage(rawMessage);

                // notify of a test run complete and bail out.
                testRunEventsHandler.HandleTestRunComplete(completeArgs, null, null, null);

                this.CleanupCommunicationIfProcessExit();
            }
            catch (Exception ex)
            {
                EqtTrace.Error(ex);
                throw ex;
            }
        }
Exemple #28
0
        private TestExecutorExtensionManager GetExecutorExtensionManager(string extensionAssembly)
        {
            try
            {
                if (string.IsNullOrEmpty(extensionAssembly) ||
                    string.Equals(extensionAssembly, Constants.UnspecifiedAdapterPath))
                {
                    // full execution. Since the extension manager is cached this can be created multiple times without harming performance.
                    return(TestExecutorExtensionManager.Create());
                }
                else
                {
                    return(TestExecutorExtensionManager.GetExecutionExtensionManager(extensionAssembly));
                }
            }
            catch (Exception ex)
            {
                EqtTrace.Error(
                    "BaseRunTests: GetExecutorExtensionManager: Exception occurred while loading extensions {0}",
                    ex);

                return(null);
            }
        }
        /// <inheritdoc/>
        public Collection <AttachmentSet> TestCaseEnded(TestCaseEndEventArgs testCaseEndEventArgs)
        {
            if (!this.isDataCollectionEnabled)
            {
                return(new Collection <AttachmentSet>());
            }

            var context = new DataCollectionContext(this.dataCollectionEnvironmentContext.SessionDataCollectionContext.SessionId, testCaseEndEventArgs.TestElement);

            testCaseEndEventArgs.Context = context;

            this.SendEvent(testCaseEndEventArgs);

            List <AttachmentSet> result = null;

            try
            {
                result = this.attachmentManager.GetAttachments(testCaseEndEventArgs.Context);
            }
            catch (Exception ex)
            {
                if (EqtTrace.IsErrorEnabled)
                {
                    EqtTrace.Error("DataCollectionManager.TestCaseEnded: Failed to get attachments : {0}", ex);
                }

                return(new Collection <AttachmentSet>(result));
            }

            if (EqtTrace.IsVerboseEnabled)
            {
                this.LogAttachments(result);
            }

            return(new Collection <AttachmentSet>(result));
        }
        private void AddSearchDirectoriesSpecifiedInRunSettingsToAssemblyResolver(AssemblyResolver assemblyResolver, string baseDirectory)
        {
            // Check if user specified any adapter settings
            MSTestAdapterSettings adapterSettings = MSTestSettingsProvider.Settings;

            if (adapterSettings != null)
            {
                try
                {
                    var additionalSearchDirectories = adapterSettings.GetDirectoryListWithRecursiveProperty(baseDirectory);
                    if (additionalSearchDirectories?.Count > 0)
                    {
                        assemblyResolver.AddSearchDirectoriesFromRunSetting(additionalSearchDirectories);
                    }
                }
                catch (Exception exception)
                {
                    EqtTrace.Error(
                        "DesktopTestSourceHost.AddSearchDirectoriesSpecifiedInRunSettingsToAssemblyResolver(): Exception hit while trying to set assembly resolver for domain. Exception : {0} \n Message : {1}",
                        exception,
                        exception.Message);
                }
            }
        }