Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-990111: Add easy logging improvements #849

Merged
merged 47 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
5bd6504
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 13, 2024
3cfcc69
Merge branch 'master' of https://github.com/snowflakedb/snowflake-con…
sfc-gh-ext-simba-lf Jan 13, 2024
e0dbec7
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 15, 2024
7da9e4f
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 15, 2024
8a9e74a
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
e592ce4
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
55b0694
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
387ab55
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
9383efc
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
dcf951e
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
2e7c648
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
1d208cf
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
b2d3c10
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
98b716d
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
7646360
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
5911f7a
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
9d2596c
SNOW-990111: Add easy logging improvements
sfc-gh-ext-simba-lf Jan 16, 2024
8d31672
SNOW-990111: Refactor easy logging improvements
sfc-gh-ext-simba-lf Jan 17, 2024
e185fbd
SNOW-990111: Refactor easy logging improvements
sfc-gh-ext-simba-lf Jan 17, 2024
bcd0936
SNOW-990111: Refactor easy logging improvements
sfc-gh-ext-simba-lf Jan 19, 2024
08e3785
SNOW-990111: Refactor easy logging improvements
sfc-gh-ext-simba-lf Jan 19, 2024
e40116a
SNOW-990111: Refactor easy logging changes
sfc-gh-ext-simba-lf Jan 23, 2024
47d07bf
SNOW-990111: Refactor easy logging changes
sfc-gh-ext-simba-lf Jan 23, 2024
1c43705
SNOW-990111: Refactor easy logging changes
sfc-gh-ext-simba-lf Jan 23, 2024
eff9425
SNOW-990111: Add test for EasyLoggerUtil
sfc-gh-ext-simba-lf Jan 24, 2024
f708021
SNOW-990111: Modify config finder tests
sfc-gh-ext-simba-lf Jan 24, 2024
8837fe7
SNOW-990111: Modify config finder tests
sfc-gh-ext-simba-lf Jan 24, 2024
5db9b0b
Merge branch 'master' of https://github.com/snowflakedb/snowflake-con…
sfc-gh-ext-simba-lf Jan 24, 2024
16a9979
SNOW-990111: Modify config finder tests
sfc-gh-ext-simba-lf Jan 24, 2024
190308d
SNOW-990111: Modify config finder tests
sfc-gh-ext-simba-lf Jan 24, 2024
49832a8
SNOW-990111: Modify config finder tests
sfc-gh-ext-simba-lf Jan 24, 2024
e17dcf3
SNOW-990111: Modify config finder tests
sfc-gh-ext-simba-lf Jan 24, 2024
ea8d52e
SNOW-990111: Modify config finder tests
sfc-gh-ext-simba-lf Jan 24, 2024
3fabd05
SNOW-990111: Remove umask test and create dotnet subfolder with Mono.…
sfc-gh-ext-simba-lf Jan 25, 2024
5635772
SNOW-990111: Rename EasyLoggingUtil and edit EasyLoggerManagerTest
sfc-gh-ext-simba-lf Jan 25, 2024
31fefeb
SNOW-990111: Update EasyLoggingStarterTest
sfc-gh-ext-simba-lf Jan 25, 2024
bb70c74
SNOW-990111: Refactor tests
sfc-gh-ext-simba-lf Jan 25, 2024
9f2c874
SNOW-990111: Refactor hash set to list
sfc-gh-ext-simba-lf Jan 26, 2024
16335ab
SNOW-990111: Modify check to allow permissions without user write
sfc-gh-ext-simba-lf Jan 26, 2024
0f266fc
SNOW-990111: Refactor getting home directory
sfc-gh-ext-simba-lf Jan 26, 2024
a5be03f
Refactor UnixOperations and clean up
sfc-gh-ext-simba-lf Jan 30, 2024
6524998
Refactor UnixOperations and clean up
sfc-gh-ext-simba-lf Jan 30, 2024
59945da
Refactor checking directory permissions
sfc-gh-ext-simba-lf Jan 30, 2024
17e9efe
Revert log directory check, remove unused variable, and refactor perm…
sfc-gh-ext-simba-lf Jan 31, 2024
065941f
Remove unused variable
sfc-gh-ext-simba-lf Jan 31, 2024
1c0babc
SNOW-990111-: Modify warning message
sfc-gh-ext-simba-lf Feb 1, 2024
dd824e6
SNOW-990111: Move where logging is executed
sfc-gh-ext-simba-lf Feb 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using System;
using System.IO;
using System.Runtime.InteropServices;
using Mono.Unix;
using Moq;
using NUnit.Framework;
using Snowflake.Data.Configuration;
Expand All @@ -17,13 +19,19 @@ public class EasyLoggingConfigFinderTest
private const string InputConfigFilePath = "input_config.json";
private const string EnvironmentalConfigFilePath = "environmental_config.json";
private const string HomeDirectory = "/home/user";
private static readonly string s_driverConfigFilePath = Path.Combine(".", EasyLoggingConfigFinder.ClientConfigFileName);
private const string DriverDirectory = ".";
private static readonly string s_driverConfigFilePath = Path.Combine(DriverDirectory, EasyLoggingConfigFinder.ClientConfigFileName);
private static readonly string s_homeConfigFilePath = Path.Combine(HomeDirectory, EasyLoggingConfigFinder.ClientConfigFileName);
private static readonly string s_tempConfigFilePath = Path.Combine(Path.GetTempPath(), EasyLoggingConfigFinder.ClientConfigFileName);

