-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-990111 Easy Logging more tests and permission check fix
- Loading branch information
1 parent
80bfb2c
commit 291b49d
Showing
7 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
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 | ||
{ | ||
[OneTimeSetUp] | ||
public static void BeforeAll() | ||
{ | ||
if (!Directory.Exists(WorkingDirectory)) | ||
{ | ||
Directory.CreateDirectory(WorkingDirectory); | ||
} | ||
} | ||
|
||
[OneTimeTearDown] | ||
public static void AfterAll() | ||
{ | ||
Directory.Delete(WorkingDirectory, true); | ||
} | ||
|
||
[TearDown] | ||
public static void AfterEach() | ||
{ | ||
EasyLoggingStarter.Instance.Reset(EasyLoggingLogLevel.Warn); | ||
} | ||
|
||
[Test] | ||
public void TestEnableEasyLogging() | ||
{ | ||
// arrange | ||
var configFilePath = CreateConfigTempFile(Config("WARN", 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("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(Config("WARN", 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()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using Mono.Unix; | ||
using Mono.Unix.Native; | ||
using NUnit.Framework; | ||
using Snowflake.Data.Core.Tools; | ||
using static Snowflake.Data.Tests.UnitTests.Configuration.EasyLoggingConfigGenerator; | ||
|
||
namespace Snowflake.Data.Tests.Tools | ||
{ | ||
[TestFixture, NonParallelizable] | ||
public class UnixOperationsTest | ||
{ | ||
private static UnixOperations s_unixOperations; | ||
|
||
[OneTimeSetUp] | ||
public static void BeforeAll() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
return; | ||
if (!Directory.Exists(WorkingDirectory)) | ||
{ | ||
Directory.CreateDirectory(WorkingDirectory); | ||
} | ||
s_unixOperations = new UnixOperations(); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public static void AfterAll() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
return; | ||
Directory.Delete(WorkingDirectory, true); | ||
} | ||
|
||
[Test] | ||
public void TestDetectGroupOrOthersWritablePermissions( | ||
[ValueSource(nameof(GroupOrOthersWritablePermissions))] FilePermissions groupOrOthersWritablePermissions, | ||
[ValueSource(nameof(GroupNotWritablePermissions))] FilePermissions groupNotWritablePermissions, | ||
[ValueSource(nameof(OtherNotWritablePermissions))] FilePermissions otherNotWritablePermissions) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
Assert.Ignore("skip test on Windows"); | ||
} | ||
|
||
// arrange | ||
var filePath = CreateConfigTempFile("random text"); | ||
var readWriteUserPermissions = FilePermissions.S_IRUSR | FilePermissions.S_IWUSR; | ||
var filePermissions = readWriteUserPermissions | groupOrOthersWritablePermissions | groupNotWritablePermissions | otherNotWritablePermissions; | ||
Syscall.chmod(filePath, filePermissions); | ||
|
||
// act | ||
var result = s_unixOperations.CheckFileHasAnyOfPermissions(filePath, FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite); | ||
|
||
// assert | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[Test] | ||
public void TestDetectGroupOrOthersNotWritablePermissions( | ||
[ValueSource(nameof(UserPermissions))] FilePermissions userPermissions, | ||
[ValueSource(nameof(GroupNotWritablePermissions))] FilePermissions groupNotWritablePermissions, | ||
[ValueSource(nameof(OtherNotWritablePermissions))] FilePermissions otherNotWritablePermissions) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
Assert.Ignore("skip test on Windows"); | ||
} | ||
|
||
var filePath = CreateConfigTempFile("random text"); | ||
var filePermissions = userPermissions | groupNotWritablePermissions | otherNotWritablePermissions; | ||
Syscall.chmod(filePath, filePermissions); | ||
|
||
// act | ||
var result = s_unixOperations.CheckFileHasAnyOfPermissions(filePath, FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite); | ||
|
||
// assert | ||
Assert.IsFalse(result); | ||
} | ||
|
||
|
||
public static IEnumerable<FilePermissions> UserPermissions() | ||
{ | ||
yield return FilePermissions.S_IRUSR; | ||
yield return FilePermissions.S_IWUSR; | ||
yield return FilePermissions.S_IXUSR; | ||
yield return FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR; | ||
} | ||
|
||
public static IEnumerable<FilePermissions> GroupOrOthersWritablePermissions() | ||
{ | ||
yield return FilePermissions.S_IWGRP; | ||
yield return FilePermissions.S_IWOTH; | ||
yield return FilePermissions.S_IWGRP | FilePermissions.S_IWOTH; | ||
} | ||
|
||
public static IEnumerable<FilePermissions> GroupNotWritablePermissions() | ||
{ | ||
yield return 0; | ||
yield return FilePermissions.S_IRGRP; | ||
yield return FilePermissions.S_IXGRP; | ||
yield return FilePermissions.S_IRGRP | FilePermissions.S_IXGRP; | ||
} | ||
|
||
public static IEnumerable<FilePermissions> OtherNotWritablePermissions() | ||
{ | ||
yield return 0; | ||
yield return FilePermissions.S_IROTH; | ||
yield return FilePermissions.S_IXOTH; | ||
yield return FilePermissions.S_IROTH | FilePermissions.S_IXOTH; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters