Skip to content

Commit

Permalink
Disable http response egress - testing (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkeirstead authored Aug 2, 2021
1 parent ac972d0 commit 9bdaf41
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.AspNetCore.Http;
using Microsoft.Diagnostics.Monitoring.TestCommon;
using Microsoft.Diagnostics.Monitoring.UnitTests.Fixtures;
using Microsoft.Diagnostics.Monitoring.UnitTests.HttpApi;
using Microsoft.Diagnostics.Monitoring.UnitTests.Models;
using Microsoft.Diagnostics.Monitoring.UnitTests.Options;
using Microsoft.Diagnostics.Monitoring.UnitTests.Runners;
using Microsoft.Diagnostics.Monitoring.WebApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.FileFormats;
using Microsoft.FileFormats.ELF;
using Microsoft.FileFormats.MachO;
Expand All @@ -24,6 +27,7 @@
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using DiagnosticPortConnectionMode = Microsoft.Diagnostics.Monitoring.UnitTests.Options.DiagnosticPortConnectionMode;

namespace Microsoft.Diagnostics.Monitoring.UnitTests
{
Expand All @@ -35,7 +39,9 @@ public class EgressTests : IDisposable
private readonly DirectoryInfo _tempEgressPath;

private const string FileProviderName = "files";


// This should be identical to the error message found in Strings.resx
private const string DisabledHTTPEgressErrorMessage = "HTTP egress is not enabled.";

public EgressTests(ITestOutputHelper outputHelper, ServiceProviderFixture serviceProviderFixture)
{
Expand Down Expand Up @@ -153,7 +159,6 @@ await ScenarioRunner.SingleTarget(
Assert.Equal(HttpStatusCode.TooManyRequests, ex.StatusCode);
Assert.Equal((int)HttpStatusCode.TooManyRequests, ex.Details.Status.GetValueOrDefault());


await CancelEgressOperation(apiClient, response1);
await CancelEgressOperation(apiClient, response2);

Expand Down Expand Up @@ -211,6 +216,41 @@ await ScenarioRunner.SingleTarget(
});
}

/// <summary>
/// Tests that turning off HTTP egress results in an error for dumps and logs (gcdumps and traces are currently not tested)
/// </summary>
[Fact]
public async Task DisableHttpEgressTest()
{
await ScenarioRunner.SingleTarget(
_outputHelper,
_httpClientFactory,
DiagnosticPortConnectionMode.Connect,
TestAppScenarios.AsyncWait.Name,
appValidate: async (appRunner, appClient) =>
{
ProcessInfo processInfo = await appClient.GetProcessAsync(appRunner.ProcessId);
Assert.NotNull(processInfo);

// Dump Error Check
ValidationProblemDetailsException validationProblemDetailsExceptionDumps = await Assert.ThrowsAsync<ValidationProblemDetailsException>(
() => appClient.CaptureDumpAsync(appRunner.ProcessId, DumpType.Mini));
Assert.Equal(HttpStatusCode.BadRequest, validationProblemDetailsExceptionDumps.StatusCode);
Assert.Equal(StatusCodes.Status400BadRequest, validationProblemDetailsExceptionDumps.Details.Status);
Assert.Equal(DisabledHTTPEgressErrorMessage, validationProblemDetailsExceptionDumps.Message);

// Logs Error Check
ValidationProblemDetailsException validationProblemDetailsExceptionLogs = await Assert.ThrowsAsync<ValidationProblemDetailsException>(
() => appClient.CaptureLogsAsync(appRunner.ProcessId, TestTimeouts.LogsDuration, LogLevel.None, LogFormat.NDJson));
Assert.Equal(HttpStatusCode.BadRequest, validationProblemDetailsExceptionLogs.StatusCode);
Assert.Equal(StatusCodes.Status400BadRequest, validationProblemDetailsExceptionLogs.Details.Status);
Assert.Equal(DisabledHTTPEgressErrorMessage, validationProblemDetailsExceptionLogs.Message);

await appRunner.SendCommandAsync(TestAppScenarios.AsyncWait.Commands.Continue);
},
disableHttpEgress: true);
}

private async Task<HttpResponseMessage> TraceWithDelay(ApiClient client, int processId, bool delay = true)
{
HttpResponseMessage message = await client.ApiCall(FormattableString.Invariant($"/trace?pid={processId}&durationSeconds=-1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal sealed class MonitorRunner : IAsyncDisposable
private readonly string _runnerTmpPath =
Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"));

private bool _isDiposed;
private bool _isDisposed;

/// <summary>
/// The path of the currently executing assembly.
Expand Down Expand Up @@ -93,6 +93,11 @@ internal sealed class MonitorRunner : IAsyncDisposable
/// </summary>
public bool DisableAuthentication { get; set; }

/// <summary>
/// Determines whether HTTP egress is disabled when starting dotnet-monitor.
/// </summary>
public bool DisableHttpEgress { get; set; }

/// <summary>
/// Determines whether a temporary api key should be generated while starting dotnet-monitor.
/// </summary>
Expand Down Expand Up @@ -132,11 +137,11 @@ public async ValueTask DisposeAsync()
{
lock (_adapter)
{
if (_isDiposed)
if (_isDisposed)
{
return;
}
_isDiposed = true;
_isDisposed = true;
}

_adapter.ReceivedStandardOutputLine -= StandardOutputCallback;
Expand Down Expand Up @@ -191,6 +196,11 @@ public async Task StartAsync(CancellationToken token)
argsList.Add("--no-auth");
}

if (DisableHttpEgress)
{
argsList.Add("--no-http-egress");
}

if (UseTempApiKey)
{
argsList.Add("--temp-apikey");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static async Task SingleTarget(
Func<AppRunner, ApiClient, Task> appValidate,
Func<ApiClient, int, Task> postAppValidate = null,
Action<AppRunner> configureApp = null,
Action<MonitorRunner> configureTool = null)
Action<MonitorRunner> configureTool = null,
bool disableHttpEgress = false)
{
DiagnosticPortHelper.Generate(
mode,
Expand All @@ -34,6 +35,7 @@ public static async Task SingleTarget(
toolRunner.ConnectionMode = mode;
toolRunner.DiagnosticPortPath = diagnosticPortPath;
toolRunner.DisableAuthentication = true;
toolRunner.DisableHttpEgress = disableHttpEgress;

configureTool?.Invoke(toolRunner);

Expand Down

0 comments on commit 9bdaf41

Please sign in to comment.