示例#1
0
        /// <summary>
        /// Update the test adapter paths provided through run settings to be used by the test plugin cache.
        /// </summary>
        /// <param name="payload">
        /// The before test run start payload
        /// </param>
        private void AddExtensionAssemblies(BeforeTestRunStartPayload payload)
        {
            try
            {
                var customTestAdaptersPaths = RunSettingsUtilities.GetTestAdaptersPaths(payload.SettingsXml);

                // In case of dotnet vstest with code coverage, data collector needs to be picked up from publish folder.
                // Therefore, adding source dll folders to search datacollectors in these.
                var datacollectorSearchPaths = new HashSet <string>();
                foreach (var source in payload.Sources)
                {
                    datacollectorSearchPaths.Add(Path.GetDirectoryName(source));
                }

                if (customTestAdaptersPaths != null)
                {
                    datacollectorSearchPaths.UnionWith(customTestAdaptersPaths);
                }

                List <string> extensionAssemblies = new List <string>();
                foreach (var datacollectorSearchPath in datacollectorSearchPaths)
                {
                    var adapterPath =
                        Path.GetFullPath(Environment.ExpandEnvironmentVariables(datacollectorSearchPath));
                    if (!this.fileHelper.DirectoryExists(adapterPath))
                    {
                        EqtTrace.Warning(string.Format("AdapterPath Not Found:", adapterPath));
                        continue;
                    }

                    extensionAssemblies.AddRange(
                        this.fileHelper.EnumerateFiles(
                            adapterPath,
                            SearchOption.AllDirectories,
                            TestPlatformConstants.DataCollectorEndsWithPattern));
                }

                if (extensionAssemblies.Count > 0)
                {
                    TestPluginCache.Instance.UpdateExtensions(extensionAssemblies, skipExtensionFilters: false);
                }
            }
            catch (Exception e)
            {
                // If any exception is thrown while updating additional assemblies, log the exception in eqt trace.
                if (EqtTrace.IsErrorEnabled)
                {
                    EqtTrace.Error("DataCollectionRequestHandler.AddExtensionAssemblies: Exception occurred: {0}", e);
                }
            }
        }
        public void ProcessRequestsShouldSetDefaultTimeoutIfNoEnvVarialbeSet()
        {
            var beforeTestRunSTartPayload = new BeforeTestRunStartPayload {
                SettingsXml = "settingsxml", Sources = new List <string> {
                    "test1.dll"
                }
            };

            this.mockDataSerializer.Setup(x => x.DeserializePayload <BeforeTestRunStartPayload>(It.Is <Message>(y => y.MessageType == MessageType.BeforeTestRunStart)))
            .Returns(beforeTestRunSTartPayload);

            this.requestHandler.ProcessRequests();

            this.mockDataCollectionTestCaseEventHandler.Verify(h => h.WaitForRequestHandlerConnection(EnvironmentHelper.DefaultConnectionTimeout * 1000));
        }
        public void ProcessRequestsShouldInitializeTestCaseEventHandlerIfTestCaseLevelEventsAreEnabled()
        {
            var beforeTestRunSTartPayload = new BeforeTestRunStartPayload {
                SettingsXml = "settingsxml", Sources = new List <string> {
                    "test1.dll"
                }
            };

            this.mockDataSerializer.Setup(x => x.DeserializePayload <BeforeTestRunStartPayload>(It.Is <Message>(y => y.MessageType == MessageType.BeforeTestRunStart)))
            .Returns(beforeTestRunSTartPayload);

            this.requestHandler.ProcessRequests();

            this.mockDataCollectionTestCaseEventHandler.Verify(x => x.InitializeCommunication(), Times.Once);
            this.mockDataCollectionTestCaseEventHandler.Verify(x => x.ProcessRequests(), Times.Once);
            this.mockDataCollectionTestCaseEventHandler.Verify(x => x.WaitForRequestHandlerConnection(It.IsAny <int>()), Times.Once);
        }
        public void ProcessRequestsShouldAddSourceDirectoryToTestPluginCache()
        {
            var testHostLaunchedPayload = new TestHostLaunchedPayload();

            testHostLaunchedPayload.ProcessId = 1234;

            var    temp        = Path.GetTempPath();
            string runSettings = "<RunSettings><RunConfiguration><TestAdaptersPaths></TestAdaptersPaths></RunConfiguration></RunSettings>";

            this.mockCommunicationManager.SetupSequence(x => x.ReceiveMessage()).Returns(this.beforeTestRunStart)
            .Returns(new Message()
            {
                MessageType = MessageType.TestHostLaunched, Payload = JToken.FromObject(testHostLaunchedPayload)
            })
            .Returns(this.afterTestRunEnd);

            this.mockDataCollectionManager.Setup(x => x.SessionStarted(It.IsAny <SessionStartEventArgs>())).Returns(true);
            this.mockDataCollectionManager.Setup(x => x.TestHostLaunched(It.IsAny <int>()));
            this.mockDataSerializer.Setup(x => x.DeserializePayload <TestHostLaunchedPayload>(It.Is <Message>(y => y.MessageType == MessageType.TestHostLaunched)))
            .Returns(testHostLaunchedPayload);
            var beforeTestRunSTartPayload = new BeforeTestRunStartPayload
            {
                SettingsXml = runSettings,
                Sources     = new List <string>
                {
                    Path.Combine(temp, "dir1", "test1.dll"),
                    Path.Combine(temp, "dir2", "test2.dll"),
                    Path.Combine(temp, "dir3", "test3.dll")
                }
            };

            this.mockDataSerializer.Setup(x => x.DeserializePayload <BeforeTestRunStartPayload>(It.Is <Message>(y => y.MessageType == MessageType.BeforeTestRunStart)))
            .Returns(beforeTestRunSTartPayload);
            this.mockFileHelper.Setup(x => x.DirectoryExists($@"{temp}dir1")).Returns(true);
            this.mockFileHelper.Setup(x => x.EnumerateFiles($@"{temp}dir1", SearchOption.AllDirectories, @"Collector.dll")).Returns(new List <string> {
                Path.Combine(temp, "dir1", "abc.DataCollector.dll")
            });

            this.requestHandler.ProcessRequests();

            this.mockFileHelper.Verify(x => x.EnumerateFiles($@"{temp}dir1", SearchOption.AllDirectories, @"Collector.dll"), Times.Once);
            Assert.IsTrue(TestPluginCache.Instance.GetExtensionPaths(@"Collector.dll").Contains(Path.Combine(temp, "dir1", "abc.DataCollector.dll")));
        }
        public void ProcessRequestsShouldProcessRequests()
        {
            var testHostLaunchedPayload = new TestHostLaunchedPayload();

            testHostLaunchedPayload.ProcessId = 1234;

            this.mockCommunicationManager.SetupSequence(x => x.ReceiveMessage()).Returns(this.beforeTestRunStart)
            .Returns(new Message()
            {
                MessageType = MessageType.TestHostLaunched, Payload = JToken.FromObject(testHostLaunchedPayload)
            })
            .Returns(this.afterTestRunEnd);

            this.mockDataCollectionManager.Setup(x => x.SessionStarted(It.IsAny <SessionStartEventArgs>())).Returns(true);
            this.mockDataCollectionManager.Setup(x => x.TestHostLaunched(It.IsAny <int>()));
            this.mockDataSerializer.Setup(x => x.DeserializePayload <TestHostLaunchedPayload>(It.Is <Message>(y => y.MessageType == MessageType.TestHostLaunched)))
            .Returns(testHostLaunchedPayload);
            var beforeTestRunSTartPayload = new BeforeTestRunStartPayload {
                SettingsXml = "settingsxml", Sources = new List <string> {
                    "test1.dll"
                }
            };

            this.mockDataSerializer.Setup(x => x.DeserializePayload <BeforeTestRunStartPayload>(It.Is <Message>(y => y.MessageType == MessageType.BeforeTestRunStart)))
            .Returns(beforeTestRunSTartPayload);

            this.requestHandler.ProcessRequests();

            this.mockDataCollectionTestCaseEventHandler.Verify(x => x.InitializeCommunication(), Times.Once);
            this.mockDataCollectionTestCaseEventHandler.Verify(x => x.WaitForRequestHandlerConnection(It.IsAny <int>()), Times.Once);
            this.mockDataCollectionTestCaseEventHandler.Verify(x => x.ProcessRequests(), Times.Once);

            // Verify SessionStarted events
            this.mockDataCollectionManager.Verify(x => x.SessionStarted(It.IsAny <SessionStartEventArgs>()), Times.Once);
            this.mockCommunicationManager.Verify(x => x.SendMessage(MessageType.BeforeTestRunStartResult, It.IsAny <BeforeTestRunStartResult>()), Times.Once);

            // Verify TestHostLaunched events
            this.mockDataCollectionManager.Verify(x => x.TestHostLaunched(1234), Times.Once);

            // Verify AfterTestRun events.
            this.mockDataCollectionManager.Verify(x => x.SessionEnded(It.IsAny <bool>()), Times.Once);
            this.mockCommunicationManager.Verify(x => x.SendMessage(MessageType.AfterTestRunEndResult, It.IsAny <AfterTestRunEndResult>()), Times.Once);
        }
        /// <inheritdoc/>
        public BeforeTestRunStartResult SendBeforeTestRunStartAndGetResult(string settingsXml, IEnumerable <string> sources, bool isTelemetryOptedIn, ITestMessageEventHandler runEventsHandler)
        {
            var isDataCollectionStarted     = false;
            BeforeTestRunStartResult result = null;

            if (EqtTrace.IsVerboseEnabled)
            {
                EqtTrace.Verbose("DataCollectionRequestSender.SendBeforeTestRunStartAndGetResult : Send BeforeTestRunStart message with settingsXml {0} and sources {1}: ", settingsXml, sources.ToString());
            }

            var payload = new BeforeTestRunStartPayload
            {
                SettingsXml        = settingsXml,
                Sources            = sources,
                IsTelemetryOptedIn = isTelemetryOptedIn
            };

            this.communicationManager.SendMessage(MessageType.BeforeTestRunStart, payload);

            while (!isDataCollectionStarted)
            {
                var message = this.communicationManager.ReceiveMessage();

                if (EqtTrace.IsVerboseEnabled)
                {
                    EqtTrace.Verbose("DataCollectionRequestSender.SendBeforeTestRunStartAndGetResult : Received message: {0}", message);
                }

                if (message.MessageType == MessageType.DataCollectionMessage)
                {
                    var dataCollectionMessageEventArgs = this.dataSerializer.DeserializePayload <DataCollectionMessageEventArgs>(message);
                    this.LogDataCollectorMessage(dataCollectionMessageEventArgs, runEventsHandler);
                }
                else if (message.MessageType == MessageType.BeforeTestRunStartResult)
                {
                    isDataCollectionStarted = true;
                    result = this.dataSerializer.DeserializePayload <BeforeTestRunStartResult>(message);
                }
            }

            return(result);
        }
        public void ProcessRequestShouldInitializeDataCollectorsWithCorrectSettings()
        {
            var beforeTestRunStartPayload = new BeforeTestRunStartPayload {
                SettingsXml = "settingsxml", Sources = new List <string> {
                    "test1.dll"
                }
            };

            this.mockDataSerializer.Setup(x => x.DeserializePayload <BeforeTestRunStartPayload>(It.Is <Message>(y => y.MessageType == MessageType.BeforeTestRunStart)))
            .Returns(beforeTestRunStartPayload);
            var message = new Message()
            {
                MessageType = MessageType.BeforeTestRunStart, Payload = JToken.FromObject(beforeTestRunStartPayload)
            };

            this.mockCommunicationManager.SetupSequence(x => x.ReceiveMessage()).Returns(message).Returns(this.afterTestRunEnd);
            this.requestHandler.ProcessRequests();

            this.mockDataCollectionManager.Verify(x => x.InitializeDataCollectors("settingsxml"), Times.Once);
        }
        public void ProcessRequestShouldNotEnableTelemetryIfTelemetryEnablingNotRequested()
        {
            var beforeTestRunStartPayload = new BeforeTestRunStartPayload {
                SettingsXml = "settingsxml", Sources = new List <string> {
                    "test1.dll", "test2.dll"
                }, IsTelemetryOptedIn = false
            };

            this.mockRequestData.Setup(r => r.IsTelemetryOptedIn).Returns(false);
            this.mockDataSerializer.Setup(x => x.DeserializePayload <BeforeTestRunStartPayload>(It.Is <Message>(y => y.MessageType == MessageType.BeforeTestRunStart)))
            .Returns(beforeTestRunStartPayload);
            var message = new Message()
            {
                MessageType = MessageType.BeforeTestRunStart, Payload = JToken.FromObject(beforeTestRunStartPayload)
            };

            this.mockCommunicationManager.SetupSequence(x => x.ReceiveMessage()).Returns(message).Returns(this.afterTestRunEnd);
            this.requestHandler.ProcessRequests();

            this.mockRequestData.VerifySet(r => r.IsTelemetryOptedIn = It.IsAny <bool>(), Times.Never);
            this.mockRequestData.VerifySet(r => r.MetricsCollection  = It.IsAny <IMetricsCollection>(), Times.Never);
        }
        public void ProcessRequestShouldCallSessionStartWithCorrectTestSources()
        {
            var beforeTestRunStartPayload = new BeforeTestRunStartPayload {
                SettingsXml = "settingsxml", Sources = new List <string> {
                    "test1.dll", "test2.dll"
                }
            };

            this.mockDataSerializer.Setup(x => x.DeserializePayload <BeforeTestRunStartPayload>(It.Is <Message>(y => y.MessageType == MessageType.BeforeTestRunStart)))
            .Returns(beforeTestRunStartPayload);
            var message = new Message()
            {
                MessageType = MessageType.BeforeTestRunStart, Payload = JToken.FromObject(beforeTestRunStartPayload)
            };

            this.mockCommunicationManager.SetupSequence(x => x.ReceiveMessage()).Returns(message).Returns(this.afterTestRunEnd);
            this.requestHandler.ProcessRequests();

            this.mockDataCollectionManager.Verify(x => x.SessionStarted(It.Is <SessionStartEventArgs>(
                                                                            y => y.GetPropertyValue <IEnumerable <string> >("TestSources").Contains("test1.dll") &&
                                                                            y.GetPropertyValue <IEnumerable <string> >("TestSources").Contains("test2.dll"))));
        }