From 8430da62cbbe1f928f52ceb3c196c58d242a5e63 Mon Sep 17 00:00:00 2001 From: "Scott Beddall (from Dev Box)" Date: Thu, 9 May 2024 13:38:20 -0700 Subject: [PATCH] add additional debug output --- .../RecordSessionTests.cs | 4 +++- .../Common/DebugLogger.cs | 14 +++++++++++++ .../Common/RecordMatcher.cs | 20 ++++++++++++++++--- .../Common/StringSanitizer.cs | 13 ++++++++++-- .../Azure.Sdk.Tools.TestProxy/Startup.cs | 2 ++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs index ded14d6b1cb..11aaaa033c0 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs @@ -271,7 +271,9 @@ public void RecordMatcherThrowsExceptionsWithDetails() "Body differences:" + Environment.NewLine + "Request and record bodies do not match at index 40:" + Environment.NewLine + " request: \"e and long.\"" + Environment.NewLine + - " record: \"e and long but it also doesn't\"" + Environment.NewLine, + " record: \"e and long but it also doesn't\"" + Environment.NewLine + + "Remaining Unmatched Entries:" + Environment.NewLine + + " -> http://remote-host" + Environment.NewLine, exception.Message); } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs index a6f41f12a1e..2aab799fcff 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Text.Json; using Microsoft.AspNetCore.Http.Extensions; +using Azure.Sdk.Tools.TestProxy.CommandOptions; namespace Azure.Sdk.Tools.TestProxy.Common { @@ -31,6 +32,8 @@ public static class DebugLogger // internal for testing internal static ILogger Logger { get; set; } + private static string LogMode { get; set; } + public static void ConfigureLogger(ILoggerFactory factory) { if (Logger == null && factory != null) @@ -39,6 +42,11 @@ public static void ConfigureLogger(ILoggerFactory factory) } } + public static void ConfigureLoggerVerboseLevel(DefaultOptions defaultOptions) + { + LogMode = "VerboseSanitizer"; + } + /// /// Used to retrieve the final log level setting. This is a "runtime" setting that is checking the result AFTER /// accounting for launchSettings, appSettings, and environment variable settings. @@ -215,5 +223,11 @@ private static string _generateLogLine(HttpRequest req, IEnumerable ent } } - throw new TestRecordingMismatchException(GenerateException(request, bestScoreEntry)); + throw new TestRecordingMismatchException(GenerateException(request, bestScoreEntry, entries)); } public virtual int CompareBodies(byte[] requestBody, byte[] recordBody, StringBuilder descriptionBuilder = null) @@ -213,7 +213,7 @@ private string NormalizeUri(string uriToNormalize) return req.ToUri().ToString(); } - private string GenerateException(RecordEntry request, RecordEntry bestScoreEntry) + private string GenerateException(RecordEntry request, RecordEntry bestScoreEntry, IList entries) { StringBuilder builder = new StringBuilder(); builder.AppendLine($"Unable to find a record for the request {request.RequestMethod} {request.RequestUri}"); @@ -244,6 +244,20 @@ private string GenerateException(RecordEntry request, RecordEntry bestScoreEntry CompareBodies(request.Request.Body, bestScoreEntry.Request.Body, builder); + // todo: enable MODES here? + if (entries != null && entries.Count > 0) + { + builder.AppendLine("Remaining Unmatched Entries:"); + foreach(var entry in entries) + { + builder.AppendLine($" -> {entry.RequestUri}"); + } + } + else + { + builder.AppendLine("There were no entries remaining to be matched against."); + } + return builder.ToString(); } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/StringSanitizer.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/StringSanitizer.cs index 855ce62f70d..bec9057d5a0 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/StringSanitizer.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/StringSanitizer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net; using System.Text; using System.Text.RegularExpressions; @@ -49,7 +49,11 @@ public static Regex GetRegex(string regex) /// An updated value of the input string, with replacement operations completed if necessary. public static string ReplaceValue(string inputValue, string targetValue, string replacementValue) { - return inputValue.Replace(targetValue, replacementValue); + var result = inputValue.Replace(targetValue, replacementValue); + + DebugLogger.LogVerbose($"String Sanitized Applied:\nBefore: \n{inputValue}\nAfter: \n{result}"); + + return result; } /// @@ -112,6 +116,11 @@ public static string SanitizeValue(string inputValue, string replacementValue, s replacement = rx.Replace(inputValue, replacementValue); } + if (regex != null) + { + DebugLogger.LogVerbose($"Regex \"{regex??"N/A"}\" Applied:\nBefore:\n{inputValue}\nAfter:\n{replacement}"); + } + return replacement; } } diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs index b4285640fec..392b327a3d0 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs @@ -76,6 +76,8 @@ private static async Task Run(object commandObj) DefaultStore = Resolver.ResolveStore(defaultOptions.StoragePlugin ?? "GitStore"); var assetsJson = string.Empty; + DebugLogger.ConfigureLoggerVerboseLevel(defaultOptions); + switch (commandObj) { case ConfigLocateOptions configOptions: