Beispiel #1
0
        public void PerformStepTest_ShouldFinishOkButDoNothing()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder     = "temporary\\folder";
            flowData.VhdFileDestinationFolder   = null;
            flowData.OperatingSystemDriveLetter = 'C';
            flowData.VhdFileName = "test.vhdx";

            Mock <VirtualDiskDecorator>        diskMock           = new Mock <VirtualDiskDecorator>();
            Mock <BiosPartitionTableDecorator> partitionTableMock = new Mock <BiosPartitionTableDecorator>();
            Mock <PartitionInfoDecorator>      partitionInfoMock  = new Mock <PartitionInfoDecorator>();

            CloneTempVhdToFinalVhdMigrationStep step = new CloneTempVhdToFinalVhdMigrationStep(loggerMock.Object,
                                                                                               fileSystemHelperMock.Object,
                                                                                               fileSystemMock.Object,
                                                                                               flowData);

            //act
            step.PerformStep();

            //assert
            partitionTableMock.Verify(x => x.CreatePrimaryBySector(It.IsAny <long>(), It.IsAny <long>(), It.IsAny <byte>(), It.IsAny <bool>()), Times.Never);
            this.fileSystemHelperMock.Verify(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileTemporaryFolder}\\{flowData.VhdFileName}")), Times.Never);
            this.fileSystemHelperMock.Verify(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileDestinationFolder}\\{flowData.VhdFileName}")), Times.Never);
            this.fileSystemHelperMock.Verify(x => x.CloneNtfsFileSystem(It.IsAny <Stream>(), It.IsAny <Stream>(), It.IsAny <ILogger>()), Times.Never);
        }
        public void CheckPrerequisitiesTest_ShouldFail()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder = "temporary\\folder";
            flowData.AddVhdToBootManager    = true;

            this.fileSystemMock.Setup(x => x.File.Exists(It.IsAny <string>()))
            .Returns <string>((path) =>
            {
                return(false);
            });

            AddFinalVhdToBootManagerMigrationStep step = new AddFinalVhdToBootManagerMigrationStep(loggerMock.Object,
                                                                                                   fileSystemHelperMock.Object,
                                                                                                   fileSystemMock.Object,
                                                                                                   flowData);

            //act
            string[] messages = new string[0];
            bool     result   = step.CheckPrerequisities(ref messages);

            //assert
            Assert.IsFalse(result);
            Assert.AreEqual(1, messages.Where(x => x.Contains("BCDEDIT was not found on your system")).Count());
        }
        public void PerformStepTest_ShouldFinishOkButDoNothing()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder = "temporary\\folder";
            flowData.AddVhdToBootManager    = false;


            AddFinalVhdToBootManagerMigrationStep step = new AddFinalVhdToBootManagerMigrationStep(loggerMock.Object,
                                                                                                   fileSystemHelperMock.Object,
                                                                                                   fileSystemMock.Object,
                                                                                                   flowData);

            //act
            step.PerformStep();

            //assert
            this.fileSystemHelperMock.Verify(x => x.ExecuteBcdeditCommand(
                                                 It.Is <string>(script => script.Contains("/copy")),
                                                 It.IsAny <ILogger>()), Times.Never);

            this.fileSystemHelperMock.Verify(x => x.ExecuteBcdeditCommand(
                                                 It.Is <string>(script => script.Contains("/set")),
                                                 It.IsAny <ILogger>()), Times.Never);
        }
        public void PerformStepTest_ShouldNotBeAbleToCreateNewBootManagerEntry()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder = "temporary\\folder";
            flowData.AddVhdToBootManager    = true;

            Guid newEntryId = Guid.NewGuid();

            this.fileSystemHelperMock.Setup(x => x.ExecuteBcdeditCommand(
                                                It.Is <string>(script => script.Contains("/copy")),
                                                It.IsAny <ILogger>()))
            .Returns($"Operation failed");

            AddFinalVhdToBootManagerMigrationStep step = new AddFinalVhdToBootManagerMigrationStep(loggerMock.Object,
                                                                                                   fileSystemHelperMock.Object,
                                                                                                   fileSystemMock.Object,
                                                                                                   flowData);

            //act
            ArgumentException aex = Assert.Throws <ArgumentException>(() =>
            {
                step.PerformStep();
            });

            //assert
            this.fileSystemHelperMock.Verify(x => x.ExecuteBcdeditCommand(
                                                 It.Is <string>(script => script.Contains("/copy")),
                                                 It.IsAny <ILogger>()), Times.Once);

            this.fileSystemHelperMock.Verify(x => x.ExecuteBcdeditCommand(
                                                 It.Is <string>(script => script.Contains("/set") && script.Contains(newEntryId.ToString())),
                                                 It.IsAny <ILogger>()), Times.Never);
        }
        public void CheckPrerequisitiesTest_ShouldFail()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileDestinationFolder = "destination\\folder";

            this.fileSystemMock.Setup(x => x.File.Exists(It.IsAny <string>()))
            .Returns <string>((path) =>
            {
                return(path == flowData.VhdFileDestinationFolder + "\\" ? true : false);
            });

            CreateFinalVhdMigrationStep step = new CreateFinalVhdMigrationStep(loggerMock.Object,
                                                                               fileSystemHelperMock.Object,
                                                                               fileSystemMock.Object,
                                                                               flowData);

            //act
            string[] messages = new string[0];
            bool     result   = step.CheckPrerequisities(ref messages);

            //assert
            Assert.IsFalse(result);
            Assert.AreEqual(1, messages.Where(x => x.Contains("already exists")).Count());
            Assert.AreEqual(1, messages.Where(x => x.Contains("DISKPART was not found")).Count());
        }
        public void CheckPrerequisitiesTest_ShouldBeOk()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileDestinationFolder = "destination\\folder";

            this.fileSystemMock.Setup(x => x.File.Exists(It.IsAny <string>()))
            .Returns <string>((path) =>
            {
                return(path == flowData.VhdFileDestinationFolder + "\\" ? false : true);
            });

            CreateFinalVhdMigrationStep step = new CreateFinalVhdMigrationStep(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 CheckPrerequisitiesTest_ShouldFail()
        {
            //arrange
            osHelperMock.Setup(x => x.IsOperatingSystemSupported())
            .Returns(() => { return(false); });

            osHelperMock.Setup(x => x.IsBitlockerEnabledOnSystemDrive())
            .Returns(() => { return(true); });

            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder   = null;
            flowData.VhdFileDestinationFolder = "final\\path";

            systemDriveInfoMock.Setup(x => x.AvailableFreeSpace)
            .Returns(49);
            systemDriveInfoMock.Setup(x => x.TotalSize)
            .Returns(100);

            PrepareMigrationStep step = new PrepareMigrationStep(loggerMock.Object,
                                                                 osHelperMock.Object,
                                                                 fileSystemHelperMock.Object,
                                                                 flowData);

            //act
            string[] messages = new string[0];
            bool     result   = step.CheckPrerequisities(ref messages);

            //assert
            Assert.IsFalse(result);
            Assert.AreEqual(1, messages.Where(x => x.Contains("store temporary")).Count());
            Assert.AreEqual(1, messages.Where(x => x.Contains("Not supported operating system detected")).Count());
            Assert.AreEqual(1, messages.Where(x => x.Contains("Not enough available free space on system drive")).Count());
            Assert.AreEqual(1, messages.Where(x => x.Contains("Migration of operating system to VHD native boot is not supported when BitLocker is enabled")).Count());
        }
Beispiel #8
0
 public AddFinalVhdToBootManagerMigrationStep(ILogger <AddFinalVhdToBootManagerMigrationStep> logger,
                                              IFileSystemHelper fileSystemHelper,
                                              IFileSystem fileSystem,
                                              MigrationFlowData migrationData) : base(migrationData)
 {
     this.logger           = logger;
     this.fileSystemHelper = fileSystemHelper;
     this.fileSystem       = fileSystem;
 }
Beispiel #9
0
 public PrepareMigrationStep(ILogger <PrepareMigrationStep> logger,
                             IOperatingSystemHelper osHelper,
                             IFileSystemHelper fileSystemHelper,
                             MigrationFlowData migrationData) : base(migrationData)
 {
     this.logger           = logger;
     this.osHelper         = osHelper;
     this.fileSystemHelper = fileSystemHelper;
 }
 public CreateFinalVhdMigrationStep(ILogger <CreateFinalVhdMigrationStep> logger,
                                    IFileSystemHelper fileSystemHelper,
                                    IFileSystem fileSystem,
                                    MigrationFlowData migrationData) : base(migrationData)
 {
     this.logger           = logger;
     this.fileSystemHelper = fileSystemHelper;
     this.fileSystem       = fileSystem;
 }
 public FixClonedOsRegistryMigrationStep(ILogger <FixClonedOsRegistryMigrationStep> logger,
                                         IFileSystemHelper fileSystemHelper,
                                         System.IO.Abstractions.IFileSystem fileSystem,
                                         MigrationFlowData migrationData) : base(migrationData)
 {
     this.logger           = logger;
     this.fileSystemHelper = fileSystemHelper;
     this.fileSystem       = fileSystem;
 }
Beispiel #12
0
 public ShrinkVhdVolumeMigrationStep(ILogger <ShrinkVhdVolumeMigrationStep> logger,
                                     IFileSystemHelper fileSystemHelper,
                                     System.IO.Abstractions.IFileSystem fileSystem,
                                     MigrationFlowData migrationData) : base(migrationData)
 {
     this.logger           = logger;
     this.fileSystemHelper = fileSystemHelper;
     this.fileSystem       = fileSystem;
 }
 public CloneTempVhdToFinalVhdMigrationStep(ILogger <CloneTempVhdToFinalVhdMigrationStep> logger,
                                            IFileSystemHelper fileSystemHelper,
                                            System.IO.Abstractions.IFileSystem fileSystem,
                                            MigrationFlowData migrationData) : base(migrationData)
 {
     this.logger           = logger;
     this.fileSystemHelper = fileSystemHelper;
     this.fileSystem       = fileSystem;
 }
Beispiel #14
0
 public CloneOSMigrationStep(ILogger <CloneOSMigrationStep> logger,
                             IOperatingSystemHelper osHelper,
                             IFileSystemHelper fileSystemHelper,
                             MigrationFlowData migrationData,
                             IVssImplementation vss) : base(migrationData)
 {
     this.logger           = logger;
     this.osHelper         = osHelper;
     this.fileSystemHelper = fileSystemHelper;
     this.vss = vss;
 }
 public CreateTemporaryVhdMigrationStep(ILogger <CreateTemporaryVhdMigrationStep> logger,
                                        IOperatingSystemHelper osHelper,
                                        IFileSystemHelper fileSystemHelper,
                                        IFileSystem fileSystem,
                                        MigrationFlowData migrationData) : base(migrationData)
 {
     this.logger           = logger;
     this.osHelper         = osHelper;
     this.fileSystemHelper = fileSystemHelper;
     this.fileSystem       = fileSystem;
 }
        public void PerformStepTest_ShouldFinishOk()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder   = "temporary\\folder";
            flowData.VhdFileDestinationFolder = "destination\\folder";
            flowData.VhdFileName = "test.vhdx";
            flowData.VhdFileType = Logic.Models.Enums.VhdType.VhdxDynamic;
            flowData.DestinationVhdMaxFileSize = 200000000;
            flowData.DesiredTempVhdShrinkSize  = 100000000;
            string vhdFullName        = $"{flowData.VhdFileDestinationFolder}\\{flowData.VhdFileName}";
            long   maximumVhdSizeInMb = flowData.DestinationVhdMaxFileSize >> 20;// / (1024 * 1024);
            string vhdTypeString      = flowData.VhdFileType == VhdType.VhdDynamic || flowData.VhdFileType == VhdType.VhdxDynamic
                ? "expandable" : "fixed";


            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();

            CreateFinalVhdMigrationStep step = new CreateFinalVhdMigrationStep(loggerMock.Object,
                                                                               fileSystemHelperMock.Object,
                                                                               fileSystemMock.Object,
                                                                               flowData);

            //act
            step.PerformStep();

            //assert
            streamWriterMock.Verify(x => x.WriteLine(It.Is <string>(str => str.Contains($"create vdisk file={vhdFullName} maximum={maximumVhdSizeInMb} type={vhdTypeString}"))), Times.Once);
            streamWriterMock.Verify(x => x.WriteLine(It.Is <string>(str => str.Contains($"select vdisk file={vhdFullName}"))), 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);
        }
Beispiel #17
0
 private static void DisplaySummary(MigrationFlowData migrationData, ILogger logger)
 {
     logger.LogInformation($"Configuration that will be used for migration:");
     if (migrationData.TemporaryVhdFileIsTheFinalOne)
     {
         logger.LogInformation($"Destination image folder: {migrationData.VhdFileTemporaryFolder}");
     }
     else
     {
         logger.LogInformation($"Temporary image folder: {migrationData.VhdFileTemporaryFolder}");
         logger.LogInformation($"Destination image folder: {migrationData.VhdFileDestinationFolder}");
         logger.LogInformation($"Temporary image will be deleted after completion: {migrationData.DeleteTemporaryVhdFile}");
     }
     logger.LogInformation($"Image file name: {migrationData.VhdFileName}");
     logger.LogInformation($"Image will be added to boot manager: {migrationData.AddVhdToBootManager}");
 }
        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);
        }
Beispiel #19
0
        public void CheckPrerequisitiesTest_ShouldBeOk()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder = "temporary\\folder";

            CloneTempVhdToFinalVhdMigrationStep step = new CloneTempVhdToFinalVhdMigrationStep(loggerMock.Object,
                                                                                               fileSystemHelperMock.Object,
                                                                                               fileSystemMock.Object,
                                                                                               flowData);

            //act
            string[] messages = new string[0];
            bool     result   = step.CheckPrerequisities(ref messages);

            //assert
            Assert.IsTrue(result);
        }
