blob: 6951022f557d8e290d9f624550304aa11e06d490 [file] [log] [blame]
/*=========================================================================
* Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
* This product is protected by U.S. and international copyright
* and intellectual property laws. Pivotal products are covered by
* one or more patents listed at http://www.pivotal.io/patents.
*=========================================================================
*/
package com.gemstone.gemfire.internal.cache.persistence;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import com.gemstone.gemfire.internal.FileUtil;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
/**
* TODO: fails when running integrationTest from gradle command-line on Windows 7
*
com.gemstone.gemfire.internal.cache.DiskRegionJUnitTest > testAssertionErrorIfMissingOplog FAILED
junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:55)
at junit.framework.Assert.assertTrue(Assert.java:22)
at junit.framework.Assert.assertTrue(Assert.java:31)
at com.gemstone.gemfire.internal.cache.DiskRegionJUnitTest.testAssertionErrorIfMissingOplog(DiskRegionJUnitTest.java:2630)
*
* Tests for the BackupInspector.
*
* @author rholmes
*/
@Category(UnitTest.class)
public class BackupInspectorJUnitTest {
private static final String UNIX_INCREMENTAL_BACKUP_SCRIPT = "#!/bin/bash -e\ncd `dirname $0`\n\n#Restore a backup of gemfire persistent data to the location it was backed up\n#from.\n#This script will refuse to restore if the original data still exists.\n\n#This script was automatically generated by the gemfire backup utility.\n\n#Test for existing originals. If they exist, do not restore the backup.\ntest -e '/Users/rholmes/Projects/gemfire/test/cacheserver/disk3/BACKUPbar.if' && echo 'Backup not restored. Refusing to overwrite /Users/rholmes/Projects/gemfire/test/cacheserver/disk3/BACKUPbar.if' && exit 1 \ntest -e '/Users/rholmes/Projects/gemfire/test/cacheserver/disk1/BACKUPfoo.if' && echo 'Backup not restored. Refusing to overwrite /Users/rholmes/Projects/gemfire/test/cacheserver/disk1/BACKUPfoo.if' && exit 1 \n\n#Restore data\nmkdir -p '/Users/rholmes/Projects/gemfire/test/cacheserver/disk3'\ncp -rp 'diskstores/bar/dir0'/* '/Users/rholmes/Projects/gemfire/test/cacheserver/disk3'\nmkdir -p '/Users/rholmes/Projects/gemfire/test/cacheserver/disk4'\nmkdir -p '/Users/rholmes/Projects/gemfire/test/cacheserver/disk1'\ncp -rp 'diskstores/foo/dir0'/* '/Users/rholmes/Projects/gemfire/test/cacheserver/disk1'\nmkdir -p '/Users/rholmes/Projects/gemfire/test/cacheserver/disk2'\n\n#Incremental backup. Restore baseline originals from a previous backup.\ncp -p '/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/bar/dir0/BACKUPbar_1.drf' '/Users/rholmes/Projects/gemfire/test/cacheserver/disk3/BACKUPbar_1.drf'\ncp -p '/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/foo/dir1/BACKUPfoo_2.crf' '/Users/rholmes/Projects/gemfire/test/cacheserver/disk2/BACKUPfoo_2.crf'\ncp -p '/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/foo/dir1/BACKUPfoo_2.drf' '/Users/rholmes/Projects/gemfire/test/cacheserver/disk2/BACKUPfoo_2.drf'\ncp -p '/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/bar/dir1/BACKUPbar_2.drf' '/Users/rholmes/Projects/gemfire/test/cacheserver/disk4/BACKUPbar_2.drf'\ncp -p '/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/foo/dir0/BACKUPfoo_1.crf' '/Users/rholmes/Projects/gemfire/test/cacheserver/disk1/BACKUPfoo_1.crf'\ncp -p '/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/bar/dir1/BACKUPbar_2.crf' '/Users/rholmes/Projects/gemfire/test/cacheserver/disk4/BACKUPbar_2.crf'\ncp -p '/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/bar/dir0/BACKUPbar_1.crf' '/Users/rholmes/Projects/gemfire/test/cacheserver/disk3/BACKUPbar_1.crf'\ncp -p '/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/foo/dir0/BACKUPfoo_1.drf' '/Users/rholmes/Projects/gemfire/test/cacheserver/disk1/BACKUPfoo_1.drf'";
private static final String UNIX_FULL_BACKUP_SCRIPT = "#!/bin/bash -e\ncd `dirname $0`\n\n#Restore a backup of gemfire persistent data to the location it was backed up\n#from.\n#This script will refuse to restore if the original data still exists.\n\n#This script was automatically generated by the gemfire backup utility.\n\n#Test for existing originals. If they exist, do not restore the backup.\ntest -e '/Users/rholmes/Projects/gemfire/test/cacheserver/disk3/BACKUPbar.if' && echo 'Backup not restored. Refusing to overwrite /Users/rholmes/Projects/gemfire/test/cacheserver/disk3/BACKUPbar.if' && exit 1 \ntest -e '/Users/rholmes/Projects/gemfire/test/cacheserver/disk1/BACKUPfoo.if' && echo 'Backup not restored. Refusing to overwrite /Users/rholmes/Projects/gemfire/test/cacheserver/disk1/BACKUPfoo.if' && exit 1 \n\n#Restore data\nmkdir -p '/Users/rholmes/Projects/gemfire/test/cacheserver/disk3'\ncp -rp 'diskstores/bar/dir0'/* '/Users/rholmes/Projects/gemfire/test/cacheserver/disk3'\nmkdir -p '/Users/rholmes/Projects/gemfire/test/cacheserver/disk4'\ncp -rp 'diskstores/bar/dir1'/* '/Users/rholmes/Projects/gemfire/test/cacheserver/disk4'\nmkdir -p '/Users/rholmes/Projects/gemfire/test/cacheserver/disk1'\ncp -rp 'diskstores/foo/dir0'/* '/Users/rholmes/Projects/gemfire/test/cacheserver/disk1'\nmkdir -p '/Users/rholmes/Projects/gemfire/test/cacheserver/disk2'\ncp -rp 'diskstores/foo/dir1'/* '/Users/rholmes/Projects/gemfire/test/cacheserver/disk2'";
private static final String WINDOWS_INCREMENTAL_BACKUP_SCRIPT = "rem echo off\n\nrem Restore a backup of gemfire persistent data to the location it was backed up\nrem from.\nrem This script will refuse to restore if the original data still exists.\n\nrem This script was automatically generated by the gemfire backup utility.\n\nrem Test for existing originals. If they exist, do not restore the backup.\nIF EXIST \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\\BACKUPbar.if\" echo \"Backup not restored. Refusing to overwrite \\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\\BACKUPbar.if\" && exit /B 1 \nIF EXIST \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\\BACKUPfoo.if\" echo \"Backup not restored. Refusing to overwrite \\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\\BACKUPfoo.if\" && exit /B 1 \n\nrem Restore data\nxcopy \"diskstores\\bar\\dir0\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\" /I /E\nxcopy \"diskstores\\foo\\dir0\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\" /I /E\n\nrem Incremental backup. Restore baseline originals from a previous backup.\ncopy \\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\bar\\dir0\\BACKUPbar_1.drf\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\\BACKUPbar_1.drf\"\ncopy \"\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\foo\\dir1\\BACKUPfoo_2.crf\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk2\\BACKUPfoo_2.crf\"\ncopy \"\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\foo\\dir1\\BACKUPfoo_2.drf\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk2\\BACKUPfoo_2.drf\"\ncopy \"\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\bar\\dir1\\BACKUPbar_2.drf\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk4\\BACKUPbar_2.drf\"\ncopy \"\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\foo\\dir0\\BACKUPfoo_1.crf\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\\BACKUPfoo_1.crf\"\ncopy \"\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\bar\\dir1\\BACKUPbar_2.crf\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk4\\BACKUPbar_2.crf\"\ncopy \"\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\bar\\dir0\\BACKUPbar_1.crf\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\\BACKUPbar_1.crf\"\ncopy \"\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\foo\\dir0\\BACKUPfoo_1.drf\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\\BACKUPfoo_1.drf\"";
private static final String WINDOWS_FULL_BACKUP_SCRIPT = "rem echo off\n\nrem Restore a backup of gemfire persistent data to the location it was backed up\nrem from.\nrem This script will refuse to restore if the original data still exists.\n\nrem This script was automatically generated by the gemfire backup utility.\n\nrem Test for existing originals. If they exist, do not restore the backup.\nIF EXIST \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\\BACKUPbar.if\" echo \"Backup not restored. Refusing to overwrite \\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\\BACKUPbar.if\" && exit /B 1 \nIF EXIST \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\\BACKUPfoo.if\" echo \"Backup not restored. Refusing to overwrite \\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\\BACKUPfoo.if\" && exit /B 1 \n\nrem Restore data\nxcopy \"diskstores\\bar\\dir0\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\"\nxcopy \"diskstores\\bar\\dir1\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk4\"\nxcopy \"diskstores\\foo\\dir0\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\"\nxcopy \"diskstores\\foo\\dir1\" \"\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk2\"";
private static final String OPLOG_FILENAME_1 = "BACKUPbar_1.drf";
private static final String OPLOG_FILENAME_2 = "BACKUPfoo_1.crf";
private static final String OPLOG_FILENAME_3 = "BACKUPfoo_1.drf";
private static final String UNIX_COPY_FROM_1 = "/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/bar/dir0/BACKUPbar_1.drf";
private static final String UNIX_COPY_FROM_2 = "/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/foo/dir0/BACKUPfoo_1.crf";
private static final String UNIX_COPY_FROM_3 = "/Users/rholmes/Projects/gemfire/test/backup/2012-05-24-09-42/rholmes_mbp_410_v1_56425/diskstores/foo/dir0/BACKUPfoo_1.drf";
private static final String WINDOWS_COPY_FROM_1 = "\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\bar\\dir0\\BACKUPbar_1.drf";
private static final String WINDOWS_COPY_FROM_2 = "\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\foo\\dir0\\BACKUPfoo_1.crf";
private static final String WINDOWS_COPY_FROM_3 = "\\Users\\rholmes\\Projects\\gemfire\\test\\backup\\2012-05-24-09-42\\rholmes_mbp_410_v1_56425\\diskstores\\foo\\dir0\\BACKUPfoo_1.drf";
private static final String UNIX_COPY_TO_1 = "/Users/rholmes/Projects/gemfire/test/cacheserver/disk3/BACKUPbar_1.drf";
private static final String UNIX_COPY_TO_2 = "/Users/rholmes/Projects/gemfire/test/cacheserver/disk1/BACKUPfoo_1.crf";
private static final String UNIX_COPY_TO_3 = "/Users/rholmes/Projects/gemfire/test/cacheserver/disk1/BACKUPfoo_1.drf";
private static final String WINDOWS_COPY_TO_1 = "\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk3\\BACKUPbar_1.drf";
private static final String WINDOWS_COPY_TO_2 = "\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\\BACKUPfoo_1.crf";
private static final String WINDOWS_COPY_TO_3 = "\\Users\\rholmes\\Projects\\gemfire\\test\\cacheserver\\disk1\\BACKUPfoo_1.drf";
/**
* Temporary incremental backup directory.
*/
private File incrementalBackupDir = null;
/**
* Temporary full backup directory.
*/
private File fullBackupDir = null;
/**
* Set up data for all tests.
*/
@Before
public void setUp() throws Exception {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
/*
* Create an incremental backup on the file system.
*/
this.incrementalBackupDir = new File(tempDir,"incremental");
assertTrue(this.incrementalBackupDir.mkdir());
File incrementalRestoreFile = null;
if(BackupInspector.isWindows()) {
incrementalRestoreFile = new File(this.incrementalBackupDir,WindowsBackupInspector.RESTORE_FILE);
PrintWriter writer = new PrintWriter(incrementalRestoreFile);
writer.write(WINDOWS_INCREMENTAL_BACKUP_SCRIPT);
writer.close();
} else {
incrementalRestoreFile = new File(this.incrementalBackupDir,UnixBackupInspector.RESTORE_FILE);
PrintWriter writer = new PrintWriter(incrementalRestoreFile);
writer.write(UNIX_INCREMENTAL_BACKUP_SCRIPT);
writer.close();
}
/*
* Create a full backup on the file system.
*/
this.fullBackupDir = new File(tempDir,"backup");
assertTrue(this.fullBackupDir.mkdir());
File fullRestoreFile = null;
if(BackupInspector.isWindows()) {
fullRestoreFile = new File(this.fullBackupDir,WindowsBackupInspector.RESTORE_FILE);
PrintWriter writer = new PrintWriter(fullRestoreFile);
writer.write(WINDOWS_FULL_BACKUP_SCRIPT);
writer.close();
} else {
fullRestoreFile = new File(this.fullBackupDir,UnixBackupInspector.RESTORE_FILE);
PrintWriter writer = new PrintWriter(fullRestoreFile);
writer.write(UNIX_FULL_BACKUP_SCRIPT);
writer.close();
}
}
/**
* Delete leftover data for all tests.
*/
@After
public void tearDown() throws Exception {
if((null != this.incrementalBackupDir) && (this.incrementalBackupDir.exists())) {
FileUtil.delete(this.incrementalBackupDir);
}
if((null != this.fullBackupDir) && (this.fullBackupDir.exists())) {
FileUtil.delete(fullBackupDir);
}
}
/**
* Tests that an IOException is thrown for a non-existent restore script.
* @throws Exception
*/
@Test
public void testNonExistentScriptFile()
throws Exception {
boolean ioexceptionThrown = false;
try {
@SuppressWarnings("unused")
BackupInspector inspector = BackupInspector.createInspector(new File(System.getProperty("java.io.tmpdir")));
} catch (IOException e) {
ioexceptionThrown = true;
}
assertTrue(ioexceptionThrown);
}
/**
* Tests copy lines for windows.
* @param inspector a BackupInspector.
*/
private void testIncrementalBackupScriptForWindows(BackupInspector inspector)
throws Exception {
assertEquals(WINDOWS_COPY_FROM_1,inspector.getCopyFromForOplogFile(OPLOG_FILENAME_1));
assertEquals(WINDOWS_COPY_TO_1,inspector.getCopyToForOplogFile(OPLOG_FILENAME_1));
assertEquals(WINDOWS_COPY_FROM_2,inspector.getCopyFromForOplogFile(OPLOG_FILENAME_2));
assertEquals(WINDOWS_COPY_TO_2,inspector.getCopyToForOplogFile(OPLOG_FILENAME_2));
assertEquals(WINDOWS_COPY_FROM_3,inspector.getCopyFromForOplogFile(OPLOG_FILENAME_3));
assertEquals(WINDOWS_COPY_TO_3,inspector.getCopyToForOplogFile(OPLOG_FILENAME_3));
}
/**
* Tests copy lines for unix.
* @param inspector a BackupInspector.
* @throws Exception
*/
private void testIncrementalBackupScriptForUnix(BackupInspector inspector)
throws Exception {
assertEquals(UNIX_COPY_FROM_1,inspector.getCopyFromForOplogFile(OPLOG_FILENAME_1));
assertEquals(UNIX_COPY_TO_1,inspector.getCopyToForOplogFile(OPLOG_FILENAME_1));
assertEquals(UNIX_COPY_FROM_2,inspector.getCopyFromForOplogFile(OPLOG_FILENAME_2));
assertEquals(UNIX_COPY_TO_2,inspector.getCopyToForOplogFile(OPLOG_FILENAME_2));
assertEquals(UNIX_COPY_FROM_3,inspector.getCopyFromForOplogFile(OPLOG_FILENAME_3));
assertEquals(UNIX_COPY_TO_3,inspector.getCopyToForOplogFile(OPLOG_FILENAME_3));
}
/**
* Tests that the parser succeeds for an incremental backup restore script.
* @throws Exception
*/
@Test
public void testIncrementalBackupScript()
throws Exception {
BackupInspector inspector = BackupInspector.createInspector(incrementalBackupDir);
assertTrue(inspector.isIncremental());
Set<String> oplogFiles = inspector.getIncrementalOplogFileNames();
assertFalse(oplogFiles.isEmpty());
assertEquals(8,oplogFiles.size());
assertTrue(oplogFiles.contains(OPLOG_FILENAME_1));
assertTrue(oplogFiles.contains(OPLOG_FILENAME_2));
assertTrue(oplogFiles.contains(OPLOG_FILENAME_3));
if(BackupInspector.isWindows()) {
testIncrementalBackupScriptForWindows(inspector);
} else {
testIncrementalBackupScriptForUnix(inspector);
}
}
/**
* Tests that the parser works with a full backup restore script.
* @throws Exception
*/
@Test
public void testFullBackupScript()
throws Exception {
BackupInspector inspector = BackupInspector.createInspector(fullBackupDir);
assertFalse(inspector.isIncremental());
assertTrue(inspector.getIncrementalOplogFileNames().isEmpty());
assertNull(inspector.getScriptLineForOplogFile(OPLOG_FILENAME_1));
}
}