Skip to content

Instantly share code, notes, and snippets.

@martinjt
Created March 10, 2021 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinjt/b0e81fcd266cdcfd7bafe3599f569686 to your computer and use it in GitHub Desktop.
Save martinjt/b0e81fcd266cdcfd7bafe3599f569686 to your computer and use it in GitHub Desktop.
Testing for Get Actions in Pulumi
using System.Collections.Immutable;
using System.Threading.Tasks;
using Moq;
using Pulumi;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.Storage;
using Pulumi.AzureNative.Storage.Inputs;
using Pulumi.Testing;
using Xunit;
using System.Linq;
namespace pulumi_get_unittest
{
public class UnitTest1
{
public static Task<ImmutableArray<Pulumi.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 Pulumi.Deployment.TestAsync<T>(mocks.Object, new TestOptions { IsPreview = false });
}
[Fact]
public async Task Test1()
{
var resources = await RunAsync<TestStack>();
var account = resources.OfType<StorageAccount>().FirstOrDefault();
account.Name.Apply(n => {
Assert.StartsWith("test", n);
return false;
});
}
}
public class TestStack : Stack
{
[Output]
public Output<string> StorageConnectionString { 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
}));
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