/// <summary>
        /// Processes the given test step.
        /// </summary>
        /// <param name="test">The test step that should be processed.</param>
        /// <param name="environmentParameters">The collection that provides the parameters for the environment.</param>
        /// <returns>The state of the test after the test step has been executed.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="test"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     Thrown if <paramref name="test"/> is not a <see cref="XCopyTestStep"/>.
        /// </exception>
        public override TestExecutionState Process(TestStep test, IEnumerable<InputParameter> environmentParameters)
        {
            {
                Lokad.Enforce.Argument(() => test);
                Lokad.Enforce.With<ArgumentException>(
                    test is XCopyTestStep,
                    Resources.Exceptions_Messages_InvalidTestStep);
            }

            var testStep = test as XCopyTestStep;
            if (!m_FileSystem.Directory.Exists(testStep.Destination))
            {
                try
                {
                    m_FileSystem.Directory.CreateDirectory(testStep.Destination);
                }
                catch (IOException)
                {
                    var errorMessage = string.Format(
                        CultureInfo.InvariantCulture,
                        "Failed to create test directory at location: {0}",
                        testStep.Destination);

                    Diagnostics.Log(LevelToLog.Error, XCopyDeployConstants.LogPrefix, errorMessage);
                    m_SectionBuilder.AddErrorMessage(errorMessage);

                    return TestExecutionState.Failed;
                }
            }

            var directory = TestFileLocationFor(testStep.Order);

            m_CurrentState = TestExecutionState.Running;
            m_SectionBuilder.Initialize("X-copy installer.");
            try
            {
                try
                {
                    var installerFiles = m_FileSystem.Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories);
                    foreach (var installerFile in installerFiles)
                    {
                        var relativePath = installerFile.Substring(directory.Length).TrimStart(Path.DirectorySeparatorChar);
                        var destination = Path.Combine(testStep.Destination, relativePath);
                        try
                        {
                            var destinationDirectory = Path.GetDirectoryName(destination);
                            if (!m_FileSystem.Directory.Exists(destinationDirectory))
                            {
                                m_FileSystem.Directory.CreateDirectory(destinationDirectory);
                            }
                        }
                        catch (IOException e)
                        {
                            var errorMessage = string.Format(
                                CultureInfo.CurrentCulture,
                                "Failed to install {0}. Error while creating directory: {1}",
                                installerFile,
                                e);

                            Diagnostics.Log(LevelToLog.Error, XCopyDeployConstants.LogPrefix, errorMessage);
                            m_SectionBuilder.AddErrorMessage(errorMessage);

                            m_CurrentState = TestExecutionState.Failed;
                            break;
                        }

                        try
                        {
                            var copyInformation = string.Format(
                                CultureInfo.InvariantCulture,
                                "Copying from: {0}; To: {1}",
                                installerFile,
                                destination);
                            Diagnostics.Log(
                                LevelToLog.Debug,
                                XCopyDeployConstants.LogPrefix,
                                copyInformation);
                            m_SectionBuilder.AddInformationMessage(copyInformation);

                            m_FileSystem.File.Copy(installerFile, destination);
                        }
                        catch (IOException e)
                        {
                            var errorMessage = string.Format(
                                CultureInfo.CurrentCulture,
                                "Failed to install {0}. Error: {1}",
                                installerFile,
                                e);

                            Diagnostics.Log(LevelToLog.Error, XCopyDeployConstants.LogPrefix, errorMessage);
                            m_SectionBuilder.AddErrorMessage(errorMessage);

                            m_CurrentState = TestExecutionState.Failed;
                            break;
                        }

                        var informationMessage = string.Format(
                            CultureInfo.CurrentCulture,
                            "Installed: {0}.",
                            installerFile);
                        Diagnostics.Log(LevelToLog.Info, XCopyDeployConstants.LogPrefix, informationMessage);
                        m_SectionBuilder.AddInformationMessage(informationMessage);
                    }

                    m_CurrentState = TestExecutionState.Passed;
                }
                finally
                {
                    TransferReportFiles(m_SectionBuilder, testStep);
                }
            }
            finally
            {
                m_SectionBuilder.FinalizeAndStore(m_CurrentState == TestExecutionState.Passed);
            }

            return m_CurrentState;
        }
        /// <summary>
        /// Transfers the report files back to the host.
        /// </summary>
        /// <param name="sectionBuilder">The object that stores the report messages.</param>
        /// <param name="testStep">The test step for which the files should be uploaded.</param>
        /// <param name="additionalFilesToInclude">A collection containing the additional files that should be included in the report.</param>
        protected void TransferReportFiles(
            ITestSectionBuilder sectionBuilder,
            TestStep testStep,
            IEnumerable<string> additionalFilesToInclude = null)
        {
            try
            {
                var filesToTransfer = new Dictionary<FileInfo, DirectoryInfo>();
                foreach (var file in testStep.ReportFiles)
                {
                    if (File.Exists(file.Path))
                    {
                        filesToTransfer.Add(new FileInfo(file.Path), new DirectoryInfo(Path.GetDirectoryName(file.Path)));
                    }
                }

                foreach (var directory in testStep.ReportDirectories)
                {
                    if (Directory.Exists(directory.Path))
                    {
                        var files = Directory.GetFiles(directory.Path, "*.*", SearchOption.AllDirectories);
                        foreach (var file in files)
                        {
                            filesToTransfer.Add(new FileInfo(file), new DirectoryInfo(directory.Path));
                        }
                    }
                }

                if (additionalFilesToInclude != null)
                {
                    foreach (var file in additionalFilesToInclude)
                    {
                        if (File.Exists(file))
                        {
                            filesToTransfer.Add(new FileInfo(file), new DirectoryInfo(Path.GetDirectoryName(file)));
                        }
                    }
                }

                if (testStep.ReportIncludesSystemLog)
                {
                    var logFile = ConsoleExecuteConstants.LogPath();
                    if (File.Exists(logFile))
                    {
                        filesToTransfer.Add(new FileInfo(logFile), new DirectoryInfo(Path.GetDirectoryName(logFile)));
                    }
                }

                m_ReportFileUploader(testStep.Order, filesToTransfer);

                sectionBuilder.AddInformationMessage(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Uploaded report files for test step: {0}",
                        testStep.Order));
            }
            catch (Exception e)
            {
                sectionBuilder.AddWarningMessage(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Failed to upload report files for test step: {0}. Error was: {1}",
                        testStep.Order,
                        e));
            }
        }
        /// <summary>
        /// Processes the given test step.
        /// </summary>
        /// <param name="test">The test step that should be processed.</param>
        /// <param name="environmentParameters">The collection that provides the parameters for the environment.</param>
        /// <returns>The state of the test after the test step has been executed.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="test"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     Thrown if <paramref name="test"/> is not a <see cref="ScriptExecuteTestStep"/>.
        /// </exception>
        public override TestExecutionState Process(TestStep test, IEnumerable<InputParameter> environmentParameters)
        {
            {
                Lokad.Enforce.Argument(() => test);
                Lokad.Enforce.With<ArgumentException>(
                    test is ConsoleExecuteTestStep,
                    Resources.Exceptions_Messages_InvalidTestStep);
            }

            var testStep = test as ConsoleExecuteTestStep;

            m_CurrentState = TestExecutionState.Running;
            m_SectionBuilder.Initialize("Application execution");
            try
            {
                try
                {
                    var parameters = testStep.Parameters
                        .OrderBy(p => int.Parse(p.Key, CultureInfo.InvariantCulture))
                        .Select(p => p.Value)
                        .ToList();
                    int returnCode = RunApplication(testStep.ExecutableFilePath, parameters);
                    if (returnCode != 0)
                    {
                        var errorMessage = string.Format(
                            CultureInfo.CurrentCulture,
                            "Failed to execute {0} {1}.",
                            m_FileSystem.Path.GetFileName(testStep.ExecutableFilePath),
                            string.Join(" ", parameters));
                        Diagnostics.Log(LevelToLog.Error, ConsoleExecuteConstants.LogPrefix, errorMessage);
                        m_SectionBuilder.AddErrorMessage(errorMessage);

                        m_CurrentState = TestExecutionState.Failed;
                    }
                    else
                    {
                        var informationMessage = string.Format(
                            CultureInfo.CurrentCulture,
                            "Executed: {0} {1}.",
                            m_FileSystem.Path.GetFileName(testStep.ExecutableFilePath),
                            string.Join(" ", parameters));
                        Diagnostics.Log(LevelToLog.Info, ConsoleExecuteConstants.LogPrefix, informationMessage);
                        m_SectionBuilder.AddInformationMessage(informationMessage);

                        m_CurrentState = TestExecutionState.Passed;
                    }
                }
                finally
                {
                    TransferReportFiles(m_SectionBuilder, testStep);
                }
            }
            finally
            {
                m_SectionBuilder.FinalizeAndStore(m_CurrentState == TestExecutionState.Passed);
            }

            return m_CurrentState;
        }
        private TestStep Patch(TestStep testStep)
        {
            var result = StoredTestSteps.Find(testStep.Id) ?? StoredTestSteps.Add(testStep);
            if (!result.IsPatched && !result.IsPatching)
            {
                result.IsPatching = true;
                try
                {
                    var selectedTestEnvironment = GetTestEnvironmentsById(result.TestEnvironmentId)
                        .Select(Patch)
                        .FirstOrDefault();
                    result.TestEnvironment = selectedTestEnvironment;

                    var selectedParameters = GetTestStepParametersByTestStepId(result.Id)
                        .Select(id => TestStepParameter(id.Value))
                        .ToList();
                    result.TestStepParameters = selectedParameters;

                    var selectedReportDirectories = GetTestStepReportDirectoriesByTestStepId(result.Id)
                        .Select(id => TestStepReportDirectory(id.Value))
                        .ToList();
                    result.TestStepReportDirectories = selectedReportDirectories;

                    var selectedReportFiles = GetTestStepReportFilesByTestStepId(result.Id)
                        .Select(id => TestStepReportFile(id.Value))
                        .ToList();
                    result.TestStepReportFiles = selectedReportFiles;

                    result.IsPatched = true;
                }
                finally
                {
                    result.IsPatching = false;
                }
            }

            return result;
        }
 /// <summary>
 /// Processes the given test step.
 /// </summary>
 /// <param name="test">The test step that should be processed.</param>
 /// <param name="environmentParameters">The collection that provides the parameters for the environment.</param>
 /// <returns>The state of the test after the test step has been executed.</returns>
 public abstract TestExecutionState Process(TestStep test, IEnumerable<InputParameter> environmentParameters);
        private static bool Update(TestStep stored, TestStep changed)
        {
            stored.Order = changed.Order;
            stored.OnFailure = changed.OnFailure;
            if (stored.TestEnvironmentId != changed.TestEnvironmentId)
            {
                stored.TestEnvironment = null;
                stored.TestEnvironmentId = changed.TestEnvironmentId;

                return true;
            }

            return false;
        }
        /// <summary>
        /// Updates the test environment with the data from the given object.
        /// </summary>
        /// <param name="testStep">The test step.</param>
        public void Update(TestStep testStep)
        {
            VerifySchemaVersion();

            var storedTestStep = TestStep(testStep.Id);
            if (storedTestStep != null)
            {
                if (!ReferenceEquals(storedTestStep, testStep))
                {
                    var storedConsoleExecuteTestStep = storedTestStep as ConsoleExecuteTestStep;
                    if (storedConsoleExecuteTestStep != null)
                    {
                        Update(storedConsoleExecuteTestStep, testStep as ConsoleExecuteTestStep);
                    }

                    var storedMsiInstallTestStep = storedTestStep as MsiInstallTestStep;
                    if (storedMsiInstallTestStep != null)
                    {
                        Update(storedMsiInstallTestStep, testStep as MsiInstallTestStep);
                    }

                    var storedScriptExecuteTestStep = storedTestStep as ScriptExecuteTestStep;
                    if (storedScriptExecuteTestStep != null)
                    {
                        Update(storedScriptExecuteTestStep, testStep as ScriptExecuteTestStep);
                    }

                    var storedXCopyTestStep = storedTestStep as XCopyTestStep;
                    if (storedXCopyTestStep != null)
                    {
                        Update(storedXCopyTestStep, testStep as XCopyTestStep);
                    }
                }

                var entry = Entry(storedTestStep);
                entry.State = EntityState.Modified;
            }
        }
        /// <summary>
        /// Processes the given test step.
        /// </summary>
        /// <param name="test">The test step that should be processed.</param>
        /// <param name="environmentParameters">The collection that provides the parameters for the environment.</param>
        /// <returns>The state of the test after the test step has been executed.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="test"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     Thrown if <paramref name="test"/> is not a <see cref="MsiInstallTestStep"/>.
        /// </exception>
        public override TestExecutionState Process(TestStep test, IEnumerable<InputParameter> environmentParameters)
        {
            {
                Lokad.Enforce.Argument(() => test);
                Lokad.Enforce.With<ArgumentException>(
                    test is MsiInstallTestStep,
                    Resources.Exceptions_Messages_InvalidTestStep);
            }

            m_CurrentState = TestExecutionState.Running;

            var testStep = test as MsiInstallTestStep;
            var directory = TestFileLocationFor(testStep.Order);
            var tempPath = CreateLogPath();
            if (m_CurrentState != TestExecutionState.Running)
            {
                return m_CurrentState;
            }

            m_SectionBuilder.Initialize("MSI install");
            try
            {
                var logFiles = new List<string>();
                try
                {
                    var installerFiles = m_FileSystem.Directory.GetFiles(directory, "*.msi", SearchOption.TopDirectoryOnly);
                    if (!installerFiles.Any())
                    {
                        const string noFilesFoundMessage = "No installer files found.";
                        Diagnostics.Log(LevelToLog.Error, ScriptExecuteConstants.LogPrefix, noFilesFoundMessage);
                        m_SectionBuilder.AddErrorMessage(noFilesFoundMessage);
                        m_CurrentState = TestExecutionState.Failed;
                    }

                    foreach (var installerFile in installerFiles)
                    {
                        // Log everything to the default log file.
                        var logFile = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(installerFile) + ".log");
                        logFiles.Add(logFile);
                        var arguments = new List<string>
                            {
                                QuietSwitch,
                                string.Format(CultureInfo.InvariantCulture, LogSwitch, logFile),
                                string.Format(CultureInfo.InvariantCulture, InstallSwitch, installerFile),
                            };

                        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 installer parameter {0} with value {1}",
                                parameterSet.Key,
                                parameterSet.Value);
                            Diagnostics.Log(LevelToLog.Debug, MsiDeployConstants.LogPrefix, parameterInfo);
                            m_SectionBuilder.AddInformationMessage(parameterInfo);

                            arguments.Add(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    ParameterSwitch,
                                    parameterSet.Key,
                                    parameterSet.Value));
                        }

                        int returnCode = RunInstaller(arguments, installerFile);
                        if (returnCode != 0)
                        {
                            var errorMessage = string.Format(
                                CultureInfo.CurrentCulture,
                                "Failed to install {0}.",
                                installerFile);
                            Diagnostics.Log(LevelToLog.Error, MsiDeployConstants.LogPrefix, errorMessage);
                            m_SectionBuilder.AddErrorMessage(errorMessage);

                            m_CurrentState = TestExecutionState.Failed;
                        }
                        else
                        {
                            var informationMessage = string.Format(
                                CultureInfo.CurrentCulture,
                                "Installed: {0}.",
                                installerFile);
                            Diagnostics.Log(LevelToLog.Info, MsiDeployConstants.LogPrefix, informationMessage);
                            m_SectionBuilder.AddInformationMessage(informationMessage);

                            m_CurrentState = TestExecutionState.Passed;
                        }
                    }
                }
                finally
                {
                    TransferReportFiles(
                        m_SectionBuilder,
                        testStep,
                        testStep.ReportIncludesSystemLog ? logFiles : null);
                }
            }
            finally
            {
                m_SectionBuilder.FinalizeAndStore(m_CurrentState == TestExecutionState.Passed);
            }

            return m_CurrentState;
        }
