Skip to content
Permalink
Browse files

Fix JENKINS-21861

Add check for MatrixBuild
  • Loading branch information
Alex Earl
Alex Earl committed Jan 16, 2015
1 parent 9961b79 commit 8a47a78f5ed91c15f18e3a0cf2e2696b48514bec
@@ -2,8 +2,11 @@

import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.plugins.emailext.plugins.ContentBuilder;
import hudson.plugins.emailext.plugins.ZipDataSource;
import java.io.ByteArrayInputStream;
@@ -80,22 +83,22 @@ public String getName() {

private static final String DATA_SOURCE_NAME = "build.log";

private final AbstractBuild<?,?> build;
private final Run<?,?> run;
private final boolean compress;

public LogFileDataSource(AbstractBuild<?,?> build, boolean compress) {
this.build = build;
public LogFileDataSource(Run<?,?> run, boolean compress) {
this.run = run;
this.compress = compress;
}

public InputStream getInputStream() throws IOException {
InputStream res;
long logFileLength = build.getLogText().length();
long logFileLength = run.getLogText().length();
long pos = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();

while(pos < logFileLength) {
pos = build.getLogText().writeLogTo(pos, bao);
pos = run.getLogText().writeLogTo(pos, bao);
}

res = new ByteArrayInputStream(bao.toByteArray());
@@ -112,7 +115,7 @@ public OutputStream getOutputStream() throws IOException {

public String getContentType() {
return MimetypesFileTypeMap.getDefaultFileTypeMap()
.getContentType(build.getLogFile());
.getContentType(run.getLogFile());
}

public String getName() {
@@ -190,9 +193,9 @@ public void attach(Multipart multipart, ExtendedEmailPublisherContext context) {
}
}

public static void attachBuildLog(ExtendedEmailPublisherContext context, Multipart multipart, boolean compress) {
private static void attachSingleLog(ExtendedEmailPublisherContext context, Run<?,?> run, Multipart multipart, boolean compress) {
try {
File logFile = context.getBuild().getLogFile();
File logFile = run.getLogFile();
long maxAttachmentSize = context.getPublisher().getDescriptor().getMaxAttachmentSize();

if (maxAttachmentSize > 0 && logFile.length() >= maxAttachmentSize) {
@@ -207,14 +210,28 @@ public static void attachBuildLog(ExtendedEmailPublisherContext context, Multipa
context.getListener().getLogger().println("Request made to compress build log");
}

fileSource = new LogFileDataSource(context.getBuild(), compress);
attachment.setFileName("build." + (compress ? "zip" : "log"));
fileSource = new LogFileDataSource(run, compress);
if(run instanceof MatrixRun)
attachment.setFileName("build" + "-" + ((MatrixRun)run).getParent().getCombination().toString('-', '-') + "." + (compress ? "zip" : "log"));
else
attachment.setFileName("build." + (compress ? "zip" : "log"));
attachment.setDataHandler(new DataHandler(fileSource));
multipart.addBodyPart(attachment);
} catch (MessagingException e) {
} catch(MessagingException e) {
context.getListener().error("Error attaching build log to message: " + e.getMessage());
}
}

public static void attachBuildLog(ExtendedEmailPublisherContext context, Multipart multipart, boolean compress) {
if(context.getBuild() instanceof MatrixBuild) {
MatrixBuild build = (MatrixBuild)context.getBuild();
for(MatrixRun run : build.getExactRuns()) {
attachSingleLog(context, run, multipart, compress);
}
} else {
attachSingleLog(context, context.getBuild(), multipart, compress);
}
}

@Deprecated
public static void attachBuildLog(ExtendedEmailPublisher publisher, Multipart multipart, AbstractBuild<?, ?> build, BuildListener listener, boolean compress) {
@@ -4,17 +4,22 @@
import hudson.matrix.AxisList;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.labels.LabelAtom;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.RecipientProvider;
import hudson.plugins.emailext.plugins.trigger.AlwaysTrigger;
import hudson.plugins.emailext.plugins.trigger.PreBuildTrigger;
import hudson.plugins.emailext.plugins.recipients.ListRecipientProvider;
import hudson.slaves.DumbSlave;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
@@ -37,6 +42,7 @@ public void before() throws Throwable {
publisher = new ExtendedEmailPublisher();
publisher.defaultSubject = "%DEFAULT_SUBJECT";
publisher.defaultContent = "%DEFAULT_CONTENT";
publisher.attachBuildLog = false;

project = createMatrixProject();
project.getPublishersList().add( publisher );
@@ -63,8 +69,7 @@ public void testPreBuildMatrixBuildSendParentOnly() throws Exception {
addEmailType( trigger );
publisher.getConfiguredTriggers().add( trigger );
MatrixBuild build = project.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);

j.assertBuildStatusSuccess(build);

assertThat( "Email should have been triggered, so we should see it in the logs.", build.getLog( 100 ),
hasItems( "Email was triggered for: " + PreBuildTrigger.TRIGGER_NAME ) );
@@ -88,7 +93,7 @@ public void testPreBuildMatrixBuildSendSlavesOnly() throws Exception{
}

@Test
public void testPreBuildMatrixBuildSendSlavesAndParent() throws Exception{
public void testPreBuildMatrixBuildSendSlavesAndParent() throws Exception {
addSlaveToProject(0,1);
List<RecipientProvider> recProviders = Collections.emptyList();
publisher.setMatrixTriggerMode(MatrixTriggerMode.BOTH);
@@ -102,6 +107,42 @@ public void testPreBuildMatrixBuildSendSlavesAndParent() throws Exception{
j.assertBuildStatusSuccess(build);
assertEquals( 3, Mailbox.get( "solganik@gmail.com" ).size() );
}

@Test
public void testAttachBuildLogForAllAxes() throws Exception {
publisher.setMatrixTriggerMode(MatrixTriggerMode.ONLY_PARENT);
publisher.attachBuildLog = true;
addSlaveToProject(0,1,2);
List<RecipientProvider> recProviders = Collections.emptyList();
AlwaysTrigger trigger = new AlwaysTrigger(recProviders, "$DEFAULT_RECIPIENTS",
"$DEFAULT_REPLYTO", "$DEFAULT_SUBJECT", "$DEFAULT_CONTENT", "", 0, "project");
addEmailType( trigger );
publisher.getConfiguredTriggers().add( trigger );
MatrixBuild build = project.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);

assertThat( "Email should have been triggered, so we should see it in the logs.", build.getLog( 100 ),
hasItems( "Email was triggered for: " + AlwaysTrigger.TRIGGER_NAME ) );

assertEquals( 1, Mailbox.get( "solganik@gmail.com" ).size() );

Message msg = Mailbox.get("solganik@gmail.com").get(0);

assertTrue("Message should be multipart", msg instanceof MimeMessage);
assertTrue("Content should be a MimeMultipart", msg.getContent() instanceof MimeMultipart);

MimeMultipart part = (MimeMultipart)msg.getContent();

assertEquals("Should have four body items (message + attachment)", 4, part.getCount());

int i = 1;
for(MatrixRun r : build.getExactRuns()) {
String fileName = "build" + "-" + r.getParent().getCombination().toString('-', '-') + ".log";
BodyPart attach = part.getBodyPart(i);
assertTrue("There should be a log named \"" + fileName + "\" attached", fileName.equalsIgnoreCase(attach.getFileName()));
i++;
}
}

private void addEmailType( EmailTrigger trigger ) {
trigger.setEmail( new EmailType()

0 comments on commit 8a47a78

Please sign in to comment.
You can’t perform that action at this time.