Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cplat202103nic test #21526

Merged
merged 10 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -598,6 +598,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 @@ -872,9 +888,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 @@ -892,7 +908,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 @@ -941,23 +956,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