Skip to content

Commit

Permalink
Update unit tests (#23124)
Browse files Browse the repository at this point in the history
* Update unit tests
  * update the usage logic of resourceGroup to avoid NullPointerException
* Update ManagementInterceptor.cs
* Update ExpandResourceTests.cs
  • Loading branch information
dvbb authored Aug 5, 2021
1 parent 0153de7 commit cc89d33
Show file tree
Hide file tree
Showing 15 changed files with 451 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Resources.Subscription Subscription
{
get
{
return ArmClient.GetSubscriptions().Get(TestEnvironment.SubscriptionId).Value;
return ArmClient.DefaultSubscription;
}
}

Expand Down Expand Up @@ -116,9 +116,7 @@ private ComputeManagementClient GetComputeManagementClient()

protected async Task<Response<Resources.ResourceGroup>> CreateResourceGroup(string name)
{
string location = TestEnvironment.Location;
await ResourceGroupsOperations.CreateOrUpdateAsync(name, new Resources.Models.ResourceGroup(location));
return await ArmClient.DefaultSubscription.GetResourceGroups().GetAsync(name);
return await Subscription.GetResourceGroups().CreateOrUpdateAsync(name, new ResourceGroupData(TestEnvironment.Location));
}

public async Task CreateVm(
Expand Down Expand Up @@ -304,6 +302,25 @@ public async Task<ExpressRouteCircuit> UpdateDefaultExpressRouteCircuitWithMicro
return getCircuitResponse;
}

public async Task<PublicIPAddress> CreateDefaultPublicIpAddress(string name, string domainNameLabel, string location, PublicIPAddressContainer publicIPAddressContainer)
{
var publicIp = new PublicIPAddressData()
{
Location = location,
Tags = { { "key", "value" } },
PublicIPAllocationMethod = IPAllocationMethod.Dynamic,
DnsSettings = new PublicIPAddressDnsSettings() { DomainNameLabel = domainNameLabel }
};

// Put nic1PublicIpAddress
Operation<PublicIPAddress> putPublicIpAddressOperation = await publicIPAddressContainer.StartCreateOrUpdateAsync(name, publicIp);
Response<PublicIPAddress> putPublicIpAddressResponse = await putPublicIpAddressOperation.WaitForCompletionAsync();
Assert.AreEqual("Succeeded", putPublicIpAddressResponse.Value.Data.ProvisioningState.ToString());
Response<PublicIPAddress> getPublicIpAddressResponse = await publicIPAddressContainer.GetAsync(name);

return getPublicIpAddressResponse;
}

public async Task<PublicIPAddress> CreateDefaultPublicIpAddress(string name, string resourceGroupName, string domainNameLabel, string location)
{
var publicIp = new PublicIPAddressData()
Expand Down Expand Up @@ -359,7 +376,40 @@ public async Task<NetworkInterface> CreateNetworkInterface(string name, string r

return getNicResponse;
}
public async Task<NetworkInterface> CreateNetworkInterface(string name, string publicIpAddressId, string subnetId,
string location, string ipConfigName, NetworkInterfaceContainer networkInterfaceContainer)
{
var nicParameters = new NetworkInterfaceData()
{
Location = location,
Tags = { { "key", "value" } },
IpConfigurations = {
new NetworkInterfaceIPConfiguration()
{
Name = ipConfigName,
PrivateIPAllocationMethod = IPAllocationMethod.Dynamic,
Subnet = new SubnetData() { Id = subnetId }
}
}
};

if (!string.IsNullOrEmpty(publicIpAddressId))
{
nicParameters.IpConfigurations[0].PublicIPAddress = new PublicIPAddressData() { /*Id = publicIpAddressId*/ };
}

// Test NIC apis
await networkInterfaceContainer.StartCreateOrUpdateAsync(name, nicParameters);
Response<NetworkInterface> getNicResponse = await networkInterfaceContainer.GetAsync(name);
Assert.AreEqual(getNicResponse.Value.Data.Name, name);

// because its a single CA nic, primaryOnCA is always true
Assert.True(getNicResponse.Value.Data.IpConfigurations[0].Primary);

Assert.AreEqual("Succeeded", getNicResponse.Value.Data.ProvisioningState.ToString());

return getNicResponse;
}
public async Task<VirtualNetwork> CreateVirtualNetwork(string vnetName, string subnetName, string resourceGroupName, string location)
{
var vnet = new VirtualNetworkData()
Expand All @@ -384,6 +434,29 @@ public async Task<VirtualNetwork> CreateVirtualNetwork(string vnetName, string s
return getVnetResponse;
}

public async Task<VirtualNetwork> CreateVirtualNetwork(string vnetName, string subnetName, string location, VirtualNetworkContainer virtualNetworkContainer)
{
var vnet = new VirtualNetworkData()
{
Location = location,

AddressSpace = new AddressSpace()
{
AddressPrefixes = { "10.0.0.0/16", }
},
DhcpOptions = new DhcpOptions()
{
DnsServers = { "10.1.1.1", "10.1.2.4" }
},
Subnets = { new SubnetData() { Name = subnetName, AddressPrefix = "10.0.0.0/24", } }
};

await virtualNetworkContainer.StartCreateOrUpdateAsync(vnetName, vnet);
Response<VirtualNetwork> getVnetResponse = await virtualNetworkContainer.GetAsync(vnetName);

return getVnetResponse;
}

public static string GetChildLbResourceId(string subscriptionId, string resourceGroupName, string lbname, string childResourceType, string childResourceName)
{
return
Expand All @@ -406,6 +479,11 @@ protected LoadBalancerContainer GetLoadBalancerContainer(string resourceGroupNam
return GetResourceGroup(resourceGroupName).GetLoadBalancers();
}

protected LoadBalancerContainer GetLoadBalancerContainer(Resources.ResourceGroup resourceGroup)
{
return resourceGroup.GetLoadBalancers();
}

protected PublicIPAddressContainer GetPublicIPAddressContainer(string resourceGroupName)
{
return GetResourceGroup(resourceGroupName).GetPublicIPAddresses();
Expand All @@ -416,6 +494,10 @@ protected VirtualNetworkContainer GetVirtualNetworkContainer(string resourceGrou
return GetResourceGroup(resourceGroupName).GetVirtualNetworks();
}

protected VirtualNetworkContainer GetVirtualNetworkContainer(Resources.ResourceGroup resourceGroup)
{
return resourceGroup.GetVirtualNetworks();
}
protected NetworkInterfaceContainer GetNetworkInterfaceContainer(string resourceGroupName)
{
return GetResourceGroup(resourceGroupName).GetNetworkInterfaces();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,22 @@ public async Task ExpandResourceTest()
string resourceGroupName = Recording.GenerateAssetName("csmrg");

string location = await NetworkManagementTestUtilities.GetResourceLocation(ResourceManagementClient, "Microsoft.Network/loadBalancers");
await ResourceGroupsOperations.CreateOrUpdateAsync(resourceGroupName, new Resources.Models.ResourceGroup(location));
var resourceGroup = await CreateResourceGroup(resourceGroupName);

// Create lbPublicIP
string lbPublicIpName = Recording.GenerateAssetName("azsmnet");
string lbDomaingNameLabel = Recording.GenerateAssetName("azsmnet");

PublicIPAddress lbPublicIp = await CreateDefaultPublicIpAddress(
lbPublicIpName,
resourceGroupName,
lbDomaingNameLabel,
location);
location,
resourceGroup.Value.GetPublicIPAddresses());

// Create Vnet
string vnetName = Recording.GenerateAssetName("azsmnet");
string subnetName = Recording.GenerateAssetName("azsmnet");

VirtualNetwork vnet = await CreateVirtualNetwork(vnetName, subnetName, resourceGroupName, location);
VirtualNetwork vnet = await CreateVirtualNetwork(vnetName, subnetName, location, resourceGroup.Value.GetVirtualNetworks());

// Create Nics
string nic1name = Recording.GenerateAssetName("azsmnet");
Expand All @@ -66,27 +65,27 @@ public async Task ExpandResourceTest()

NetworkInterface nic1 = await CreateNetworkInterface(
nic1name,
resourceGroupName,
null,
vnet.Data.Subnets[0].Id,
location,
"ipconfig");
"ipconfig",
resourceGroup.Value.GetNetworkInterfaces());

NetworkInterface nic2 = await CreateNetworkInterface(
nic2name,
resourceGroupName,
null,
vnet.Data.Subnets[0].Id,
location,
"ipconfig");
"ipconfig",
resourceGroup.Value.GetNetworkInterfaces());

NetworkInterface nic3 = await CreateNetworkInterface(
nic3name,
resourceGroupName,
null,
vnet.Data.Subnets[0].Id,
location,
"ipconfig");
"ipconfig",
resourceGroup.Value.GetNetworkInterfaces());

// Create the LoadBalancer
var lbName = Recording.GenerateAssetName("azsmnet");
Expand Down Expand Up @@ -187,9 +186,10 @@ public async Task ExpandResourceTest()
};

// Create the loadBalancer
var loadBalancerContainer = GetResourceGroup(resourceGroupName).GetLoadBalancers();
var loadBalancerContainer = resourceGroup.Value.GetLoadBalancers();
Operation<LoadBalancer> putLoadBalancerOperation = await loadBalancerContainer.StartCreateOrUpdateAsync(lbName, loadBalancer);
await putLoadBalancerOperation.WaitForCompletionAsync();;
await putLoadBalancerOperation.WaitForCompletionAsync();
;
Response<LoadBalancer> getLoadBalancer = await loadBalancerContainer.GetAsync(lbName);

// Associate the nic with LB
Expand All @@ -201,15 +201,17 @@ public async Task ExpandResourceTest()
//nic3.IpConfigurations.First().LoadBalancerInboundNatRules.Add(getLoadBalancer.Value.InboundNatRules[1]);

// Put Nics
var networkInterfaceContainer = GetResourceGroup(resourceGroupName).GetNetworkInterfaces();
var networkInterfaceContainer = resourceGroup.Value.GetNetworkInterfaces();
NetworkInterfacesCreateOrUpdateOperation createOrUpdateOperation1 = await networkInterfaceContainer.StartCreateOrUpdateAsync(nic1name, nic1.Data);
await createOrUpdateOperation1.WaitForCompletionAsync();;
await createOrUpdateOperation1.WaitForCompletionAsync();

NetworkInterfacesCreateOrUpdateOperation createOrUpdateOperation2 = await networkInterfaceContainer.StartCreateOrUpdateAsync(nic2name, nic2.Data);
await createOrUpdateOperation2.WaitForCompletionAsync();;
await createOrUpdateOperation2.WaitForCompletionAsync();
;

NetworkInterfacesCreateOrUpdateOperation createOrUpdateOperation3 = await networkInterfaceContainer.StartCreateOrUpdateAsync(nic3name, nic3.Data);
await createOrUpdateOperation3.WaitForCompletionAsync();;
await createOrUpdateOperation3.WaitForCompletionAsync();
;

// Get Nics
await networkInterfaceContainer.GetAsync(nic1name);
Expand Down Expand Up @@ -265,7 +267,7 @@ public async Task ExpandResourceTest()
}

// Get subnet with expanded ipconfigurations
Response<Subnet> subnet = await GetResourceGroup(resourceGroupName).GetVirtualNetworks().Get(vnetName).Value.GetSubnets().GetAsync(
Response<Subnet> subnet = await resourceGroup.Value.GetVirtualNetworks().Get(vnetName).Value.GetSubnets().GetAsync(
subnetName,
"IPConfigurations");

Expand All @@ -278,7 +280,7 @@ public async Task ExpandResourceTest()
}

// Get publicIPAddress with expanded ipconfigurations
Response<PublicIPAddress> publicip = await GetResourceGroup(resourceGroupName).GetPublicIPAddresses().GetAsync(
Response<PublicIPAddress> publicip = await resourceGroup.Value.GetPublicIPAddresses().GetAsync(
lbPublicIpName,
"IPConfiguration");

Expand All @@ -289,7 +291,8 @@ public async Task ExpandResourceTest()

// Delete LoadBalancer
Operation deleteOperation = await loadBalancerContainer.Get(lbName).Value.StartDeleteAsync();
await deleteOperation.WaitForCompletionResponseAsync();;
await deleteOperation.WaitForCompletionResponseAsync();
;

// Verify Delete
AsyncPageable<LoadBalancer> listLoadBalancerAP = loadBalancerContainer.GetAllAsync();
Expand All @@ -302,7 +305,7 @@ public async Task ExpandResourceTest()
await networkInterfaceContainer.Get(nic3name).Value.StartDeleteAsync();

// Delete all PublicIPAddresses
await GetResourceGroup(resourceGroupName).GetPublicIPAddresses().Get(lbPublicIpName).Value.StartDeleteAsync();
await resourceGroup.Value.GetPublicIPAddresses().Get(lbPublicIpName).Value.StartDeleteAsync();
}
}
}
Loading

0 comments on commit cc89d33

Please sign in to comment.