final String TEST4 = "test4.txt";
File file4 = new File(this.bundleFilesDir, TEST4);
writeFile(TEST4, file4);
// ----- initial deployment -----
BundleDeployRequest request = new BundleDeployRequest();
request.setBundleFilesLocation(this.bundleFilesDir);
request.setResourceDeployment(createNewBundleDeployment(deployment));
request.setBundleManagerProvider(new MockBundleManagerProvider());
request.setAbsoluteDestinationDirectory(this.destDir);
BundleDeployResult results = plugin.deployBundle(request);
assertResultsSuccess(results);
// test that the files were put where we expected them to be
File file1Dest = new File(this.destDir, "subdir/" + TEST1);
File file2Dest = new File(this.destDir, TEST2);
File file3Dest = new File(externalDir, TEST3);
File file4Dest = new File(externalDir, TEST4);
assert TEST1.equals(readFile(file1Dest)); // inside dest dir
assert TEST2.equals(readFile(file2Dest)); // inside dest dir
assert TEST3.equals(readFile(file3Dest)); // outside dest dir
assert TEST4.equals(readFile(file4Dest)); // outside dest dir
// ----- prepare to update the bundle ----
cleanPluginDirs(); // clean everything but the dest dir - we want to keep the metadata (this should not purge ext/ dir)
prepareBeforeTestMethod(); // prepare for our new test
// our src files will have different content for this bundle deployment compared to the initial deployment
writeFile(TEST1 + "update", file1);
writeFile(TEST2 + "update", file2);
writeFile(TEST3 + "update", file3);
writeFile(TEST4 + "update", file4);
// change our initial deployment files, RHQ should see the changes and back these files up
writeFile(TEST1 + "modified", file1Dest);
writeFile(TEST2 + "modified", file2Dest);
writeFile(TEST3 + "modified", file3Dest);
writeFile(TEST4 + "modified", file4Dest);
// ----- update deployment -----
deployment.setId(1);
request = new BundleDeployRequest();
request.setBundleFilesLocation(this.bundleFilesDir);
request.setResourceDeployment(createNewBundleDeployment(deployment));
request.setBundleManagerProvider(new MockBundleManagerProvider());
request.setAbsoluteDestinationDirectory(this.destDir);
results = plugin.deployBundle(request);
assertResultsSuccess(results);
// test that all files were updated
assert (TEST1 + "update").equals(readFile(file1Dest)); // inside dest dir
assert (TEST2 + "update").equals(readFile(file2Dest)); // inside dest dir
assert (TEST3 + "update").equals(readFile(file3Dest)); // outside dest dir
assert (TEST4 + "update").equals(readFile(file4Dest)); // outside dest dir
// test that our changed files that were under dest dir were properly backed up
DeploymentsMetadata metadata = new DeploymentsMetadata(this.destDir);
File backupDir = metadata.getDeploymentBackupDirectory(deployment.getId());
File file1Backup = new File(backupDir, "subdir/" + TEST1);
File file2Backup = new File(backupDir, TEST2);
assert file1Backup.isFile() : "should have been backed up: " + file1Backup;
assert file2Backup.isFile() : "should have been backed up: " + file2Backup;
assert (TEST1 + "modified").equals(readFile(file1Backup)) : "bad backup file: " + file1Backup;
assert (TEST2 + "modified").equals(readFile(file2Backup)) : "bad backup file: " + file2Backup;
// test that our changed files that were above dest dir were properly backed up
Map<String, File> winDirs = metadata.getDeploymentExternalBackupDirectoriesForWindows(deployment.getId());
if (winDirs == null) {
// we are running on non-windows platform
backupDir = metadata.getDeploymentExternalBackupDirectory(deployment.getId());
} else {
// we are on windows, our test only uses a single drive root, so we can grab the only item in the map
assert winDirs.size() == 1 : "should only have 1 ext backup dir on windows: " + winDirs;
backupDir = winDirs.values().iterator().next().getAbsoluteFile();
}
File file3Backup;
File file4Backup;
boolean isWindows = (File.separatorChar == '\\');
if (isWindows) {
StringBuilder file3AbsPath = new StringBuilder(file3Dest.getAbsolutePath());
StringBuilder file4AbsPath = new StringBuilder(file4Dest.getAbsolutePath());
FileUtil.stripDriveLetter(file3AbsPath);
FileUtil.stripDriveLetter(file4AbsPath);
file3Backup = new File(backupDir, file3AbsPath.toString());
file4Backup = new File(backupDir, file4AbsPath.toString());
} else {
file3Backup = new File(backupDir, file3Dest.getAbsolutePath());
file4Backup = new File(backupDir, file4Dest.getAbsolutePath());
}
assert file3Backup.isFile() : "should have been backed up: " + file3Backup;
assert file4Backup.isFile() : "should have been backed up: " + file4Backup;
assert (TEST3 + "modified").equals(readFile(file3Backup)) : "bad backup file: " + file3Backup;
assert (TEST4 + "modified").equals(readFile(file4Backup)) : "bad backup file: " + file4Backup;
// ----- revert to last deployment, restoring backed up files
deployment.setId(2);
request = new BundleDeployRequest();
request.setBundleFilesLocation(this.bundleFilesDir);
request.setResourceDeployment(createNewBundleDeployment(deployment));
request.setBundleManagerProvider(new MockBundleManagerProvider());
request.setAbsoluteDestinationDirectory(this.destDir);
request.setRevert(true);
results = plugin.deployBundle(request);
assertResultsSuccess(results);
// make sure our files were reverted, giving us back the files that were backed up
assert readFile(file1Backup).equals(readFile(file1Dest)); // inside dest dir