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

Retrieving Disk Paths via DiskManager #440

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,24 @@ public void UnixDiskManagerAppliesRetriesOnFailedAttemptsToCreateMountPoints()
Assert.IsTrue(attempts == 4);
}

[Test]
public async Task UnixDiskManagerReturnsListofDiskPaths()
{
this.testProcess.OnHasExited = () => true;
this.testProcess.OnStart = () => true;
this.testProcess.StandardOutput.Append(Resources.lshw_disk_storage_results);

List<string> accessPaths = new List<string>
{
"/mnt",
};

IEnumerable<string> diskPaths = await this.diskManager.GetDiskPathsAsync("osdisk:false", CancellationToken.None)
.ConfigureAwait(false);

CollectionAssert.AreEqual(diskPaths, accessPaths);
}

private class TestUnixDiskManager : UnixDiskManager
{
public TestUnixDiskManager(ProcessManager processManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,94 @@ public async Task WindowsDiskManagerGetsTheExpectedDisks_Scenario1()
actualDisks.ToList().ForEach(disk => Assert.IsTrue(disk.Volumes.Count() == 2));
}

[Test]
public async Task WindowsDiskManagerReturnsListofDiskPaths()
{
this.testProcess.OnHasExited = () => true;
this.testProcess.OnStart = () => true;

this.standardInput.BytesWritten += (sender, data) =>
{
string input = data.ToString().Trim();
if (input.Contains($"select disk"))
{
int diskIndex = int.Parse(Regex.Match(input, "[0-9]+").Value);
this.testProcess.StandardOutput.Append($"Disk {diskIndex} is now the selected disk.");
}
else if (input.Contains($"select partition"))
{
int partitionIndex = int.Parse(Regex.Match(input, "[0-9]+").Value);
this.testProcess.StandardOutput.Append($"Partition {partitionIndex} is now the selected partition.");
}
else if (input.Contains("list disk"))
{
StringBuilder listDiskResults = new StringBuilder()
.AppendLine(" ")
.AppendLine(" Disk ### Status Size Free Dyn Gpt")
.AppendLine(" -------- ------------- ------- ------- --- ---")
.AppendLine(" Disk 0 Online 127 GB 1024 KB ");

this.testProcess.StandardOutput.Append(listDiskResults.ToString());
}
else if (input.Contains("list partition"))
{
StringBuilder listPartitionResults = new StringBuilder()
.AppendLine(" ")
.AppendLine(" Partition ### Type Size Offset")
.AppendLine(" ------------- ---------------- ------- -------")
.AppendLine(" Partition 1 Primary 500 MB 1024 KB")
.AppendLine(" Partition 2 Primary 126 GB 501 MB");

this.testProcess.StandardOutput.Append(listPartitionResults.ToString());
}
else if (input.Contains($"detail disk"))
{
StringBuilder detailDiskResults = new StringBuilder()
.AppendLine(" ")
.AppendLine("Virtual HD ATA Device")
.AppendLine("Disk ID: EF349D83")
.AppendLine("Type : ATA")
.AppendLine("Status : Online")
.AppendLine("Path : 0")
.AppendLine("Target : 0")
.AppendLine("LUN ID : 0")
.AppendLine(" ")
.AppendLine(" Volume ### Ltr Label Fs Type Size Status Info ")
.AppendLine(" ---------- --- ----------- ----- ---------- ------- --------- --------")
.AppendLine(" Volume 0 System Rese NTFS Partition 500 MB Healthy System ")
.AppendLine(" Volume 1 C Windows NTFS Partition 126 GB Healthy Boot ");

this.testProcess.StandardOutput.Append(detailDiskResults.ToString());
}
else if (input.Contains($"detail partition"))
{
StringBuilder detailPartitionResults = new StringBuilder()
.AppendLine(" ")
.AppendLine("Partition 1")
.AppendLine("Type : 07")
.AppendLine("Hidden: No")
.AppendLine("Active: Yes")
.AppendLine("Offset in Bytes: 525336576")
.AppendLine(" ")
.AppendLine(" Volume ### Ltr Label Fs Type Size Status Info ")
.AppendLine(" ---------- --- ----------- ----- ---------- ------- --------- --------")
.AppendLine(" Volume 1 C Windows NTFS Partition 126 GB Healthy Boot ");

this.testProcess.StandardOutput.Append(detailPartitionResults.ToString());
}
};

List<string> accessPaths = new List<string>
{
"C:\\"
};

IEnumerable<string> diskPaths = await this.diskManager.GetDiskPathsAsync(DiskFilters.DefaultDiskFilter, CancellationToken.None)
.ConfigureAwait(false);

CollectionAssert.AreEqual(diskPaths, accessPaths);
}

private class TestWindowsDiskManager : WindowsDiskManager
{
public TestWindowsDiskManager(ProcessManager processManager)
Expand Down
3 changes: 3 additions & 0 deletions src/VirtualClient/VirtualClient.Core/DiskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@ protected DiskManager(ILogger logger)

/// <inheritdoc/>
public abstract Task<IEnumerable<Disk>> GetDisksAsync(CancellationToken cancellationToken);

/// <inheritdoc/>
public abstract Task<IEnumerable<string>> GetDiskPathsAsync(string diskFilter, CancellationToken cancellationToken);
}
}
7 changes: 7 additions & 0 deletions src/VirtualClient/VirtualClient.Core/IDiskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@ public interface IDiskManager
/// </summary>
/// <param name="cancellationToken">A token that can be used to cancel the operation.</param>
Task<IEnumerable<Disk>> GetDisksAsync(CancellationToken cancellationToken);

