Example #1
0
        public virtual void Initialize()
        {
            lock (_initializeLock)
            {
                if (_initialized)
                {
                    return;
                }

                _initialized = true;

                TestLogger?.WriteLine(RemoteServiceFixtures.RemoteApplication.AppName);


                var numberOfTries = 0;

                try
                {
                    var appIsExercisedNormally = true;

                    do
                    {
                        TestLogger?.WriteLine("Test Home" + RemoteApplication.DestinationNewRelicHomeDirectoryPath);
                        appIsExercisedNormally = true;

                        RemoteApplication.TestLogger = new XUnitTestLogger(TestLogger);

                        RemoteApplication.DeleteWorkingSpace();

                        RemoteApplication.CopyToRemote();

                        SetupConfiguration();

                        var captureStandardOutput = RemoteApplication.CaptureStandardOutput;

                        RemoteApplication.Start(CommandLineArguments, captureStandardOutput);

                        try
                        {
                            ExerciseApplication();
                        }
                        catch (Exception ex)
                        {
                            appIsExercisedNormally = false;
                            TestLogger?.WriteLine("Exception occurred in try number " + (numberOfTries + 1) + " : " + ex.ToString());
                        }
                        finally
                        {
                            if (!DelayKill)
                            {
                                ShutdownRemoteApplication();

                                if (captureStandardOutput)
                                {
                                    RemoteApplication.CapturedOutput.WriteProcessOutputToLog("RemoteApplication:");

                                    // Most of our tests run in HostedWebCore, but some don't, e.g. the self-hosted
                                    // WCF tests. For the HWC tests we carefully validate the console output in order
                                    // to detect process-level failures that may cause test flickers. For the self-
                                    // hosted tests, unfortunately, we just punt that.
                                    if (RemoteApplication.ValidateHostedWebCoreOutput)
                                    {
                                        SubprocessLogValidator.ValidateHostedWebCoreConsoleOutput(RemoteApplication.CapturedOutput.StandardOutput, TestLogger);
                                    }
                                    else
                                    {
                                        TestLogger?.WriteLine("Note: child process is not required for log validation because _remoteApplication.ValidateHostedWebCoreOutput = false");
                                    }
                                }
                                else
                                {
                                    TestLogger?.WriteLine("Note: child process application does not redirect output because _remoteApplication.CaptureStandardOutput = false. HostedWebCore validation cannot take place without the standard output. This is common for non-web and self-hosted applications.");
                                }

                                RemoteApplication.WaitForExit();

                                appIsExercisedNormally = RemoteApplication.ExitCode == 0;

                                TestLogger?.WriteLine($"Remote application exited with a {(appIsExercisedNormally ? "success" : "failure")} exit code of {RemoteApplication.ExitCode}.");
                            }
                            else
                            {
                                TestLogger?.WriteLine("Note: Due to DelayKill being used, no process output or agent log validation was performed to verify that the application started and ran successfully.");
                            }

                            if (!appIsExercisedNormally && DelayKill)
                            {
                                RemoteApplication.Kill();

                                if (captureStandardOutput)
                                {
                                    RemoteApplication.CapturedOutput.WriteProcessOutputToLog("[RemoteApplicationFixture]: Initialize");
                                }

                                RemoteApplication.WaitForExit();
                            }

                            Thread.Sleep(1000);

                            numberOfTries++;
                        }
                    } while (!appIsExercisedNormally && numberOfTries < MaxTries);

                    if (!appIsExercisedNormally)
                    {
                        TestLogger?.WriteLine($"Test App wasn't exercised normally after {MaxTries} tries.");
                        throw new Exception($"Test App wasn't exercised normally after {MaxTries} tries.");
                    }
                }
                finally
                {
                    TestLogger?.WriteLine("===== Begin Agent log file =====");
                    try
                    {
                        TestLogger?.WriteLine(AgentLog.GetFullLogAsString());
                    }
                    catch (Exception)
                    {
                        TestLogger?.WriteLine("No log file found.");
                    }
                    TestLogger?.WriteLine("----- End of Agent log file -----");
                }
            }
        }
