Skip to content

Commit

Permalink
handle case when directory creation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Feb 20, 2024
1 parent 10083e6 commit d506fa6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
25 changes: 25 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/EasyLoggingIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,30 @@ public void TestFailToEnableEasyLoggingWhenConfigHasWrongPermissions()
Assert.IsFalse(EasyLoggerManager.HasEasyLoggingAppender());
}
}

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

// arrange
var configFilePath = CreateConfigTempFile(s_workingDirectory, Config("WARN", "/"));
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.That(thrown.InnerException.Message, Does.Contain("Failed to create logs directory"));
Assert.IsFalse(EasyLoggerManager.HasEasyLoggingAppender());
}

}
}
}
23 changes: 23 additions & 0 deletions Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ public void TestThatDoesNotFailWhenLogDirectoryPermissionIsNot700()
FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR), Times.Never);
}

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

// arrange
t_easyLoggingProvider
.Setup(provider => provider.ProvideConfig(ConfigPath))
.Returns(s_configWithErrorLevel);
t_unixOperations
.Setup(u => u.CreateDirectoryWithPermissions(s_expectedLogPath, FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR))
.Returns((int)Errno.EPERM);

// act
var thrown = Assert.Throws<Exception>(() => t_easyLoggerStarter.Init(ConfigPath));

// assert
Assert.That(thrown.Message, Does.Contain("Failed to create logs directory"));
}

[Test]
public void TestThatConfiguresEasyLoggingOnlyOnceWhenInitializedWithConfigPath()
{
Expand Down
7 changes: 6 additions & 1 deletion Snowflake.Data/Core/Session/EasyLoggingStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ private string GetLogPath(string logPath)
{
Directory.CreateDirectory(logPathOrDefault);
}
_unixOperations.CreateDirectoryWithPermissions(pathWithDotnetSubdirectory,
var createDirResult = _unixOperations.CreateDirectoryWithPermissions(pathWithDotnetSubdirectory,
FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR);
if (createDirResult != 0)
{
s_logger.Error($"Failed to create logs directory: {pathWithDotnetSubdirectory}");
throw new Exception("Failed to create logs directory");
}
}
}
CheckDirPermissionsOnlyAllowUser(pathWithDotnetSubdirectory);
Expand Down
4 changes: 2 additions & 2 deletions Snowflake.Data/Core/Tools/UnixOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ internal class UnixOperations
{
public static readonly UnixOperations Instance = new UnixOperations();

public virtual void CreateDirectoryWithPermissions(string path, FilePermissions permissions)
public virtual int CreateDirectoryWithPermissions(string path, FilePermissions permissions)
{
Syscall.mkdir(path, permissions);
return Syscall.mkdir(path, permissions);
}

public virtual FileAccessPermissions GetDirPermissions(string path)
Expand Down

0 comments on commit d506fa6

Please sign in to comment.