|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace VirtualClient.Dependencies |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Linq; |
| 9 | + using System.Threading; |
| 10 | + using System.Threading.Tasks; |
| 11 | + using Moq; |
| 12 | + using NUnit.Framework; |
| 13 | + using VirtualClient.Common; |
| 14 | + using VirtualClient.Common.Telemetry; |
| 15 | + using VirtualClient.Contracts; |
| 16 | + |
| 17 | + [TestFixture] |
| 18 | + [Category("Unit")] |
| 19 | + public class StripeDisksTests |
| 20 | + { |
| 21 | + private const string PackageName = "system_config"; |
| 22 | + private MockFixture mockFixture; |
| 23 | + private DependencyPath systemConfigPackage; |
| 24 | + private IEnumerable<Disk> linuxDisks; |
| 25 | + |
| 26 | + [SetUp] |
| 27 | + public void SetupTest() |
| 28 | + { |
| 29 | + this.mockFixture = new MockFixture(); |
| 30 | + this.mockFixture.Setup(PlatformID.Unix); |
| 31 | + this.mockFixture.File.Setup(f => f.Exists(It.IsAny<string>())).Returns(true); |
| 32 | + this.mockFixture.Parameters["PackageName"] = PackageName; |
| 33 | + this.SetupSystemConfigPackage(); |
| 34 | + |
| 35 | + this.linuxDisks = this.mockFixture.CreateDisks(PlatformID.Unix); |
| 36 | + this.mockFixture.SetupDisks(this.linuxDisks.ToArray()); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public async Task StripeDisksExecutesTheExpectedCommand() |
| 41 | + { |
| 42 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 43 | + |
| 44 | + string expectedScriptPath = this.GetExpectedScriptPath("stripe_disks.sh"); |
| 45 | + string expectedMountDir = $"/home/{Environment.UserName}/mnt_raid0"; |
| 46 | + string expectedDiskPaths = string.Join(",", this.linuxDisks.Where(d => !d.IsOperatingSystem()).Select(d => d.DevicePath)); |
| 47 | + |
| 48 | + bool confirmed = false; |
| 49 | + this.mockFixture.ProcessManager.OnProcessCreated = (process) => |
| 50 | + { |
| 51 | + string expectedCommand = $"sudo bash {expectedScriptPath} --disks \"{expectedDiskPaths}\" --mountDirectory {expectedMountDir}"; |
| 52 | + if (process.FullCommand() == expectedCommand) |
| 53 | + { |
| 54 | + confirmed = true; |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 59 | + { |
| 60 | + await component.ExecuteAsync(CancellationToken.None); |
| 61 | + } |
| 62 | + |
| 63 | + Assert.IsTrue(confirmed); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public async Task StripeDisksLimitsDisksByDiskCount() |
| 68 | + { |
| 69 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 70 | + this.mockFixture.Parameters["DiskCount"] = 2; |
| 71 | + |
| 72 | + string expectedScriptPath = this.GetExpectedScriptPath("stripe_disks.sh"); |
| 73 | + string expectedMountDir = $"/home/{Environment.UserName}/mnt_raid0"; |
| 74 | + IEnumerable<Disk> nonOsDisks = this.linuxDisks.Where(d => !d.IsOperatingSystem()); |
| 75 | + string expectedDiskPaths = string.Join(",", nonOsDisks.OrderByDescending(d => d.SizeInBytes(PlatformID.Unix)).Take(2).Select(d => d.DevicePath)); |
| 76 | + |
| 77 | + bool confirmed = false; |
| 78 | + this.mockFixture.ProcessManager.OnProcessCreated = (process) => |
| 79 | + { |
| 80 | + string expectedCommand = $"sudo bash {expectedScriptPath} --disks \"{expectedDiskPaths}\" --mountDirectory {expectedMountDir}"; |
| 81 | + if (process.FullCommand() == expectedCommand) |
| 82 | + { |
| 83 | + confirmed = true; |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 88 | + { |
| 89 | + await component.ExecuteAsync(CancellationToken.None); |
| 90 | + } |
| 91 | + |
| 92 | + Assert.IsTrue(confirmed); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public async Task StripeDisksResolvesTheMountDirectoryForNonRootUser() |
| 97 | + { |
| 98 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 99 | + |
| 100 | + string expectedMountDir = $"/home/{Environment.UserName}/mnt_raid0"; |
| 101 | + |
| 102 | + bool confirmed = false; |
| 103 | + this.mockFixture.ProcessManager.OnProcessCreated = (process) => |
| 104 | + { |
| 105 | + if (process.FullCommand().Contains($"--mountDirectory {expectedMountDir}")) |
| 106 | + { |
| 107 | + confirmed = true; |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 112 | + { |
| 113 | + await component.ExecuteAsync(CancellationToken.None); |
| 114 | + } |
| 115 | + |
| 116 | + Assert.IsTrue(confirmed); |
| 117 | + } |
| 118 | + |
| 119 | + [Test] |
| 120 | + public async Task StripeDisksResolvesTheMountDirectoryForRootUser() |
| 121 | + { |
| 122 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 123 | + |
| 124 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 125 | + { |
| 126 | + component.PlatformSpecifics.SetEnvironmentVariable(EnvironmentVariable.SUDO_USER, "root"); |
| 127 | + |
| 128 | + bool confirmed = false; |
| 129 | + this.mockFixture.ProcessManager.OnProcessCreated = (process) => |
| 130 | + { |
| 131 | + if (process.FullCommand().Contains("--mountDirectory /mnt_raid0")) |
| 132 | + { |
| 133 | + confirmed = true; |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + await component.ExecuteAsync(CancellationToken.None); |
| 138 | + Assert.IsTrue(confirmed); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + [Test] |
| 143 | + public async Task StripeDisksResolvesTheMountDirectoryWhenSudoUserIsSet() |
| 144 | + { |
| 145 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 146 | + |
| 147 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 148 | + { |
| 149 | + component.PlatformSpecifics.SetEnvironmentVariable(EnvironmentVariable.SUDO_USER, "user01"); |
| 150 | + |
| 151 | + bool confirmed = false; |
| 152 | + this.mockFixture.ProcessManager.OnProcessCreated = (process) => |
| 153 | + { |
| 154 | + if (process.FullCommand().Contains("--mountDirectory /home/user01/mnt_raid0")) |
| 155 | + { |
| 156 | + confirmed = true; |
| 157 | + } |
| 158 | + }; |
| 159 | + |
| 160 | + await component.ExecuteAsync(CancellationToken.None); |
| 161 | + Assert.IsTrue(confirmed); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + [Test] |
| 166 | + [TestCase("mount_points")] |
| 167 | + [TestCase("/mount_points")] |
| 168 | + [TestCase("/mount_points/")] |
| 169 | + [TestCase(" /mount_points/ ")] |
| 170 | + public async Task StripeDisksResolvesTheMountDirectoryWhenMountLocationIsProvided(string mountLocation) |
| 171 | + { |
| 172 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 173 | + this.mockFixture.Parameters["MountLocation"] = mountLocation; |
| 174 | + |
| 175 | + string expectedMountDir = $"/{mountLocation.Trim().Trim('/')}/mnt_raid0"; |
| 176 | + |
| 177 | + bool confirmed = false; |
| 178 | + this.mockFixture.ProcessManager.OnProcessCreated = (process) => |
| 179 | + { |
| 180 | + if (process.FullCommand().Contains($"--mountDirectory {expectedMountDir}")) |
| 181 | + { |
| 182 | + confirmed = true; |
| 183 | + } |
| 184 | + }; |
| 185 | + |
| 186 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 187 | + { |
| 188 | + await component.ExecuteAsync(CancellationToken.None); |
| 189 | + } |
| 190 | + |
| 191 | + Assert.IsTrue(confirmed); |
| 192 | + } |
| 193 | + |
| 194 | + [Test] |
| 195 | + public async Task StripeDisksResolvesTheMountDirectoryWithCustomPrefix() |
| 196 | + { |
| 197 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 198 | + this.mockFixture.Parameters["MountPointPrefix"] = "mnt_test"; |
| 199 | + |
| 200 | + string expectedMountDir = $"/home/{Environment.UserName}/mnt_test_raid0"; |
| 201 | + |
| 202 | + bool confirmed = false; |
| 203 | + this.mockFixture.ProcessManager.OnProcessCreated = (process) => |
| 204 | + { |
| 205 | + if (process.FullCommand().Contains($"--mountDirectory {expectedMountDir}")) |
| 206 | + { |
| 207 | + confirmed = true; |
| 208 | + } |
| 209 | + }; |
| 210 | + |
| 211 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 212 | + { |
| 213 | + await component.ExecuteAsync(CancellationToken.None); |
| 214 | + } |
| 215 | + |
| 216 | + Assert.IsTrue(confirmed); |
| 217 | + } |
| 218 | + |
| 219 | + [Test] |
| 220 | + public void StripeDisksThrowsWhenScriptIsNotFound() |
| 221 | + { |
| 222 | + this.mockFixture.File.Setup(f => f.Exists(It.IsAny<string>())).Returns(false); |
| 223 | + |
| 224 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 225 | + { |
| 226 | + DependencyException exc = Assert.ThrowsAsync<DependencyException>(() => component.ExecuteAsync(CancellationToken.None)); |
| 227 | + Assert.AreEqual(ErrorReason.DependencyNotFound, exc.Reason); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + [Test] |
| 232 | + public void StripeDisksThrowsWhenNoDisksMatchFilter() |
| 233 | + { |
| 234 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 235 | + this.mockFixture.SetupDisks(this.linuxDisks.Where(d => d.IsOperatingSystem()).ToArray()); |
| 236 | + |
| 237 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 238 | + { |
| 239 | + DependencyException exc = Assert.ThrowsAsync<DependencyException>(() => component.ExecuteAsync(CancellationToken.None)); |
| 240 | + Assert.AreEqual(ErrorReason.DependencyNotFound, exc.Reason); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + [Test] |
| 245 | + public void StripeDisksThrowsWhenDiskCountExceedsAvailableDisks() |
| 246 | + { |
| 247 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 248 | + int nonOsDiskCount = this.linuxDisks.Count(d => !d.IsOperatingSystem()); |
| 249 | + this.mockFixture.Parameters["DiskCount"] = nonOsDiskCount + 1; |
| 250 | + |
| 251 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 252 | + { |
| 253 | + DependencyException exc = Assert.ThrowsAsync<DependencyException>(() => component.ExecuteAsync(CancellationToken.None)); |
| 254 | + Assert.AreEqual(ErrorReason.DependencyNotFound, exc.Reason); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + [Test] |
| 259 | + public async Task StripeDisksExecutesTheExpectedCommandOnWindows() |
| 260 | + { |
| 261 | + this.mockFixture = new MockFixture(); |
| 262 | + this.mockFixture.Setup(PlatformID.Win32NT); |
| 263 | + this.mockFixture.File.Setup(f => f.Exists(It.IsAny<string>())).Returns(true); |
| 264 | + this.mockFixture.Parameters["PackageName"] = PackageName; |
| 265 | + this.SetupSystemConfigPackage(); |
| 266 | + this.mockFixture.Parameters["DiskFilter"] = "OSDisk:false"; |
| 267 | + |
| 268 | + IEnumerable<Disk> windowsDisks = this.mockFixture.CreateDisks(PlatformID.Win32NT); |
| 269 | + this.mockFixture.SetupDisks(windowsDisks.ToArray()); |
| 270 | + string expectedDiskPaths = string.Join(",", windowsDisks.Where(d => !d.IsOperatingSystem()).Select(d => d.DevicePath)); |
| 271 | + |
| 272 | + string expectedScriptPath = this.GetExpectedScriptPath("stripe_disks.cmd"); |
| 273 | + |
| 274 | + bool confirmed = false; |
| 275 | + this.mockFixture.ProcessManager.OnProcessCreated = (process) => |
| 276 | + { |
| 277 | + string expectedCommand = $"cmd /c {expectedScriptPath} --disks \"{expectedDiskPaths}\""; |
| 278 | + if (process.FullCommand() == expectedCommand) |
| 279 | + { |
| 280 | + confirmed = true; |
| 281 | + } |
| 282 | + }; |
| 283 | + |
| 284 | + using (StripeDisks component = new StripeDisks(this.mockFixture.Dependencies, this.mockFixture.Parameters)) |
| 285 | + { |
| 286 | + await component.ExecuteAsync(CancellationToken.None); |
| 287 | + } |
| 288 | + |
| 289 | + Assert.IsTrue(confirmed); |
| 290 | + } |
| 291 | + |
| 292 | + private void SetupSystemConfigPackage() |
| 293 | + { |
| 294 | + this.systemConfigPackage = new DependencyPath( |
| 295 | + PackageName, |
| 296 | + this.mockFixture.GetPackagePath(PackageName)); |
| 297 | + |
| 298 | + this.mockFixture.SetupPackage(this.systemConfigPackage); |
| 299 | + } |
| 300 | + |
| 301 | + private string GetExpectedScriptPath(string scriptFileName) |
| 302 | + { |
| 303 | + return this.mockFixture.Combine( |
| 304 | + this.systemConfigPackage.Path, |
| 305 | + this.mockFixture.PlatformSpecifics.PlatformArchitectureName, |
| 306 | + scriptFileName); |
| 307 | + } |
| 308 | + } |
| 309 | +} |
0 commit comments