Example #2
0
        public virtual void Initialize()
        {
            lock (_initializeLock)
            {
                if (_initialized)
                {
                    return;
                }

                _initialized = true;

                TestLogger?.WriteLine(RemoteServiceFixtures.RemoteApplication.AppName);


                var numberOfTries = 0;

                try
                {
                    var retryTest = false;
                    var exceptionInExerciseApplication = false;
                    var applicationHadNonZeroExitCode  = false;


                    do
                    {
                        TestLogger?.WriteLine("Test Home" + RemoteApplication.DestinationNewRelicHomeDirectoryPath);

                        // reset these for each loop iteration
                        exceptionInExerciseApplication = false;
                        applicationHadNonZeroExitCode  = false;
                        retryTest = false;

                        RemoteApplication.TestLogger = new XUnitTestLogger(TestLogger);

                        RemoteApplication.DeleteWorkingSpace();

                        RemoteApplication.CopyToRemote();

                        SetupConfiguration();

                        var captureStandardOutput = RemoteApplication.CaptureStandardOutput;

                        RemoteApplication.Start(CommandLineArguments, captureStandardOutput);

                        try
                        {
                            ExerciseApplication();
                        }
                        catch (Exception ex)
                        {
                            exceptionInExerciseApplication = true;
                            TestLogger?.WriteLine("Exception occurred in try number " + (numberOfTries + 1) + " : " + ex.ToString());
                        }
                        finally
                        {
                            ShutdownRemoteApplication();

                            if (captureStandardOutput)
                            {
                                RemoteApplication.CapturedOutput.WriteProcessOutputToLog("RemoteApplication:");

                                // Most of our tests run in HostedWebCore, but some don't, e.g. the self-hosted
                                // WCF tests. For the HWC tests we carefully validate the console output in order
                                // to detect process-level failures that may cause test flickers. For the self-
                                // hosted tests, unfortunately, we just punt that.
                                if (RemoteApplication.ValidateHostedWebCoreOutput)
                                {
                                    SubprocessLogValidator.ValidateHostedWebCoreConsoleOutput(RemoteApplication.CapturedOutput.StandardOutput, TestLogger);
                                }
                                else
                                {
                                    TestLogger?.WriteLine("Note: child process is not required for log validation because _remoteApplication.ValidateHostedWebCoreOutput = false");
                                }
                            }
                            else
                            {
                                TestLogger?.WriteLine("Note: child process application does not redirect output because _remoteApplication.CaptureStandardOutput = false. HostedWebCore validation cannot take place without the standard output. This is common for non-web and self-hosted applications.");
                            }

                            RemoteApplication.WaitForExit();

                            applicationHadNonZeroExitCode = RemoteApplication.ExitCode != 0;

                            TestLogger?.WriteLine($"Remote application exited with a {(applicationHadNonZeroExitCode ? "failure" : "success")} exit code of {RemoteApplication.ExitCode}.");

                            retryTest = exceptionInExerciseApplication || applicationHadNonZeroExitCode;

                            if (retryTest)
                            {
                                var message = $"Retrying test. Exception caught when exercising test app = {exceptionInExerciseApplication}, application had non-zero exit code = {applicationHadNonZeroExitCode}.";
                                TestLogger?.WriteLine(message);
                                Thread.Sleep(1000);
                                numberOfTries++;
                            }
                        }
                    } while (retryTest && numberOfTries < MaxTries);

                    if (retryTest)
                    {
                        var message = ($"Test failed after {MaxTries} tries.");
                        TestLogger?.WriteLine(message);
                        throw new Exception(message);
                    }
                }

                finally
                {
                    TestLogger?.WriteLine("===== Begin Agent log file =====");
                    try
                    {
                        TestLogger?.WriteLine(AgentLog.GetFullLogAsString());
                    }
                    catch (Exception)
                    {
                        TestLogger?.WriteLine("No log file found.");
                    }
                    TestLogger?.WriteLine("----- End of Agent log file -----");
                }
            }
        }