Beispiel #20
0
        public void PerformStepTest_ShouldNotCreateTempDir()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder = "temporary\\folder";

            StreamWriter swMock = new StreamWriter(new MemoryStream());

            this.fileSystemMock.Setup(x => x.File.CreateText(It.IsAny <string>()))
            .Returns <string>((path) =>
            {
                return(swMock);
            })
            .Verifiable();

            this.fileSystemMock.Setup(x => x.Directory.Exists(It.IsAny <string>()))
            .Returns <string>((path) =>
            {
                return(path == flowData.VhdFileTemporaryFolder ? true : false);
            });

            this.fileSystemMock.Setup(x => x.Directory.CreateDirectory(It.IsAny <string>()))
            .Verifiable();

            this.fileSystemMock.Setup(x => x.File.Delete(It.IsAny <string>()))
            .Verifiable();

            CreateTemporaryVhdMigrationStep step = new CreateTemporaryVhdMigrationStep(loggerMock.Object,
                                                                                       osHelperMock.Object,
                                                                                       fileSystemHelperMock.Object,
                                                                                       fileSystemMock.Object,
                                                                                       flowData);

            //act
            step.PerformStep();
            swMock.Dispose();

            //assert
            this.fileSystemMock.Verify(x => x.Directory.CreateDirectory(It.IsAny <string>()), Times.Never);
            this.fileSystemMock.Verify(x => x.File.CreateText(It.IsAny <string>()), Times.Once);
            this.fileSystemMock.Verify(x => x.File.Delete(It.IsAny <string>()), Times.Once);
        }
        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     = "F:\\temporary\\folder";
            flowData.AddVhdToBootManager        = true;
            flowData.OperatingSystemDriveLetter = 'C';

            Guid newEntryId = Guid.NewGuid();

            this.fileSystemHelperMock.Setup(x => x.ExecuteBcdeditCommand(
                                                It.Is <string>(script => script.Contains("/copy")),
                                                It.IsAny <ILogger>()))
            .Returns($"Cloned successfully {{{newEntryId}}}");

            this.fileSystemHelperMock.Setup(x => x.ExecuteBcdeditCommand(
                                                It.Is <string>(script => script.Contains("/set") && script.Contains(newEntryId.ToString())),
                                                It.IsAny <ILogger>()))
            .Returns($"Operation completed successfully.");

            AddFinalVhdToBootManagerMigrationStep step = new AddFinalVhdToBootManagerMigrationStep(loggerMock.Object,
                                                                                                   fileSystemHelperMock.Object,
                                                                                                   fileSystemMock.Object,
                                                                                                   flowData);

            //act
            step.PerformStep();

            //assert
            this.fileSystemHelperMock.Verify(x => x.ExecuteBcdeditCommand(
                                                 It.Is <string>(script => script.Contains("/copy")),
                                                 It.IsAny <ILogger>()), Times.Once);

            this.fileSystemHelperMock.Verify(x => x.ExecuteBcdeditCommand(
                                                 It.Is <string>(script => script.Contains("/set") && script.Contains(newEntryId.ToString())),
                                                 It.IsAny <ILogger>()), Times.Exactly(2));

            this.fileSystemHelperMock.Verify(x => x.ExecuteBcdeditCommand(
                                                 It.Is <string>(script => script.Contains("/set") && script.Contains("vhd=[" + flowData.VhdFileTemporaryFolder[0])),
                                                 It.IsAny <ILogger>()), Times.Exactly(2));
        }
