-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
6bb2ad1
commit 924765b
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
tests/Agent/IntegrationTests/IntegrationTestHelpers/AuditLogFile.cs
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,82 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Contracts; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using Xunit.Abstractions; | ||
|
||
namespace NewRelic.Agent.IntegrationTestHelpers | ||
{ | ||
public class AuditLogFile : AgentLogBase | ||
{ | ||
private readonly string _filePath; | ||
private readonly string _fileName; | ||
|
||
public bool Found => File.Exists(_filePath); | ||
|
||
public AuditLogFile(string logDirectoryPath, ITestOutputHelper testLogger, string fileName = "", TimeSpan? timeoutOrZero = null, bool throwIfNotFound = true) | ||
: base(testLogger) | ||
{ | ||
Contract.Assert(logDirectoryPath != null); | ||
|
||
_fileName = fileName; | ||
|
||
var timeout = timeoutOrZero ?? TimeSpan.Zero; | ||
|
||
var timeTaken = Stopwatch.StartNew(); | ||
|
||
var searchPattern = _fileName != string.Empty ? _fileName : "*_audit.log"; | ||
|
||
do | ||
{ | ||
var mostRecentlyUpdatedFile = Directory.Exists(logDirectoryPath) ? | ||
Directory.GetFiles(logDirectoryPath, searchPattern) | ||
.OrderByDescending(File.GetLastWriteTimeUtc) | ||
.FirstOrDefault() : null; | ||
|
||
if (mostRecentlyUpdatedFile != null) | ||
{ | ||
_filePath = mostRecentlyUpdatedFile; | ||
return; | ||
} | ||
|
||
Thread.Sleep(TimeSpan.FromSeconds(1)); | ||
} while (timeTaken.Elapsed < timeout); | ||
|
||
if (throwIfNotFound) | ||
throw new Exception($"Waited {timeout.TotalSeconds:N0}s but didn't find an audit log matching {Path.Combine(logDirectoryPath, searchPattern)}."); | ||
} | ||
|
||
public override IEnumerable<string> GetFileLines() | ||
{ | ||
if (!Found) | ||
yield break; | ||
|
||
string line; | ||
using (var fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
using (var streamReader = new StreamReader(fileStream)) | ||
while ((line = streamReader.ReadLine()) != null) | ||
{ | ||
yield return line; | ||
} | ||
} | ||
|
||
public string GetFullLogAsString() | ||
{ | ||
using (var fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
using (var streamReader = new StreamReader(fileStream)) | ||
{ | ||
return streamReader.ReadToEnd(); | ||
} | ||
} | ||
|
||
public const string AuditLogLinePrefixRegex = @"^.*?NewRelic Audit: "; | ||
public const string AuditDataSentLogLineRegex = AuditLogLinePrefixRegex + "Data Sent from the InstrumentedApp : .*"; | ||
public const string AuditDataReceivedLogLineRegex = AuditLogLinePrefixRegex + "Data Received from the Collector : .*"; | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
tests/Agent/IntegrationTests/IntegrationTests/Logging/AuditLogTests.cs
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,59 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Linq; | ||
using NewRelic.Agent.IntegrationTestHelpers; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace NewRelic.Agent.IntegrationTests.Logging.AuditLog | ||
{ | ||
[NetCoreTest] | ||
public class AuditLogTests : NewRelicIntegrationTest<RemoteServiceFixtures.AspNetCoreMvcBasicRequestsFixture> | ||
{ | ||
private readonly RemoteServiceFixtures.AspNetCoreMvcBasicRequestsFixture _fixture; | ||
|
||
public AuditLogTests(RemoteServiceFixtures.AspNetCoreMvcBasicRequestsFixture fixture, ITestOutputHelper output) : | ||
base(fixture) | ||
{ | ||
_fixture = fixture; | ||
_fixture.TestLogger = output; | ||
|
||
_fixture.AuditLogExpected = true; | ||
|
||
_fixture.AddActions | ||
( | ||
setupConfiguration: () => | ||
{ | ||
var configModifier = new NewRelicConfigModifier(fixture.DestinationNewRelicConfigFilePath); | ||
|
||
configModifier.EnableAuditLog(true); | ||
}, | ||
exerciseApplication: () => | ||
{ | ||
_fixture.Get(); | ||
|
||
_fixture.AgentLog.WaitForLogLine(AgentLogBase.ShutdownLogLineRegex, TimeSpan.FromMinutes(2)); | ||
} | ||
); | ||
|
||
_fixture.Initialize(); | ||
|
||
} | ||
|
||
[Fact] | ||
public void AuditLogExistsAndHasSentAndReceivedData() | ||
{ | ||
Assert.True(_fixture.AuditLog.Found); | ||
|
||
var dataSentLogLines = _fixture.AuditLog.TryGetLogLines(AuditLogFile.AuditDataSentLogLineRegex).ToList(); | ||
var dataReceivedLogLines = _fixture.AuditLog.TryGetLogLines(AuditLogFile.AuditDataReceivedLogLineRegex).ToList(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.True(dataSentLogLines.Count == 2 * dataReceivedLogLines.Count); // audit log always contains 2 "sent" lines for every "received" line | ||
}); | ||
} | ||
} | ||
} |