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

Force Detach test #18553

Merged
merged 18 commits into from
Mar 4, 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 @@ -96,6 +96,19 @@ public static void ValidateVirtualMachineSizeListResponse(IEnumerable<VirtualMac
Assert.NotNull(vmSizeProperties);
CompareVMSizes(expectedVMSizeProperties, vmSizeProperties);
}

/// <summary>
/// Marks a data disk to be detached.
/// </summary>
public static void MarkDataDiskToBeDetached(DataDisk disk, string detachOption = null)
{
Assert.NotNull(disk);
disk.ToBeDetached = true;
if (!string.IsNullOrEmpty(detachOption))
{
disk.DetachOption = detachOption;
}
}

private static List<VirtualMachineSize> GetExpectedVirtualMachineSize(bool hasAZ, bool? writeAcceleratorEnabled = null, bool hasDiffDisks = false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.Storage.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Compute.Tests
Expand Down Expand Up @@ -130,6 +132,76 @@ public void TestVMDataDiskScenario()
}
}
}

[Fact]
[Trait("Name", "TestVMDataDiskScenario_ManagedDisk_ForceDetach")]
public void TestVMDataDiskScenario_ManagedDisk_ForceDetach()
{
using (MockContext context = MockContext.Start(this.GetType()))
{
string originalTestLocation = Environment.GetEnvironmentVariable("AZURE_VM_TEST_LOCATION");
Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", "eastus2euap");
EnsureClientsInitialized(context);

ImageReference imageReference = GetPlatformVMImage(useWindowsImage: true);
string resourceGroupName = TestUtilities.GenerateName(TestPrefix);
string storageAccountForDisksName = TestUtilities.GenerateName(TestPrefix);
string availabilitySetName = TestUtilities.GenerateName(TestPrefix);

try
{
StorageAccount storageAccountForDisks = CreateStorageAccount(resourceGroupName, storageAccountForDisksName);

Action<VirtualMachine> addManagedDataDiskToVM = vm =>
{
vm.HardwareProfile.VmSize = VirtualMachineSizeTypes.StandardA4;
vm.StorageProfile.DataDisks = new List<DataDisk>();

var diskName = "dataDisk" + TestUtilities.GenerateGuid();
var dd = new DataDisk
{
Caching = CachingTypes.None,
DiskSizeGB = 10,
CreateOption = DiskCreateOptionTypes.Empty,
Lun = 0,
Name = diskName,
ManagedDisk = new ManagedDiskParameters()
{
StorageAccountType = StorageAccountType.StandardLRS
}
};
vm.StorageProfile.DataDisks.Add(dd);

var testStatus = new InstanceViewStatus
{
Code = "test",
Message = "test"
};

var testStatusList = new List<InstanceViewStatus> { testStatus };
};

VirtualMachine inputVM;
CreateVM(resourceGroupName, availabilitySetName, storageAccountForDisks, imageReference, out inputVM, addManagedDataDiskToVM, hasManagedDisks: true);

VirtualMachine getVMWithInstanceViewResponse = m_CrpClient.VirtualMachines.Get(resourceGroupName, inputVM.Name, InstanceViewTypes.InstanceView);
ValidateVMInstanceView(inputVM, getVMWithInstanceViewResponse, hasManagedDisks: true);

DataDisk diskToBeForceDetached = getVMWithInstanceViewResponse.StorageProfile.DataDisks.FirstOrDefault(disk => disk.Lun == 0);
Assert.NotNull(diskToBeForceDetached);

Helpers.MarkDataDiskToBeDetached(diskToBeForceDetached, "ForceDetach");

var forceDetachVMResponse = m_CrpClient.VirtualMachines.CreateOrUpdate(resourceGroupName, getVMWithInstanceViewResponse.Name, getVMWithInstanceViewResponse);
Assert.Equal(0, forceDetachVMResponse.StorageProfile.DataDisks.Count);
}
finally
{
Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", originalTestLocation);
m_ResourcesClient.ResourceGroups.Delete(resourceGroupName);
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Compute.Tests;
Expand Down
Loading