示例#9
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;
        }
        /// <summary>
        /// Processes the given test step.
        /// </summary>
        /// <param name="test">The test step that should be processed.</param>
        /// <param name="environmentParameters">The collection that provides the parameters for the environment.</param>
        /// <returns>The state of the test after the test step has been executed.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="test"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     Thrown if <paramref name="test"/> is not a <see cref="ScriptExecuteTestStep"/>.
        /// </exception>
        public override TestExecutionState Process(TestStep test, IEnumerable<InputParameter> environmentParameters)
        {
            {
                Lokad.Enforce.Argument(() => test);
                Lokad.Enforce.With<ArgumentException>(
                    test is ScriptExecuteTestStep,
                    Resources.Exceptions_Messages_InvalidTestStep);
            }

            var testStep = test as ScriptExecuteTestStep;
            var directory = TestFileLocationFor(testStep.Order);

            m_CurrentState = TestExecutionState.Running;
            m_SectionBuilder.Initialize("Script execution");
            try
            {
                try
                {
                    var logText = string.Format(
                        CultureInfo.InvariantCulture,
                        "Executing script of type: {0}",
                        testStep.ScriptLanguage);
                    Diagnostics.Log(
                        LevelToLog.Debug,
                        ScriptExecuteConstants.LogPrefix,
                        logText);
                    m_SectionBuilder.AddInformationMessage(logText);

                    switch (testStep.ScriptLanguage)
                    {
                        case ScriptLanguage.Powershell:
                            m_CurrentState = ProcessPowershellScript(testStep, environmentParameters, directory);
                            break;
                        default:
                            Diagnostics.Log(
                                LevelToLog.Error,
                                ScriptExecuteConstants.LogPrefix,
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    "Unknown script language: {0}",
                                    testStep.ScriptLanguage));

                            m_SectionBuilder.AddErrorMessage("Unknown test script language.");
                            m_CurrentState = TestExecutionState.Failed;
                            break;
                    }
                }
                finally
                {
                    TransferReportFiles(m_SectionBuilder, testStep);
                }
            }
            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);
                m_CurrentState = TestExecutionState.Failed;
            }
            finally
            {
                m_SectionBuilder.FinalizeAndStore(m_CurrentState == TestExecutionState.Passed);
            }

            return m_CurrentState;
        }