Skip to content

Commit

Permalink
Cplat202103nic test (#21526)
Browse files Browse the repository at this point in the history
* Update VMNetworkInterfaceTests.cs

* Update VMTestBase.cs

* Update VMScaleSetNetworkProfileTests.cs

* Update VMScaleSetTestsBase.cs

* Create TestVirtualMachineNicConfiguration.json

* Create TestFlexVMScaleSetWithPublicIP.json

* Delete TestFlexVMScaleSetWithPublicIP.json

* Create TestFlexVMScaleSetWithPublicIP.json

* Record TestFlexVMScaleSetWithPublicIP

* Fix UT region

Co-authored-by: Adam Sandor <[email protected]>
Co-authored-by: Bashar Gharaibeh <[email protected]>
  • Loading branch information
3 people authored Jun 3, 2021
1 parent 7fd25e9 commit 4a9b24f
Show file tree
Hide file tree
Showing 6 changed files with 4,506 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,73 @@ public void TestMultiIpConfigForMultiNICVM()
}
}
}

[Fact]
public void TestVirtualMachineNicConfiguration()
{
string originalTestLocation = Environment.GetEnvironmentVariable("AZURE_VM_TEST_LOCATION");
using (MockContext context = MockContext.Start(this.GetType()))
{
// Hard code the location to "eastus2euap".
// This is because NRP is still deploying to other regions and is not available worldwide.
// Before changing the default location, we have to save it to be reset it at the end of the test.
// Since ComputeManagementTestUtilities.DefaultLocation is a static variable and can affect other tests if it is not reset.
Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", "eastus2euap");
EnsureClientsInitialized(context);

ImageReference imageRef = GetPlatformVMImage(useWindowsImage: true);

string rgName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
//string asName = ComputeManagementTestUtilities.GenerateName("as");
string storageAccountName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
VirtualMachine inputVM;
try
{
// Create the resource Group, it might have been already created during StorageAccount creation.
var resourceGroup = m_ResourcesClient.ResourceGroups.CreateOrUpdate(
rgName,
new ResourceGroup
{
Location = m_location,
Tags = new Dictionary<string, string>() { { rgName, DateTime.UtcNow.ToString("u") } }
});

// Create Storage Account, so that both the VMs can share it
var storageAccountOutput = CreateStorageAccount(rgName, storageAccountName);

Subnet subnetResponse = CreateVNET(rgName);

VirtualMachineNetworkInterfaceConfiguration vmNicConfig = CreateNICConfig(subnetResponse);

//string asetId = CreateAvailabilitySet(rgName, asName);

inputVM = CreateDefaultVMInput(rgName, storageAccountName, imageRef, asetId: null, vmNicConfig: vmNicConfig, networkApiVersion: NetworkApiVersion.TwoZeroTwoZeroHyphenMinusOneOneHyphenMinusZeroOne);

string expectedVMReferenceId = Helpers.GetVMReferenceId(m_subId, rgName, inputVM.Name);

var createOrUpdateResponse = m_CrpClient.VirtualMachines.CreateOrUpdate(
rgName, inputVM.Name, inputVM);

Assert.NotNull(createOrUpdateResponse);

var getVMResponse = m_CrpClient.VirtualMachines.Get(rgName, inputVM.Name);

ValidateVM(inputVM, getVMResponse, expectedVMReferenceId, hasUserDefinedAS: false);

var getNicResponse = m_NrpClient.NetworkInterfaces.List(rgName);
// TODO AutoRest: Recording Passed, but these assertions failed in Playback mode
Assert.NotNull(getNicResponse.FirstOrDefault().MacAddress);
Assert.NotNull(getNicResponse.FirstOrDefault().Primary);
Assert.True(getNicResponse.FirstOrDefault().Primary != null && getNicResponse.FirstOrDefault().Primary.Value);
}
finally
{
Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", originalTestLocation);
// Cleanup the created resources
m_ResourcesClient.ResourceGroups.Delete(rgName);
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,22 @@ protected NetworkInterface CreateMultiIpConfigNIC(string rgName, Subnet subnet,
var getNicResponse = m_NrpClient.NetworkInterfaces.Get(rgName, nicname);
return getNicResponse;
}

protected static VirtualMachineNetworkInterfaceConfiguration CreateNICConfig(Subnet subnetResponse)
{
List<VirtualMachineNetworkInterfaceIPConfiguration> ipConfigs = new List<VirtualMachineNetworkInterfaceIPConfiguration>()
{
new VirtualMachineNetworkInterfaceIPConfiguration()
{
Name = ComputeManagementTestUtilities.GenerateName("ipConfig"),
Primary = true,
Subnet = new Microsoft.Azure.Management.Compute.Models.SubResource(subnetResponse.Id),
PublicIPAddressConfiguration = new VirtualMachinePublicIPAddressConfiguration(ComputeManagementTestUtilities.GenerateName("ip"), deleteOption: DeleteOptions.Detach.ToString())
}
};
VirtualMachineNetworkInterfaceConfiguration vmNicConfig = new VirtualMachineNetworkInterfaceConfiguration(ComputeManagementTestUtilities.GenerateName("nicConfig"), primary: true, deleteOption: DeleteOptions.Delete.ToString(), ipConfigurations: ipConfigs);
return vmNicConfig;
}

private static string GetChildAppGwResourceId(string subscriptionId,
string resourceGroupName,
Expand Down Expand Up @@ -880,9 +896,9 @@ protected string CreateProximityPlacementGroup(string rgName, string ppgName)
return CreateProximityPlacementGroup(m_subId, rgName, ppgName, m_CrpClient, m_location);
}

protected VirtualMachine CreateDefaultVMInput(string rgName, string storageAccountName, ImageReference imageRef, string asetId, string nicId, bool hasManagedDisks = false,
protected VirtualMachine CreateDefaultVMInput(string rgName, string storageAccountName, ImageReference imageRef, string asetId, string nicId = null, bool hasManagedDisks = false,
string vmSize = "Standard_A1_v2", string osDiskStorageAccountType = "Standard_LRS", string dataDiskStorageAccountType = "Standard_LRS", bool? writeAcceleratorEnabled = null,
string diskEncryptionSetId = null)
string diskEncryptionSetId = null, VirtualMachineNetworkInterfaceConfiguration vmNicConfig = null, string networkApiVersion = null)
{
// Generate Container name to hold disk VHds
string containerName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
Expand All @@ -900,7 +916,6 @@ protected VirtualMachine CreateDefaultVMInput(string rgName, string storageAccou
{
Location = m_location,
Tags = new Dictionary<string, string>() { { "RG", "rg" }, { "testTag", "1" } },
AvailabilitySet = new Microsoft.Azure.Management.Compute.Models.SubResource() { Id = asetId },
HardwareProfile = new HardwareProfile
{
VmSize = vmSize
Expand Down Expand Up @@ -949,23 +964,43 @@ protected VirtualMachine CreateDefaultVMInput(string rgName, string storageAccou
}
},
},
NetworkProfile = new CM.NetworkProfile
{
NetworkInterfaces = new List<NetworkInterfaceReference>
{
new NetworkInterfaceReference
{
Id = nicId
}
}
},
OsProfile = new OSProfile
{
AdminUsername = "Foo12",
AdminPassword = PLACEHOLDER,
ComputerName = ComputerName
}
};

if (!string.IsNullOrEmpty(asetId))
{
vm.AvailabilitySet = new Microsoft.Azure.Management.Compute.Models.SubResource() { Id = asetId };
}

CM.NetworkProfile vmNetworkProfile = new CM.NetworkProfile();
if (!string.IsNullOrEmpty(nicId))
{
vmNetworkProfile.NetworkInterfaces = new List<NetworkInterfaceReference>
{
new NetworkInterfaceReference
{
Id = nicId
}
};
}
if (vmNicConfig != null)
{
vmNetworkProfile.NetworkInterfaceConfigurations = new List<VirtualMachineNetworkInterfaceConfiguration>
{
vmNicConfig
};
}
if (!string.IsNullOrEmpty(networkApiVersion))
{
vmNetworkProfile.NetworkApiVersion = networkApiVersion;
}

vm.NetworkProfile = vmNetworkProfile;

if(dataDiskStorageAccountType == StorageAccountTypes.UltraSSDLRS)
{
Expand Down
Loading

0 comments on commit 4a9b24f

Please sign in to comment.