Beispiel #23
0
        private static MigrationFlowData BuildMigrationData(OperationModeEnum operationMode, IFileSystemHelper fileSystemHelper, bool addToBootManager)
        {
            MigrationFlowData migrationData   = new MigrationFlowData();
            IDriveInfo        systemDriveInfo = fileSystemHelper.GetSystemDriveInfo();

            migrationData.OperatingSystemDriveLetter = systemDriveInfo.Name.First();
            migrationData.TemporaryVhdFileMaxSize    = systemDriveInfo.TotalSize + (200 << 20) /*200MB*/;
            migrationData.VhdFileName            = "VHDNBOM_System_Image.vhdx";
            migrationData.VhdFileType            = Logic.Models.Enums.VhdType.VhdxDynamic;
            migrationData.VhdFileTemporaryFolder = vhdTemporaryFolderPath ?? fileSystemHelper.FindBestLocationForVhdTemporaryFolder();
            migrationData.AddVhdToBootManager    = addToBootManager;

            if (operationMode == OperationModeEnum.MigrateCurrentOsToVhd)
            {
                migrationData.DeleteTemporaryVhdFile    = true;
                migrationData.DestinationVhdMaxFileSize = (systemDriveInfo.AvailableFreeSpace - Constants.FiveGigs + Constants.OneGig) + (200 << 20);
                migrationData.DesiredTempVhdShrinkSize  = (systemDriveInfo.TotalSize - (systemDriveInfo.AvailableFreeSpace - Constants.FiveGigs));
                migrationData.VhdFileDestinationFolder  = $"{migrationData.OperatingSystemDriveLetter}:\\VHD_Boot";
            }

            return(migrationData);
        }
