public void InstallWithFailingPowershellFile()
        {
            RetrieveFileDataForTestStep testFileLocation = index => @"c:\a\b";
            UploadReportFilesForTestStep uploader = (index, upload) => { };

            var fileSystem = new MockFileSystem(
                new Dictionary<string, MockFileData>
                    {
                        { @"c:\a\b\c.ps1", new MockFileData("throw 'FAIL'") }
                    });

            var sectionBuilder = new Mock<ITestSectionBuilder>();
            var diagnostics = new SystemDiagnostics((p, s) => { }, null);
            var installer = new ScriptExecuteTestStepProcessor(
               testFileLocation,
               uploader,
               diagnostics,
               fileSystem,
               sectionBuilder.Object);

            var data = new ScriptExecuteTestStep
                {
                    pk_TestStepId = 1,
                    Order = 2,
                    ScriptLanguage = ScriptLanguage.Powershell,
                };

            var result = installer.Process(data, new List<InputParameter>());
            Assert.AreEqual(TestExecutionState.Crashed, result);
        }
        /// <summary>
        /// Adds a new script executing test step.
        /// </summary>
        /// <param name="testStep">The test step.</param>
        public void Add(ScriptExecuteTestStep testStep)
        {
            VerifySchemaVersion();

            var result = StoredTestSteps.Add(testStep) as ScriptExecuteTestStep;
            Debug.Assert(result != null, "The test step should be a script execute test step.");

            Patch(result);
        }
        private ScriptExecuteTestStep Patch(ScriptExecuteTestStep scriptTestStep)
        {
            var result = Patch((TestStep)scriptTestStep) as ScriptExecuteTestStep;
            Debug.Assert(result != null, "The test step should be a script execute test step.");

            return result;
        }
        private void Update(ScriptExecuteTestStep stored, ScriptExecuteTestStep changed)
        {
            var shouldPatch = Update(stored as TestStep, changed as TestStep);
            stored.Language = changed.Language;

            if (shouldPatch)
            {
                stored.IsPatched = false;
                Patch(stored);
            }
        }
