modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java | 37 ++++++--- modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java | 11 ++ modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java | 40 ++++++++-- 3 files changed, 68 insertions(+), 20 deletions(-)
New commits: commit 35fcafbd977ca41cb2cbff7be81787543c5224e8 Author: John Sanda jsanda@redhat.com Date: Mon Dec 12 13:39:37 2011 -0500
[BZ 760263] Adding check to avoid generating empty content zip file
When the server requests drift content from the agent, it is possible that the requested files may not exist. DriftFilesSender now checks to make sure that there is actually content before making the request to zip it up and send it to the server.
diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java index d8d2b46..4181472 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java @@ -1,22 +1,16 @@ package org.rhq.core.pc.drift;
-import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.List; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.rhq.common.drift.ChangeSetReader; import org.rhq.common.drift.FileEntry; import org.rhq.common.drift.Headers; import org.rhq.core.domain.drift.DriftFile; import org.rhq.core.util.stream.StreamUtil;
+import java.io.*; +import java.util.List; + public class DriftFilesSender implements Runnable {
private Log log = LogFactory.getLog(DriftFilesSender.class); @@ -54,6 +48,10 @@ public class DriftFilesSender implements Runnable { @Override public void run() { try { + if (log.isInfoEnabled()) { + log.info("Preparing to send content to server for " + defToString()); + } + File changeSet = changeSetMgr.findChangeSet(resourceId, headers.getDriftDefinitionName()); File contentDir = new File(changeSet.getParentFile(), "content"); contentDir.mkdir(); @@ -63,11 +61,25 @@ public class DriftFilesSender implements Runnable { if (file == null || !file.exists()) { log.warn("Unable to find file for " + driftFile); } else { + if (log.isDebugEnabled()) { + log.debug("Copying " + file.getPath() + " to " + contentDir.getPath()); + } StreamUtil.copy(new BufferedInputStream(new FileInputStream(file)), new BufferedOutputStream( new FileOutputStream(new File(contentDir, driftFile.getHashId()))), true); } } - driftClient.sendChangeSetContentToServer(resourceId, headers.getDriftDefinitionName(), contentDir); + + File[] content = contentDir.listFiles(); + if (content == null || content.length == 0) { + if (log.isDebugEnabled()) { + log.debug("Could not find any of the requested content for " + defToString()); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Sending " + content.length + " files to the server for " + defToString()); + } + driftClient.sendChangeSetContentToServer(resourceId, headers.getDriftDefinitionName(), contentDir); + } } catch (IOException e) { log.error("Failed to send drift files.", e); } @@ -87,4 +99,9 @@ public class DriftFilesSender implements Runnable { reader.close(); } } + + private String defToString() { + return "[resourceId: " + resourceId + ", driftDefinitionId: " + headers.getDriftDefinitionId() + + ", driftDefinitionName: " + headers.getDriftDefinitionName() + "]"; + } } diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java index 209ba61..0f0c26a 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftClientTestStub.java @@ -1,9 +1,9 @@ package org.rhq.core.pc.drift;
-import java.io.File; - import org.rhq.core.domain.drift.DriftDefinition;
+import java.io.File; + class DriftClientTestStub implements DriftClient {
private File basedir; @@ -14,6 +14,8 @@ class DriftClientTestStub implements DriftClient {
private int reportMissingBaseDirInvocationCount;
+ private int sendChangeSetContentInvocationCount; + @Override public void sendChangeSetToServer(DriftDetectionSummary detectionSummary) { ++sendChangeSetInvocationCount; @@ -28,6 +30,11 @@ class DriftClientTestStub implements DriftClient {
@Override public void sendChangeSetContentToServer(int resourceId, String driftDefinitionName, File contentDir) { + ++sendChangeSetContentInvocationCount; + } + + public int getSendChangeSetContentInvocationCount() { + return sendChangeSetContentInvocationCount; }
@Override diff --git a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java index 5fd6528..77db6b2 100644 --- a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java +++ b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java @@ -19,18 +19,17 @@
package org.rhq.core.pc.drift;
-import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - import org.rhq.common.drift.ChangeSetWriter; import org.rhq.common.drift.Headers; import org.rhq.core.domain.drift.DriftChangeSetCategory; import org.rhq.core.domain.drift.DriftFile; import org.rhq.core.domain.drift.JPADriftFile; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.List;
import static org.rhq.common.drift.FileEntry.addedFileEntry; import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE; @@ -39,7 +38,7 @@ import static org.testng.Assert.assertTrue;
public class DriftFilesSenderTest extends DriftTest {
- DriftClient driftClient; + DriftClientTestStub driftClient;
DriftFilesSender sender;
@@ -161,6 +160,31 @@ public class DriftFilesSenderTest extends DriftTest { assertFileCopiedToContentDir(contentDir, server2ConfHash); }
+ @Test + public void doNotSendEmptyZipFileToServer() throws Exception { + String driftDefName = "empty_content_test"; + + File confDir = mkdir(resourceDir, "conf"); + File serverConf = createRandomFile(confDir, "server.conf"); + + String serverConfHash = sha256(serverConf); + + Headers headers = createHeaders(driftDefName, COVERAGE); + + ChangeSetWriter writer = changeSetMgr.getChangeSetWriter(resourceId(), headers); + writer.write(addedFileEntry("conf/server.conf", serverConfHash)); + writer.close(); + + serverConf.delete(); + + sender.setDriftFiles(driftFiles(serverConfHash)); + sender.setHeaders(headers); + sender.run(); + + assertEquals(driftClient.getSendChangeSetContentInvocationCount(), 0, + "Do not call DriftClient to send content to server when there is no content to send."); + } + /** * This method only verifies that a file having a particular hash or SHA-256 has been * copied to the specified content directory.
rhq-commits@lists.fedorahosted.org