Skip to content

Commit

Permalink
SNOW-990111: Add easy logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Jan 16, 2024
1 parent e592ce4 commit 55b0694
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
19 changes: 0 additions & 19 deletions Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
36 changes: 34 additions & 2 deletions Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.IO;
using System.Runtime.InteropServices;
using Moq;
using NUnit.Framework;
using Snowflake.Data.Configuration;
Expand Down Expand Up @@ -39,7 +40,7 @@ public class EasyLoggingStarterTest
LogPath = LogPath
}
};

[ThreadStatic]
private static Mock<EasyLoggingConfigProvider> t_easyLoggingProvider;

Expand All @@ -60,7 +61,38 @@ public void BeforeEach()
t_directoryOperations = new Mock<DirectoryOperations>();
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()
{
Expand Down
11 changes: 6 additions & 5 deletions Snowflake.Data/Core/Session/EasyLoggingStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");

Check warning on line 128 in Snowflake.Data/Core/Session/EasyLoggingStarter.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Session/EasyLoggingStarter.cs#L127-L128

Added lines #L127 - L128 were not covered by tests
}
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}");
}
}
}

Check warning on line 136 in Snowflake.Data/Core/Session/EasyLoggingStarter.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Session/EasyLoggingStarter.cs#L131-L136

Added lines #L131 - L136 were not covered by tests
}
Expand Down

0 comments on commit 55b0694

Please sign in to comment.