Skip to content

Commit

Permalink
[JENKINS-33635] option to enable logstash globally (#54)
Browse files Browse the repository at this point in the history
With the ConsoleLogFilter we can enable it globally to send the log to
the indexer. Unfortunately this will not work for pipeline builds.

* add description to enable globally field in jellyx

* rename test method

make it more clear that logstash should not be enabled
  • Loading branch information
mwinter69 authored and jakub-bochenski committed Mar 13, 2018
1 parent 4eb3d59 commit 113d19c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
@@ -1,6 +1,5 @@
package jenkins.plugins.logstash;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
Expand Down Expand Up @@ -35,6 +34,7 @@ public class LogstashConfiguration extends GlobalConfiguration
private static final Logger LOGGER = Logger.getLogger(LogstashConfiguration.class.getName());
private LogstashIndexer<?> logstashIndexer;
private boolean dataMigrated = false;
private boolean enableGlobally = false;
private transient LogstashIndexer<?> activeIndexer;

public LogstashConfiguration()
Expand All @@ -43,6 +43,16 @@ public LogstashConfiguration()
activeIndexer = logstashIndexer;
}

public boolean isEnableGlobally()
{
return enableGlobally;
}

public void setEnableGlobally(boolean enableGlobally)
{
this.enableGlobally = enableGlobally;
}

/**
* Returns the current logstash indexer configuration.
*
Expand Down
Expand Up @@ -57,6 +57,12 @@ LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream)

private boolean isLogstashEnabled(Run<?, ?> build)
{
LogstashConfiguration configuration = LogstashConfiguration.getInstance();
if (configuration.isEnableGlobally())
{
return true;
}

if (build.getParent() instanceof BuildableItemWithBuildWrappers)
{
BuildableItemWithBuildWrappers project = (BuildableItemWithBuildWrappers)build.getParent();
Expand Down
Expand Up @@ -4,5 +4,8 @@
<f:entry>
<f:dropdownDescriptorSelector title="${%Indexer Type}" field="logstashIndexer" descriptors="${descriptor.indexerTypes}"/>
</f:entry>
<f:entry title="Enable Globally" field="enableGlobally" description="This will not enable it for pipeline jobs.">
<f:checkbox/>
</f:entry>
</f:section>
</j:jelly>
@@ -0,0 +1 @@
Checking will make all non pipeline builds forward their log to the above indexer.
Expand Up @@ -2,7 +2,7 @@

import static org.junit.Assert.*;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.mockito.Mockito.verify;

import hudson.model.AbstractBuild;
Expand All @@ -24,9 +24,19 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.crypto.*"})
@PrepareForTest(LogstashConfiguration.class)
public class LogstashConsoloLogFilterTest {

@Mock
private LogstashConfiguration logstashConfiguration;

// Extension of the unit under test that avoids making calls to statics or constructors
static class MockLogstashConsoloeLogFilter extends LogstashConsoleLogFilter {
LogstashWriter writer;
Expand Down Expand Up @@ -62,6 +72,10 @@ LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream) {
@Before
public void before() throws Exception {
buildWrappers = new DescribableList<BuildWrapper,Descriptor<BuildWrapper>>(mockProject);
PowerMockito.mockStatic(LogstashConfiguration.class);
when(LogstashConfiguration.getInstance()).thenReturn(logstashConfiguration);
when(logstashConfiguration.isEnableGlobally()).thenReturn(false);

when(mockWriter.isConnectionBroken()).thenReturn(false);
when(mockBuild.getParent()).thenReturn(mockProject);
when(mockProject.getBuildWrappersList()).thenReturn(buildWrappers);
Expand All @@ -77,7 +91,7 @@ public void after() throws Exception {
}

@Test
public void decorateLoggerSuccess() throws Exception {
public void decorateLoggerSuccessBuildWrapper() throws Exception {
buildWrappers.add(new LogstashBuildWrapper());
MockLogstashConsoloeLogFilter buildWrapper = new MockLogstashConsoloeLogFilter(mockWriter);

Expand All @@ -93,7 +107,7 @@ public void decorateLoggerSuccess() throws Exception {
}

@Test
public void decorateLoggerSuccessNoLogstashBuildWrapper() throws Exception {
public void decorateLoggerSuccessLogstashNotEnabled() throws Exception {
MockLogstashConsoloeLogFilter buildWrapper = new MockLogstashConsoloeLogFilter(mockWriter);

// Unit under test
Expand Down Expand Up @@ -122,4 +136,21 @@ public void decorateLoggerSuccessBadWriter() throws Exception {
assertEquals("Error was not written", "Mocked Constructor failure", buffer.toString());
verify(mockWriter).isConnectionBroken();
}

@Test
public void decorateLoggerSuccessEnabledGlobally() throws IOException, InterruptedException
{
when(logstashConfiguration.isEnableGlobally()).thenReturn(true);
MockLogstashConsoloeLogFilter buildWrapper = new MockLogstashConsoloeLogFilter(mockWriter);

// Unit under test
OutputStream result = buildWrapper.decorateLogger(mockBuild, buffer);

// Verify results
assertNotNull("Result was null", result);
assertTrue("Result is not the right type", result instanceof LogstashOutputStream);
assertSame("Result has wrong writer", mockWriter, ((LogstashOutputStream) result).getLogstashWriter());
assertEquals("Results don't match", "", buffer.toString());
verify(mockWriter).isConnectionBroken();
}
}
Expand Up @@ -212,4 +212,23 @@ public void passwordsAreMaskedWithoutMaskPasswordsBuildWrapper() throws Exceptio
assertThat(logline,not(containsString("myPassword")));
}
}

@Test
public void enableGlobally() throws Exception
{
when(logstashConfiguration.isEnableGlobally()).thenReturn(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(), is(4));
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"));
}

}

0 comments on commit 113d19c

Please sign in to comment.