Skip to content

Commit

Permalink
Blueprint test (#40326)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcgallan authored Nov 30, 2023
1 parent 8ea3745 commit 8a7823c
Show file tree
Hide file tree
Showing 10 changed files with 694 additions and 13 deletions.
2 changes: 1 addition & 1 deletion sdk/blueprint/Azure.ResourceManager.Blueprint/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "net",
"TagPrefix": "net/blueprint/Azure.ResourceManager.Blueprint",
"Tag": ""
"Tag": "net/blueprint/Azure.ResourceManager.Blueprint_168558b38f"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,38 @@ namespace Azure.ResourceManager.Blueprint.Tests
{
public class BlueprintManagementTestBase : ManagementRecordedTestBase<BlueprintManagementTestEnvironment>
{
protected AzureLocation DefaultLocation => AzureLocation.EastUS;
protected ArmClient Client { get; private set; }

protected BlueprintManagementTestBase(bool isAsync, RecordedTestMode mode)
: base(isAsync, mode)
protected SubscriptionResource DefaultSubscription { get; private set; }
public BlueprintManagementTestBase(bool isAsync) : base(isAsync)
{
}

protected BlueprintManagementTestBase(bool isAsync)
: base(isAsync)
public BlueprintManagementTestBase(bool isAsync, RecordedTestMode mode) : base(isAsync, mode)
{
}

[SetUp]
public void CreateCommonClient()
public async Task CreateCommonClient()
{
Client = GetArmClient();
DefaultSubscription = await Client.GetDefaultSubscriptionAsync().ConfigureAwait(false);
}

protected async Task<ResourceGroupResource> CreateResourceGroup(SubscriptionResource subscription, string rgNamePrefix, AzureLocation location)
protected async Task<ResourceGroupResource> CreateResourceGroupAsync()
{
string rgName = Recording.GenerateAssetName(rgNamePrefix);
ResourceGroupData input = new ResourceGroupData(location);
var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, input);
return lro.Value;
var resourceGroupName = Recording.GenerateAssetName("testRG-");
var rgOp = await DefaultSubscription.GetResourceGroups().CreateOrUpdateAsync(
WaitUntil.Completed,
resourceGroupName,
new ResourceGroupData(DefaultLocation)
{
Tags =
{
{ "test", "env" }
}
});
return rgOp.Value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ namespace Azure.ResourceManager.Blueprint.Tests
public class BlueprintManagementTestEnvironment : TestEnvironment
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using Azure.Core;
using Azure.ResourceManager.Blueprint.Models;
using Azure.ResourceManager.Models;
using Azure.ResourceManager.Resources.Models;
using NUnit.Framework;

namespace Azure.ResourceManager.Blueprint.Tests.Helpers
{
public static class ResourceDataHelpers
{
public static void AssertResource(ResourceData r1, ResourceData r2)
{
Assert.AreEqual(r1.Name, r2.Name);
Assert.AreEqual(r1.Id, r2.Id);
Assert.AreEqual(r1.ResourceType, r2.ResourceType);
}

#region ArtifactData
//TemplateData
public static void AssertArtifactData(ArtifactData data1, ArtifactData data2)
{
AssertResource(data1, data2);
Assert.AreEqual(data1.Kind, data2.Kind);
}
public static ArtifactData GetTemplateArtifactData()
{
ArtifactData data = new TemplateArtifact(BinaryData.FromObjectAsJson(new Dictionary<string, object>()
{
["$schema"] = "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
["contentVersion"] = "1.0.0.0",
["outputs"] = new Dictionary<string, object>()
{
["storageAccountName"] = new Dictionary<string, object>()
{
["type"] = "string",
["value"] = "[variables('storageAccountName')]"
}
},
["parameters"] = new Dictionary<string, object>()
{
["storageAccountType"] = new Dictionary<string, object>()
{
["type"] = "string",
["allowedValues"] = new object[] { "Standard_LRS", "Standard_GRS", "Standard_ZRS", "Premium_LRS" },
["defaultValue"] = "Standard_LRS",
["metadata"] = new Dictionary<string, object>()
{
["description"] = "Storage Account type"
}
}
},
["resources"] = new object[] { new Dictionary<string, object>()
{
["name"] = "[variables('storageAccountName')]",
["type"] = "Microsoft.Storage/storageAccounts",
["apiVersion"] = "2016-01-01",
["kind"] = "Storage",
["location"] = "[resourceGroup().location]",
["properties"] = new Dictionary<string, object>()
{
},
["sku"] = new Dictionary<string, object>()
{
["name"] = "[parameters('storageAccountType')]"}} },
["variables"] = new Dictionary<string, object>()
{
["storageAccountName"] = "[concat(uniquestring(resourceGroup().id), 'standardsa')]"
}
}), new Dictionary<string, ParameterValue>()
{
["storageAccountType"] = new ParameterValue()
{
Value = BinaryData.FromString("\"Standard_LRS\""),
},
})
{
ResourceGroup = "storageRG",
};
return data;
}
public static void AssertTemplateArtifactData(TemplateArtifact data1, TemplateArtifact data2)
{
AssertResource(data1, data2);
Assert.AreEqual(data1.Description, data2.Description);
Assert.AreEqual(data1.DisplayName, data2.DisplayName);
Assert.AreEqual(data1.Template, data2.Template);
}
//PolicyAssignmentArtifact
public static ArtifactData GetPolicyAssignmentArtifactData()
{
ArtifactData data = new PolicyAssignmentArtifact("/providers/Microsoft.Authorization/policyDefinitions/1e30110a-5ceb-460c-a204-c1c3969c6d62", new Dictionary<string, ParameterValue>()
{
["tagName"] = new ParameterValue()
{
Value = BinaryData.FromString("\"costCenter\""),
},
["tagValue"] = new ParameterValue()
{
Value = BinaryData.FromString("\"Standard_LRS\""),
},
})
{
DisplayName = "force costCenter tag on all resources",
};
return data;
}
public static void AssertPolicyArtifactData(PolicyAssignmentArtifact data1, PolicyAssignmentArtifact data2)
{
AssertResource(data1, data2);
Assert.AreEqual(data1.Description, data2.Description);
Assert.AreEqual(data1.DisplayName, data2.DisplayName);
Assert.AreEqual(data1.PolicyDefinitionId, data2.PolicyDefinitionId);
}
//RoleAssignmentArtifact
public static ArtifactData GetRoleAssignmentData()
{
ArtifactData data = new RoleAssignmentArtifact("/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7", BinaryData.FromString("\"[parameters('owners')]\""))
{
DisplayName = "enforce owners of given subscription",
};
return data;
}
public static void AssertRoleAssignmentArtifactData(RoleAssignmentArtifact data1, RoleAssignmentArtifact data2)
{
AssertResource(data1, data2);
Assert.AreEqual(data1.Description, data2.Description);
Assert.AreEqual(data1.DisplayName, data2.DisplayName);
Assert.AreEqual(data1.RoleDefinitionId, data2.RoleDefinitionId);
}
#endregion

#region Assignment
// Assignment Data
public static AssignmentData GetAssignmentData(string blueprintId)
{
ParameterValue[] owners =
{
new ParameterValue()
{
Value = BinaryData.FromString("\"[email protected]\"")
},
new ParameterValue()
{
Value = BinaryData.FromString("\"[email protected]\"")
},
};
IDictionary<string, ParameterValue> parameter = new Dictionary<string, ParameterValue>()
{
["storageAccountType"] = new ParameterValue()
{
Value = BinaryData.FromString("\"Standard_LRS\"")
},
["costCenter"] = new ParameterValue()
{
Value = BinaryData.FromString("\"Contoso/Online/Shopping/Production\"")
},
["owners"] = new ParameterValue()
{
Value = BinaryData.FromObjectAsJson(new ParameterValue[]
{
new ParameterValue()
{
Value = BinaryData.FromString("\"[email protected]\"")
},
new ParameterValue()
{
Value = BinaryData.FromString("\"[email protected]\"")
},
})
}
};
IDictionary<string, ResourceGroupValue> resourceGroup = new Dictionary<string, ResourceGroupValue>()
{
["storageRG"] = new ResourceGroupValue()
{
Name = "defaultRG",
Location = AzureLocation.EastUS
}
};
AssignmentData data = new AssignmentData(new Models.ManagedServiceIdentity(Models.ManagedServiceIdentityType.SystemAssigned), parameter, resourceGroup, AzureLocation.EastUS)
{
Description = "sdk test assignment",
BlueprintId = blueprintId
};
return data;
}

public static void AssertAssignmentData(AssignmentData data1, AssignmentData data2)
{
AssertResource(data1, data2);
Assert.AreEqual(data1.Description, data2.Description);
Assert.AreEqual(data1.Location, data2.Location);
}
#endregion

#region Blueprint
public static BlueprintData GetBlueprintData()
{
BlueprintData data = new BlueprintData()
{
Description = "blueprint contains all artifact kinds {'template', 'rbac', 'policy'}",
TargetScope = BlueprintTargetScope.Subscription,
Parameters =
{
["costCenter"] = new ParameterDefinition(TemplateParameterType.String)
{
DisplayName = "force cost center tag for all resources under given subscription.",
},
["owners"] = new ParameterDefinition(TemplateParameterType.Array)
{
DisplayName = "assign owners to subscription along with blueprint assignment.",
},
["storageAccountType"] = new ParameterDefinition(TemplateParameterType.String)
{
DisplayName = "storage account type.",
},
},
ResourceGroups =
{
["storageRG"] = new ResourceGroupDefinition()
{
DisplayName = "storage resource group",
Description = "Contains storageAccounts that collect all shoebox logs.",
},
},
};
return data;
}

public static void AssertBlueprint(BlueprintData data1, BlueprintData data2)
{
AssertResource(data1, data2);
//Assert.AreEqual(data1.Status , data2.Status);
Assert.AreEqual (data1.Description , data2.Description);
//Assert.AreEqual(data1.Parameters., data2.Parameters.ElementAt(0).Value);
//Assert.AreEqual(data1.Parameters.ElementAt(1), data2.Parameters.ElementAt(1));
}
#endregion

#region publishedBlueprint
public static PublishedBlueprintData GetPublishedBlueprintData(string printName)
{
return new PublishedBlueprintData()
{
Description = "published for Assignment",
BlueprintName = printName,
};
}
public static void AssertPublishedBlueprintData(PublishedBlueprintData data1, PublishedBlueprintData data2)
{
AssertResource(data1 , data2);
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Blueprint;
using Azure.ResourceManager.Blueprint.Models;
using NUnit.Framework;
using Azure.ResourceManager.Blueprint.Tests.Helpers;
using System;
using System.Threading;

namespace Azure.ResourceManager.Blueprint.Tests
{
public class AssignmentTest : BlueprintManagementTestBase
{
public AssignmentTest(bool isAsync) :
base(isAsync)//, RecordedTestMode.Record)
{
}

[Test]
public async Task AssignmentApiTest()
{
//prepare
string printName = Recording.GenerateAssetName("blueprint-");
string version = Recording.GenerateAssetName("v");
string assignmentName = Recording.GenerateAssetName("assignment-");
string assignmrntName2 = Recording.GenerateAssetName("assignment-");
string assignmentName3 = Recording.GenerateAssetName("assignment-");
var resourceGroup = await CreateResourceGroupAsync();
string resourceScope = $"/subscriptions/{resourceGroup.Data.Id.SubscriptionId}";
ResourceIdentifier scopeId = new ResourceIdentifier(string.Format("{0}", resourceScope));
//CreateOrUpdate
BlueprintCollection blueprintCollection = Client.GetBlueprints(scopeId);
var printInput = ResourceDataHelpers.GetBlueprintData();
var blueprintResource = (await blueprintCollection.CreateOrUpdateAsync(WaitUntil.Completed, printName, printInput)).Value;
var publishedCollection = blueprintResource.GetPublishedBlueprints();
var publishedInput = ResourceDataHelpers.GetPublishedBlueprintData(blueprintResource.Data.Name);
var publishedResource = (await publishedCollection.CreateOrUpdateAsync(WaitUntil.Completed, version, publishedInput)).Value;
AssignmentCollection collection = Client.GetAssignments(scopeId);
var input = ResourceDataHelpers.GetAssignmentData(blueprintResource.Id);
var resource = (await collection.CreateOrUpdateAsync(WaitUntil.Completed, assignmentName, input)).Value;
Assert.AreEqual(assignmentName, resource.Data.Name);
//Get
var resource2 = (await collection.GetAsync(assignmentName)).Value;
ResourceDataHelpers.AssertAssignmentData(resource.Data, resource2.Data);
//GetAll
_ = await collection.CreateOrUpdateAsync(WaitUntil.Completed, assignmrntName2, input);
_ = await collection.CreateOrUpdateAsync(WaitUntil.Completed, assignmentName3, input);
int count = 0;
await foreach (var assignment in collection.GetAllAsync())
{
count++;
}
Assert.GreaterOrEqual(count, 2);
//4.Exist
Assert.IsTrue(await collection.ExistsAsync(assignmentName));
Assert.IsFalse(await collection.ExistsAsync(assignmentName + "1"));

Assert.ThrowsAsync<ArgumentNullException>(async () => _ = await collection.ExistsAsync(null));
//Resouece operation
//.Get
var resource3 = (await collection.GetAsync(assignmentName)).Value;
ResourceDataHelpers.AssertAssignmentData(resource.Data, resource3.Data);
}
}
}
Loading

0 comments on commit 8a7823c

Please sign in to comment.