Beispiel #24
0
        public void CheckPrerequisitiesTest_ShouldBeOk()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder = "temporary\\folder";

            this.vssBackupComponentsMock.Setup(x => x.IsVolumeSupported(It.IsAny <string>()))
            .Returns(true);

            CloneOSMigrationStep step = new CloneOSMigrationStep(loggerMock.Object,
                                                                 osHelperMock.Object,
                                                                 fileSystemHelperMock.Object,
                                                                 flowData,
                                                                 vssImplementationMock.Object);

            //act
            string[] messages = new string[0];
            bool     result   = step.CheckPrerequisities(ref messages);

            //assert
            Assert.IsTrue(result);
        }
Beispiel #25
0
        public MigratorEngine(ILogger <MigratorEngine> logger, MigrationFlowData migrationData)
        {
            this.logger        = logger;
            this.migrationData = migrationData;
            this.container     = DependencyResolverAccessor.Container;
            if (this.container == null)
            {
                logger.LogCritical("Unable to access dependency injection container. Process cannot continue");
                throw new Exception("Unable to access dependency injection container.");
            }

            //resolve all migration steps in proper order
            stepsToExecute = new List <BaseMigrationStep>()
            {
                container.Resolve <PrepareMigrationStep>(),
                container.Resolve <CreateTemporaryVhdMigrationStep>(),
                container.Resolve <CloneOSMigrationStep>(),
                container.Resolve <FixClonedOsRegistryMigrationStep>(),
                container.Resolve <ShrinkVhdVolumeMigrationStep>(),
                container.Resolve <CreateFinalVhdMigrationStep>(),
                container.Resolve <CloneTempVhdToFinalVhdMigrationStep>(),
                container.Resolve <AddFinalVhdToBootManagerMigrationStep>(),
            };
        }
