Skip to content

Commit

Permalink
update logging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scbedd committed Oct 6, 2023
1 parent f8ce4de commit af3aa19
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
16 changes: 8 additions & 8 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/LoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task PlaybackLogsSanitizedRequest()
HttpResponse response = new DefaultHttpContext().Response;
await testRecordingHandler.HandlePlaybackRequest(recordingId, request, response);

AssertLogs(logger);
AssertLogs(logger, 4, 8);
}
finally
{
Expand Down Expand Up @@ -92,7 +92,7 @@ public async Task RecordingHandlerLogsSanitizedRequests()

try
{
AssertLogs(logger);
AssertLogs(logger, 2, 8);
}
finally
{
Expand All @@ -101,27 +101,27 @@ public async Task RecordingHandlerLogsSanitizedRequests()
}
}

private static void AssertLogs(TestLogger logger)
private static void AssertLogs(TestLogger logger, int offset, int expectedLength)
{
Assert.Equal(4, logger.Logs.Count);
Assert.Equal(expectedLength, logger.Logs.Count);
Assert.Equal(
$"URI: [ http://127.0.0.1:5000/admin/addsanitizer]{Environment.NewLine}Headers: " +
"[{\"Host\":[\"127.0.0.1:5000\"],\"x-abstraction-identifier\":[\"HeaderRegexSanitizer\"]," +
"\"Content-Length\":[\"92\"]}]" + Environment.NewLine,
logger.Logs[0].ToString());
logger.Logs[0 + offset].ToString());
Assert.Equal(
"Request Body Content{\"key\":\"Location\",\"value\":\"https://fakeazsdktestaccount.table.core.windows.net/Tables\"}",
logger.Logs[1].ToString());
logger.Logs[1 + offset].ToString());
// sanitizer request body is currently duplicated for each key/value pair
Assert.Equal(
"Request Body Content{\"key\":\"Location\",\"value\":\"https://fakeazsdktestaccount.table.core.windows.net/Tables\"}",
logger.Logs[2].ToString());
logger.Logs[2 + offset].ToString());
Assert.Equal("URI: [ https://fakeazsdktestaccount.table.core.windows.net/Tables]" +
Environment.NewLine + "Headers: [{\"Accept\":[\"application/json;odata=minimalmetadata\"],\"Accept-Encoding\":[\"gzip, deflate\"],\"Authorization\":[\"Sanitized\"],\"Connection\":[\"keep-alive\"]," +
"\"Content-Length\":[\"12\"],\"Content-Type\":[\"application/octet-stream\"],\"DataServiceVersion\":[\"3.0\"],\"Date\":[\"Tue, 18 May 2021 23:27:42 GMT\"]," +
"\"User-Agent\":[\"azsdk-python-data-tables/12.0.0b7 Python/3.8.6 (Windows-10-10.0.19041-SP0)\"],\"x-ms-client-request-id\":[\"a4c24b7a-b830-11eb-a05e-10e7c6392c5a\"]," +
"\"x-ms-date\":[\"Tue, 18 May 2021 23:27:42 GMT\"],\"x-ms-version\":[\"2019-02-02\"]}]" + Environment.NewLine,
logger.Logs[3].ToString());
logger.Logs[3 + offset].ToString());
}

private static async Task AddSanitizerAsync(RecordingHandler testRecordingHandler, TestLogger logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"profiles": {
"Azure.Sdk.Tools.TestProxy": {
"commandName": "Project",
"commandLineArgs": "--storage-location=\"C:/repo/azure-sdk-for-java\"",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"Logging__LogLevel__Microsoft": "Information"
"Logging__LogLevel__Microsoft": "Debug",
"Logging__LogLevel__Default": "Debug"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
Expand Down
18 changes: 18 additions & 0 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public RecordingHandler(string targetDirectory, IAssetsStore store = null, Store
#region recording functionality
public void StopRecording(string sessionId, IDictionary<string, string> variables = null, bool saveRecording = true)
{

var id = Guid.NewGuid().ToString();
DebugLogger.LogInformation($"RECORD STOP BEGIN {id}.");

if (!RecordingSessions.TryRemove(sessionId, out var recordingSession))
{
return;
Expand Down Expand Up @@ -149,6 +153,8 @@ public void StopRecording(string sessionId, IDictionary<string, string> variable
stream.Write(Encoding.UTF8.GetBytes(Environment.NewLine));
}
}

DebugLogger.LogInformation($"RECORD STOP END {id}.");
}

/// <summary>
Expand All @@ -168,6 +174,7 @@ private async Task RestoreAssetsJson(string assetsJson = null, bool forceCheckou
public async Task StartRecordingAsync(string sessionId, HttpResponse outgoingResponse, string assetsJson = null)
{
var id = Guid.NewGuid().ToString();
DebugLogger.LogInformation($"RECORD START BEGIN {id}.");

await RestoreAssetsJson(assetsJson, false);

Expand All @@ -182,6 +189,7 @@ public async Task StartRecordingAsync(string sessionId, HttpResponse outgoingRes
throw new HttpException(HttpStatusCode.InternalServerError, $"Unexpectedly failed to add new recording session under id {id}.");
}

DebugLogger.LogInformation($"RECORD START END {id}.");
outgoingResponse.Headers.Add("x-recording-id", id);
}

Expand Down Expand Up @@ -360,6 +368,8 @@ public HttpRequestMessage CreateUpstreamRequest(HttpRequest incomingRequest, byt
public async Task StartPlaybackAsync(string sessionId, HttpResponse outgoingResponse, RecordingType mode = RecordingType.FilePersisted, string assetsPath = null)
{
var id = Guid.NewGuid().ToString();
DebugLogger.LogInformation($"PLAYBACK START BEGIN {id}.");

ModifiableRecordSession session;

if (mode == RecordingType.InMemory)
Expand Down Expand Up @@ -402,10 +412,16 @@ public async Task StartPlaybackAsync(string sessionId, HttpResponse outgoingResp

// Write to the response
await outgoingResponse.WriteAsync(json);

DebugLogger.LogInformation($"PLAYBACK START END {id}.");
}

public void StopPlayback(string recordingId, bool purgeMemoryStore = false)
{

var id = Guid.NewGuid().ToString();
DebugLogger.LogInformation($"PLAYBACK STOP BEGIN {id}.");

if (!PlaybackSessions.TryRemove(recordingId, out var session))
{
throw new HttpException(HttpStatusCode.BadRequest, $"There is no active playback session under recording id {recordingId}.");
Expand All @@ -427,6 +443,8 @@ public void StopPlayback(string recordingId, bool purgeMemoryStore = false)

GC.Collect();
}

DebugLogger.LogInformation($"PLAYBACK STOP END {id}.");
}

public async Task HandlePlaybackRequest(string recordingId, HttpRequest incomingRequest, HttpResponse outgoingResponse)
Expand Down

0 comments on commit af3aa19

Please sign in to comment.