Skip to content

Commit 55ff871

Browse files
authored
Add Scenario tests for ComputeFleet (Azure#45776)
1 parent ad40a28 commit 55ff871

File tree

5 files changed

+310
-4
lines changed

5 files changed

+310
-4
lines changed

sdk/computefleet/Azure.ResourceManager.ComputeFleet/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "net",
44
"TagPrefix": "net/computefleet/Azure.ResourceManager.ComputeFleet",
5-
"Tag": ""
5+
"Tag": "net/computefleet/Azure.ResourceManager.ComputeFleet_18fc3c7f41"
66
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
33
<ProjectReference Include="..\src\Azure.ResourceManager.ComputeFleet.csproj" />
4+
<PackageReference Include="Azure.ResourceManager.Compute" />
5+
</ItemGroup>
6+
<ItemGroup>
7+
<ProjectReference Include="$(AzureCoreTestFramework)" />
48
</ItemGroup>
59
</Project>

sdk/computefleet/Azure.ResourceManager.ComputeFleet/tests/ComputeFleetManagementTestBase.cs

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using Azure.ResourceManager.Resources;
77
using Azure.ResourceManager.TestFramework;
88
using NUnit.Framework;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
912
using System.Threading.Tasks;
1013

1114
namespace Azure.ResourceManager.ComputeFleet.Tests
@@ -15,6 +18,11 @@ public class ComputeFleetManagementTestBase : ManagementRecordedTestBase<Compute
1518
protected ArmClient Client { get; private set; }
1619
protected SubscriptionResource DefaultSubscription { get; private set; }
1720

21+
protected AzureLocation DefaultLocation => AzureLocation.EastUS2;
22+
23+
protected ResourceGroupResource _resourceGroup;
24+
protected GenericResourceCollection _genericResourceCollection;
25+
1826
protected ComputeFleetManagementTestBase(bool isAsync, RecordedTestMode mode)
1927
: base(isAsync, mode)
2028
{
@@ -32,12 +40,110 @@ public async Task CreateCommonClient()
3240
DefaultSubscription = await Client.GetDefaultSubscriptionAsync().ConfigureAwait(false);
3341
}
3442

35-
protected async Task<ResourceGroupResource> CreateResourceGroup(SubscriptionResource subscription, string rgNamePrefix, AzureLocation location)
43+
protected async Task<ResourceGroupResource> CreateResourceGroup(SubscriptionResource subscription)
3644
{
37-
string rgName = Recording.GenerateAssetName(rgNamePrefix);
38-
ResourceGroupData input = new ResourceGroupData(location);
45+
string rgName = Recording.GenerateAssetName("testFleetRG-");
46+
ResourceGroupData input = new ResourceGroupData(DefaultLocation);
3947
var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, input);
4048
return lro.Value;
4149
}
50+
51+
protected async Task<GenericResource> CreateVirtualNetwork()
52+
{
53+
var ip = await CreatePublicIPAddress();
54+
var nat = await CreateNatGateway(ip.Data.Id);
55+
var nsg = await CreateNetworkSecurityGroups();
56+
var vnetName = Recording.GenerateAssetName("testVNet-");
57+
var subnetName = Recording.GenerateAssetName("testSubnet-");
58+
ResourceIdentifier vnetId = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.Network/virtualNetworks/{vnetName}");
59+
var addressSpaces = new Dictionary<string, object>()
60+
{
61+
{ "addressPrefixes", new List<string>() { "10.0.0.0/16" } }
62+
};
63+
var subnet = new Dictionary<string, object>()
64+
{
65+
{ "name", subnetName },
66+
{ "properties", new Dictionary<string, object>()
67+
{
68+
{ "addressPrefix", "10.0.2.0/24" },
69+
{ "networkSecurityGroup", new Dictionary<string, object>()
70+
{
71+
{"id", nsg.Data.Id.ToString() }
72+
}
73+
},
74+
{ "natGateway", new Dictionary<string, object>()
75+
{
76+
{"id", nat.Data.Id.ToString() }
77+
}
78+
}
79+
} }
80+
};
81+
var subnets = new List<object>() { subnet };
82+
var input = new GenericResourceData(DefaultLocation)
83+
{
84+
Properties = BinaryData.FromObjectAsJson(new Dictionary<string, object>()
85+
{
86+
{ "addressSpace", addressSpaces },
87+
{ "subnets", subnets }
88+
})
89+
};
90+
var operation = await _genericResourceCollection.CreateOrUpdateAsync(WaitUntil.Completed, vnetId, input);
91+
return operation.Value;
92+
}
93+
94+
protected ResourceIdentifier GetSubnetId(GenericResource vnet)
95+
{
96+
var properties = vnet.Data.Properties.ToObjectFromJson() as Dictionary<string, object>;
97+
var subnets = properties["subnets"] as IEnumerable<object>;
98+
var subnet = subnets.First() as IDictionary<string, object>;
99+
return new ResourceIdentifier(subnet["id"] as string);
100+
}
101+
102+
protected async Task<GenericResource> CreateNetworkSecurityGroups()
103+
{
104+
var networkSecurityGroups = Recording.GenerateAssetName("testVNetSecurityGroups-");
105+
ResourceIdentifier networkSecurityGroupsId = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroups}");
106+
var input = new GenericResourceData(DefaultLocation){};
107+
var operation = await _genericResourceCollection.CreateOrUpdateAsync(WaitUntil.Completed, networkSecurityGroupsId, input);
108+
return operation.Value;
109+
}
110+
111+
protected async Task<GenericResource> CreatePublicIPAddress()
112+
{
113+
var publicIPAddressName = Recording.GenerateAssetName("testPublicIP-");
114+
ResourceIdentifier publicIPAddress = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.Network/publicIPAddresses/{publicIPAddressName}");
115+
var input = new GenericResourceData(DefaultLocation) {
116+
Sku = new() { Name = "Standard" },
117+
Properties = BinaryData.FromObjectAsJson(new Dictionary<string, object>()
118+
{
119+
{ "publicIPAllocationMethod", "Static"}
120+
})
121+
};
122+
var operation = await _genericResourceCollection.CreateOrUpdateAsync(WaitUntil.Completed, publicIPAddress, input);
123+
return operation.Value;
124+
}
125+
126+
protected async Task<GenericResource> CreateNatGateway(ResourceIdentifier publicIp)
127+
{
128+
var natName = Recording.GenerateAssetName("testNatGateway-");
129+
ResourceIdentifier nat = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.Network/natGateways/{natName}");
130+
var input = new GenericResourceData(DefaultLocation)
131+
{
132+
Sku = new() { Name = "Standard" },
133+
Properties = BinaryData.FromObjectAsJson(new Dictionary<string, object>()
134+
{
135+
{ "publicIpAddresses", new List<object>()
136+
{
137+
new Dictionary<string, object>()
138+
{
139+
{ "id", publicIp.ToString() }
140+
}
141+
}
142+
}
143+
})
144+
};
145+
var operation = await _genericResourceCollection.CreateOrUpdateAsync(WaitUntil.Completed, nat, input);
146+
return operation.Value;
147+
}
42148
}
43149
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Threading.Tasks;
5+
using Azure.Core.TestFramework;
6+
using NUnit.Framework;
7+
8+
namespace Azure.ResourceManager.ComputeFleet.Tests
9+
{
10+
internal class ComputeFleetCRUDTests : ComputeFleetTestBase
11+
{
12+
public ComputeFleetCRUDTests(bool isAsync)
13+
: base(isAsync)
14+
{
15+
}
16+
17+
[TestCase]
18+
public async Task TestCreateComputeFleet()
19+
{
20+
var computeFleetCollection = await GetComputeFleetCollectionAsync();
21+
var vnet = await CreateVirtualNetwork();
22+
var computeFleetName = Recording.GenerateAssetName("testFleetViaSDK-");
23+
var computeFleetData = GetBasicComputeFleetData(DefaultLocation, computeFleetName, GetSubnetId(vnet));
24+
25+
// Create the compute fleet
26+
var createFleetResult = await computeFleetCollection.CreateOrUpdateAsync(WaitUntil.Completed, computeFleetName, computeFleetData);
27+
Assert.AreEqual(computeFleetName, createFleetResult.Value.Data.Name);
28+
Assert.AreEqual(DefaultLocation, createFleetResult.Value.Data.Location);
29+
30+
// Get the compute fleet
31+
var getComputeFleet = await computeFleetCollection.GetAsync(computeFleetName);
32+
Assert.AreEqual(computeFleetName, getComputeFleet.Value.Data.Name);
33+
34+
// Delete the compute fleet
35+
await createFleetResult.Value.DeleteAsync(WaitUntil.Completed);
36+
}
37+
}
38+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Core;
5+
using Azure.Core.TestFramework;
6+
using Azure.ResourceManager.ComputeFleet.Models;
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
10+
namespace Azure.ResourceManager.ComputeFleet.Tests
11+
{
12+
public class ComputeFleetTestBase: ComputeFleetManagementTestBase
13+
{
14+
private const string dummySSHKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+wWK73dCr+jgQOAxNsHAnNNNMEMWOHYEccp6wJm2gotpr9katuF/ZAdou5AaW1C61slRkHRkpRRX9FA9CYBiitZgvCCz+3nWNN7l/Up54Zps/pHWGZLHNJZRYyAB6j5yVLMVHIHriY49d/GZTZVNB8GoJv9Gakwc/fuEZYYl4YDFiGMBP///TzlI4jhiJzjKnEvqPFki5p2ZRJqcbCiF4pJrxUQR/RXqVFQdbRLZgYfJ8xGB878RENq3yQ39d8dVOkq4edbkzwcUmwwwkYVPIoDGsYLaRHnG+To7FvMeyO7xDVQkMKzopTQV8AuKpyvpqu0a9pWOMaiCyDytO7GGN [email protected]";
15+
16+
public ComputeFleetTestBase(bool isAsync)
17+
: base(isAsync)
18+
{
19+
}
20+
21+
public ComputeFleetTestBase(bool isAsync, RecordedTestMode mode)
22+
: base(isAsync, mode)
23+
{
24+
}
25+
26+
protected async Task<ComputeFleetCollection> GetComputeFleetCollectionAsync()
27+
{
28+
_genericResourceCollection = Client.GetGenericResources();
29+
_resourceGroup = await CreateResourceGroup(DefaultSubscription);
30+
return _resourceGroup.GetComputeFleets();
31+
}
32+
33+
#region ComputeFleetData
34+
35+
public static ComputeFleetData GetBasicComputeFleetData(
36+
AzureLocation location,
37+
string computerNamePrefix,
38+
ResourceIdentifier subnetId,
39+
int capacity = 2,
40+
string adminUsername = "adminuser",
41+
string allocationStrategy = "LowestPrice")
42+
{
43+
var storageProfile = new ComputeFleetVmssStorageProfile()
44+
{
45+
OSDisk = new(ComputeFleetDiskCreateOptionType.FromImage)
46+
{
47+
Caching = ComputeFleetCachingType.ReadWrite,
48+
ManagedDisk = new()
49+
{
50+
StorageAccountType = ComputeFleetStorageAccountType.StandardLrs
51+
}
52+
},
53+
ImageReference = new()
54+
{
55+
Publisher = "Canonical",
56+
Offer = "UbuntuServer",
57+
Sku = "16.04-LTS",
58+
Version = "latest"
59+
}
60+
};
61+
62+
var osProfile = new ComputeFleetVmssOSProfile()
63+
{
64+
ComputerNamePrefix = computerNamePrefix,
65+
AdminUsername = adminUsername,
66+
LinuxConfiguration = new ComputeFleetLinuxConfiguration()
67+
{
68+
IsPasswordAuthenticationDisabled = true,
69+
Ssh = new()
70+
{
71+
PublicKeys =
72+
{
73+
new()
74+
{
75+
Path = $"/home/{adminUsername}/.ssh/authorized_keys",
76+
KeyData = dummySSHKey
77+
}
78+
}
79+
}
80+
}
81+
};
82+
83+
var computeFleetVmssIPConfiguration = new ComputeFleetVmssIPConfiguration()
84+
{
85+
Name = "internalIpConfig",
86+
Properties = new()
87+
{
88+
Subnet = new()
89+
{
90+
Id = subnetId
91+
}
92+
}
93+
};
94+
95+
var computefleetVmssNetworkConfiguration = new ComputeFleetVmssNetworkConfiguration()
96+
{
97+
Name = "exampleNic",
98+
Properties = new ComputeFleetVmssNetworkConfigurationProperties(
99+
new List<ComputeFleetVmssIPConfiguration>()
100+
{
101+
computeFleetVmssIPConfiguration
102+
})
103+
{
104+
IsPrimary = true,
105+
IsAcceleratedNetworkingEnabled = false
106+
}
107+
};
108+
109+
var networkProfile = new ComputeFleetVmssNetworkProfile()
110+
{
111+
NetworkInterfaceConfigurations =
112+
{
113+
computefleetVmssNetworkConfiguration
114+
},
115+
NetworkApiVersion = "2022-07-01"
116+
};
117+
118+
var computeProfile = new ComputeFleetComputeProfile()
119+
{
120+
ComputeApiVersion = "2023-09-01",
121+
PlatformFaultDomainCount = 1,
122+
BaseVirtualMachineProfile = new()
123+
{
124+
StorageProfile = storageProfile,
125+
OSProfile = osProfile,
126+
NetworkProfile = networkProfile
127+
}
128+
};
129+
130+
return new ComputeFleetData(location:location)
131+
{
132+
Properties = new ComputeFleetProperties(
133+
vmSizesProfile: new List<ComputeFleetVmSizeProfile>() {
134+
new("Standard_D2s_v3"),
135+
new("Standard_D4s_v3"),
136+
new("Standard_E2s_v3")
137+
},
138+
computeProfile: computeProfile)
139+
{
140+
SpotPriorityProfile = new()
141+
{
142+
Capacity = capacity,
143+
AllocationStrategy = allocationStrategy,
144+
EvictionPolicy = "Delete",
145+
IsMaintainEnabled = false
146+
},
147+
RegularPriorityProfile = new()
148+
{
149+
Capacity = capacity,
150+
MinCapacity = capacity,
151+
AllocationStrategy = allocationStrategy,
152+
}
153+
}
154+
};
155+
}
156+
#endregion
157+
}
158+
}

0 commit comments

Comments
 (0)