@Test public void testRestoreFromDirectory() throws Exception { // create a backed up structure with DIFF back ups final TestHudsonBackup tester = new TestHudsonBackup(); tester.setup(); tester.performHudsonDiffBackup(createMockPluginNoNextBuildNumber()); // now destroy the hudson directory and recreate the root dir FileUtils.deleteDirectory(jenkinsHome); jenkinsHome.mkdirs(); final File[] files = backupDir.listFiles(); Assert.assertEquals(2, files.length); final File tmpBackupDir = files[0]; final BackupSet set = new BackupSet(tmpBackupDir); Assert.assertTrue(set.isValid()); Assert.assertFalse(set.isInZipFile()); final HudsonRestore restore = new HudsonRestore( jenkinsHome, backupDir.getAbsolutePath(), Utils.getDateFromBackupDirectory(tmpBackupDir), false, false); restore.restore(); final FileCollector fc = new FileCollector(); final List<String> restoredFiles = fc.getFilesAsString(jenkinsHome); final int nrRestored = restoredFiles.size(); Assert.assertEquals( originalFiles.size(), nrRestored + 3); // + 3 because original has more files that were not // backed up on purpose (next build number, secret.key, workspace/neverBackupme.txt) }
@Test public void testRestoreFromZipFileWithNextBuildNumber() throws Exception { // create a backed up structure with DIFF back ups final TestHudsonBackup tester = new TestHudsonBackup(); tester.setup(); tester.performHudsonDiffBackup(createMockPluginBackupNextBuildNumber()); // remember the date to restore final File[] tmpFiles = backupDir.listFiles(); Assert.assertEquals(2, tmpFiles.length); final Date backupDate = Utils.getDateFromBackupDirectory(tmpFiles[0]); // move backups to ZIP file Utils.moveOldBackupsToZipFile(backupDir, null); // now destroy the hudson directory and recreate the root dir FileUtils.deleteDirectory(jenkinsHome); jenkinsHome.mkdirs(); final File[] files = backupDir.listFiles(); Assert.assertEquals(1, files.length); final File zipFile = files[0]; final BackupSet set = new BackupSet(zipFile); Assert.assertTrue(set.isValid()); Assert.assertTrue(set.isInZipFile()); final HudsonRestore restore = new HudsonRestore(jenkinsHome, backupDir.getAbsolutePath(), backupDate, true, false); restore.restore(); final FileCollector fc = new FileCollector(); final List<String> restoredFiles = fc.getFilesAsString(jenkinsHome); final int nrRestored = restoredFiles.size(); Assert.assertEquals( originalFiles.size(), nrRestored + 2); // + 3 because original has more files that were not // backed up on purpose (secret.key, workspace/neverBackupme.txt) Assert.assertTrue( TestHelper.containsStringEndingWith( restoredFiles, HudsonBackup.NEXT_BUILD_NUMBER_FILE_NAME)); Assert.assertFalse(TestHelper.containsStringEndingWith(restoredFiles, "secret.key")); }