// 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(string type, string name, ImmutableDictionary inputs, string? provider, string? id) { var outputs = ImmutableDictionary.CreateBuilder(); // Forward all input parameters as resource outputs, so that we could test them. outputs.AddRange(inputs); if (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. id ??= $"{name}_id"; return Task.FromResult((id, (object)outputs)); } public Task CallAsync(string token, ImmutableDictionary inputs, string? provider) { var outputs = ImmutableDictionary.CreateBuilder(); if (token == "aws:index/getAmi:getAmi") { outputs.Add("architecture", "x86_64"); outputs.Add("id", "ami-0eb1f3cdeeb8eed2a"); } return Task.FromResult((object)outputs); } } /// /// Helper methods to streamlines unit testing experience. /// public static class Testing { /// /// Run the tests for a given stack type. /// public static Task> RunAsync() where T : Stack, new() { return Deployment.TestAsync(new Mocks(), new TestOptions { IsPreview = false }); } /// /// Extract the value from an output. /// public static Task GetValueAsync(this Output output) { var tcs = new TaskCompletionSource(); output.Apply(v => { tcs.SetResult(v); return v; }); return tcs.Task; } } }