Skip to content

Commit

Permalink
[FIX JENKINS-30144] backup nodes directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kna committed Dec 10, 2016
1 parent 44ec27a commit 7ba83aa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Expand Up @@ -63,6 +63,7 @@ public class HudsonBackup {
public static final String USERSCONTENTS_DIR_NAME = "userContent";
public static final String NEXT_BUILD_NUMBER_FILE_NAME = "nextBuildNumber";
public static final String PLUGINS_DIR_NAME = "plugins";
public static final String NODES_DIR_NAME = "nodes";
public static final String CONFIG_XML = "config.xml";
public static final String XML_FILE_EXTENSION = ".xml";
public static final String JPI_FILE_EXTENSION = ".jpi";
Expand Down Expand Up @@ -157,6 +158,7 @@ public void backup() throws IOException {
backupGlobalXmls();
backupJobs();
backupRootFolder(USERS_DIR_NAME);
backupNodes();

if (plugin.isBackupUserContents())
backupRootFolder(USERSCONTENTS_DIR_NAME);
Expand Down Expand Up @@ -293,6 +295,15 @@ private void backupAdditionalFiles() throws IOException {
LOGGER.info("DONE backing up Additional Files.");
}

private void backupNodes() throws IOException {
LOGGER.fine("Backing up nodes configuration files...");

final IOFileFilter filter = FileFilterUtils.nameFileFilter(CONFIG_XML);
backupRootFolder(NODES_DIR_NAME, filter);

LOGGER.fine("DONE backing up nodes configuration files.");
}

private File createBackupDirectory(File jobBackupdirectory, File jobDirectory, File configurationDirectory) {
String pathToConfiguration = configurationDirectory.getAbsolutePath();
String pathToJob = jobDirectory.getAbsolutePath();
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/jvnet/hudson/plugins/thinbackup/TestHelper.java
Expand Up @@ -21,6 +21,7 @@ public class TestHelper {
public static final String CONFIG_XML_CONTENTS = "FILLED WITH DATA... ";
public static final String CONCRET_BUILD_DIRECTORY_NAME = "2011-01-08_22-26-40";
public static final String TEST_JOB_NAME = "test";
public static final String TEST_NODE_NAME = "node1";

public static File createBasicFolderStructure(File base) throws IOException {
File root = new File(base, "RootDirForHudsonBackupTest");
Expand All @@ -33,6 +34,7 @@ public static File createBasicFolderStructure(File base) throws IOException {
new File(root, "hudson.model.UpdateCenter.xml").createNewFile();
new File(root, HudsonBackup.JOBS_DIR_NAME).mkdir();
new File(root, HudsonBackup.USERS_DIR_NAME).mkdir();
new File(root, HudsonBackup.NODES_DIR_NAME).mkdir();
new File(root, HudsonBackup.USERSCONTENTS_DIR_NAME).mkdir();
new File(root, "plugins").mkdir();

Expand Down Expand Up @@ -196,4 +198,15 @@ public static ThinBackupPluginImpl createMockPlugin(File jenkinsHome, File backu

return mockPlugin;
}

public static File createNode(File jenkinsHome, String nodeName) throws IOException {
final File testNode = new File(new File(jenkinsHome, HudsonBackup.NODES_DIR_NAME), nodeName);
testNode.mkdirs();
final File config = new File(testNode, "config.xml");
config.createNewFile();
final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(config));
out.write(CONFIG_XML_CONTENTS.getBytes());
out.close();
return testNode;
}
}
Expand Up @@ -319,4 +319,32 @@ public void testBackupKeptBuildsOnly_keep() throws Exception {
list = build.list();
Assert.assertEquals(7, list.length);
}

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

TestHelper.createNode(jenkinsHome, TestHelper.TEST_NODE_NAME);

final ThinBackupPluginImpl mockPlugin = TestHelper.createMockPlugin(jenkinsHome, backupDir);

new HudsonBackup(mockPlugin, BackupType.FULL, cal.getTime(), mockHudson).backup();

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

final File nodes = new File(backup, HudsonBackup.NODES_DIR_NAME);
list = nodes.list();
Assert.assertEquals(1, list.length);
Assert.assertEquals(TestHelper.TEST_NODE_NAME, list[0]);

final File node = new File(nodes, TestHelper.TEST_NODE_NAME);
list = node.list();
Assert.assertEquals(1, list.length);
Assert.assertEquals(HudsonBackup.CONFIG_XML, list[0]);
}
}

0 comments on commit 7ba83aa

Please sign in to comment.