public void CheckPrerequisitiesTest_ShouldBeOk() { //arrange MigrationFlowData flowData = new MigrationFlowData(); ShrinkVhdVolumeMigrationStep step = new ShrinkVhdVolumeMigrationStep(loggerMock.Object, fileSystemHelperMock.Object, fileSystemMock.Object, flowData); //act string[] messages = new string[0]; bool result = step.CheckPrerequisities(ref messages); //assert Assert.IsTrue(result); }
public void PerformStepTest_ShouldFinishOkButDoNothing() { //arrange MigrationFlowData flowData = new MigrationFlowData(); flowData.VhdFileTemporaryFolder = "temporary\\folder"; flowData.VhdFileDestinationFolder = null; flowData.VhdFileName = "test.vhdx"; flowData.DestinationVhdMaxFileSize = 101; Mock <StreamWriter> streamWriterMock = new Mock <StreamWriter>(new MemoryStream()); streamWriterMock.Setup(x => x.WriteLine(It.IsAny <string>())) .Verifiable(); StreamWriter swMock = streamWriterMock.Object; this.fileSystemMock.Setup(x => x.File.CreateText(It.IsAny <string>())) .Returns <string>((path) => { return(swMock); }) .Verifiable(); this.fileSystemMock.Setup(x => x.Directory.CreateDirectory(It.IsAny <string>())) .Verifiable(); this.fileSystemMock.Setup(x => x.File.Delete(It.IsAny <string>())) .Verifiable(); ShrinkVhdVolumeMigrationStep step = new ShrinkVhdVolumeMigrationStep(loggerMock.Object, fileSystemHelperMock.Object, fileSystemMock.Object, flowData); //act step.PerformStep(); //assert streamWriterMock.Verify(x => x.WriteLine(It.Is <string>(str => str.Contains("shrink desired"))), Times.Never); this.fileSystemMock.Verify(x => x.File.CreateText(It.IsAny <string>()), Times.Never); this.fileSystemMock.Verify(x => x.File.Delete(It.IsAny <string>()), Times.Never); }
public void PerformStepTest_ShouldFinishOk() { //arrange MigrationFlowData flowData = new MigrationFlowData(); flowData.VhdFileTemporaryFolder = "temporary\\folder"; flowData.VhdFileDestinationFolder = "destination\\folder"; flowData.VhdFileName = "test.vhdx"; flowData.DestinationVhdMaxFileSize = 200000000; flowData.DesiredTempVhdShrinkSize = 100000000; long desiredShrinkSize = flowData.DesiredTempVhdShrinkSize >> 20; // / (1024 * 1024); Mock <StreamWriter> streamWriterMock = new Mock <StreamWriter>(new MemoryStream()); streamWriterMock.Setup(x => x.WriteLine(It.IsAny <string>())) .Verifiable(); StreamWriter swMock = streamWriterMock.Object; this.fileSystemMock.Setup(x => x.File.CreateText(It.IsAny <string>())) .Returns <string>((path) => { return(swMock); }) .Verifiable(); this.fileSystemMock.Setup(x => x.Directory.CreateDirectory(It.IsAny <string>())) .Verifiable(); this.fileSystemMock.Setup(x => x.File.Delete(It.IsAny <string>())) .Verifiable(); Mock <VirtualDiskDecorator> diskMock = new Mock <VirtualDiskDecorator>(); Mock <BiosPartitionTableDecorator> partitionTableMock = new Mock <BiosPartitionTableDecorator>(); Mock <PartitionInfoDecorator> partitionInfoMock = new Mock <PartitionInfoDecorator>(); Mock <SparseStream> sparseStreamMock = new Mock <SparseStream>(); sparseStreamMock.Setup(x => x.Length) .Returns(flowData.DestinationVhdMaxFileSize); SparseStream sparseStream = sparseStreamMock.Object; partitionInfoMock.Setup(x => x.Open()) .Returns(sparseStream) .Verifiable(); var partInfo = partitionInfoMock.Object; partitionTableMock.Setup(x => x.Partitions) .Returns(new List <PartitionInfoDecorator>() { partInfo }) .Verifiable(); diskMock.Setup(x => x.Partitions) .Returns(partitionTableMock.Object); this.fileSystemHelperMock.Setup(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileTemporaryFolder}\\{flowData.VhdFileName}"))) .Returns(diskMock.Object) .Verifiable(); ShrinkVhdVolumeMigrationStep step = new ShrinkVhdVolumeMigrationStep(loggerMock.Object, fileSystemHelperMock.Object, fileSystemMock.Object, flowData); //act step.PerformStep(); //assert streamWriterMock.Verify(x => x.WriteLine(It.Is <string>(str => str.Contains($"shrink desired={desiredShrinkSize}"))), Times.Once); this.fileSystemHelperMock.Verify(x => x.ExecuteDiskpart(It.IsAny <string>(), It.IsAny <ILogger>()), Times.Once); this.fileSystemMock.Verify(x => x.File.CreateText(It.IsAny <string>()), Times.Once); this.fileSystemMock.Verify(x => x.File.Delete(It.IsAny <string>()), Times.Once); this.fileSystemHelperMock.Verify(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileTemporaryFolder}\\{flowData.VhdFileName}")), Times.Once); }
public void PerformStepTest_ShouldThrowExceptionOnWrongShrinkSize() { //arrange MigrationFlowData flowData = new MigrationFlowData(); flowData.VhdFileTemporaryFolder = "temporary\\folder"; flowData.VhdFileDestinationFolder = "destination\\folder"; flowData.VhdFileName = "test.vhdx"; flowData.DestinationVhdMaxFileSize = 101; Mock <StreamWriter> streamWriterMock = new Mock <StreamWriter>(new MemoryStream()); streamWriterMock.Setup(x => x.WriteLine(It.IsAny <string>())) .Verifiable(); StreamWriter swMock = streamWriterMock.Object; this.fileSystemMock.Setup(x => x.File.CreateText(It.IsAny <string>())) .Returns <string>((path) => { return(swMock); }) .Verifiable(); this.fileSystemMock.Setup(x => x.Directory.CreateDirectory(It.IsAny <string>())) .Verifiable(); this.fileSystemMock.Setup(x => x.File.Delete(It.IsAny <string>())) .Verifiable(); Mock <VirtualDiskDecorator> diskMock = new Mock <VirtualDiskDecorator>(); Mock <BiosPartitionTableDecorator> partitionTableMock = new Mock <BiosPartitionTableDecorator>(); Mock <PartitionInfoDecorator> partitionInfoMock = new Mock <PartitionInfoDecorator>(); Mock <SparseStream> sparseStreamMock = new Mock <SparseStream>(); sparseStreamMock.Setup(x => x.Length) .Returns(flowData.DestinationVhdMaxFileSize + 1); //returns not supported vhd partition size SparseStream sparseStream = sparseStreamMock.Object; partitionInfoMock.Setup(x => x.Open()) .Returns(sparseStream) .Verifiable(); var partInfo = partitionInfoMock.Object; partitionTableMock.Setup(x => x.Partitions) .Returns(new List <PartitionInfoDecorator>() { partInfo }) .Verifiable(); diskMock.Setup(x => x.Partitions) .Returns(partitionTableMock.Object); this.fileSystemHelperMock.Setup(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileTemporaryFolder}\\{flowData.VhdFileName}"))) .Returns(diskMock.Object) .Verifiable(); ShrinkVhdVolumeMigrationStep step = new ShrinkVhdVolumeMigrationStep(loggerMock.Object, fileSystemHelperMock.Object, fileSystemMock.Object, flowData); //act Exception ex = Assert.Throws <Exception>(() => { step.PerformStep(); }); //assert Assert.IsTrue(ex.Message.Contains("Volume after shrink operation is too big to successfully migrate to system drive")); streamWriterMock.Verify(x => x.WriteLine(It.Is <string>(str => str.Contains("shrink desired"))), Times.Once); this.fileSystemHelperMock.Verify(x => x.ExecuteDiskpart(It.IsAny <string>(), It.IsAny <ILogger>()), Times.Once); this.fileSystemMock.Verify(x => x.File.CreateText(It.IsAny <string>()), Times.Once); this.fileSystemMock.Verify(x => x.File.Delete(It.IsAny <string>()), Times.Once); this.fileSystemHelperMock.Verify(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileTemporaryFolder}\\{flowData.VhdFileName}")), Times.Once); }