-
Notifications
You must be signed in to change notification settings - Fork 16
/
Deployment_Run.cs
340 lines (316 loc) · 17.1 KB
/
Deployment_Run.cs
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2016-2019, Pulumi Corporation
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Pulumi.Testing;
namespace Pulumi
{
public partial class Deployment
{
/// <summary>
/// <see cref="RunAsync(Func{Task{IDictionary{string,object}}}, StackOptions)"/> for more details.
/// </summary>
/// <param name="action">Callback that creates stack resources.</param>
public static Task<int> RunAsync(Action action)
=> RunAsync(() =>
{
action();
return ImmutableDictionary<string, object?>.Empty;
});
/// <summary>
/// <see cref="RunAsync(Func{Task{IDictionary{string, object}}}, StackOptions)"/> for more details.
/// </summary>
/// <param name="func">Callback that creates stack resources.</param>
/// <returns>A dictionary of stack outputs.</returns>
public static Task<int> RunAsync(Func<IDictionary<string, object?>> func)
=> RunAsync(() => Task.FromResult(func()));
/// <summary>
/// <see cref="RunAsync(Func{Task{IDictionary{string, object}}}, StackOptions)"/> for more details.
/// </summary>
/// <param name="func">Callback that creates stack resources.</param>
public static Task<int> RunAsync(Func<Task> func)
=> RunAsync(async () =>
{
await func().ConfigureAwait(false);
return ImmutableDictionary<string, object?>.Empty;
});
/// <summary>
/// <see cref="RunAsync(Func{Task{IDictionary{string, object}}}, StackOptions)"/> is an
/// entry-point to a Pulumi application. .NET applications should perform all startup logic
/// they need in their <c>Main</c> method and then end with:
/// <para>
/// <c>
/// static Task<int> Main(string[] args)
/// {
/// // program initialization code ...
///
/// return Deployment.Run(async () =>
/// {
/// // Code that creates resources.
/// });
/// }
/// </c>
/// </para>
/// Importantly: Cloud resources cannot be created outside of the lambda passed to any of the
/// <see cref="Deployment.RunAsync(Action)"/> overloads. Because cloud Resource construction is
/// inherently asynchronous, the result of this function is a <see cref="Task{T}"/> which should
/// then be returned or awaited. This will ensure that any problems that are encountered during
/// the running of the program are properly reported. Failure to do this may lead to the
/// program ending early before all resources are properly registered.
/// <para/>
/// The function passed to <see cref="RunAsync(Func{Task{IDictionary{string, object}}}, StackOptions)"/>
/// can optionally return an <see cref="IDictionary{TKey, TValue}"/>. The keys and values
/// in this dictionary will become the outputs for the Pulumi Stack that is created.
/// </summary>
/// <param name="func">Callback that creates stack resources.</param>
/// <param name="options">Stack options.</param>
public static Task<int> RunAsync(Func<Task<IDictionary<string, object?>>> func, StackOptions? options = null)
=> CreateRunnerAndRunAsync(() => new Deployment(), runner => runner.RunAsync(func, options));
/// <summary>
/// <see cref="RunAsync{TStack}()"/> is an entry-point to a Pulumi
/// application. .NET applications should perform all startup logic they
/// need in their <c>Main</c> method and then end with:
/// <para>
/// <c>
/// static Task<int> Main(string[] args) {// program
/// initialization code ...
///
/// return Deployment.Run<MyStack>();}
/// </c>
/// </para>
/// <para>
/// Deployment will instantiate a new stack instance based on the type
/// passed as TStack type parameter. Importantly, cloud resources cannot
/// be created outside of the <see cref="Stack"/> component.
/// </para>
/// <para>
/// Because cloud Resource construction is inherently asynchronous, the
/// result of this function is a <see cref="Task{T}"/> which should then
/// be returned or awaited. This will ensure that any problems that are
/// encountered during the running of the program are properly reported.
/// Failure to do this may lead to the program ending early before all
/// resources are properly registered.
/// </para>
/// </summary>
public static Task<int> RunAsync<TStack>() where TStack : Stack, new()
=> CreateRunnerAndRunAsync(() => new Deployment(), runner => runner.RunAsync<TStack>());
/// <summary>
/// <see cref="RunAsync{TStack}()"/> is an entry-point to a Pulumi
/// application. .NET applications should perform all startup logic they
/// need in their <c>Main</c> method and then end with:
/// <para>
/// <c>
/// static Task<int> Main(string[] args) {// program
/// initialization code ...
///
/// return Deployment.Run<MyStack>(serviceProvider);}
/// </c>
/// </para>
/// <para>
/// Deployment will instantiate a new stack instance based on the type
/// passed as TStack type parameter using the serviceProvider.
/// Importantly, cloud resources cannot be created outside of the
/// <see cref="Stack"/> component.
/// </para>
/// <para>
/// Because cloud Resource construction is inherently asynchronous, the
/// result of this function is a <see cref="Task{T}"/> which should then
/// be returned or awaited. This will ensure that any problems that are
/// encountered during the running of the program are properly reported.
/// Failure to do this may lead to the program ending early before all
/// resources are properly registered.
/// </para>
/// </summary>
public static Task<int> RunAsync<TStack>(IServiceProvider serviceProvider) where TStack : Stack
=> CreateRunnerAndRunAsync(() => new Deployment(), runner => runner.RunAsync<TStack>(serviceProvider));
/// <summary>
/// Entry point to test a Pulumi application. Deployment will
/// instantiate a new stack instance based on the type passed as TStack
/// type parameter using the given service provider. This method creates
/// no real resources.
/// Note: Currently, unit tests that call
/// <see cref="TestWithServiceProviderAsync{TStack}(IMocks, IServiceProvider, TestOptions)"/>
/// must run serially; parallel execution is not supported.
/// </summary>
/// <param name="mocks">Hooks to mock the engine calls.</param>
/// <param name="serviceProvider"></param>
/// <param name="options">Optional settings for the test run.</param>
/// <typeparam name="TStack">The type of the stack to test.</typeparam>
/// <returns>Test result containing created resources and errors, if any.</returns>
public static Task<ImmutableArray<Resource>> TestWithServiceProviderAsync<TStack>(IMocks mocks, IServiceProvider serviceProvider, TestOptions? options = null)
where TStack : Stack
=> TestAsync(mocks, runner => runner.RunAsync<TStack>(serviceProvider), options);
/// <summary>
/// Entry point to test a Pulumi application. Deployment will
/// instantiate a new stack instance based on the type passed as TStack
/// type parameter. This method creates no real resources.
/// Note: Currently, unit tests that call <see cref="TestAsync{TStack}(IMocks, TestOptions)"/>
/// must run serially; parallel execution is not supported.
/// </summary>
/// <param name="mocks">Hooks to mock the engine calls.</param>
/// <param name="options">Optional settings for the test run.</param>
/// <typeparam name="TStack">The type of the stack to test.</typeparam>
/// <returns>Test result containing created resources and errors, if any.</returns>
public static Task<ImmutableArray<Resource>> TestAsync<TStack>(IMocks mocks, TestOptions? options = null)
where TStack : Stack, new()
=> TestAsync(mocks, runner => runner.RunAsync<TStack>(), options);
/// <summary>
/// Used inside TestAsync overloads where users have a function that creates resources
/// in which case an internal TestStack is used to create the resources.
///
/// This function takes the created resources from the TestStack and filters it out of the created resources
/// (since it is internal) and obtains the outputs returned, if any from that TestStack.
/// </summary>
/// <param name="resources">The created resources from TestAsync</param>
/// <returns>Resources and outputs</returns>
private static (ImmutableArray<Resource> Resources, IDictionary<string, object?> Outputs) TestResults(
ImmutableArray<Resource> resources)
{
var result = new List<Resource>();
IDictionary<string, object?> outputs = new Dictionary<string, object?>();
foreach (var resource in resources)
{
if (resource is TestStack testStack)
{
// Obtain the outputs from the test stack
outputs = testStack.Outputs;
// Since TestStack is internal, Skip adding it as part of the resources created by the callback
continue;
}
result.Add(resource);
}
return (result.ToImmutableArray(), outputs);
}
/// <summary>
/// Entry point to test a Pulumi application. Deployment will
/// run the provided function that creates resources but doesn't actually deploy them
/// Note: Currently, unit tests that call this function
/// must run serially; parallel execution is not supported.
/// </summary>
/// <param name="testMocks">Hooks to mock the engine calls.</param>
/// <param name="testOptions">Optional settings for the test run.</param>
/// <param name="createResources">The function which creates resources and returns outputs.</param>
/// <returns>Test result containing created resources and outputs, if any.</returns>
public static async Task<(ImmutableArray<Resource> Resources, IDictionary<string, object?> Outputs)> TestAsync(
IMocks testMocks,
TestOptions testOptions,
Func<Task<IDictionary<string, object?>>> createResources)
{
var createdResources = await TestAsync(
mocks: testMocks,
runAsync: runner => runner.RunAsync(() => new TestStack(createResources)),
testOptions);
return TestResults(createdResources);
}
/// <summary>
/// Entry point to test a Pulumi application. Deployment will
/// run the provided function that creates resources but doesn't actually deploy them
/// Note: Currently, unit tests that call this function
/// must run serially; parallel execution is not supported.
/// </summary>
/// <param name="testMocks">Hooks to mock the engine calls.</param>
/// <param name="testOptions">Optional settings for the test run.</param>
/// <param name="createResources">The function which creates resources and returns outputs.</param>
/// <returns>Test result containing created resources and outputs, if any.</returns>
public static async Task<(ImmutableArray<Resource> Resources, IDictionary<string, object?> Outputs)> TestAsync(
IMocks testMocks,
TestOptions testOptions,
Func<IDictionary<string, object?>> createResources)
{
var createdResources = await TestAsync(
mocks: testMocks,
runAsync: runner => runner.RunAsync(() => new TestStack(createResources)),
testOptions);
return TestResults(createdResources);
}
/// <summary>
/// Entry point to test a Pulumi application. Deployment will
/// run the provided function that creates resources but doesn't actually deploy them
/// Note: Currently, unit tests that call this function
/// must run serially; parallel execution is not supported.
/// </summary>
/// <param name="testMocks">Hooks to mock the engine calls.</param>
/// <param name="testOptions">Optional settings for the test run.</param>
/// <param name="createResources">The function which creates resources and returns outputs.</param>
/// <returns>Test result containing created resources and outputs, if any.</returns>
public static async Task<ImmutableArray<Resource>> TestAsync(
IMocks testMocks,
TestOptions testOptions,
Func<Task> createResources)
{
var createdResources = await TestAsync(
mocks: testMocks,
runAsync: runner => runner.RunAsync(() => new TestStack(createResources)),
testOptions);
return TestResults(createdResources).Resources;
}
/// <summary>
/// Entry point to test a Pulumi application. Deployment will
/// run the provided function that creates resources but doesn't actually deploy them
/// Note: Currently, unit tests that call this function
/// must run serially; parallel execution is not supported.
/// </summary>
/// <param name="testMocks">Hooks to mock the engine calls.</param>
/// <param name="testOptions">Optional settings for the test run.</param>
/// <param name="createResources">The function which creates resources and returns outputs.</param>
/// <returns>Test result containing created resources and outputs, if any.</returns>
public static async Task<ImmutableArray<Resource>> TestAsync(
IMocks testMocks,
TestOptions testOptions,
Action createResources)
{
var createdResources = await TestAsync(
mocks: testMocks,
runAsync: runner => runner.RunAsync(() => new TestStack(createResources)),
testOptions);
return TestResults(createdResources).Resources;
}
private static async Task<ImmutableArray<Resource>> TestAsync(IMocks mocks, Func<IRunner, Task<int>> runAsync, TestOptions? options = null)
{
var result = await TryTestAsync(mocks, runAsync, options);
if (result.Exception != null)
{
throw result.Exception;
}
return result.Resources;
}
/// <summary>
/// Like `TestAsync`, but instead of throwing the errors
/// detected in the engine, returns them in the result tuple.
/// This enables tests to observe partially constructed
/// `Resources` vector in presence of deliberate errors.
/// </summary>
internal static async Task<(ImmutableArray<Resource> Resources, Exception? Exception)> TryTestAsync(
IMocks mocks, Func<IRunner, Task<int>> runAsync, TestOptions? options = null)
{
var engine = new MockEngine();
var monitor = new MockMonitor(mocks);
await CreateRunnerAndRunAsync(() => new Deployment(engine, monitor, options), runAsync).ConfigureAwait(false);
Exception? err = engine.Errors.Count switch
{
1 => new RunException(engine.Errors.Single()),
var v when v > 1 => new AggregateException(engine.Errors.Select(e => new RunException(e))),
_ => null
};
return (Resources: monitor.Resources.ToImmutableArray(), Exception: err);
}
internal static Task<(ImmutableArray<Resource> Resources, Exception? Exception)> TryTestAsync<TStack>(
IMocks mocks, TestOptions? options = null)
where TStack : Stack, new()
=> TryTestAsync(mocks, runner => runner.RunAsync<TStack>(), options);
// this method *must* remain marked async
// in order to protect the scope of the AsyncLocal Deployment.Instance we cannot elide the task (return it early)
// if the task is returned early and not awaited, than it is possible for any code that runs before the eventual await
// to be executed synchronously and thus have multiple calls to one of the Run methods affecting each others Deployment.Instance
internal static async Task<int> CreateRunnerAndRunAsync(
Func<Deployment> deploymentFactory,
Func<IRunner, Task<int>> runAsync)
{
var deployment = deploymentFactory();
Instance = new DeploymentInstance(deployment);
return await runAsync(deployment._runner).ConfigureAwait(false);
}
}
}