Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added aditional params option #98

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/main/java/jenkins/plugins/logstash/LogstashWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package jenkins.plugins.logstash;


import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.model.Run;
Expand All @@ -44,6 +43,7 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* A writer that wraps all Logstash DAOs. Handles error reporting and per build connection state.
Expand All @@ -69,10 +69,17 @@ public class LogstashWriter implements Serializable {
private final String agentName;

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset) {
this(run, error, listener, charset, null, null);
this(run, error, listener, charset, null, null, null);
}

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, Map<String, String> additionalParams) {
this(run, error, listener, charset, null, null, additionalParams);
}
public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, String stageName, String agentName) {
this(run, error, listener, charset, stageName, agentName, null);
}

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, String stageName, String agentName, Map<String, String> additionalParams) {
this.errorStream = error != null ? error : System.err;
this.stageName = stageName;
this.agentName = agentName;
Expand All @@ -85,10 +92,11 @@ public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener,
this.buildData = null;
} else {
this.jenkinsUrl = getJenkinsUrl();
this.buildData = getBuildData();
this.buildData = getBuildData(additionalParams);
}
}


/**
* Gets the charset that Jenkins is using during this build.
*
Expand Down Expand Up @@ -162,11 +170,11 @@ LogstashIndexerDao getIndexerDao() {
return LogstashConfiguration.getInstance().getIndexerInstance();
}

BuildData getBuildData() {
BuildData getBuildData(Map<String, String> additionalParams) {
if (build instanceof AbstractBuild) {
return new BuildData((AbstractBuild<?, ?>) build, new Date(), listener);
return new BuildData((AbstractBuild<?, ?>) build, new Date(), listener, additionalParams);
} else {
return new BuildData(build, new Date(), listener, stageName, agentName);
return new BuildData(build, new Date(), listener, stageName, agentName, additionalParams);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,23 @@ public List<String> getFailedTests()
private Map<String, String> buildVariables;
private Set<String> sensitiveBuildVariables;
private TestData testResults = null;

// Freestyle project build

public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener listener) {
this(build, currentTime, listener, null);
}
// Freestyle project build
public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener listener, Map<String, String> additionalParams) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems there is no way to use this additional params in freestile builds, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any stapler bindings updated, how would it be configured?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now it won't be implemented in freestyle builds

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, can you please remove those extra constructors?
Otherwise LGTM

initData(build, currentTime);

// build.getDuration() is always 0 in Notifiers
rootProjectName = build.getRootBuild().getProject().getName();
additionalParams = additionalParams == null ? new HashMap<String, String>() : additionalParams;
rootFullProjectName = build.getRootBuild().getProject().getFullName();
rootProjectDisplayName = build.getRootBuild().getDisplayName();
rootBuildNum = build.getRootBuild().getNumber();
buildVariables = build.getBuildVariables();
buildVariables = new HashMap<String,String>(buildVariables);
buildVariables.putAll(additionalParams);
This conversation was marked as resolved.
Show resolved Hide resolved
sensitiveBuildVariables = build.getSensitiveBuildVariables();

// Get environment build variables and merge them into the buildVariables map
Expand All @@ -211,30 +217,37 @@ public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener liste
buildVariables.putAll(build.getEnvironment(listener));
} catch (Exception e) {
// no base build env vars to merge
LOGGER.log(WARNING,"Unable update logstash buildVariables with EnvVars from " + build.getDisplayName(),e);
LOGGER.log(WARNING, "Unable update logstash buildVariables with EnvVars from " + build.getDisplayName(), e);
}

for (String key : sensitiveBuildVariables) {
buildVariables.remove(key);
}
}

public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, String stageName, String agentName){
this(build, currentTime, listener, stageName, agentName, null);
}

// Pipeline project build
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, String stageName, String agentName) {
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, String stageName, String agentName,
Map<String, String> additionalParams) {
initData(build, currentTime);

additionalParams = additionalParams == null ? new HashMap<String, String>() : additionalParams;
this.agentName = agentName;
this.stageName = stageName;
rootProjectName = projectName;
rootFullProjectName = fullProjectName;
rootProjectDisplayName = displayName;
rootBuildNum = buildNum;
this.buildVariables = new HashMap<String, String>(additionalParams);

try {
// TODO: sensitive variables are not filtered, c.f. https://stackoverflow.com/questions/30916085
buildVariables = build.getEnvironment(listener);
// TODO: sensitive variables are not filtered, c.f.
// https://stackoverflow.com/questions/30916085
buildVariables.putAll(build.getEnvironment(listener));
} catch (IOException | InterruptedException e) {
LOGGER.log(WARNING,"Unable to get environment for " + build.getDisplayName(),e);
buildVariables = new HashMap<>();
LOGGER.log(WARNING, "Unable to get environment for " + build.getDisplayName(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jenkins.plugins.logstash.pipeline;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jenkinsci.plugins.workflow.steps.Step;
Expand All @@ -27,12 +29,14 @@ public class LogstashSendStep extends Step

private int maxLines;
private boolean failBuild;
private Map<String,String> additionalParams;

@DataBoundConstructor
public LogstashSendStep(int maxLines, boolean failBuild)
public LogstashSendStep(int maxLines, boolean failBuild, Map<String,String> additionalParams)
{
this.maxLines = maxLines;
this.failBuild = failBuild;
this.additionalParams = additionalParams;
}

public int getMaxLines()
Expand All @@ -48,7 +52,7 @@ public boolean isFailBuild()
@Override
public StepExecution start(StepContext context) throws Exception
{
return new Execution(context, maxLines, failBuild);
return new Execution(context, maxLines, failBuild, additionalParams);
}

@SuppressFBWarnings(value="SE_TRANSIENT_FIELD_NOT_RESTORED", justification="Only used when starting.")
Expand All @@ -59,29 +63,36 @@ private static class Execution extends SynchronousNonBlockingStepExecution<Void>

private transient final int maxLines;
private transient final boolean failBuild;
private transient final Map<String, String> additionalParams;

Execution(StepContext context, int maxLines, boolean failBuild)
{
super(context);
this.maxLines = maxLines;
this.failBuild = failBuild;
this.additionalParams = new HashMap<String,String>();
}

Execution(StepContext context, int maxLines, boolean failBuild, Map<String, String> additionalParams)
{
super(context);
this.maxLines = maxLines;
this.failBuild = failBuild;
this.additionalParams = additionalParams;
}

@Override
protected Void run() throws Exception
{
protected Void run() throws Exception {
Run<?, ?> run = getContext().get(Run.class);
TaskListener listener = getContext().get(TaskListener.class);
PrintStream errorStream = listener.getLogger();
LogstashWriter logstash = new LogstashWriter(run, errorStream, listener, run.getCharset());
LogstashWriter logstash = new LogstashWriter(run, errorStream, listener, run.getCharset(), this.additionalParams);
logstash.writeBuildLog(maxLines);
if (failBuild && logstash.isConnectionBroken())
{
if (failBuild && logstash.isConnectionBroken()) {
throw new Exception("Failed to send data to Indexer");
}
return null;
}

}

@Extension
Expand Down Expand Up @@ -110,4 +121,4 @@ public Set<? extends Class<?>> getRequiredContext()
return contexts;
}
}
}
}
This conversation was marked as resolved.
Show resolved Hide resolved
11 changes: 8 additions & 3 deletions src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -58,19 +60,22 @@ static LogstashWriter createLogstashWriter(final AbstractBuild<?, ?> testBuild,
final String url,
final LogstashIndexerDao indexer,
final BuildData data) {
return new LogstashWriter(testBuild, error, null, testBuild.getCharset()) {
HashMap<String, String> additionalParams = new HashMap<String, String>();
additionalParams.put("param1", "value1");
This conversation was marked as resolved.
Show resolved Hide resolved
additionalParams.put("param2", "value2");
return new LogstashWriter(testBuild, error, null, testBuild.getCharset(), additionalParams) {
@Override
LogstashIndexerDao getIndexerDao() {
return indexer;
}

@Override
BuildData getBuildData() {
BuildData getBuildData(Map<String, String> additionalParams) {
assertNotNull("BuildData should never be requested for missing dao.", this.getDao());

// For testing, providing null data means use the actual method
if (data == null) {
return super.getBuildData();
return super.getBuildData(additionalParams);
} else {
return data;
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/jenkins/plugins/logstash/PipelineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand Down Expand Up @@ -128,4 +130,43 @@ public void logstashSend() throws Exception
JSONObject data = firstLine.getJSONObject("data");
assertThat(data.getString("result"),equalTo("SUCCESS"));
}

@Test
public void logstashSendWithAdditionalParams() throws Exception
{
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
HashMap<String, String> someMap = new HashMap<String,String>();
someMap.put("param1", "value1");
someMap.put("param2", "value2");
p.setDefinition(new CpsFlowDefinition(
"echo 'Message'\n" +
"currentBuild.result = 'SUCCESS'\n" +
"def someMap = [param1:'value1', param2:'value2']\n" +
"logstashSend failBuild: true, maxLines: 5, additionalParams: someMap"
, true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0).get());
List<JSONObject> dataLines = memoryDao.getOutput();
assertThat(dataLines.size(), equalTo(1));
JSONObject firstLine = dataLines.get(0);
JSONObject data = firstLine.getJSONObject("data");
assertThat(data.getString("result"),equalTo("SUCCESS"));
This conversation was marked as resolved.
Show resolved Hide resolved
}

public String mapFormatter(Map<String, Object> lhm1,String result) {
This conversation was marked as resolved.
Show resolved Hide resolved
result=result+"["+"\n";
for (Map.Entry<String, Object> entry : lhm1.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Map) {
Map<String, Object> subMap = (Map<String, Object>)value;
result=result+key+" : ";
result=result+mapFormatter(subMap,"");
}
else {
result=result+key+" : "+value+","+"\n";
}
}
result=result.replaceAll(",$", "")+"]"+"\n";
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -404,4 +405,30 @@ public void rootProjectFullName() throws Exception
verifyMocks();
verifyTestResultActions();
}

@Test
public void additionalParamsAreAddedCorrectly() throws Exception
{
when(mockBuild.getEnvironments()).thenReturn(new EnvironmentList(Arrays.asList(mockEnvironment)));
when(mockBuild.getBuildVariables()).thenReturn(new HashMap<String, String>());
when(mockComputer.getNode()).thenReturn(mockNode);
final String buildVarKey = "BuildVarKey";
final String buildVarVal = "BuildVarVal";
when(mockBuild.getEnvironment(mockListener)).thenReturn(new EnvVars(buildVarKey, buildVarVal));
when(mockBuild.getSensitiveBuildVariables()).thenReturn(new HashSet<>());
doNothing().when(mockEnvironment).buildEnvVars(any());

HashMap<String,String> additionalParams = new HashMap<String, String>();
additionalParams.put("param1", "value1");
additionalParams.put("param2", "value2");

BuildData buildData = new BuildData(mockBuild, mockDate, mockListener, additionalParams);

Assert.assertEquals("Missing additionalParam: param1", "value1", buildData.getBuildVariables().get("param1"));
Assert.assertEquals("Missing additionalParam: param1", "value2", buildData.getBuildVariables().get("param2"));

verify(mockEnvironment).buildEnvVars(any());
verifyMocks();
verifyTestResultActions();
}
}