-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathWebApplicationBuilderExtensionsTest.cs
More file actions
293 lines (250 loc) · 14.1 KB
/
WebApplicationBuilderExtensionsTest.cs
File metadata and controls
293 lines (250 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using OpenTelemetry.Trace;
using Steeltoe.Common.Options;
using Steeltoe.Common.Security;
using Steeltoe.Connector;
using Steeltoe.Discovery;
using Steeltoe.Discovery.Eureka;
using Steeltoe.Extensions.Configuration.CloudFoundry;
using Steeltoe.Extensions.Configuration.ConfigServer;
using Steeltoe.Extensions.Configuration.Kubernetes;
using Steeltoe.Extensions.Configuration.Placeholder;
using Steeltoe.Extensions.Configuration.RandomValue;
using Steeltoe.Extensions.Logging;
using Steeltoe.Extensions.Logging.DynamicSerilog;
using Steeltoe.Management.Endpoint;
using Steeltoe.Management.Endpoint.Hypermedia;
using Steeltoe.Management.OpenTelemetry.Exporters;
using Steeltoe.Management.OpenTelemetry.Trace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
namespace Steeltoe.Bootstrap.Autoconfig.Test
{
public class WebApplicationBuilderExtensionsTest
{
private const int ConfigurationProviderCountDelta = 2;
[Fact]
public void ConfigServerConfiguration_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Extensions_Configuration_ConfigServerCore, SteeltoeAssemblies.Steeltoe_Extensions_Configuration_CloudFoundryCore);
var config = host.Services.GetServices<IConfiguration>().First(c => c is ConfigurationManager) as IConfigurationRoot;
// WebApplication.CreateBuilder() automatically includes a few builders
Assert.Equal(ConfigurationProviderCountDelta + 9, config.Providers.Count());
Assert.Single(config.Providers.OfType<CloudFoundryConfigurationProvider>());
Assert.Single(config.Providers.OfType<ConfigServerConfigurationProvider>());
}
[Fact]
public void CloudFoundryConfiguration_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Extensions_Configuration_CloudFoundryCore);
var config = host.Services.GetServices<IConfiguration>().First(c => c is ConfigurationManager) as IConfigurationRoot;
Assert.Equal(ConfigurationProviderCountDelta + 8, config.Providers.Count());
Assert.Single(config.Providers.OfType<CloudFoundryConfigurationProvider>());
}
[Fact(Skip = "Requires Kubernetes")]
public void KubernetesConfiguration_IsAutowired()
{
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", "TEST");
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Extensions_Configuration_KubernetesCore);
var config = host.Services.GetServices<IConfiguration>().First(c => c is ConfigurationManager) as IConfigurationRoot;
Assert.Equal(11, config.Providers.Count());
Assert.Equal(2, config.Providers.OfType<KubernetesConfigMapProvider>().Count());
Assert.Equal(2, config.Providers.OfType<KubernetesSecretProvider>().Count());
}
[Fact]
public void RandomValueConfiguration_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Extensions_Configuration_RandomValueBase);
var config = host.Services.GetServices<IConfiguration>().First(c => c is ConfigurationManager) as IConfigurationRoot;
Assert.Equal(ConfigurationProviderCountDelta + 8, config.Providers.Count());
Assert.Single(config.Providers.OfType<RandomValueProvider>());
}
[Fact]
public void PlaceholderResolver_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Extensions_Configuration_PlaceholderBase);
var config = host.Services.GetServices<IConfiguration>().First(c => c is ConfigurationManager) as IConfigurationRoot;
Assert.Single(config.Providers.OfType<PlaceholderResolverProvider>());
}
[Fact]
public void Connectors_AreAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Connector_ConnectorCore);
var config = host.Services.GetServices<IConfiguration>().First(c => c is ConfigurationManager) as IConfigurationRoot;
var services = host.Services;
Assert.Equal(ConfigurationProviderCountDelta + 8, config.Providers.Count());
Assert.Single(config.Providers.OfType<ConnectionStringConfigurationProvider>());
Assert.NotNull(services.GetService<StackExchange.Redis.ConnectionMultiplexer>());
Assert.NotNull(services.GetService<MongoDB.Driver.MongoClient>());
using (var scope = host.Services.CreateScope())
{
Assert.NotNull(scope.ServiceProvider.GetService<MySql.Data.MySqlClient.MySqlConnection>());
Assert.NotNull(scope.ServiceProvider.GetService<Oracle.ManagedDataAccess.Client.OracleConnection>());
Assert.NotNull(scope.ServiceProvider.GetService<Npgsql.NpgsqlConnection>());
Assert.NotNull(scope.ServiceProvider.GetService<RabbitMQ.Client.ConnectionFactory>());
Assert.NotNull(scope.ServiceProvider.GetService<System.Data.SqlClient.SqlConnection>());
}
}
[Fact]
public void DynamicSerilog_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Extensions_Logging_DynamicSerilogCore);
var loggerProvider = (IDynamicLoggerProvider)host.Services.GetService(typeof(IDynamicLoggerProvider));
Assert.IsType<SerilogDynamicProvider>(loggerProvider);
}
[Fact]
public void ServiceDiscoveryBase_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Discovery_ClientBase);
var discoveryClient = host.Services.GetServices<IDiscoveryClient>();
Assert.Single(discoveryClient);
Assert.IsType<EurekaDiscoveryClient>(discoveryClient.First());
}
[Fact]
public void ServiceDiscoveryCore_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Discovery_ClientCore);
var discoveryClient = host.Services.GetServices<IDiscoveryClient>();
Assert.Single(discoveryClient);
Assert.IsType<EurekaDiscoveryClient>(discoveryClient.First());
}
[Fact]
public async Task KubernetesActuators_AreAutowired()
{
var webApp = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Management_KubernetesCore);
webApp.UseRouting();
await webApp.StartAsync();
var managementEndpoint = webApp.Services.GetServices<ActuatorEndpoint>();
var filter = webApp.Services.GetServices<IStartupFilter>().FirstOrDefault();
Assert.Single(managementEndpoint);
Assert.NotNull(filter);
await ActuatorTestAsync(webApp.GetTestClient());
}
[Fact]
public async Task AllActuators_AreAutowired()
{
var webApp = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Management_EndpointCore);
webApp.UseRouting();
await webApp.StartAsync();
var managementEndpoint = webApp.Services.GetServices<ActuatorEndpoint>();
var filter = webApp.Services.GetServices<IStartupFilter>().FirstOrDefault(f => f is AllActuatorsStartupFilter);
Assert.Single(managementEndpoint);
Assert.NotNull(filter);
await ActuatorTestAsync(webApp.GetTestClient());
}
[Fact]
public async Task WavefrontMetricsExporter_IsAutowired()
{
var webAppBuilder = WebApplication.CreateBuilder();
webAppBuilder.Configuration.AddInMemoryCollection(TestHelpers._wavefrontConfiguration);
var exclusions = new List<string>() { SteeltoeAssemblies.Steeltoe_Management_EndpointCore };
webAppBuilder.AddSteeltoe(SteeltoeAssemblies.AllAssemblies.Except(exclusions));
webAppBuilder.WebHost.UseTestServer();
var webApp = webAppBuilder.Build();
webApp.UseRouting();
await webApp.StartAsync();
var exporter = webApp.Services.GetService<WavefrontMetricsExporter>();
Assert.NotNull(exporter);
}
[Fact]
public async Task WavefrontTraceExporter_IsAutowired()
{
var webAppBuilder = WebApplication.CreateBuilder();
webAppBuilder.Configuration.AddInMemoryCollection(TestHelpers._wavefrontConfiguration);
var exclusions = new List<string>() { SteeltoeAssemblies.Steeltoe_Management_TracingCore };
webAppBuilder.AddSteeltoe(SteeltoeAssemblies.AllAssemblies.Except(exclusions));
webAppBuilder.WebHost.UseTestServer();
var webApp = webAppBuilder.Build();
webApp.UseRouting();
await webApp.StartAsync();
var tracerProvider = webApp.Services.GetService<TracerProvider>();
Assert.NotNull(tracerProvider);
var processor = tracerProvider.GetType().GetProperty("Processor", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).GetValue(tracerProvider);
var exporter = processor.GetType().GetField("exporter", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(processor);
Assert.NotNull(exporter);
Assert.IsType<WavefrontTraceExporter>(exporter);
}
[Fact]
public void TracingBase_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Management_TracingBase);
var tracerProvider = host.Services.GetService<TracerProvider>();
Assert.NotNull(host.Services.GetService<IHostedService>());
Assert.NotNull(host.Services.GetService<ITracingOptions>());
Assert.NotNull(tracerProvider);
Assert.NotNull(host.Services.GetService<IDynamicMessageProcessor>());
// confirm instrumentation(s) were added as expected
var instrumentations = OpenTelemetrySdkReflection.GetTracerProviderInstrumentations(tracerProvider);
Assert.NotNull(instrumentations);
Assert.Single(instrumentations);
Assert.Contains(instrumentations, obj => obj.GetType().Name.Contains("Http"));
Assert.DoesNotContain(instrumentations, obj => obj.GetType().Name.Contains("AspNetCore"));
}
[Fact]
public void TracingCore_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Management_TracingCore);
var tracerProvider = host.Services.GetService<TracerProvider>();
Assert.NotNull(host.Services.GetService<IHostedService>());
Assert.NotNull(host.Services.GetService<ITracingOptions>());
Assert.NotNull(tracerProvider);
Assert.NotNull(host.Services.GetService<IDynamicMessageProcessor>());
// confirm instrumentation(s) were added as expected
var instrumentations = OpenTelemetrySdkReflection.GetTracerProviderInstrumentations(tracerProvider);
Assert.NotNull(instrumentations);
Assert.Equal(2, instrumentations.Count);
Assert.Contains(instrumentations, obj => obj.GetType().Name.Contains("Http"));
Assert.Contains(instrumentations, obj => obj.GetType().Name.Contains("AspNetCore"));
}
[Fact]
public void CloudFoundryContainerSecurity_IsAutowired()
{
var host = GetWebApplicationWithSteeltoe(SteeltoeAssemblies.Steeltoe_Security_Authentication_CloudFoundryCore);
var config = host.Services.GetServices<IConfiguration>().First(c => c is ConfigurationManager) as IConfigurationRoot;
Assert.Equal(ConfigurationProviderCountDelta + 8, config.Providers.Count());
Assert.Single(config.Providers.OfType<PemCertificateProvider>());
Assert.NotNull(host.Services.GetRequiredService<IOptions<CertificateOptions>>());
Assert.NotNull(host.Services.GetRequiredService<ICertificateRotationService>());
Assert.NotNull(host.Services.GetRequiredService<IAuthorizationHandler>());
}
private WebApplication GetWebApplicationWithSteeltoe(params string[] steeltoeInclusions)
{
var webAppBuilder = WebApplication.CreateBuilder();
webAppBuilder.Configuration.AddInMemoryCollection(TestHelpers._fastTestsConfiguration);
webAppBuilder.AddSteeltoe(SteeltoeAssemblies.AllAssemblies.Except(steeltoeInclusions));
webAppBuilder.WebHost.UseTestServer();
return webAppBuilder.Build();
}
private async Task ActuatorTestAsync(HttpClient testClient)
{
var response = await testClient.GetAsync("/actuator");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
response = await testClient.GetAsync("/actuator/info");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
response = await testClient.GetAsync("/actuator/health");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
response = await testClient.GetAsync("/actuator/health/liveness");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("\"LivenessState\":\"CORRECT\"", await response.Content.ReadAsStringAsync());
response = await testClient.GetAsync("/actuator/health/readiness");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("\"ReadinessState\":\"ACCEPTING_TRAFFIC\"", await response.Content.ReadAsStringAsync());
}
}
}