Skip to content
Permalink
Browse files

Included support for the promoted builds plugin and job promotions, a…

…ddressing the open issue JENKINS-21472
  • Loading branch information...
Stephen
Stephen committed Oct 13, 2014
1 parent 728bc25 commit c20130bd2ff910794e0b2b655ad2deb00e1b90cd
@@ -56,6 +56,7 @@

public static final String BUILDS_DIR_NAME = "builds";
public static final String CONFIGURATIONS_DIR_NAME = "configurations";
public static final String PROMOTIONS_DIR_NAME = "promotions";
public static final String JOBS_DIR_NAME = "jobs";
public static final String USERS_DIR_NAME = "users";
public static final String ARCHIVE_DIR_NAME = "archive";
@@ -235,11 +236,19 @@ private void backupJob(final File jobDirectory, final File jobsBackupDirectory,
if (isMatrixJob(jobDirectory)) {
List<File> configurations = findAllConfigurations(new File(jobDirectory, HudsonBackup.CONFIGURATIONS_DIR_NAME));
for (File configurationDirectory : configurations) {
File configurationBackupDirectory = createConfigurationBackupDirectory(jobBackupDirectory, jobDirectory, configurationDirectory);
File configurationBackupDirectory = createBackupDirectory(jobBackupDirectory, jobDirectory, configurationDirectory);
backupJobConfigFor(configurationDirectory, configurationBackupDirectory);
backupBuildsFor(configurationDirectory, configurationBackupDirectory);
}
}
if (isPromotedJob(jobDirectory)) {
List<File> promotions = findAllConfigurations(new File(jobDirectory, HudsonBackup.PROMOTIONS_DIR_NAME));
for (File promotionDirectory : promotions) {
File promotionBackupDirectory = createBackupDirectory(jobBackupDirectory, jobDirectory, promotionDirectory);
backupJobConfigFor(promotionDirectory, promotionBackupDirectory);
backupBuildsFor(promotionDirectory, promotionBackupDirectory);
}
}
}

private void backupPluginArchives() throws IOException {
@@ -286,7 +295,7 @@ private void backupAdditionalFiles() throws IOException {
LOGGER.info("DONE backing up Additional Files.");
}

private File createConfigurationBackupDirectory(File jobBackupdirectory, File jobDirectory, File configurationDirectory) {
private File createBackupDirectory(File jobBackupdirectory, File jobDirectory, File configurationDirectory) {
String pathToConfiguration = configurationDirectory.getAbsolutePath();
String pathToJob = jobDirectory.getAbsolutePath();

@@ -308,6 +317,10 @@ private boolean isMatrixJob(File jobDirectory) {
return new File(jobDirectory, CONFIGURATIONS_DIR_NAME).isDirectory();
}

private boolean isPromotedJob(File jobDirectory) {
return new File(jobDirectory, PROMOTIONS_DIR_NAME).isDirectory();
}

private void backupJobConfigFor(final File jobDirectory, final File jobBackupDirectory) throws IOException {
final IOFileFilter filter = FileFilterUtils.and(
FileFilterUtils.or(
@@ -135,6 +135,20 @@ public static void addSingleConfigurationResult(File job) throws IOException, In
addBuildNumber(nextBuildnumber);
}

public static void addSinglePromotionResult(File job) throws IOException, InterruptedException {
File promotions = new File(job, HudsonBackup.PROMOTIONS_DIR_NAME);
promotions.mkdir();
File promotion_x = new File(promotions, "promotion-x");
promotion_x.mkdir();

addNewBuildToJob(promotion_x);

new File(promotion_x, "config.xml").createNewFile();
File nextBuildnumber = new File(promotion_x, "nextBuildNumber");
nextBuildnumber.createNewFile();
addBuildNumber(nextBuildnumber);
}

public static boolean containsStringEndingWith(final List<String> strings, final String pattern) {
boolean contains = false;

@@ -0,0 +1,85 @@
package org.jvnet.hudson.plugins.thinbackup.backup;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import hudson.model.ItemGroup;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.plugins.thinbackup.TestHelper;
import org.jvnet.hudson.plugins.thinbackup.ThinBackupPeriodicWork.BackupType;
import org.jvnet.hudson.plugins.thinbackup.ThinBackupPluginImpl;
import org.jvnet.hudson.plugins.thinbackup.utils.Utils;

public class TestBackupPromotedJob {

private File backupDir;
private File jenkinsHome;

@Before
public void setup() throws IOException, InterruptedException {
File base = new File(System.getProperty("java.io.tmpdir"));
backupDir = TestHelper.createBackupFolder(base);

jenkinsHome = TestHelper.createBasicFolderStructure(base);
File jobDir = TestHelper.createJob(jenkinsHome, TestHelper.TEST_JOB_NAME);
TestHelper.addNewBuildToJob(jobDir);

TestHelper.addSinglePromotionResult(jobDir);
}

@After
public void tearDown() throws Exception {
FileUtils.deleteDirectory(jenkinsHome);
FileUtils.deleteDirectory(backupDir);
FileUtils.deleteDirectory(new File(Utils.THINBACKUP_TMP_DIR));
}

private ThinBackupPluginImpl createMockPlugin() {
final ThinBackupPluginImpl mockPlugin = mock(ThinBackupPluginImpl.class);

when(mockPlugin.getHudsonHome()).thenReturn(jenkinsHome);
when(mockPlugin.getFullBackupSchedule()).thenReturn("");
when(mockPlugin.getDiffBackupSchedule()).thenReturn("");
when(mockPlugin.getExpandedBackupPath()).thenReturn(backupDir.getAbsolutePath());
when(mockPlugin.getNrMaxStoredFull()).thenReturn(-1);
when(mockPlugin.isCleanupDiff()).thenReturn(false);
when(mockPlugin.isMoveOldBackupsToZipFile()).thenReturn(false);
when(mockPlugin.isBackupBuildResults()).thenReturn(true);
when(mockPlugin.isBackupBuildArchive()).thenReturn(false);
when(mockPlugin.isBackupNextBuildNumber()).thenReturn(false);
when(mockPlugin.getExcludedFilesRegex()).thenReturn("");

return mockPlugin;
}

@Test
public void testFullBuildResultsBackup() throws IOException {
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE - 10));

final ThinBackupPluginImpl mockPlugin = createMockPlugin();

new HudsonBackup(mockPlugin, BackupType.FULL, cal.getTime(), mock(ItemGroup.class)).backup();

String[] list = backupDir.list();
Assert.assertEquals(1, list.length);
final File backup = new File(backupDir, list[0]);
list = backup.list();
Assert.assertEquals(6, list.length);

File jobBackup = new File(backup, "jobs/"+TestHelper.TEST_JOB_NAME);

Assert.assertTrue(new File(jobBackup, HudsonBackup.PROMOTIONS_DIR_NAME).exists());
Assert.assertTrue(new File(jobBackup, HudsonBackup.PROMOTIONS_DIR_NAME+"/promotion-x").exists());
Assert.assertTrue(new File(jobBackup, HudsonBackup.PROMOTIONS_DIR_NAME+"/promotion-x/"+HudsonBackup.BUILDS_DIR_NAME+"/"+TestHelper.CONCRET_BUILD_DIRECTORY_NAME+"/build.xml").exists());
}

}

0 comments on commit c20130b

Please sign in to comment.
You can’t perform that action at this time.