-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathFunctionTestServerBuilder.cs
More file actions
166 lines (151 loc) · 7.96 KB
/
FunctionTestServerBuilder.cs
File metadata and controls
166 lines (151 loc) · 7.96 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
// Copyright 2020, Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Google.Cloud.Functions.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Reflection;
namespace Google.Cloud.Functions.Testing
{
/// <summary>
/// Builder type for <see cref="FunctionTestServer"/>, allowing for fine-tuning of the test server.
/// </summary>
public class FunctionTestServerBuilder
{
/// <summary>
/// The function type to be executed by the server.
/// </summary>
public Type FunctionTarget { get; }
private IEnumerable<FunctionsStartup>? _startups;
private bool _consoleLogging;
private FunctionTestServerBuilder(Type functionTarget) =>
FunctionTarget = functionTarget;
/// <summary>
/// Creates a builder for the function type specified by <typeparamref name="TFunction"/>.
/// </summary>
/// <typeparam name="TFunction">The function type to be executed by the server.</typeparam>
/// <returns>A test server builder for the given function type.</returns>
public static FunctionTestServerBuilder Create<TFunction>() => new FunctionTestServerBuilder(typeof(TFunction));
/// <summary>
/// Creates a builder for the specified function type.
/// </summary>
/// <param name="targetFunction">The function type to be executed by the server.</param>
/// <returns>A test server builder for the given function type.</returns>
public static FunctionTestServerBuilder Create(Type targetFunction) => new FunctionTestServerBuilder(targetFunction);
/// <summary>
/// Specifies the startup classes to use in the test server. By default, the regular Functions Framework
/// behavior of checking the assembly containing function target for <see cref="FunctionsStartupAttribute"/>
/// is used. This method is designed to allow test-specific configuration instead. If you call this method
/// multiple times, only the last-provided value is used.
/// </summary>
/// <param name="startups">The startup classes to use, or <c>null</c> to use ones specified in the
/// assembly containing the function target.</param>
/// <returns>The same test server builder, for method chaining.</returns>
public FunctionTestServerBuilder UseFunctionsStartups(IEnumerable<FunctionsStartup>? startups)
{
_startups = startups;
return this;
}
/// <summary>
/// Examines <paramref name="startupAttributeSource"/> (and its base class hierarchy, and the assembly in which it's declared)
/// for <see cref="FunctionsStartupAttribute" /> annotations.
/// If any such attributes are found, the startup classes specified by the attributes are used
/// instead of the usual startup classes for the target function. If no attributes
/// are found, or if <paramref name="startupAttributeSource"/> is null, this method has no effect.
/// </summary>
/// <remarks>
/// This method is automatically called by the parameterless constructor of <see cref="FunctionTestBase{TFunction}"/>,
/// with the test class type.
/// </remarks>
/// <param name="startupAttributeSource">The type to examine (including base class hierarchy) for attributes.</param>
/// <returns>The same test server builder, for method chaining.</returns>
public FunctionTestServerBuilder MaybeUseFunctionsStartupsFromAttributes(Type? startupAttributeSource)
{
if (startupAttributeSource is null)
{
return this;
}
var startups = FunctionsStartupAttribute.GetStartupTypes(startupAttributeSource.Assembly, startupAttributeSource)
.Select(type => Activator.CreateInstance(type))
.Cast<FunctionsStartup>()
.ToList();
return startups.Count > 0 ? UseFunctionsStartups(startups) : this;
}
/// <summary>
/// Specifies the startup classes to use in the test server. This is a convenience method for calling
/// <see cref="UseFunctionsStartups(IEnumerable{FunctionsStartup})"/> with a parameter array.
/// </summary>
/// <param name="startups">The startup classes to use, or <c>null</c> to use ones specified in the
/// assembly containing the function target.</param>
/// <returns>The same test server builder, for method chaining.</returns>
public FunctionTestServerBuilder UseFunctionsStartups(params FunctionsStartup[] startups) =>
UseFunctionsStartups((IEnumerable<FunctionsStartup>) startups);
/// <summary>
/// Specifies whether or not to use Functions Framework console logging in addition to the
/// in-memory logging for test purposes. By default, this is disabled (so only the in-memory logging is present).
/// </summary>
/// <param name="enabled">True to enable Functions Framework console logging; False to disable it.</param>
/// <returns>The same test server builder, for method chaining.</returns>
public FunctionTestServerBuilder UseFunctionsFrameworkConsoleLogging(bool enabled)
{
_consoleLogging = enabled;
return this;
}
/// <summary>
/// Builds a <see cref="FunctionTestServer"/> representing the configuration of this builder.
/// </summary>
/// <returns>A <see cref="FunctionTestServer"/> using the configuration of this builder.</returns>
public FunctionTestServer Build() => new FunctionTestServer(BuildHost(), FunctionTarget);
internal IHost BuildHost()
{
var loggerProvider = new MemoryLoggerProvider();
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webHostBuilder =>
{
// Unconditional configuration
webHostBuilder
.UseTestServer()
.ConfigureLogging(logging => logging.ClearProviders().AddProvider(loggerProvider))
.ConfigureServices(services => services.AddSingleton(loggerProvider).AddFunctionTarget(FunctionTarget));
// Configuration based on builder state.
if (_startups is null)
{
webHostBuilder.UseFunctionsStartups(FunctionTarget.Assembly, FunctionTarget);
}
else
{
foreach (var startup in _startups)
{
webHostBuilder.UseFunctionsStartup(startup);
}
}
if (_consoleLogging)
{
webHostBuilder.ConfigureLogging((context, logging) => logging.AddFunctionsConsoleLogging(context));
}
// When everything else is done, always configure the application to use the Functions Framework.
webHostBuilder.Configure((context, app) => app.UseFunctionsFramework(context));
})
.Build();
host.Start();
return host;
}
}
}