Skip to content

Commit

Permalink
Switched C# mocked unit tests to AzureNative. (pulumi#1035)
Browse files Browse the repository at this point in the history
Co-authored-by: pierre khoury <[email protected]>
  • Loading branch information
pierrekhouryb and pierre khoury committed Jun 30, 2021
1 parent 66212d8 commit 8ab786e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
11 changes: 7 additions & 4 deletions testing-unit-cs-mocks/Testing.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2016-2020, Pulumi Corporation

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Pulumi;
using Pulumi.Testing;
Expand All @@ -21,19 +21,22 @@ public Task<(string? id, object state)> NewResourceAsync(MockResourceArgs args)
if (!args.Inputs.ContainsKey("name"))
outputs.Add("name", args.Name);

if (args.Type == "azure:storage/blob:Blob")
if (args.Type == "azure-native:storage:Blob")
{
// Assets can't directly go through the engine.
// We don't need them in the test, so blank out the property for now.
outputs.Remove("source");
}

// For a Storage Account...
if (args.Type == "azure:storage/account:Account")
if (args.Type == "azure-native:storage:StorageAccount")
{
// ... set its web endpoint property.
// Normally this would be calculated by Azure, so we have to mock it.
outputs.Add("primaryWebEndpoint", $"https://{args.Name}.web.core.windows.net");
outputs.Add("primaryEndpoints", new Dictionary<string, string>
{
{ "web", $"https://{args.Name}.web.core.windows.net" },
}.ToImmutableDictionary());
}

// Default the resource ID to `{name}_id`.
Expand Down
2 changes: 1 addition & 1 deletion testing-unit-cs-mocks/UnitTesting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Pulumi.Azure" Version="4.*" />
<PackageReference Include="Pulumi.AzureNative" Version="1.*" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 22 additions & 13 deletions testing-unit-cs-mocks/WebsiteStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

using System.IO;
using Pulumi;
using Pulumi.Azure.Core;
using Storage = Pulumi.Azure.Storage;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.Storage;
using Pulumi.AzureNative.Storage.Inputs;

public class WebsiteStack : Stack
{
Expand All @@ -14,31 +15,39 @@ public WebsiteStack()
Tags = { { "Environment", "production" } }
});

var storageAccount = new Storage.Account("wwwprodsa", new Storage.AccountArgs
var storageAccount = new StorageAccount("wwwprodsa", new StorageAccountArgs
{
ResourceGroupName = resourceGroup.Name,
AccountTier = "Standard",
AccountReplicationType = "LRS",
StaticWebsite = new Storage.Inputs.AccountStaticWebsiteArgs
Sku = new SkuArgs
{
IndexDocument = "index.html"
}
Name = SkuName.Standard_LRS
},
Kind = Kind.BlobStorage
});

// Enable static website support
var staticWebsite = new StorageAccountStaticWebsite("staticWebsite", new StorageAccountStaticWebsiteArgs
{
AccountName = storageAccount.Name,
ResourceGroupName = resourceGroup.Name,
IndexDocument = "index.html",
});

var files = Directory.GetFiles("wwwroot");
foreach (var file in files)
{
var blob = new Storage.Blob(file, new Storage.BlobArgs
var blob = new Blob(file, new BlobArgs
{
ContentType = "application/html",
Source = new FileAsset(file),
StorageAccountName = storageAccount.Name,
StorageContainerName = "$web",
Type = "Block"
ResourceGroupName = resourceGroup.Name,
AccountName = storageAccount.Name,
ContainerName = staticWebsite.ContainerName,
});
}

this.Endpoint = storageAccount.PrimaryWebEndpoint;
this.Endpoint = storageAccount.PrimaryEndpoints.Apply(
primaryEndpoints => primaryEndpoints.Web);
}

[Output] public Output<string> Endpoint { get; set; }
Expand Down
20 changes: 9 additions & 11 deletions testing-unit-cs-mocks/WebsiteStackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
using FluentAssertions;
using NUnit.Framework;
using Pulumi;
using Pulumi.Azure.Core;
using Pulumi.Testing;
using Storage = Pulumi.Azure.Storage;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.Storage;

namespace UnitTesting
{
/// <summary>
/// Unit testing examples.
/// </summary>
[TestFixture]
public class WebserverStackTests
public class WebsiteStackTests
{
private static Task<ImmutableArray<Resource>> TestAsync()
private static Task<ImmutableArray<Pulumi.Resource>> TestAsync()
{
return Deployment.TestAsync<WebsiteStack>(new Mocks(), new TestOptions {IsPreview = false});
return Pulumi.Deployment.TestAsync<WebsiteStack>(new Mocks(), new TestOptions {IsPreview = false});
}

[Test]
Expand All @@ -44,21 +44,19 @@ public async Task ResourceGroupHasEnvironmentTag()
}

[Test]
public async Task StorageAccountBelongsToResourceGroup()
public async Task StorageAccountExists()
{
var resources = await TestAsync();
var storageAccount = resources.OfType<Storage.Account>().SingleOrDefault();
var storageAccounts = resources.OfType<StorageAccount>();
var storageAccount = storageAccounts.SingleOrDefault();
storageAccount.Should().NotBeNull("Storage account not found");

var resourceGroupName = await storageAccount.ResourceGroupName.GetValueAsync();
resourceGroupName.Should().Be("www-prod-rg");
}

[Test]
public async Task UploadsTwoFiles()
{
var resources = await TestAsync();
var files = resources.OfType<Storage.Blob>().ToList();
var files = resources.OfType<Blob>().ToList();
files.Count.Should().Be(2, "Should have uploaded files from `wwwroot`");
}

Expand Down

0 comments on commit 8ab786e

Please sign in to comment.