From e3b8a12ec11aa19e61b722f37b4bcad97bbec41f Mon Sep 17 00:00:00 2001 From: Dean Ferreyra Date: Sat, 12 Sep 2020 16:59:41 -0700 Subject: [PATCH] Unit test improvements per review comments --- Duplicati/UnitTest/ProblematicPathTests.cs | 7 +- Duplicati/UnitTest/SymLinkTests.cs | 94 +++++++++------------- 2 files changed, 38 insertions(+), 63 deletions(-) diff --git a/Duplicati/UnitTest/ProblematicPathTests.cs b/Duplicati/UnitTest/ProblematicPathTests.cs index ac1d075f1..eb2e815d6 100644 --- a/Duplicati/UnitTest/ProblematicPathTests.cs +++ b/Duplicati/UnitTest/ProblematicPathTests.cs @@ -81,12 +81,7 @@ namespace Duplicati.UnitTest Assert.AreEqual(0, restoreResults.Errors.Count()); Assert.AreEqual(0, restoreResults.Warnings.Count()); - foreach (string directory in directories) - { - string directoryName = SystemIO.IO_OS.PathGetFileName(directory); - string restoredDirectory = SystemIO.IO_OS.PathCombine(this.RESTOREFOLDER, directoryName); - TestUtils.AssertDirectoryTreesAreEquivalent(directory, restoredDirectory, true, "Restore"); - } + TestUtils.AssertDirectoryTreesAreEquivalent(this.DATAFOLDER, this.RESTOREFOLDER, true, "Restore"); // List results using * should return a match for each directory. IListResults listResults = c.List(SystemIO.IO_OS.PathCombine(dirWithAsterisk, file)); diff --git a/Duplicati/UnitTest/SymLinkTests.cs b/Duplicati/UnitTest/SymLinkTests.cs index 4b52bb384..b2a178f09 100755 --- a/Duplicati/UnitTest/SymLinkTests.cs +++ b/Duplicati/UnitTest/SymLinkTests.cs @@ -14,7 +14,10 @@ namespace Duplicati.UnitTest { [Test] [Category("SymLink")] - public void SymLinkPolicy() + [TestCase(Options.SymlinkStrategy.Store)] + [TestCase(Options.SymlinkStrategy.Follow)] + [TestCase(Options.SymlinkStrategy.Ignore)] + public void SymLinkPolicy(Options.SymlinkStrategy symlinkPolicy) { // Create symlink target directory const string targetDirName = "target"; @@ -41,65 +44,42 @@ namespace Duplicati.UnitTest Assert.Ignore($"Client could not create a symbolic link. Error reported: {e.Message}"); } - // Perform backups using all symlink policies and verify restores - var symlinkPolicies = new[] { Options.SymlinkStrategy.Store, Options.SymlinkStrategy.Follow, Options.SymlinkStrategy.Ignore }; + // Backup all files with given symlink policy Dictionary restoreOptions = new Dictionary(this.TestOptions) { ["restore-path"] = this.RESTOREFOLDER }; - foreach (var symlinkPolicy in symlinkPolicies) + Dictionary backupOptions = new Dictionary(this.TestOptions) { ["symlink-policy"] = symlinkPolicy.ToString() }; + using (Controller c = new Controller("file://" + this.TARGETFOLDER, backupOptions, null)) { - // Backup all files with given symlink policy - Dictionary backupOptions = new Dictionary(this.TestOptions) { ["symlink-policy"] = symlinkPolicy.ToString() }; - using (Controller c = new Controller("file://" + this.TARGETFOLDER, backupOptions, null)) - { - IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER }); - Assert.AreEqual(0, backupResults.Errors.Count()); - Assert.AreEqual(0, backupResults.Warnings.Count()); - } - // Restore all files - using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null)) - { - IRestoreResults restoreResults = c.Restore(null); - Assert.AreEqual(0, restoreResults.Errors.Count()); - Assert.AreEqual(0, restoreResults.Warnings.Count()); + IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER }); + Assert.AreEqual(0, backupResults.Errors.Count()); + Assert.AreEqual(0, backupResults.Warnings.Count()); + } + // Restore all files + using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null)) + { + IRestoreResults restoreResults = c.Restore(null); + Assert.AreEqual(0, restoreResults.Errors.Count()); + Assert.AreEqual(0, restoreResults.Warnings.Count()); - // Verify that symlink policy was followed - var restoreSymlinkDir = systemIO.PathCombine(this.RESTOREFOLDER, symlinkDirName); - switch (symlinkPolicy) - { - case Options.SymlinkStrategy.Store: - // Restore should contain an actual symlink to the original target - Assert.That(systemIO.IsSymlink(restoreSymlinkDir), Is.True); - var restoredSymlinkFullPath = systemIO.PathGetFullPath(systemIO.GetSymlinkTarget(restoreSymlinkDir)); - var symlinkTargetFullPath = systemIO.PathGetFullPath(targetDir); - Assert.That(restoredSymlinkFullPath, Is.EqualTo(symlinkTargetFullPath)); - break; - case Options.SymlinkStrategy.Follow: - // Restore should contain a regular directory with copies of the files in the symlink target - Assert.That(systemIO.IsSymlink(restoreSymlinkDir), Is.False); - TestUtils.AssertDirectoryTreesAreEquivalent(targetDir, restoreSymlinkDir, true, $"Symlink policy: {Options.SymlinkStrategy.Store}"); - break; - case Options.SymlinkStrategy.Ignore: - // Restore should not contain the symlink at all - Assert.That(systemIO.DirectoryExists(restoreSymlinkDir), Is.False); - break; - } - // Prepare for a fresh backup and restore cycle - foreach (var dir in systemIO.EnumerateDirectories(this.RESTOREFOLDER)) - { - systemIO.DirectoryDelete(dir, true); - } - foreach (var file in systemIO.EnumerateFiles(this.RESTOREFOLDER)) - { - systemIO.FileDelete(file); - } - c.DeleteAllRemoteFiles(); - if (systemIO.FileExists(this.DBFILE)) - { - systemIO.FileDelete(this.DBFILE); - } - if (systemIO.FileExists($"{this.DBFILE}-journal")) - { - systemIO.FileDelete($"{this.DBFILE}-journal"); - } + // Verify that symlink policy was followed + var restoreSymlinkDir = systemIO.PathCombine(this.RESTOREFOLDER, symlinkDirName); + switch (symlinkPolicy) + { + case Options.SymlinkStrategy.Store: + // Restore should contain an actual symlink to the original target + Assert.That(systemIO.IsSymlink(restoreSymlinkDir), Is.True); + var restoredSymlinkFullPath = systemIO.PathGetFullPath(systemIO.GetSymlinkTarget(restoreSymlinkDir)); + var symlinkTargetFullPath = systemIO.PathGetFullPath(targetDir); + Assert.That(restoredSymlinkFullPath, Is.EqualTo(symlinkTargetFullPath)); + break; + case Options.SymlinkStrategy.Follow: + // Restore should contain a regular directory with copies of the files in the symlink target + Assert.That(systemIO.IsSymlink(restoreSymlinkDir), Is.False); + TestUtils.AssertDirectoryTreesAreEquivalent(targetDir, restoreSymlinkDir, true, $"Symlink policy: {Options.SymlinkStrategy.Store}"); + break; + case Options.SymlinkStrategy.Ignore: + // Restore should not contain the symlink at all + Assert.That(systemIO.DirectoryExists(restoreSymlinkDir), Is.False); + break; } } }