Skip to content

Instantly share code, notes, and snippets.

@martinjt
Created March 11, 2021 16:18
Show Gist options
  • Save martinjt/8e398509e49b2c4d7016ee6343158030 to your computer and use it in GitHub Desktop.
Save martinjt/8e398509e49b2c4d7016ee6343158030 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Moq;
using Pulumi;
using Pulumi.Testing;
namespace pulumi_get_unittest
{
public class PulumiMockHelper
{
private readonly Dictionary<string, Dictionary<string, object>> _registry = new Dictionary<string, Dictionary<string, object>>();
public PulumiMockHelper()
{
SetupListStorageKeysMock();
SetupStackReference();
}
public Task<ImmutableArray<Resource>> RunAsync<T>(IServiceProvider serviceProvider) where T : Stack
{
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) => {
if (_registry.ContainsKey($"{type}_{name}"))
{
return (id ?? "", _registry[$"{type}_{name}"]);
}
Console.WriteLine($"Call to {type}_{name} not mocked");
var newDict = new Dictionary<string, object>(inputs) {
{ "name", name }
};
return (id ?? "", newDict.ToImmutableDictionary());
});
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) => {
if (_registry.ContainsKey(token))
{
return _registry[token];
}
Console.WriteLine($"Call to {token} not mocked");
return args;
});
return Deployment.TestWithServiceProviderAsync<T>(mocks.Object, serviceProvider, new TestOptions { IsPreview = false, StackName = "blah.blah" });
}
private void SetupListStorageKeysMock()
{
_registry.Add("azure-native:storage:listStorageAccountKeys", new Dictionary<string, object> {
{
"keys", new [] {
new Dictionary<string, object> {
{ "keyName", "key1" },
{ "value", Guid.NewGuid().ToString() }
}
}
}
});
}
private void SetupStackReference()
{
_registry.Add("pulumi:pulumi:StackReference_shared", new Dictionary<string, object> {
{
"outputs", new Dictionary<string, object> {
{ "Gateways", "blah" }
}
}
});
}
}
}
using System.Collections.Immutable;
using System.Threading.Tasks;
using Pulumi;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.Storage;
using Pulumi.AzureNative.Storage.Inputs;
using Xunit;
using System.Linq;
using System;
using Microsoft.Extensions.DependencyInjection;
namespace pulumi_get_unittest
{
public class UnitTest1
{
private ImmutableArray<Pulumi.Resource> _resources;
private readonly IServiceProvider _serviceProvider;
public UnitTest1()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<TestStack>();
_serviceProvider = serviceCollection.BuildServiceProvider();
}
[Fact]
public async Task Test1()
{
var pulumiHelper = new PulumiMockHelper();
var resources = await pulumiHelper.RunAsync<TestStack>(_serviceProvider);
var account = resources.OfType<StorageAccount>().FirstOrDefault();
account.Name.Apply(n =>
{
try {
Assert.Equal("test", n);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
});
}
}
public class TestStack : Stack
{
[Output]
public Output<string> StorageConnectionString { get; set; }
[Output]
public Output<string> Gateway { get; set; }
public TestStack()
{
var resourceGroup = new ResourceGroup("test");
var appStorage = new StorageAccount("test", new StorageAccountArgs
{
ResourceGroupName = resourceGroup.Name,
Sku = new SkuArgs
{
Name = SkuName.Standard_LRS,
},
Kind = Pulumi.AzureNative.Storage.Kind.StorageV2,
});
var accountKeys = Output.Tuple(resourceGroup.Name, appStorage.Name)
.Apply(p => ListStorageAccountKeys.InvokeAsync(new ListStorageAccountKeysArgs
{
ResourceGroupName = "p.Item1",
AccountName = "p.Item2"
}));
var stackReference = new StackReference("shared", new StackReferenceArgs {
Name = "shared"
});
var gateways = stackReference.RequireOutput("Gateways").Apply(_ => _.ToString());
this.Gateway = gateways;
this.StorageConnectionString = Output.Format($"DefaultEndpointsProtocol=https;AccountName={appStorage.Name};AccountKey={accountKeys.Apply(a => a.Keys[0].Value)}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment