-
Notifications
You must be signed in to change notification settings - Fork 878
/
Copy pathTesting.cs
72 lines (62 loc) · 2.26 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
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
// Copyright 2016-2020, Pulumi Corporation
using System.Collections.Immutable;
using System.Threading.Tasks;
using Pulumi;
using Pulumi.Testing;
namespace UnitTesting
{
class Mocks : IMocks
{
public Task<(string? id, object state)> NewResourceAsync(MockResourceArgs args)
{
var outputs = ImmutableDictionary.CreateBuilder<string, object>();
// Forward all input parameters as resource outputs, so that we could test them.
outputs.AddRange(args.Inputs);
if (args.Type == "aws:ec2/instance:Instance")
{
outputs.Add("publicIp", "203.0.113.12");
outputs.Add("publicDns", "ec2-203-0-113-12.compute-1.amazonaws.com");
}
// Default the resource ID to `{name}_id`.
// We could also format it as `/subscription/abc/resourceGroups/xyz/...` if that was important for tests.
args.Id ??= $"{args.Name}_id";
return Task.FromResult<(string? id, object state)>((args.Id, (object)outputs));
}
public Task<object> CallAsync(MockCallArgs args)
{
var outputs = ImmutableDictionary.CreateBuilder<string, object>();
if (args.Token == "aws:index/getAmi:getAmi")
{
outputs.Add("architecture", "x86_64");
outputs.Add("id", "ami-0eb1f3cdeeb8eed2a");
}
return Task.FromResult((object)outputs);
}
}
/// <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()
{
return Deployment.TestAsync<T>(new Mocks(), new TestOptions { IsPreview = false });
}
/// <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;
}
}
}