Beispiel #26
0
        public void CheckPrerequisitiesTest_ShouldFail()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder = "temporary\\folder";

            this.vssBackupComponentsMock.Setup(x => x.IsVolumeSupported(It.IsAny <string>()))
            .Returns(false);

            CloneOSMigrationStep step = new CloneOSMigrationStep(loggerMock.Object,
                                                                 osHelperMock.Object,
                                                                 fileSystemHelperMock.Object,
                                                                 flowData,
                                                                 vssImplementationMock.Object);

            //act
            string[] messages = new string[0];
            bool     result   = step.CheckPrerequisities(ref messages);

            //assert
            Assert.IsFalse(result);
            Assert.AreEqual(1, messages.Where(x => x.Contains("System volume does not support VSS")).Count());
        }
        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);
        }
Beispiel #29
0
        public void PerformStepTest_ShouldFinishOk()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder     = "temporary\\folder";
            flowData.VhdFileDestinationFolder   = "destination\\folder";
            flowData.OperatingSystemDriveLetter = 'C';
            flowData.VhdFileName            = "test.vhdx";
            flowData.DeleteTemporaryVhdFile = true;

            MemoryStream streamMock = new MemoryStream();
            StreamWriter swMock     = new StreamWriter(streamMock);
            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(100);
            SparseStream sparseStream = sparseStreamMock.Object;

            partitionTableMock.Setup(x => x.CreatePrimaryBySector(It.IsAny <long>(), It.IsAny <long>(), It.IsAny <byte>(), It.IsAny <bool>()))
            .Returns(0)
            .Verifiable();

            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);

            diskMock.Setup(x => x.Geometry)
            .Returns(new Geometry(100, 1, 2, 512));

            this.fileSystemHelperMock.Setup(x => x.OpenVhdx(It.IsAny <string>()))
            .Returns(diskMock.Object)
            .Verifiable();

            this.fileSystemHelperMock.Setup(x => x.CloneNtfsFileSystem(It.IsAny <Stream>(), It.IsAny <Stream>(), It.IsAny <ILogger>()))
            .Verifiable();

            this.fileSystemMock.Setup(x => x.File.Delete(It.IsAny <string>()));

            CloneTempVhdToFinalVhdMigrationStep step = new CloneTempVhdToFinalVhdMigrationStep(loggerMock.Object,
                                                                                               fileSystemHelperMock.Object,
                                                                                               fileSystemMock.Object,
                                                                                               flowData);

            //act
            step.PerformStep();
            swMock.Dispose();

            //assert
            partitionTableMock.Verify(x => x.CreatePrimaryBySector(It.IsAny <long>(), It.IsAny <long>(), It.IsAny <byte>(), It.IsAny <bool>()), Times.Once);
            this.fileSystemHelperMock.Verify(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileTemporaryFolder}\\{flowData.VhdFileName}")), Times.Once);
            this.fileSystemHelperMock.Verify(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileDestinationFolder}\\{flowData.VhdFileName}")), Times.Once);
            this.fileSystemHelperMock.Verify(x => x.CloneNtfsFileSystem(It.IsAny <Stream>(), It.IsAny <Stream>(), It.IsAny <ILogger>()), Times.Once);
            this.fileSystemMock.Verify(x => x.File.Delete(It.IsAny <string>()), Times.Once);
        }
