Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import edu.umd.cs.findbugs.annotations.CheckForNull;

import hudson.ExtensionList;
import hudson.util.Secret;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -286,7 +287,6 @@ public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws

public static LogstashConfiguration getInstance()
{
return GlobalConfiguration.all().get(LogstashConfiguration.class);
return ExtensionList.lookupSingleton(LogstashConfiguration.class);
}

}
5 changes: 5 additions & 0 deletions src/main/java/jenkins/plugins/logstash/LogstashWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jenkins.model.Jenkins;
import jenkins.plugins.logstash.persistence.BuildData;
import jenkins.plugins.logstash.persistence.LogstashIndexerDao;
import jenkins.util.JenkinsJVM;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
Expand Down Expand Up @@ -159,6 +160,10 @@ public boolean isConnectionBroken() {

// Method to encapsulate calls for unit-testing
LogstashIndexerDao getIndexerDao() {
if (!JenkinsJVM.isJenkinsJVM()){
return null;
}

return LogstashConfiguration.getInstance().getIndexerInstance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
import java.util.ArrayList;
import java.util.List;

import hudson.model.labels.LabelAtom;
import org.jenkinsci.plugins.envinject.EnvInjectBuildWrapper;
import org.jenkinsci.plugins.envinject.EnvInjectJobPropertyInfo;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -62,6 +66,8 @@ public void setup() throws Exception

slave = jenkins.createSlave();
slave.setLabelString("myLabel");
jenkins.createOnlineSlave(new LabelAtom("test"));

project = jenkins.createFreeStyleProject();
}

Expand Down Expand Up @@ -206,19 +212,50 @@ public void passwordsAreMaskedWithGlobalMaskPasswordsConfiguration() throws Exce
@Test
public void enableGlobally() throws Exception
{
LogstashConfiguration.getInstance().setEnableGlobally(true);
QueueTaskFuture<FreeStyleBuild> f = project.scheduleBuild2(0);
boolean isEnableGloballyInit = LogstashConfiguration.getInstance().isEnableGlobally();
LogstashConfiguration.getInstance().setEnableGlobally(true);
QueueTaskFuture<FreeStyleBuild> f = project.scheduleBuild2(0);

FreeStyleBuild build = f.get();
assertThat(build.getResult(), equalTo(Result.SUCCESS));
List<JSONObject> dataLines = memoryDao.getOutput();
assertThat(dataLines.size(), greaterThan(3));
JSONObject firstLine = dataLines.get(0);
FreeStyleBuild build = f.get();
assertThat(build.getResult(), equalTo(Result.SUCCESS));
List<JSONObject> dataLines = memoryDao.getOutput();
assertThat(dataLines.size(), greaterThan(3));
JSONObject firstLine = dataLines.get(0);
JSONObject lastLine = dataLines.get(dataLines.size()-1);
JSONObject data = firstLine.getJSONObject("data");
JSONObject data = firstLine.getJSONObject("data");
assertThat(data.getString("buildHost"),equalTo("Jenkins"));
assertThat(data.getString("buildLabel"),equalTo("master"));
assertThat(lastLine.getJSONArray("message").get(0).toString(),equalTo("Finished: SUCCESS"));

LogstashConfiguration.getInstance().setEnableGlobally(isEnableGloballyInit);
}

@Test
public void enableGloballyOnRemote() throws Exception
{
boolean isEnableGloballyInit = LogstashConfiguration.getInstance().isEnableGlobally();
LogstashConfiguration.getInstance().setEnableGlobally(true);

WorkflowJob p = jenkins.jenkins.createProject(WorkflowJob.class, "pipelineOnWorkerNode");
p.setDefinition(new CpsFlowDefinition("node('test') {echo 'foo'}\n", true));
WorkflowRun build = p.scheduleBuild2(0).get();

assertThat(build.getResult(), equalTo(Result.SUCCESS));
List<JSONObject> dataLines = memoryDao.getOutput();

assertThat(dataLines.size(), greaterThan(10));
boolean containsFoo = dataLines.stream()
.anyMatch(line -> line.getString("message").contains("foo"));
assertThat("Expected at least one line to contain 'foo'", containsFoo, is(true));
JSONObject firstLine = dataLines.get(0);
JSONObject lastLine = dataLines.get(dataLines.size()-1);
JSONObject data = firstLine.getJSONObject("data");

assertThat(data.getString("buildHost"),equalTo("Jenkins"));
assertThat(data.getString("buildLabel"),equalTo("master"));
assertThat(lastLine.getJSONArray("message").get(0).toString(),equalTo("Finished: SUCCESS"));

LogstashConfiguration.getInstance().setEnableGlobally(isEnableGloballyInit);
}

@Test
Expand Down
25 changes: 21 additions & 4 deletions src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package jenkins.plugins.logstash;

import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -44,12 +41,14 @@
import hudson.tasks.test.AbstractTestResultAction;
import jenkins.plugins.logstash.persistence.BuildData;
import jenkins.plugins.logstash.persistence.LogstashIndexerDao;
import jenkins.util.JenkinsJVM;
import net.sf.json.JSONObject;

@RunWith(MockitoJUnitRunner.class)
public class LogstashWriterTest {

private MockedStatic<LogstashConfiguration> mockedLogstashConfiguration;
private MockedStatic<JenkinsJVM> mockedJenkinsJVM;

// Extension of the unit under test that avoids making calls to getInstance() to get the DAO singleton
static LogstashWriter createLogstashWriter(final AbstractBuild<?, ?> testBuild,
Expand Down Expand Up @@ -150,6 +149,9 @@ public void after() throws Exception {
verifyNoMoreInteractions(mockProject);
errorBuffer.close();
mockedLogstashConfiguration.closeOnDemand();
if (mockedJenkinsJVM != null) {
mockedJenkinsJVM.closeOnDemand();
}
}

@Test
Expand Down Expand Up @@ -346,4 +348,19 @@ public void writeBuildLogGetLogError() throws Exception {
assertThat("The exception was not sent to Logstash", actualLogLines.get(0), containsString(expectedErrorLines.get(0)));
assertThat("The exception was not sent to Logstash", actualLogLines.get(1), containsString(expectedErrorLines.get(1)));
}

@Test
public void testGetIndexerDao_returnsNull_whenNotInJenkinsJVM() throws Exception {
mockedJenkinsJVM = Mockito.mockStatic(JenkinsJVM.class);
mockedJenkinsJVM.when(JenkinsJVM::isJenkinsJVM).thenReturn(false);

// Create a test instance that uses the real getIndexerDao method
LogstashWriter writer = new LogstashWriter(mockBuild, errorBuffer, mockListener, Charset.defaultCharset());

// Verify that getIndexerDao returns null when not in Jenkins JVM
LogstashIndexerDao dao = writer.getDao();

// Verify results
assertNull("getIndexerDao should return null when not in Jenkins JVM", dao);
}
}