[ThreadStatic]
private static Mock<FileOperations> t_fileOperations;

[ThreadStatic]
private static Mock<UnixOperations> t_unixOperations;

[ThreadStatic]
private static Mock<DirectoryOperations> t_directoryOperations;

[ThreadStatic]
private static Mock<EnvironmentOperations> t_environmentOperations;

Expand All @@ -34,8 +42,11 @@ public class EasyLoggingConfigFinderTest
public void Setup()
{
t_fileOperations = new Mock<FileOperations>();
t_unixOperations = new Mock<UnixOperations>();
t_directoryOperations = new Mock<DirectoryOperations>();
t_environmentOperations = new Mock<EnvironmentOperations>();
t_finder = new EasyLoggingConfigFinder(t_fileOperations.Object, t_environmentOperations.Object);
t_finder = new EasyLoggingConfigFinder(t_fileOperations.Object, t_unixOperations.Object, t_directoryOperations.Object, t_environmentOperations.Object);
MockDirectoriesExist();
MockHomeDirectory();
}

Expand All @@ -46,7 +57,6 @@ public void TestThatTakesFilePathFromTheInput()
MockFileFromEnvironmentalVariable();
MockFileOnDriverPath();
MockFileOnHomePath();
MockFileOnTempPath();

// act
var filePath = t_finder.FindConfigFilePath(InputConfigFilePath);
Expand All @@ -65,7 +75,6 @@ public void TestThatTakesFilePathFromEnvironmentVariableIfInputNotPresent(
MockFileFromEnvironmentalVariable();
MockFileOnDriverPath();
MockFileOnHomePath();
MockFileOnTempPath();

// act
var filePath = t_finder.FindConfigFilePath(inputFilePath);
Expand All @@ -80,7 +89,6 @@ public void TestThatTakesFilePathFromDriverLocationWhenNoInputParameterNorEnviro
// arrange
MockFileOnDriverPath();
MockFileOnHomePath();
MockFileOnTempPath();

// act
var filePath = t_finder.FindConfigFilePath(null);
Expand All @@ -94,7 +102,6 @@ public void TestThatTakesFilePathFromHomeLocationWhenNoInputParamEnvironmentVarN
{
// arrange
MockFileOnHomePath();
MockFileOnTempPath();

// act
var filePath = t_finder.FindConfigFilePath(null);
Expand All @@ -104,16 +111,37 @@ public void TestThatTakesFilePathFromHomeLocationWhenNoInputParamEnvironmentVarN
}

[Test]
public void TestThatTakesFilePathFromTempDirectoryWhenNoOtherWaysPossible()
public void TestThatTakesFilePathFromHomeDirectoryWhenNoOtherWaysPossible()
{
// arrange
MockFileOnTempPath();
MockFileOnHomePath();

// act
var filePath = t_finder.FindConfigFilePath(null);


// assert
Assert.AreEqual(s_homeConfigFilePath, filePath);
}

[Test]
public void TestThatConfigFileIsNotUsedIfOthersCanModifyTheConfigFile()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Ignore("skip test on Windows");
}

// arrange
MockFileOnHomePath();
MockHasFlagReturnsTrue();

// act
var thrown = Assert.Throws<Exception>(() => t_finder.FindConfigFilePath(null));

// assert
Assert.AreEqual(s_tempConfigFilePath, filePath);
Assert.IsNotNull(thrown);
Assert.AreEqual(thrown.Message, $"Error due to other users having permission to modify the config file: {s_homeConfigFilePath}");
t_unixOperations.Verify(u => u.SetFileInfo(s_homeConfigFilePath), Times.Once);
}

[Test]
Expand All @@ -139,13 +167,29 @@ public void TestThatDoesNotFailWhenSearchForOneOfDirectoriesFails()
Assert.IsNull(filePath);
t_environmentOperations.Verify(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile), Times.Once);
}

[Test]
public void TestThatDoesNotFailWhenOneOfDirectoriesNotDefined()
public void TestThatDoesNotFailWhenHomeDirectoryReturnsNull()
{
// arrange
MockHomeDirectoryReturnsNull();

// act
var filePath = t_finder.FindConfigFilePath(null);

// assert
Assert.IsNull(filePath);
t_environmentOperations.Verify(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile), Times.Once);
}

[Test]
public void TestThatDoesNotFailWhenHomeDirectoryDoesNotExist()
{
// arrange
MockFileOnHomePath();
MockHomeDirectoryDoesNotExist();
MockFileOnHomePathDoesNotExist();

// act
var filePath = t_finder.FindConfigFilePath(null);

Expand All @@ -154,6 +198,22 @@ public void TestThatDoesNotFailWhenOneOfDirectoriesNotDefined()
t_environmentOperations.Verify(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile), Times.Once);
}

private static void MockDirectoriesExist()
{
t_directoryOperations
.Setup(d => d.Exists(
It.Is<string>(dir => dir.Equals(DriverDirectory) || dir.Equals(HomeDirectory))))
.Returns(true);
}

private static void MockHasFlagReturnsTrue()
{
t_unixOperations
.Setup(f => f.CheckFileHasPermissions(
It.Is<FileAccessPermissions>(p => p.Equals(FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite))))
.Returns(true);
}

private static void MockHomeDirectory()
{
t_environmentOperations
Expand All @@ -168,6 +228,20 @@ private static void MockHomeDirectoryFails()
.Throws(() => new Exception("No home directory"));
}

private static void MockHomeDirectoryDoesNotExist()
{
t_directoryOperations
.Setup(d => d.Exists(HomeDirectory))
.Returns(false);
}

private static void MockFileOnHomePathDoesNotExist()
{
t_fileOperations
.Setup(f => f.Exists(s_homeConfigFilePath))
.Returns(false);
}

private static void MockHomeDirectoryReturnsNull()
{
t_environmentOperations
Expand Down Expand Up @@ -195,12 +269,5 @@ private static void MockFileOnHomePath()
.Setup(f => f.Exists(s_homeConfigFilePath))
.Returns(true);
}

private static void MockFileOnTempPath()
{
t_fileOperations
.Setup(f => f.Exists(s_tempConfigFilePath))
.Returns(true);
}
}
}
35 changes: 34 additions & 1 deletion Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using NUnit.Framework;
using Snowflake.Data.Configuration;
using Snowflake.Data.Core;
Expand All @@ -21,7 +22,7 @@ public class EasyLoggerManagerTest
private const string WarnMessage = "Easy logging Warn message";
private const string ErrorMessage = "Easy logging Error message";
private const string FatalMessage = "Easy logging Fatal message";
private static readonly string s_logsDirectory = Path.GetTempPath();
private static readonly string s_logsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

[ThreadStatic]
private static string t_directoryLogPath;
Expand Down Expand Up @@ -92,6 +93,38 @@ public void TestThatLogsToProperFileWithProperLogLevelOnly()
Assert.That(logLines, Has.Exactly(1).Matches<string>(s => s.Contains(FatalMessage)));
}

[Test]
public void TestThatOnlyUnknownFieldsAreLogged()
{
// arrange
string expectedFakeLogField = "fake_log_field";
string ConfigWithUnknownFields = $@"{{
""common"": {{
""LOG_LEVEL"": ""warn"",
""lOg_PaTh"": ""path"",
""{expectedFakeLogField}_1"": ""abc"",
""{expectedFakeLogField}_2"": ""123""
}}
}}";
var configFilePath = Guid.NewGuid().ToString() + ".json";
using (var writer = File.CreateText(configFilePath))
{
writer.Write(ConfigWithUnknownFields);
}
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Warn, t_directoryLogPath);
var parser = new EasyLoggingConfigParser();

// act
parser.Parse(configFilePath);

// assert
var logLines = File.ReadLines(FindLogFilePath(t_directoryLogPath));
Assert.That(logLines, Has.Exactly(2).Matches<string>(s => s.Contains($"Unknown field from config: {expectedFakeLogField}")));

// cleanup
File.Delete(configFilePath);
}

private static string RandomLogsDirectoryPath()
{
var randomName = Path.GetRandomFileName();
Expand Down
Loading
Loading