forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Testing.cs
44 lines (41 loc) · 1.58 KB
/
Testing.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
// Copyright 2016-2020, Pulumi Corporation
using System.Collections.Immutable;
using System.Threading.Tasks;
using Moq;
using Pulumi;
using Pulumi.Testing;
namespace UnitTesting
{
/// <summary>
/// Helper methods to streamlines unit testing experience.
/// </summary>
public static class Testing
{
/// <summary>
/// Run the tests for a given stack type.
/// </summary>
public static Task<ImmutableArray<Resource>> RunAsync<T>() where T : Stack, new()
{
var mocks = new Mock<IMocks>();
mocks.Setup(m => m.NewResourceAsync(It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<ImmutableDictionary<string, object>>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync((string type, string name, ImmutableDictionary<string, object> inputs, string? provider, string? id) => (id ?? "", inputs));
mocks.Setup(m => m.CallAsync(It.IsAny<string>(), It.IsAny<ImmutableDictionary<string, object>>(), It.IsAny<string>()))
.ReturnsAsync((string token, ImmutableDictionary<string, object> args, string? provider) => args);
return Deployment.TestAsync<T>(mocks.Object);
}
/// <summary>
/// Extract the value from an output.
/// </summary>
public static Task<T> GetValueAsync<T>(this Output<T> output)
{
var tcs = new TaskCompletionSource<T>();
output.Apply(v =>
{
tcs.SetResult(v);
return v;
});
return tcs.Task;
}
}
}