Skip to content

Commit

Permalink
add additional debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
scbedd committed May 9, 2024
1 parent 57382d5 commit 8430da6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
14 changes: 14 additions & 0 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand All @@ -39,6 +42,11 @@ public static void ConfigureLogger(ILoggerFactory factory)
}
}

public static void ConfigureLoggerVerboseLevel(DefaultOptions defaultOptions)
{
LogMode = "VerboseSanitizer";
}

/// <summary>
/// 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.
Expand Down Expand Up @@ -215,5 +223,11 @@ private static string _generateLogLine(HttpRequest req, IEnumerable<RecordedTest

return sb.ToString();
}

public static void LogVerbose(string output)
{
// todo: add recognition of modes from startup
System.Console.WriteLine(output);
}
}
}
20 changes: 17 additions & 3 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
Expand Down Expand Up @@ -127,7 +127,7 @@ public virtual RecordEntry FindMatch(RecordEntry request, IList<RecordEntry> 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)
Expand Down Expand Up @@ -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<RecordEntry> entries)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine($"Unable to find a record for the request {request.RequestMethod} {request.RequestUri}");
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -49,7 +49,11 @@ public static Regex GetRegex(string regex)
/// <returns>An updated value of the input string, with replacement operations completed if necessary.</returns>
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;
}

/// <summary>
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8430da6

Please sign in to comment.