Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…nector-net into SNOW-916949-Okta-429-Retry
  • Loading branch information
sfc-gh-ext-simba-lf committed Feb 9, 2024
2 parents c668419 + 9da3d62 commit 7e1060c
Show file tree
Hide file tree
Showing 16 changed files with 951 additions and 67 deletions.
99 changes: 99 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/EasyLoggingIT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
using Mono.Unix.Native;
using NUnit.Framework;
using Snowflake.Data.Client;
using Snowflake.Data.Configuration;
using Snowflake.Data.Core;
using Snowflake.Data.Log;
using static Snowflake.Data.Tests.UnitTests.Configuration.EasyLoggingConfigGenerator;

namespace Snowflake.Data.Tests.IntegrationTests
{
[TestFixture, NonParallelizable]
public class EasyLoggingIT: SFBaseTest
{
private static readonly string s_workingDirectory = Path.Combine(Path.GetTempPath(), "easy_logging_test_configs_", Path.GetRandomFileName());

[OneTimeSetUp]
public static void BeforeAll()
{
if (!Directory.Exists(s_workingDirectory))
{
Directory.CreateDirectory(s_workingDirectory);
}
}

[OneTimeTearDown]
public static void AfterAll()
{
Directory.Delete(s_workingDirectory, true);
}

[TearDown]
public static void AfterEach()
{
EasyLoggingStarter.Instance.Reset(EasyLoggingLogLevel.Warn);
}

[Test]
public void TestEnableEasyLogging()
{
// arrange
var configFilePath = CreateConfigTempFile(s_workingDirectory, Config("WARN", s_workingDirectory));
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = ConnectionString + $"CLIENT_CONFIG_FILE={configFilePath}";

// act
conn.Open();

// assert
Assert.IsTrue(EasyLoggerManager.HasEasyLoggingAppender());
}
}

[Test]
public void TestFailToEnableEasyLoggingForWrongConfiguration()
{
// arrange
var configFilePath = CreateConfigTempFile(s_workingDirectory, "random config content");
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = ConnectionString + $"CLIENT_CONFIG_FILE={configFilePath}";

// act
var thrown = Assert.Throws<SnowflakeDbException>(() => conn.Open());

// assert
Assert.That(thrown.Message, Does.Contain("Connection string is invalid: Unable to connect"));
Assert.IsFalse(EasyLoggerManager.HasEasyLoggingAppender());
}
}

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

// arrange
var configFilePath = CreateConfigTempFile(s_workingDirectory, Config("WARN", s_workingDirectory));
Syscall.chmod(configFilePath, FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IWGRP);
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = ConnectionString + $"CLIENT_CONFIG_FILE={configFilePath}";

// act
var thrown = Assert.Throws<SnowflakeDbException>(() => conn.Open());

// assert
Assert.That(thrown.Message, Does.Contain("Connection string is invalid: Unable to connect"));
Assert.IsFalse(EasyLoggerManager.HasEasyLoggingAppender());
}
}
}
}
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,16 @@ 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<EnvironmentOperations> t_environmentOperations;

Expand All @@ -34,8 +39,9 @@ public class EasyLoggingConfigFinderTest
public void Setup()
{
t_fileOperations = new Mock<FileOperations>();
t_unixOperations = new Mock<UnixOperations>();
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_environmentOperations.Object);
MockHomeDirectory();
}

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

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

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

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

// act
var filePath = t_finder.FindConfigFilePath(null);
Expand All @@ -104,16 +106,36 @@ 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}");
}

[Test]
Expand All @@ -139,13 +161,28 @@ 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();
MockFileOnHomePathDoesNotExist();

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

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

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

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

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 +247,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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ public class EasyLoggingConfigGenerator
public const string EmptyCommonConfig = @"{
""common"": {}
}";

public static readonly string WorkingDirectory = Path.Combine(Path.GetTempPath(), "easy_logging_test_configs_", Path.GetRandomFileName());

public static string CreateConfigTempFile(string fileContent)

public static string CreateConfigTempFile(string workingDirectory, string fileContent)
{
var filePath = NewConfigFilePath();
var filePath = NewConfigFilePath(workingDirectory);
using (var writer = File.CreateText(filePath))
{
writer.Write(fileContent);
Expand All @@ -26,9 +24,9 @@ public static string CreateConfigTempFile(string fileContent)
return filePath;
}

private static string NewConfigFilePath()
private static string NewConfigFilePath(string workingDirectory)
{
return Path.Combine(WorkingDirectory, Path.GetRandomFileName());
return Path.Combine(workingDirectory, Path.GetRandomFileName());
}

public static string Config(string logLevel, string logPath)
Expand All @@ -40,7 +38,8 @@ public static string Config(string logLevel, string logPath)
}
}"
.Replace("{logLevel}", SerializeParameter(logLevel))
.Replace("{logPath}", SerializeParameter(logPath));
.Replace("{logPath}", SerializeParameter(logPath))
.Replace("\\", "\\\\");
}

private static string SerializeParameter(string parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@ public class EasyLoggingConfigParserTest
private const string NotExistingFilePath = "../../../Resources/EasyLogging/not_existing_config.json";
private const string LogLevel = "info";
private const string LogPath = "./test-logs/log_file.log";
private static readonly string s_workingDirectory = Path.Combine(Path.GetTempPath(), "easy_logging_test_configs_", Path.GetRandomFileName());

[OneTimeSetUp]
public static void BeforeAll()
{
if (!Directory.Exists(WorkingDirectory))
if (!Directory.Exists(s_workingDirectory))
{
Directory.CreateDirectory(WorkingDirectory);
Directory.CreateDirectory(s_workingDirectory);
}
}

[OneTimeTearDown]
public static void AfterAll()
{
Directory.Delete(WorkingDirectory, true);
Directory.Delete(s_workingDirectory, true);
}

[Test]
public void TestThatParsesConfigFile()
{
// arrange
var parser = new EasyLoggingConfigParser();
var configFilePath = CreateConfigTempFile(Config(LogLevel, LogPath));
var configFilePath = CreateConfigTempFile(s_workingDirectory, Config(LogLevel, LogPath));

// act
var config = parser.Parse(configFilePath);
Expand Down Expand Up @@ -113,8 +114,8 @@ public static IEnumerable<string> ConfigFilesWithoutValues()
BeforeAll();
return new[]
{
CreateConfigTempFile(EmptyCommonConfig),
CreateConfigTempFile(Config(null, null))
CreateConfigTempFile(s_workingDirectory, EmptyCommonConfig),
CreateConfigTempFile(s_workingDirectory, Config(null, null))
};
}

Expand All @@ -123,8 +124,8 @@ public static IEnumerable<string> WrongConfigFiles()
BeforeAll();
return new[]
{
CreateConfigTempFile(EmptyConfig),
CreateConfigTempFile(Config("unknown", LogPath)),
CreateConfigTempFile(s_workingDirectory, EmptyConfig),
CreateConfigTempFile(s_workingDirectory, Config("unknown", LogPath)),
};
}
}
Expand Down
Loading

0 comments on commit 7e1060c

Please sign in to comment.