Skip to content

Commit

Permalink
statics make unit tests harder than they need to be...
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Jun 22, 2023
1 parent dfec477 commit 337b48d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/Agent/NewRelic/Agent/Core/Logging/AuditLog.cs
Expand Up @@ -10,11 +10,22 @@ namespace NewRelic.Agent.Core.Logging
public static class AuditLog
{
// a lazy ILogger instance that injects an "Audit" property
private static Lazy<ILogger> _lazyAuditLogger = new Lazy<ILogger>(() =>
Serilog.Log.Logger.ForContext(LogLevelExtensions.AuditLevel, LogLevelExtensions.AuditLevel));
private static Lazy<ILogger> _lazyAuditLogger = LazyAuditLogger();

public static bool IsAuditLogEnabled { get; set; } //setter is public only for unit tests, not expected to be use anywhere else

// for unit tests only
public static void ResetLazyLogger()
{
_lazyAuditLogger = LazyAuditLogger();
}

private static Lazy<ILogger> LazyAuditLogger()
{
return new Lazy<ILogger>(() =>
Serilog.Log.Logger.ForContext(LogLevelExtensions.AuditLevel, LogLevelExtensions.AuditLevel));
}

/// <summary>
/// Logs <paramref name="message"/> at the AUDIT level. This log level should be used only as dictated by the security team to satisfy auditing requirements.
/// </summary>
Expand Down
Expand Up @@ -39,7 +39,6 @@ public void SetUp()

_mockILogger = Mock.Create<ILogger>();
Log.Logger = _mockILogger;

}

private HttpCollectorWire CreateHttpCollectorWire(Dictionary<string, string> requestHeadersMap = null)
Expand Down Expand Up @@ -314,12 +313,12 @@ public void SendData_ShouldDropPayload_WhenPayloadSizeExceedsMaxSize()
Assert.AreEqual(false, _mockHttpMessageHandler.SendAsyncInvoked);
}

[Test]
[TestCase(false)]
[TestCase(true)]
public void SendData_ShouldNotCallAuditLog_UnlessAuditLogIsEnabled(bool isEnabled)
{
// Arrange
AuditLog.ResetLazyLogger();
Mock.Arrange(() => _configuration.AgentLicenseKey).Returns("license_key");
Mock.Arrange(() => _configuration.CollectorMaxPayloadSizeInBytes).Returns(1024);

Expand All @@ -341,7 +340,7 @@ public void SendData_ShouldNotCallAuditLog_UnlessAuditLogIsEnabled(bool isEnable
AuditLog.IsAuditLogEnabled = isEnabled;

// Act
var response = collectorWire.SendData("test_method", connectionInfo, serializedData, Guid.NewGuid());
var _ = collectorWire.SendData("test_method", connectionInfo, serializedData, Guid.NewGuid());

// Assert
Mock.Assert(() => mockForContextLogger.Fatal(Arg.AnyString), isEnabled ? Occurs.Exactly(3) : Occurs.Never());
Expand Down
10 changes: 8 additions & 2 deletions tests/Agent/UnitTests/Core.UnitTest/Logging/AuditLogTests.cs
Expand Up @@ -20,9 +20,16 @@ public void SetUp()
Log.Logger = _mockILogger;

// reset state for each test
AuditLog.ResetLazyLogger();
AuditLog.IsAuditLogEnabled = false;
}

[TearDown]
public void TearDown()
{
AuditLog.ResetLazyLogger();
}

[Test]
public void IncludeOnlyAuditLog_EnablesAuditLog()
{
Expand All @@ -43,9 +50,8 @@ public void ExcludeAuditLog_DisablesAuditLog()
Assert.False(AuditLog.IsAuditLogEnabled);
}

[Test]
[TestCase(false)]
[TestCase(true)]
[TestCase(false)]
public void Log_OnlyLogsWhenAuditLogEnabled(bool logEnabled)
{
var mockForContextLogger = Mock.Create<ILogger>();
Expand Down

0 comments on commit 337b48d

Please sign in to comment.