modules/common/drift/src/test/java/org/rhq/common/drift/ChangeSetWriterImplTest.java
| 46 ++-
modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
| 18 -
modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ScheduleQueueImpl.java
| 14 -
modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java
| 116 ++++++++++
4 files changed, 162 insertions(+), 32 deletions(-)
New commits:
commit 223dad5af4d8a53b4912630633a4e40b2d8a9b38
Author: John Sanda <jsanda(a)redhat.com>
Date: Tue Jun 28 15:41:44 2011 -0400
Add absolute path of basedir header entry
This commit includes two changes. First, ChangeSetWriter has been
updated to write out the full path of the basedir header.
Secondly, empty directories are excluded from the change set file.
diff --git
a/modules/common/drift/src/test/java/org/rhq/common/drift/ChangeSetWriterImplTest.java
b/modules/common/drift/src/test/java/org/rhq/common/drift/ChangeSetWriterImplTest.java
index 3f4efda..0109619 100644
---
a/modules/common/drift/src/test/java/org/rhq/common/drift/ChangeSetWriterImplTest.java
+++
b/modules/common/drift/src/test/java/org/rhq/common/drift/ChangeSetWriterImplTest.java
@@ -24,23 +24,35 @@ import static org.testng.Assert.assertTrue;
public class ChangeSetWriterImplTest {
- File changeSetsDir = new File("target", "changesets");
+ File changeSetsDir;
+
+ File resourcesDir;
@BeforeClass
public void setupChangesetsDir() throws Exception {
- deleteDirectory(changeSetsDir);
- assertTrue(changeSetsDir.mkdirs(), "Failed to create " +
changeSetsDir.getPath());
+ File basedir = new File("target", getClass().getSimpleName());
+ deleteDirectory(basedir);
+
+ basedir.mkdir();
+
+ changeSetsDir = new File(basedir, "changesets");
+ changeSetsDir.mkdir();
+
+ resourcesDir = new File(basedir, "resources");
+ resourcesDir.mkdir();
}
@Test
@SuppressWarnings("unchecked")
public void writeDirectoryEntryWithAddedFile() throws Exception {
+ File resourceDir = new File(resourcesDir, "myresource");
+
File changeSetFile = new File(changeSetsDir, "added-file-test");
- Headers headers = new Headers("added-file-test",
"myresource", COVERAGE);
+ Headers headers = new Headers("added-file-test",
resourceDir.getAbsolutePath(), COVERAGE);
ChangeSetWriterImpl writer = new ChangeSetWriterImpl(changeSetFile, headers);
- writer.writeDirectoryEntry(new DirectoryEntry("myresource/conf").add(
+ writer.writeDirectoryEntry(new DirectoryEntry("conf").add(
addedFileEntry("myconf.conf", "a34ef6")));
writer.close();
@@ -50,9 +62,9 @@ public class ChangeSetWriterImplTest {
assertEquals(lines.size(), 6, "Expected to find six lines in " +
metaDataFile.getPath() +
" - three for the header followed by three for a directory
entry.");
- assertHeaderEquals(lines, "added-file-test", "myresource",
"C");
+ assertHeaderEquals(lines, "added-file-test",
resourceDir.getAbsolutePath(), "C");
- assertEquals(lines.get(3), "myresource/conf 1", "The first line
for a directory entry should specify the " +
+ assertEquals(lines.get(3), "conf 1", "The first line for a
directory entry should specify the " +
"directory path followed by the number of lines in the entry.");
assertFileEntryEquals(lines.get(4), "a34ef6 0 myconf.conf A");
@@ -61,12 +73,14 @@ public class ChangeSetWriterImplTest {
@Test
@SuppressWarnings("unchecked")
public void writeDirectoryEntryWithRemovedFile() throws Exception {
+ File resourceDir = new File(resourcesDir, "myresource");
+
File changeSetFile = new File(changeSetsDir, "removed-file-test");
- Headers headers = new Headers("removed-file-test",
"myresource", COVERAGE);
+ Headers headers = new Headers("removed-file-test",
resourceDir.getAbsolutePath(), COVERAGE);
ChangeSetWriterImpl writer = new ChangeSetWriterImpl(changeSetFile, headers);
- writer.writeDirectoryEntry(new DirectoryEntry("myresource/conf").add(
+ writer.writeDirectoryEntry(new DirectoryEntry("conf").add(
removedFileEntry("myconf.conf", "a34ef6")));
writer.close();
@@ -76,9 +90,9 @@ public class ChangeSetWriterImplTest {
assertEquals(lines.size(), 6, "Expected to find six lines in " +
metaDataFile.getPath() +
" - three for the header followed by three for a directory
entry.");
- assertHeaderEquals(lines, "removed-file-test", "myresource",
"C");
+ assertHeaderEquals(lines, "removed-file-test",
resourceDir.getAbsolutePath(), "C");
- assertEquals(lines.get(3), "myresource/conf 1", "The first line
for a directory entry should specify the " +
+ assertEquals(lines.get(3), "conf 1", "The first line for a
directory entry should specify the " +
"directory path followed by the number of lines in the entry.");
assertFileEntryEquals(lines.get(4), "0 a34ef6 myconf.conf R");
@@ -87,12 +101,14 @@ public class ChangeSetWriterImplTest {
@Test
@SuppressWarnings("unchecked")
public void writeDirectoryEntryWithChangedFile() throws Exception {
+ File resourceDir = new File(resourcesDir, "myresource");
+
File changeSetFile = new File(changeSetsDir, "changed-file-test");
- Headers headers = new Headers("changed-file-test",
"myresource", COVERAGE);
+ Headers headers = new Headers("changed-file-test",
resourceDir.getAbsolutePath(), COVERAGE);
ChangeSetWriterImpl writer = new ChangeSetWriterImpl(changeSetFile, headers);
- writer.writeDirectoryEntry(new DirectoryEntry("myresource/conf").add(
+ writer.writeDirectoryEntry(new DirectoryEntry("conf").add(
changedFileEntry("myconf.conf", "a34ef6",
"c2d55f")));
writer.close();
@@ -102,9 +118,9 @@ public class ChangeSetWriterImplTest {
assertEquals(lines.size(), 6, "Expected to find six lines in " +
metaDataFile.getPath() +
" - three for the header followed by three for a directory
entry.");
- assertHeaderEquals(lines, "changed-file-test", "myresource",
"C");
+ assertHeaderEquals(lines, "changed-file-test",
resourceDir.getAbsolutePath(), "C");
- assertEquals(lines.get(3), "myresource/conf 1", "The first line
for a directory entry should specify the " +
+ assertEquals(lines.get(3), "conf 1", "The first line for a
directory entry should specify the " +
"directory path followed by the number of lines in the entry.");
assertFileEntryEquals(lines.get(4), "c2d55f a34ef6 myconf.conf C");
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
index 40524ed..f1f0fdb 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
@@ -52,13 +52,14 @@ public class DriftDetector implements Runnable {
return;
}
+ DriftConfiguration driftConfig = schedule.getDriftConfiguration();
+
try {
// TODO add logic to determine if there is an existing changeset
// if there is no previous changeset then we need to generate the initial
// coverage changeset
ChangeSetWriter writer =
changeSetMgr.getChangeSetWriter(schedule.getResourceId(),
- new Headers(schedule.getDriftConfiguration().getName(),
schedule.getDriftConfiguration().getBasedir(),
- COVERAGE));
+ new Headers(driftConfig.getName(), driftConfig.getBasedir(), COVERAGE));
DirectoryScanner scanner = new
DirectoryScanner(schedule.getDriftConfiguration(), writer);
scanner.scan();
@@ -69,15 +70,14 @@ public class DriftDetector implements Runnable {
schedule.updateShedule();
scheduleQueue.enqueue(schedule);
- driftMgr.sendChangeSetToServer(schedule.getResourceId(),
schedule.getDriftConfiguration());
+ driftMgr.sendChangeSetToServer(schedule.getResourceId(), driftConfig);
}
private String relativePath(File basedir, File file) {
if (basedir.equals(file)) {
- return basedir.getPath();
+ return basedir.getName();
}
- return FilenameUtils.getName(basedir.getAbsolutePath()) + separator +
- file.getAbsolutePath().substring(basedir.getAbsolutePath().length() + 1);
+ return file.getAbsolutePath().substring(basedir.getAbsolutePath().length() + 1);
}
private String sha256(File file) throws IOException {
@@ -109,9 +109,9 @@ public class DriftDetector implements Runnable {
@Override
protected void handleDirectoryEnd(File directory, int depth, Collection results)
throws IOException {
DirectoryEntry dirEntry = stack.pop();
- writer.writeDirectoryEntry(dirEntry);
-
-
+ if (dirEntry.getNumberOfFiles() > 0) {
+ writer.writeDirectoryEntry(dirEntry);
+ }
}
@Override
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ScheduleQueueImpl.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ScheduleQueueImpl.java
index 7bd2623..fe15677 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ScheduleQueueImpl.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ScheduleQueueImpl.java
@@ -12,35 +12,33 @@ public class ScheduleQueueImpl implements ScheduleQueue {
@Override
public DriftDetectionSchedule dequeue() {
- Lock writeLock = lock.writeLock();
try {
+ lock.writeLock().lock();
return queue.poll();
} finally {
- writeLock.unlock();
+ lock.writeLock().unlock();
}
}
@Override
public boolean enqueue(DriftDetectionSchedule schedule) {
- Lock writeLock = lock.writeLock();
boolean inserted = false;
try {
- writeLock.lock();
+ lock.writeLock().lock();
inserted = queue.offer(schedule);
} finally {
- writeLock.unlock();
+ lock.writeLock().unlock();
}
return inserted;
}
@Override
public void clear() {
- Lock writeLock = lock.writeLock();
try {
- writeLock.lock();
+ lock.writeLock().lock();
queue.clear();
} finally {
- writeLock.unlock();
+ lock.writeLock().unlock();
}
}
}
diff --git
a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java
b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java
new file mode 100644
index 0000000..8ec7938
--- /dev/null
+++
b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftDetectorTest.java
@@ -0,0 +1,116 @@
+package org.rhq.core.pc.drift;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.util.List;
+
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import org.rhq.common.drift.ChangeSetReader;
+import org.rhq.common.drift.ChangeSetReaderImpl;
+import org.rhq.common.drift.DirectoryEntry;
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.configuration.PropertySimple;
+import org.rhq.core.domain.drift.DriftConfiguration;
+import org.rhq.test.JMockTest;
+
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.apache.commons.io.IOUtils.readLines;
+import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE;
+import static org.testng.Assert.assertEquals;
+import static org.unitils.thirdparty.org.apache.commons.io.FileUtils.touch;
+
+public class DriftDetectorTest extends JMockTest {
+
+ File changeSetsDir;
+
+ File resourcesDir;
+
+ ChangeSetManager changeSetMgr;
+
+ ScheduleQueue scheduleQueue;
+
+ @BeforeClass
+ public void init() throws Exception {
+ File basedir = new File("target", getClass().getSimpleName());
+ deleteDirectory(basedir);
+ basedir.mkdir();
+
+ changeSetsDir = new File(basedir, "changesets");
+ changeSetsDir.mkdir();
+
+ resourcesDir = new File(basedir, "resources");
+ resourcesDir.mkdir();
+ }
+
+ @BeforeMethod
+ public void setUp() {
+ changeSetMgr = new ChangeSetManagerImpl(changeSetsDir);
+ scheduleQueue = new ScheduleQueueImpl();
+ }
+
+ @Test
+ public void generateCoverageChangeSet() throws Exception {
+ File server1Dir = new File(resourcesDir, "server-1");
+ server1Dir.mkdir();
+
+ File confDir = new File(server1Dir, "conf");
+ confDir.mkdir();
+
+ touch(new File(confDir, "server.conf"));
+
+ DriftConfiguration driftConfig = driftConfiguration("coverage-test",
server1Dir.getAbsolutePath());
+
+ int resourceId = 1;
+ DriftDetectionSchedule schedule = new DriftDetectionSchedule(resourceId,
driftConfig);
+
+ scheduleQueue.enqueue(schedule);
+
+ DriftDetector detector = new DriftDetector();
+ detector.setChangeSetManager(changeSetMgr);
+ detector.setScheduleQueue(scheduleQueue);
+
+ detector.run();
+
+ File changeSetDir = new File(new File(changeSetsDir,
Integer.toString(resourceId)), "coverage-test");
+ File changeSet = new File(changeSetDir, "changeset.txt");
+
+ List<String> lines = readLines(new BufferedInputStream(new
FileInputStream(changeSet)));
+
+ assertHeaderEquals(lines, driftConfig.getName(), driftConfig.getBasedir(),
COVERAGE.code());
+ assertThatChangeSetDoesNotContainEmptyDirs(changeSet);
+ }
+
+ DriftConfiguration driftConfiguration(String name, String basedir) {
+ Configuration config = new Configuration();
+ config.put(new PropertySimple("name", name));
+ config.put(new PropertySimple("basedir", basedir));
+
+ return new DriftConfiguration(config);
+ }
+
+ void assertHeaderEquals(List<String> lines, String... expected) {
+ assertEquals(lines.get(0), expected[0], "The first header entry should be
the drift configuration name.");
+ assertEquals(lines.get(1), expected[1], "The second header entry should be
the base directory.");
+ assertEquals(lines.get(2), expected[2], "The third header entry should be a
flag indicating the change set " +
+ "type.");
+ }
+
+ void assertThatChangeSetDoesNotContainEmptyDirs(File changeSet) throws Exception {
+ ChangeSetReader reader = new ChangeSetReaderImpl(new BufferedReader(new
FileReader(changeSet)));
+ DirectoryEntry dirEntry = reader.readDirectoryEntry();
+
+ while (dirEntry != null) {
+ Assert.assertTrue(dirEntry.getNumberOfFiles() > 0, "The change set
file should not include empty " +
+ "directories");
+ dirEntry = reader.readDirectoryEntry();
+ }
+ }
+
+}