/// <summary>
/// Grabs the available data directory on the system.
/// </summary>
/// <param name="cancellationToken"></param>
/// <param name="diskFilter">Disk filter to </param>
Task<IEnumerable<string>> GetDiskPathsAsync(string diskFilter, CancellationToken cancellationToken);
}
}
39 changes: 39 additions & 0 deletions src/VirtualClient/VirtualClient.Core/UnixDiskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,45 @@ public override Task<IEnumerable<Disk>> GetDisksAsync(CancellationToken cancella
});
}

/// <summary>
/// Grabs the available data directory on the system.
/// </summary>
/// <param name="cancellationToken"></param>
/// <param name="diskFilter">Disk filter to </param>
/// <returns></returns>
/// <exception cref="WorkloadException"></exception>
public override Task<IEnumerable<string>> GetDiskPathsAsync(string diskFilter, CancellationToken cancellationToken)
{
List<string> diskPaths = new List<string>();

return this.Logger.LogMessageAsync($"{nameof(UnixDiskManager)}.GetDiskPaths", EventContext.Persisted(), async () =>
{
IEnumerable<Disk> disks = await this.GetDisksAsync(cancellationToken).ConfigureAwait(false);
IEnumerable<Disk> disksToTest = DiskFilters.FilterDisks(disks, diskFilter, PlatformID.Unix).ToList();

if (disksToTest?.Any() != true)
{
throw new WorkloadException(
"Expected disks to test not found. Given the parameters defined for the profile action/step or those passed " +
"in on the command line, the requisite disks do not exist on the system or could not be identified based on the properties " +
"of the existing disks.",
ErrorReason.DependencyNotFound);
}

foreach (Disk disk in disksToTest)
{
string diskPath = $"{disk.GetPreferredAccessPath(PlatformID.Unix)}";

if (!string.IsNullOrEmpty(diskPath))
{
diskPaths.Add(diskPath);
}
}

return diskPaths.AsEnumerable();
});
}

private Task AssignMountPointAsync(DiskVolume volume, string mountPoint, EventContext telemetryContext, CancellationToken cancellationToken)
{
int retries = -1;
Expand Down
39 changes: 39 additions & 0 deletions src/VirtualClient/VirtualClient.Core/WindowsDiskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,45 @@ await process.WriteInput(command)
return disks;
}

/// <summary>
/// Grabs the available data directory on the system.
/// </summary>
/// <param name="cancellationToken"></param>
/// <param name="diskFilter">Disk filter to </param>
/// <returns></returns>
/// <exception cref="WorkloadException"></exception>
public override Task<IEnumerable<string>> GetDiskPathsAsync(string diskFilter, CancellationToken cancellationToken)
{
List<string> diskPaths = new List<string>();

return this.Logger.LogMessageAsync($"{nameof(WindowsDiskManager)}.GetDiskPaths", EventContext.Persisted(), async () =>
{
IEnumerable<Disk> disks = await this.GetDisksAsync(cancellationToken).ConfigureAwait(false);
IEnumerable<Disk> disksToTest = DiskFilters.FilterDisks(disks, diskFilter, PlatformID.Win32NT).ToList();

if (disksToTest?.Any() != true)
{
throw new WorkloadException(
"Expected disks to test not found. Given the parameters defined for the profile action/step or those passed " +
"in on the command line, the requisite disks do not exist on the system or could not be identified based on the properties " +
"of the existing disks.",
ErrorReason.DependencyNotFound);
}

foreach (Disk disk in disksToTest)
{
string diskPath = $"{disk.GetPreferredAccessPath(PlatformID.Win32NT)}";

if (!string.IsNullOrEmpty(diskPath))
{
diskPaths.Add(diskPath);
}
}

return diskPaths.AsEnumerable();
});
}

/// <summary>
/// Returns as set of lines that are all the exact same width.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ public Task<IEnumerable<Disk>> GetDisksAsync(CancellationToken cancellationToken
return Task.FromResult((IEnumerable<Disk>)this);
}

/// <summary>
/// Grabs the available data directory on the system.
/// </summary>
/// <param name="cancellationToken"></param>
/// <param name="diskFilter">Disk filter to </param>
public Task<IEnumerable<string>> GetDiskPathsAsync(string diskFilter, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

private void AddVolumeToDisk(Disk disk, FileSystemType fileSystemType)
{
DiskVolume newVolume = null;
Expand Down
Loading