Beispiel #30
0
        public void PerformStepTest_ShouldDeleteSnapshotOnError()
        {
            //arrange
            MigrationFlowData flowData = new MigrationFlowData();

            flowData.VhdFileTemporaryFolder     = "temporary\\folder";
            flowData.OperatingSystemDriveLetter = 'C';
            flowData.VhdFileName = "test.vhdx";

            Mock <VirtualDiskDecorator>        diskMock           = new Mock <VirtualDiskDecorator>();
            Mock <BiosPartitionTableDecorator> partitionTableMock = new Mock <BiosPartitionTableDecorator>();

            diskMock.Setup(x => x.Partitions)
            .Returns(partitionTableMock.Object);

            MemoryStream          streamMock          = new MemoryStream();
            StreamWriter          swMock              = new StreamWriter(streamMock);
            VssSnapshotProperties fakeSnapshotDetails = new VssSnapshotProperties(Guid.NewGuid(), Guid.NewGuid(), 1, "snapshotDeviceObject", "C:", "unit-testing", "unit-testing", null, null, Guid.NewGuid(), VssVolumeSnapshotAttributes.Differential, DateTime.Now, VssSnapshotState.Created);

            this.vssBackupComponentsMock.Setup(x => x.InitializeForBackup(It.IsAny <string>())).Verifiable();
            this.vssBackupComponentsMock.Setup(x => x.GatherWriterMetadata()).Verifiable();
            this.vssBackupComponentsMock.Setup(x => x.SetContext(It.IsAny <VssVolumeSnapshotAttributes>())).Verifiable();
            this.vssBackupComponentsMock.Setup(x => x.StartSnapshotSet())
            .Returns(fakeSnapshotDetails.SnapshotSetId)
            .Verifiable();
            this.vssBackupComponentsMock.Setup(x => x.AddToSnapshotSet(It.Is <string>((param) => param == $"{flowData.OperatingSystemDriveLetter}:\\")))
            .Returns(fakeSnapshotDetails.SnapshotId)
            .Verifiable();
            this.vssBackupComponentsMock.Setup(x => x.PrepareForBackup()).Verifiable();
            this.vssBackupComponentsMock.Setup(x => x.DoSnapshotSet())
            .Throws <Exception>()
            .Verifiable();
            this.vssBackupComponentsMock.Setup(x => x.DeleteSnapshot(It.Is <Guid>((snapshotId) => snapshotId == fakeSnapshotDetails.SnapshotId), It.IsAny <bool>())).Verifiable();
            this.vssBackupComponentsMock.Setup(x => x.GetSnapshotProperties(It.Is <Guid>((snapshotId) => snapshotId == fakeSnapshotDetails.SnapshotId)))
            .Returns(fakeSnapshotDetails)
            .Verifiable();

            this.fileSystemHelperMock.Setup(x => x.OpenRawDiskStream(It.Is <string>(snapshotPath => snapshotPath == fakeSnapshotDetails.SnapshotDeviceObject)))
            .Returns(streamMock)
            .Verifiable();

            this.fileSystemHelperMock.Setup(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileTemporaryFolder}\\{flowData.VhdFileName}")))
            .Returns(diskMock.Object)
            .Verifiable();

            this.fileSystemHelperMock.Setup(x => x.CloneNtfsFileSystem(It.IsAny <Stream>(), It.IsAny <Stream>(), It.IsAny <ILogger>()))
            .Verifiable();

            CloneOSMigrationStep step = new CloneOSMigrationStep(loggerMock.Object,
                                                                 osHelperMock.Object,
                                                                 fileSystemHelperMock.Object,
                                                                 flowData,
                                                                 vssImplementationMock.Object);

            //act
            Assert.Throws <Exception>(() =>
            {
                step.PerformStep();
            });
            swMock.Dispose();

            //assert
            this.vssBackupComponentsMock.Verify(x => x.InitializeForBackup(It.IsAny <string>()), Times.Once);
            this.vssBackupComponentsMock.Verify(x => x.GatherWriterMetadata(), Times.Once);
            this.vssBackupComponentsMock.Verify(x => x.SetContext(It.IsAny <VssVolumeSnapshotAttributes>()), Times.Once);
            this.vssBackupComponentsMock.Verify(x => x.StartSnapshotSet(), Times.Once);
            this.vssBackupComponentsMock.Verify(x => x.AddToSnapshotSet(It.Is <string>((param) => param == $"{flowData.OperatingSystemDriveLetter}:\\")), Times.Once);
            this.vssBackupComponentsMock.Verify(x => x.PrepareForBackup(), Times.Once);
            this.vssBackupComponentsMock.Verify(x => x.DoSnapshotSet(), Times.Once);
            this.vssBackupComponentsMock.Verify(x => x.DeleteSnapshot(It.Is <Guid>((snapshotId) => snapshotId == fakeSnapshotDetails.SnapshotId), It.IsAny <bool>()), Times.Once);
            this.fileSystemHelperMock.Verify(x => x.OpenVhdx(It.Is <string>(snapshotPath => snapshotPath == $"{flowData.VhdFileTemporaryFolder}\\{flowData.VhdFileName}")), Times.Never);
            this.fileSystemHelperMock.Verify(x => x.CloneNtfsFileSystem(It.IsAny <Stream>(), It.IsAny <Stream>(), It.IsAny <ILogger>()), Times.Never);
        }