From 55b0694228846e6140e22300a7437fdafb669630 Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf Date: Tue, 16 Jan 2024 00:09:03 -0800 Subject: [PATCH] SNOW-990111: Add easy logging improvements --- .../UnitTests/Logger/EasyLoggerManagerTest.cs | 19 ---------- .../Session/EasyLoggingStarterTest.cs | 36 +++++++++++++++++-- .../Core/Session/EasyLoggingStarter.cs | 11 +++--- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs b/Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs index db219dd09..d4b504a11 100644 --- a/Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs @@ -124,25 +124,6 @@ public void TestThatUnknownFieldsAreLogged() File.Delete(configFilePath); } - [Test] - [Ignore("This test requires manual interaction and therefore cannot be run in CI")] - public void TestThatDirectoryPermissionsFollowUmask() - { - // Note: To test with a different value than the default umask, it will have to be set before running this test - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - // arrange - EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Debug, t_directoryLogPath); - - // act - var umask = EasyLoggerUtil.AllPermissions - int.Parse(EasyLoggerUtil.CallBash("umask")); - var dirPermissions = EasyLoggerUtil.CallBash($"stat -c '%a' {t_directoryLogPath}"); - - // assert - Assert.IsTrue(umask >= int.Parse(dirPermissions)); - } - } - private static string RandomLogsDirectoryPath() { var randomName = Path.GetRandomFileName(); diff --git a/Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs b/Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs index 00a2ed9b7..de48a6385 100644 --- a/Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs @@ -4,6 +4,7 @@ using System; using System.IO; +using System.Runtime.InteropServices; using Moq; using NUnit.Framework; using Snowflake.Data.Configuration; @@ -39,7 +40,7 @@ public class EasyLoggingStarterTest LogPath = LogPath } }; - + [ThreadStatic] private static Mock t_easyLoggingProvider; @@ -60,7 +61,38 @@ public void BeforeEach() t_directoryOperations = new Mock(); t_easyLoggerStarter = new EasyLoggingStarter(t_easyLoggingProvider.Object, t_easyLoggerManager.Object, t_directoryOperations.Object); } - + + [Test] + //[Ignore("This test requires manual interaction and therefore cannot be run in CI")] + public void TestThatCreatedDirectoryPermissionsFollowUmask() + { + // Note: To test with a different value than the default umask, it will have to be set before running this test + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // arrange + t_easyLoggingProvider + .Setup(provider => provider.ProvideConfig(ConfigPath)) + .Returns(s_configWithInfoLevel); + t_directoryOperations + .Setup(provider => provider.Exists(ConfigPath)) + .Returns(Directory.Exists(ConfigPath)); + t_directoryOperations + .Setup(provider => provider.CreateDirectory(s_expectedLogPath)) + .Returns(Directory.CreateDirectory(s_expectedLogPath)); + + // act + t_easyLoggerStarter.Init(ConfigPath); + var umask = EasyLoggerUtil.AllPermissions - int.Parse(EasyLoggerUtil.CallBash("umask")); + var dirPermissions = EasyLoggerUtil.CallBash($"stat -c '%a' {s_expectedLogPath}"); + + // assert + Assert.IsTrue(umask >= int.Parse(dirPermissions)); + + // cleanup + Directory.Delete(s_expectedLogPath); + } + } + [Test] public void TestThatConfiguresEasyLoggingOnlyOnceWhenInitializedWithConfigPath() { diff --git a/Snowflake.Data/Core/Session/EasyLoggingStarter.cs b/Snowflake.Data/Core/Session/EasyLoggingStarter.cs index 4ae4e8699..0879e826b 100644 --- a/Snowflake.Data/Core/Session/EasyLoggingStarter.cs +++ b/Snowflake.Data/Core/Session/EasyLoggingStarter.cs @@ -126,11 +126,12 @@ private string GetLogPath(string logPath) if (isParsed && dirPermissions != EasyLoggerUtil.AllUserPermissions) { s_logger.Warn($"Access permission for the logs directory is {dirPermissions}"); - } - if (isParsed && dirPermissions != umask) - { - s_logger.Warn($"Setting access permission for the logs directory from {dirPermissions} to {EasyLoggerUtil.AllUserPermissions}"); - EasyLoggerUtil.CallBash($"chmod -R {EasyLoggerUtil.AllUserPermissions} {pathWithDotnetSubdirectory}"); + + if (dirPermissions != umask) + { + s_logger.Warn($"Setting access permission for the logs directory from {dirPermissions} to {EasyLoggerUtil.AllUserPermissions}"); + EasyLoggerUtil.CallBash($"chmod -R {EasyLoggerUtil.AllUserPermissions} {pathWithDotnetSubdirectory}"); + } } } }