Skip to content

Commit e647e87

Browse files
committed
More test fixes
- Wire up generated type info to more tests - Skip some test due to record bug - Check in generated source and fix up IDictionary type info - Pass IServiceProvider through ValidationContext - Set MemberNames on ValidationResult - Map more options
1 parent 03287fb commit e647e87

40 files changed

+1441
-95
lines changed

src/Microsoft.Diagnostics.Monitoring.Options/AuthenticationOptions.Validate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext vali
2020
results.Add(
2121
new ValidationResult(
2222
string.Format(
23-
OptionsDisplayStrings.ErrorMessage_MultipleAuthenticationModesSpecified)));
23+
OptionsDisplayStrings.ErrorMessage_MultipleAuthenticationModesSpecified), [nameof(MonitorApiKey), nameof(AzureAd)]));
2424
}
2525

2626
return results;

src/Tests/CollectionRuleActions.UnitTests/ActionListExecutorTests.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ActionListExecutorTests(ITestOutputHelper outputHelper)
4646
_outputHelper = outputHelper;
4747
}
4848

49-
[Fact]
49+
[Fact(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
5050
public async Task ActionListExecutor_AllActionsSucceed()
5151
{
5252
await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
@@ -73,7 +73,7 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
7373
await executor.ExecuteActions(context, startCallback, cancellationTokenSource.Token);
7474

7575
VerifyStartCallbackCount(waitForCompletion: false, callbackCount);
76-
});
76+
}, TestValidatableTypes.AddValidation);
7777
}
7878

7979
[Fact(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
@@ -119,10 +119,10 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
119119
Assert.Equal(string.Format(Strings.ErrorMessage_NonzeroExitCode, "1"), actionExecutionException.Message);
120120

121121
VerifyStartCallbackCount(waitForCompletion, callbackCount);
122-
});
122+
}, TestValidatableTypes.AddValidation);
123123
}
124124

125-
[Fact]
125+
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/61379")]
126126
public Task ActionListExecutor_FirstActionFail_DeferredCompletion()
127127
{
128128
return ActionListExecutor_FirstActionFail(waitForCompletion: false);
@@ -165,7 +165,7 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
165165
Assert.Equal(string.Format(Strings.ErrorMessage_NonzeroExitCode, "1"), actionExecutionException.Message);
166166

167167
VerifyStartCallbackCount(waitForCompletion, callbackCount);
168-
}, AddValidation);
168+
}, TestValidatableTypes.AddValidation);
169169
}
170170

171171

@@ -187,15 +187,9 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
187187
}, serviceCollection =>
188188
{
189189
serviceCollection.RegisterCollectionRuleAction<PassThroughActionFactory, PassThroughOptions>(nameof(PassThroughAction));
190-
AddValidation(serviceCollection);
190+
TestValidatableTypes.AddValidation(serviceCollection);
191191
});
192192
}
193-
194-
static void AddValidation(IServiceCollection serviceCollection)
195-
{
196-
serviceCollection.AddValidation();
197-
}
198-
199193
private static void VerifyStartCallbackCount(bool waitForCompletion, int callbackCount)
200194
{
201195
//Currently, any attempt to wait on completion will automatically trigger the start callback.

src/Tests/CollectionRuleActions.UnitTests/CollectDumpActionTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Diagnostics.Monitoring.TestCommon.Runners;
77
using Microsoft.Diagnostics.Monitoring.WebApi;
88
using Microsoft.Diagnostics.Monitoring.WebApi.Models;
9+
using Microsoft.Diagnostics.Tools.Monitor;
910
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules;
1011
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Actions;
1112
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options;
@@ -101,7 +102,7 @@ await runner.ExecuteAsync(async () =>
101102

102103
await runner.SendCommandAsync(TestAppScenarios.AsyncWait.Commands.Continue);
103104
});
104-
});
105+
}, TestValidatableTypes.AddValidation);
105106
}
106107
}
107108
}

src/Tests/CollectionRuleActions.UnitTests/CollectTraceActionTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Diagnostics.Monitoring.TestCommon.Runners;
77
using Microsoft.Diagnostics.Monitoring.WebApi;
88
using Microsoft.Diagnostics.Monitoring.WebApi.Models;
9+
using Microsoft.Diagnostics.Tools.Monitor;
910
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules;
1011
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Actions;
1112
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions;
@@ -55,7 +56,7 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
5556
}, async host =>
5657
{
5758
await PerformTrace(host, tfm);
58-
});
59+
}, TestValidatableTypes.AddValidation);
5960
}
6061

6162
[Theory]
@@ -99,7 +100,8 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
99100
options.Duration = TimeSpan.FromSeconds(2);
100101
})
101102
.SetStartupTrigger();
102-
}, host => PerformTrace(host, TargetFrameworkMoniker.Current, artifactName));
103+
}, host => PerformTrace(host, TargetFrameworkMoniker.Current, artifactName),
104+
TestValidatableTypes.AddValidation);
103105
}
104106

105107
private async Task PerformTrace(IHost host, TargetFrameworkMoniker tfm, string artifactName = null)

src/Tests/CollectionRuleActions.UnitTests/CollectionRuleActions.UnitTests.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
<PropertyGroup>
66
<TargetFrameworks>$(ToolTargetFrameworks)</TargetFrameworks>
77
<InterceptorsNamespaces>$(InterceptorsNamespaces);Microsoft.AspNetCore.Http.Validation.Generated</InterceptorsNamespaces>
8+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
89
</PropertyGroup>
910

1011
<ItemGroup>
1112
<PackageReference Include="Microsoft.AspNetCore.OpenApi" GeneratePathProperty="true" />
13+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
1214
</ItemGroup>
1315

1416
<ItemGroup>
1517
<ProjectReference Include="..\..\Microsoft.Diagnostics.Monitoring.Options\Microsoft.Diagnostics.Monitoring.Options.csproj" />
1618
<ProjectReference Include="..\..\Tools\dotnet-monitor\dotnet-monitor.csproj" />
1719
<ProjectReference Include="..\Microsoft.Diagnostics.Monitoring.TestCommon\Microsoft.Diagnostics.Monitoring.TestCommon.csproj" />
1820
<ProjectReference Include="..\Microsoft.Diagnostics.Monitoring.Tool.UnitTestCommon\Microsoft.Diagnostics.Monitoring.Tool.UnitTestCommon.csproj" />
21+
<ProjectReference Include="..\Microsoft.Diagnostics.Monitoring.Tool.UnitTestsSample\Microsoft.Diagnostics.Monitoring.Tool.UnitTestsSample.csproj" />
1922
</ItemGroup>
2023

2124
<ItemGroup>
@@ -34,4 +37,11 @@
3437

3538
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
3639

40+
<Target Name="RemoveAnalyzer" BeforeTargets="CoreCompile">
41+
<ItemGroup>
42+
<Analyzer Remove="@(Analyzer)" Condition="'%(Analyzer.FileName)' == 'Microsoft.AspNetCore.Http.ValidationsGenerator'" />
43+
</ItemGroup>
44+
</Target>
45+
46+
3747
</Project>

src/Tests/CollectionRuleActions.UnitTests/EnvironmentVariableActionTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public EnvironmentVariableActionTests(ITestOutputHelper outputHelper)
4141
/// <remarks>
4242
/// The required APIs only exist on .NET 6.0+
4343
/// </remarks>
44-
[Theory]
44+
[Theory(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
4545
[MemberData(nameof(ActionTestsHelper.GetTfms), MemberType = typeof(ActionTestsHelper))]
4646
public async Task TestSetEnvVar(TargetFrameworkMoniker tfm)
4747
{
@@ -79,7 +79,7 @@ await runner.ExecuteAsync(async () =>
7979

8080
await runner.SendCommandAsync(TestAppScenarios.EnvironmentVariables.Commands.ShutdownScenario);
8181
});
82-
});
82+
}, TestValidatableTypes.AddValidation);
8383
}
8484

8585
/// <summary>
@@ -88,7 +88,7 @@ await runner.ExecuteAsync(async () =>
8888
/// <remarks>
8989
/// The required APIs only exist on .NET 6.0+
9090
/// </remarks>
91-
[Theory]
91+
[Theory(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
9292
[MemberData(nameof(ActionTestsHelper.GetTfms), MemberType = typeof(ActionTestsHelper))]
9393
public async Task TestGetEnvVar(TargetFrameworkMoniker tfm)
9494
{
@@ -145,7 +145,7 @@ await runner.ExecuteAsync(async () =>
145145

146146
await runner.SendCommandAsync(TestAppScenarios.EnvironmentVariables.Commands.ShutdownScenario);
147147
});
148-
});
148+
}, TestValidatableTypes.AddValidation);
149149
}
150150

151151
/// <summary>
@@ -155,7 +155,7 @@ await runner.ExecuteAsync(async () =>
155155
/// <remarks>
156156
/// The required APIs only exist on .NET 6.0+
157157
/// </remarks>
158-
[Theory]
158+
[Theory(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
159159
[MemberData(nameof(ActionTestsHelper.GetTfms), MemberType = typeof(ActionTestsHelper))]
160160
public async Task TestEnvVarRoundTrip(TargetFrameworkMoniker tfm)
161161
{
@@ -199,7 +199,7 @@ await runner.ExecuteAsync(async () =>
199199
Assert.True(getResult.OutputValues.TryGetValue(CollectionRuleActionConstants.EnvironmentVariableValueName, out string value));
200200
Assert.Equal(DefaultVarValue, value);
201201
});
202-
});
202+
}, TestValidatableTypes.AddValidation);
203203
}
204204
}
205205
}

src/Tests/CollectionRuleActions.UnitTests/ExecuteActionTests.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Http.Validation;
5+
using Microsoft.AspNetCore.Mvc.Testing;
56
using Microsoft.Diagnostics.Monitoring.TestCommon;
67
using Microsoft.Diagnostics.Tools.Monitor;
78
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Actions;
89
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Exceptions;
910
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions;
11+
using Microsoft.Extensions.DependencyInjection;
1012
using Microsoft.Extensions.Options;
1113
using System;
1214
using System.Globalization;
@@ -20,20 +22,26 @@
2022