示例#5
0
        private static TestStep CopyStepAndStripNonEssentialInformation(TestStep step)
        {
            var parameters = new List<TestStepParameter>();
            foreach (var parameter in step.Parameters)
            {
                parameters.Add(
                    new TestStepParameter
                    {
                        Key = parameter.Key,
                        Value = parameter.Value,
                    });
            }

            var reportDirectories = new List<TestStepReportDirectory>();
            foreach (var directory in step.ReportDirectories)
            {
                reportDirectories.Add(
                    new TestStepReportDirectory
                    {
                        Path = directory.Path,
                    });
            }

            var reportFiles = new List<TestStepReportFile>();
            foreach (var file in step.ReportFiles)
            {
                reportFiles.Add(
                    new TestStepReportFile
                    {
                        Path = file.Path,
                    });
            }

            TestStep result = null;
            var consoleStep = step as ConsoleExecuteTestStep;
            if (consoleStep != null)
            {
                var newStep = new ConsoleExecuteTestStep
                {
                    Order = step.Order,
                    FailureMode = step.FailureMode,
                    ReportIncludesSystemLog = step.ReportIncludesSystemLog,
                    ExecutableFilePath = consoleStep.ExecutableFilePath,
                };

                result = newStep;
            }

            var msiStep = step as MsiInstallTestStep;
            if (msiStep != null)
            {
                var newStep = new MsiInstallTestStep
                {
                    Order = step.Order,
                    FailureMode = step.FailureMode,
                    ReportIncludesSystemLog = step.ReportIncludesSystemLog,
                };

                result = newStep;
            }

            var scriptStep = step as ScriptExecuteTestStep;
            if (scriptStep != null)
            {
                var newStep = new ScriptExecuteTestStep
                {
                    Order = step.Order,
                    FailureMode = step.FailureMode,
                    ReportIncludesSystemLog = step.ReportIncludesSystemLog,
                    ScriptLanguage = scriptStep.ScriptLanguage,
                };

                result = newStep;
            }

            var xcopyStep = step as XCopyTestStep;
            if (xcopyStep != null)
            {
                var newStep = new XCopyTestStep
                {
                    Order = step.Order,
                    FailureMode = step.FailureMode,
                    ReportIncludesSystemLog = step.ReportIncludesSystemLog,
                    Destination = xcopyStep.Destination,
                };

                result = newStep;
            }

            result.Parameters = parameters;
            result.ReportDirectories = reportDirectories;
            result.ReportFiles = reportFiles;
            return result;
        }
        public int RegisterScript(int environment, int order, string failureMode, bool shouldIncludeSystemLog, string language)
        {
            var testStep = new ScriptExecuteTestStep
                {
                    Order = order,
                    TestEnvironmentId = environment,
                    FailureMode = (TestStepFailureMode)Enum.Parse(typeof(TestStepFailureMode), failureMode),
                    ReportIncludesSystemLog = shouldIncludeSystemLog,
                    ScriptLanguage = (ScriptLanguage)Enum.Parse(typeof(ScriptLanguage), language),
                };

            try
            {
                m_Context.Add(testStep);
                m_Context.StoreChanges();
            }
            catch (Exception e)
            {
                m_Diagnostics.Log(
                    LevelToLog.Error,
                    WebApiConstants.LogPrefix,
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Registering the script test step failed with error: {0}",
                        e));

                throw;
            }

            return testStep.Id;
        }
        private TestExecutionState ProcessPowershellScript(
            ScriptExecuteTestStep testStep,
            IEnumerable<InputParameter> environmentParameters,
            string directory)
        {
            var result = TestExecutionState.Running;

            try
            {
                var installerFiles = m_FileSystem.Directory.GetFiles(directory, "*.ps1", SearchOption.TopDirectoryOnly);
                if (!installerFiles.Any())
                {
                    var noFilesFoundMessage = "No installer files found.";
                    Diagnostics.Log(LevelToLog.Error, ScriptExecuteConstants.LogPrefix, noFilesFoundMessage);
                    m_SectionBuilder.AddErrorMessage(noFilesFoundMessage);
                    result = TestExecutionState.Failed;
                }

                foreach (var scriptFile in installerFiles)
                {
                    var logText = string.Format(
                        CultureInfo.InvariantCulture,
                        "Loading powershell script from: {0}",
                        scriptFile);
                    Diagnostics.Log(
                        LevelToLog.Debug,
                        ScriptExecuteConstants.LogPrefix,
                        logText);
                    m_SectionBuilder.AddInformationMessage(logText);

                    var scriptContents = GetScriptContents(scriptFile);

                    // Create a new runspace for the script to execute in
                    var runSpace = RunspaceFactory.CreateRunspace();
                    runSpace.Open();
                    try
                    {
                        // Execute the script
                        var pipeline = runSpace.CreatePipeline();
                        var command = new Command(scriptContents, true);

                        var environmentTestStepParameters = environmentParameters.Select(
                            e => new TestStepParameter
                                {
                                    Key = e.Key,
                                    Value = e.Value
                                });

                        foreach (var parameterSet in testStep.Parameters.Concat(environmentTestStepParameters))
                        {
                            var parameterInfo = string.Format(
                                CultureInfo.InvariantCulture,
                                "Adding parameter {0} with value {1}",
                                parameterSet.Key,
                                parameterSet.Value);
                            Diagnostics.Log(LevelToLog.Debug, ScriptExecuteConstants.LogPrefix, parameterInfo);
                            m_SectionBuilder.AddInformationMessage(parameterInfo);

                            command.Parameters.Add(parameterSet.Key, parameterSet.Value);
                        }

                        pipeline.Commands.Add(command);
                        try
                        {
                            var output = pipeline.Invoke();
                            foreach (var obj in output)
                            {
                                var text = obj.BaseObject.ToString();
                                if (!string.IsNullOrEmpty(text))
                                {
                                    var outputMessage = string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Powershell script output: {0}",
                                        text);
                                    Diagnostics.Log(LevelToLog.Info, ScriptExecuteConstants.LogPrefix, outputMessage);
                                    m_SectionBuilder.AddInformationMessage(outputMessage);
                                }
                            }

                            result = TestExecutionState.Passed;
                        }
                        catch (Exception e)
                        {
                            var errorMessage = string.Format(
                                CultureInfo.CurrentCulture,
                                "Failed to run script: {0}. Error: {1}",
                                scriptFile,
                                e);

                            Diagnostics.Log(LevelToLog.Error, ScriptExecuteConstants.LogPrefix, errorMessage);
                            m_SectionBuilder.AddErrorMessage(errorMessage);
                            result = TestExecutionState.Crashed;
                            break;
                        }
                    }
                    finally
                    {
                        runSpace.Close();
                    }

                    var informationMessage = string.Format(
                        CultureInfo.CurrentCulture,
                        "Ran script: {0}.",
                        scriptFile);
                    Diagnostics.Log(LevelToLog.Info, ScriptExecuteConstants.LogPrefix, informationMessage);
                    m_SectionBuilder.AddInformationMessage(informationMessage);
                }
            }
            catch (Exception e)
            {
                var errorMessage = string.Format(
                    CultureInfo.CurrentCulture,
                    "Failed to run script. Error: {0}",
                    e);

                Diagnostics.Log(LevelToLog.Error, ScriptExecuteConstants.LogPrefix, errorMessage);
                m_SectionBuilder.AddErrorMessage(errorMessage);
                result = TestExecutionState.Crashed;
            }

            return result;
        }