Adding back test for windows event log (#17821)
authorSung Yoon Whang <suwhang@microsoft.com>
Mon, 30 Apr 2018 23:16:45 +0000 (16:16 -0700)
committerGitHub <noreply@github.com>
Mon, 30 Apr 2018 23:16:45 +0000 (16:16 -0700)
* update test dependencies

* add test

* Allow more leeway for time

* modify test per PR comments

tests/src/Common/test_dependencies/test_dependencies.csproj
tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.cs [new file with mode: 0644]
tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.csproj [new file with mode: 0644]

index 8945355..6337491 100644 (file)
@@ -25,6 +25,9 @@
     <PackageReference Include="System.Runtime.Intrinsics.Experimental">
       <Version>$(MicrosoftPrivateCoreFxNETCoreAppPackageVersion)</Version>
     </PackageReference>
+    <PackageReference Include="System.Diagnostics.EventLog">
+      <Version>$(MicrosoftPrivateCoreFxNETCoreAppPackageVersion)</Version>
+    </PackageReference>
   </ItemGroup>
   <PropertyGroup>
     <TargetFramework>netcoreapp2.1</TargetFramework>
diff --git a/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.cs b/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.cs
new file mode 100644 (file)
index 0000000..d299cda
--- /dev/null
@@ -0,0 +1,154 @@
+using System;
+using System.Linq;
+using System.Diagnostics;
+using System.IO;
+using System.Threading;
+
+class Program
+{
+    private static void UnhandledException(string msg)
+    {
+        A(msg);
+    }
+
+    private static void FailFast(string msg)
+    {
+        try
+        {
+            A(msg);
+        }
+        catch (ArgumentException ex)
+        {
+            Environment.FailFast(msg, ex);
+        }
+    }
+
+    private static void A(string msg)
+    {
+        B(msg);
+    }
+
+    private static void B(string msg)
+    {
+        throw new ArgumentException(msg);
+    }
+
+    private static string RandomCookie()
+    {
+        // Generate a random cookie to be used as a message to be passed as exception parameter
+        Random random = new Random();
+        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+        return new string(Enumerable.Repeat(chars, 10)
+            .Select(s => s[random.Next(s.Length)]).ToArray());
+    }
+
+    private static bool LaunchTest(string testName, string[] logEntriesToCheck, string randomCookie)
+    {
+        EventLog logBefore = new EventLog("Application");
+        int logBeforeCount = logBefore.Entries.Count;
+
+        DateTime dt = DateTime.Now;
+
+        Process testProcess = new Process();
+        string currentPath = Directory.GetCurrentDirectory();
+        string corerunPath = Environment.GetEnvironmentVariable("CORE_ROOT") + "\\corerun.exe";
+
+        testProcess.StartInfo.FileName = corerunPath;
+        testProcess.StartInfo.Arguments = currentPath + "\\WindowsEventLog.exe " + testName + " " + randomCookie;
+        testProcess.StartInfo.EnvironmentVariables["CORE_ROOT"] = Environment.GetEnvironmentVariable("CORE_ROOT");
+        testProcess.StartInfo.UseShellExecute = false;
+
+        testProcess.Start();
+        testProcess.WaitForExit();
+
+        Thread.Sleep(2000);
+
+        EventLog logAfter = new EventLog("Application");
+
+        Console.WriteLine("Found {0} entries in Event Log", logAfter.Entries.Count);
+
+        foreach (EventLogEntry entry in logAfter.Entries)
+        {
+            int checkCount = 0;
+            if (entry.TimeGenerated > dt.AddMinutes(-3))  // Grant some leeway in the time error was logged
+            {
+                String source = entry.Source;
+                String message = entry.Message;
+
+                foreach (string logEntry in logEntriesToCheck)
+                {
+                    Console.WriteLine("Checking for existence of : " + logEntry);
+                    if (message.Contains(logEntry))
+                        checkCount += 1;
+                    else
+                        Console.WriteLine("Couldn't find it in: " + message);
+                }
+
+                if (source.Contains(".NET Runtime") && checkCount == logEntriesToCheck.Length && logBeforeCount < logAfter.Entries.Count)
+                {
+                    return true;
+                }
+                else if (source.Contains(".NET Runtime"))
+                {
+                    Console.WriteLine("***      Event Log       ***");
+                    Console.WriteLine(message);
+                }
+            }
+        }
+        return false;
+    }
+
+    private static bool RunUnhandledExceptionTest()
+    {
+        string cookie = RandomCookie();
+        string[] logEntriesToCheck = { "unhandled exception", "ArgumentException", cookie };
+        return LaunchTest("UnhandledException", logEntriesToCheck, cookie);
+    }
+
+    private static bool RunFailFastTest()
+    {
+        string cookie = RandomCookie();
+        string[] logEntriesToCheck = { "The application requested process termination through System.Environment.FailFast(string message).", "ArgumentException", cookie };
+        return LaunchTest("FailFast", logEntriesToCheck, cookie);
+    }
+
+
+    public static int Main(string[] args)
+    {
+        if (args.Length == 0) // When invoked with no args, launch itself with appropriate args to cause various exceptions
+        {
+            if (!System.Runtime.InteropServices.RuntimeInformation.OSDescription.Contains("Windows"))
+            {
+                Console.WriteLine("WindowsEventLog Test: Passing on all non-Windows platform");
+                return 100;
+            }
+
+            if (!RunUnhandledExceptionTest())
+            {
+                Console.WriteLine("WindowsEventLog Test: UnhandledExceptionTest failed.");
+                return 1;
+            }
+
+            if (!RunFailFastTest())
+            {
+                Console.WriteLine("WindowsEventLog Test: FailFastTest failed.");
+                return 1;
+            }
+
+            return 100;
+        }
+
+        Debug.Assert(args.Length == 2);
+
+        if (args[0] == "UnhandledException")
+        {
+            UnhandledException(args[1]);
+        }
+        else if (args[0] == "FailFast")
+        {
+            FailFast(args[1]);
+        }
+
+        return 100; // Should never reach here 
+    }
+}
diff --git a/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.csproj b/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.csproj
new file mode 100644 (file)
index 0000000..8c9a1b8
--- /dev/null
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>WindowsEventLog</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
+  </PropertyGroup>
+  <PropertyGroup>
+    <DisableProjectBuild Condition="'$(TargetsUnix)' == 'true'">true</DisableProjectBuild>
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="WindowsEventLog.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>