2123
namespace CollectionRuleActions.UnitTests
2224
{
25+
public class ExecuteActionFixture : WebApplicationFactory<Program>
26+
{
27+
}
28+
2329
[TargetFrameworkMonikerTrait(TargetFrameworkMonikerExtensions.CurrentTargetFrameworkMoniker)]
2430
[Collection(TestCollections.CollectionRuleActions)]
25-
public sealed class ExecuteActionTests
31+
public sealed class ExecuteActionTests : IClassFixture<ExecuteActionFixture>
2632
{
2733
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
2834

2935
private readonly ITestOutputHelper _outputHelper;
36+
private readonly ExecuteActionFixture _fixture;
3037

31-
public ExecuteActionTests(ITestOutputHelper outputHelper)
38+
public ExecuteActionTests(ITestOutputHelper outputHelper, ExecuteActionFixture fixture)
3239
{
3340
_outputHelper = outputHelper;
41+
_fixture = fixture;
3442
}
3543

36-
[Fact]
44+
[Fact(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
3745
public async Task ExecuteAction_ZeroExitCode()
3846
{
3947
await ValidateAction(
@@ -52,7 +60,7 @@ await ValidateAction(
5260
});
5361
}
5462

55-
[Fact]
63+
[Fact(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
5664
public async Task ExecuteAction_NonzeroExitCode()
5765
{
5866
await ValidateAction(
@@ -72,7 +80,7 @@ await ValidateAction(
7280
});
7381
}
7482

75-
[Fact]
83+
[Fact(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
7684
public async Task ExecuteAction_TokenCancellation()
7785
{
7886
// This timeout is much shorter than the default test timeout.
@@ -99,7 +107,7 @@ await Assert.ThrowsAsync<TaskCanceledException>(
99107
});
100108
}
101109

102-
[Fact]
110+
[Fact(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
103111
public async Task ExecuteAction_TextFileOutput()
104112
{
105113
using TemporaryDirectory outputDirectory = new(_outputHelper);
@@ -126,7 +134,7 @@ await ValidateAction(
126134
Assert.Equal(testMessage, File.ReadAllText(textFileOutputPath));
127135
}
128136

129-
[Fact]
137+
[Fact(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
130138
public async Task ExecuteAction_InvalidPath()
131139
{
132140
string uniquePathName = Guid.NewGuid().ToString();
@@ -146,7 +154,7 @@ await ValidateAction(
146154
});
147155
}
148156

149-
[Fact]
157+
[Fact(Skip = "https://github.com/dotnet/aspnetcore/pull/61402")]
150158
public async Task ExecuteAction_IgnoreExitCode()
151159
{
152160
await ValidateAction(
@@ -175,9 +183,10 @@ private static void ValidateActionResult(CollectionRuleActionResult result, stri
175183
Assert.Equal(expectedExitCode, actualExitCode);
176184
}
177185

178-
private static async Task ValidateAction(Action<ExecuteOptions> optionsCallback, Func<ICollectionRuleAction, CancellationToken, Task> actionCallback)
186+
private async Task ValidateAction(Action<ExecuteOptions> optionsCallback, Func<ICollectionRuleAction, CancellationToken, Task> actionCallback)
179187
{
180-
ExecuteActionFactory factory = new(Options.Create<ValidationOptions>(new()));
188+
var validationOptions = _fixture.Services.GetService<IOptions<ValidationOptions>>();
189+
ExecuteActionFactory factory = new(_fixture.Services, validationOptions);
181190

182191
ExecuteOptions options = new();
183192

src/Tests/CollectionRuleActions.UnitTests/TestValidatableType.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Http.Validation;
5+
using Microsoft.AspNetCore.Http.Validation.Generated;
6+
using Microsoft.Extensions.DependencyInjection;
57
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options;
68
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions;
9+
using Microsoft.Diagnostics.Monitoring.WebApi;
710

811
namespace Microsoft.Diagnostics.Tools.Monitor
912
{
@@ -17,5 +20,18 @@ internal sealed class TestValidatableTypes
1720
public required CollectionRuleOptions CollectionRuleOptions { get; init; }
1821

1922
public required ExecuteOptions ExecuteOptions { get; init; }
23+
24+
public required SetEnvironmentVariableOptions SetEnvironmentVariableOptions { get; init; }
25+
26+
public required GetEnvironmentVariableOptions GetEnvironmentVariableOptions { get; init; }
27+
28+
public required CollectTraceOptions CollectTraceOptions { get; init; }
29+
30+
public required GlobalCounterOptions GlobalCounterOptions { get; init; }
31+
32+
public static void AddValidation(IServiceCollection services)
33+
{
34+
GeneratedServiceCollectionExtensions.AddValidation(services);
35+
}
2036
}
2137
}

0 commit comments

Comments
 (0)