Skip to content

Commit 113d19c

Browse files
mwinter69jakub-bochenski
authored andcommitted
[JENKINS-33635] option to enable logstash globally (#54)
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
1 parent 4eb3d59 commit 113d19c

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

src/main/java/jenkins/plugins/logstash/LogstashConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package jenkins.plugins.logstash;
22

3-
import java.net.MalformedURLException;
43
import java.net.URI;
54
import java.net.URISyntaxException;
65
import java.util.List;
@@ -35,6 +34,7 @@ public class LogstashConfiguration extends GlobalConfiguration
3534
private static final Logger LOGGER = Logger.getLogger(LogstashConfiguration.class.getName());
3635
private LogstashIndexer<?> logstashIndexer;
3736
private boolean dataMigrated = false;
37+
private boolean enableGlobally = false;
3838
private transient LogstashIndexer<?> activeIndexer;
3939

4040
public LogstashConfiguration()
@@ -43,6 +43,16 @@ public LogstashConfiguration()
4343
activeIndexer = logstashIndexer;
4444
}
4545

46+
public boolean isEnableGlobally()
47+
{
48+
return enableGlobally;
49+
}
50+
51+
public void setEnableGlobally(boolean enableGlobally)
52+
{
53+
this.enableGlobally = enableGlobally;
54+
}
55+
4656
/**
4757
* Returns the current logstash indexer configuration.
4858
*

src/main/java/jenkins/plugins/logstash/LogstashConsoleLogFilter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream)
5757

5858
private boolean isLogstashEnabled(Run<?, ?> build)
5959
{
60+
LogstashConfiguration configuration = LogstashConfiguration.getInstance();
61+
if (configuration.isEnableGlobally())
62+
{
63+
return true;
64+
}
65+
6066
if (build.getParent() instanceof BuildableItemWithBuildWrappers)
6167
{
6268
BuildableItemWithBuildWrappers project = (BuildableItemWithBuildWrappers)build.getParent();

src/main/resources/jenkins/plugins/logstash/LogstashConfiguration/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
<f:entry>
55
<f:dropdownDescriptorSelector title="${%Indexer Type}" field="logstashIndexer" descriptors="${descriptor.indexerTypes}"/>
66
</f:entry>
7+
<f:entry title="Enable Globally" field="enableGlobally" description="This will not enable it for pipeline jobs.">
8+
<f:checkbox/>
9+
</f:entry>
710
</f:section>
811
</j:jelly>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Checking will make all non pipeline builds forward their log to the above indexer.

src/test/java/jenkins/plugins/logstash/LogstashConsoloLogFilterTest.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static org.junit.Assert.*;
44
import static org.mockito.Mockito.verifyNoMoreInteractions;
5-
import static org.mockito.Mockito.when;
5+
import static org.powermock.api.mockito.PowerMockito.when;
66
import static org.mockito.Mockito.verify;
77

88
import hudson.model.AbstractBuild;
@@ -24,9 +24,19 @@
2424
import org.junit.runner.RunWith;
2525
import org.mockito.Mock;
2626
import org.mockito.junit.MockitoJUnitRunner;
27-
28-
@RunWith(MockitoJUnitRunner.class)
27+
import org.powermock.api.mockito.PowerMockito;
28+
import org.powermock.core.classloader.annotations.PowerMockIgnore;
29+
import org.powermock.core.classloader.annotations.PrepareForTest;
30+
import org.powermock.modules.junit4.PowerMockRunner;
31+
32+
@RunWith(PowerMockRunner.class)
33+
@PowerMockIgnore({"javax.crypto.*"})
34+
@PrepareForTest(LogstashConfiguration.class)
2935
public class LogstashConsoloLogFilterTest {
36+
37+
@Mock
38+
private LogstashConfiguration logstashConfiguration;
39+
3040
// Extension of the unit under test that avoids making calls to statics or constructors
3141
static class MockLogstashConsoloeLogFilter extends LogstashConsoleLogFilter {
3242
LogstashWriter writer;
@@ -62,6 +72,10 @@ LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream) {
6272
@Before
6373
public void before() throws Exception {
6474
buildWrappers = new DescribableList<BuildWrapper,Descriptor<BuildWrapper>>(mockProject);
75+
PowerMockito.mockStatic(LogstashConfiguration.class);
76+
when(LogstashConfiguration.getInstance()).thenReturn(logstashConfiguration);
77+
when(logstashConfiguration.isEnableGlobally()).thenReturn(false);
78+
6579
when(mockWriter.isConnectionBroken()).thenReturn(false);
6680
when(mockBuild.getParent()).thenReturn(mockProject);
6781
when(mockProject.getBuildWrappersList()).thenReturn(buildWrappers);
@@ -77,7 +91,7 @@ public void after() throws Exception {
7791
}
7892

7993
@Test
80-
public void decorateLoggerSuccess() throws Exception {
94+
public void decorateLoggerSuccessBuildWrapper() throws Exception {
8195
buildWrappers.add(new LogstashBuildWrapper());
8296
MockLogstashConsoloeLogFilter buildWrapper = new MockLogstashConsoloeLogFilter(mockWriter);
8397

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

95109
@Test
96-
public void decorateLoggerSuccessNoLogstashBuildWrapper() throws Exception {
110+
public void decorateLoggerSuccessLogstashNotEnabled() throws Exception {
97111
MockLogstashConsoloeLogFilter buildWrapper = new MockLogstashConsoloeLogFilter(mockWriter);
98112

99113
// Unit under test
@@ -122,4 +136,21 @@ public void decorateLoggerSuccessBadWriter() throws Exception {
122136
assertEquals("Error was not written", "Mocked Constructor failure", buffer.toString());
123137
verify(mockWriter).isConnectionBroken();
124138
}
139+
140+
@Test
141+
public void decorateLoggerSuccessEnabledGlobally() throws IOException, InterruptedException
142+
{
143+
when(logstashConfiguration.isEnableGlobally()).thenReturn(true);
144+
MockLogstashConsoloeLogFilter buildWrapper = new MockLogstashConsoloeLogFilter(mockWriter);
145+
146+
// Unit under test
147+
OutputStream result = buildWrapper.decorateLogger(mockBuild, buffer);
148+
149+
// Verify results
150+
assertNotNull("Result was null", result);
151+
assertTrue("Result is not the right type", result instanceof LogstashOutputStream);
152+
assertSame("Result has wrong writer", mockWriter, ((LogstashOutputStream) result).getLogstashWriter());
153+
assertEquals("Results don't match", "", buffer.toString());
154+
verify(mockWriter).isConnectionBroken();
155+
}
125156
}

src/test/java/jenkins/plugins/logstash/LogstashIntegrationTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,23 @@ public void passwordsAreMaskedWithoutMaskPasswordsBuildWrapper() throws Exceptio
212212
assertThat(logline,not(containsString("myPassword")));
213213
}
214214
}
215+
216+
@Test
217+
public void enableGlobally() throws Exception
218+
{
219+
when(logstashConfiguration.isEnableGlobally()).thenReturn(true);
220+
QueueTaskFuture<FreeStyleBuild> f = project.scheduleBuild2(0);
221+
222+
FreeStyleBuild build = f.get();
223+
assertThat(build.getResult(), equalTo(Result.SUCCESS));
224+
List<JSONObject> dataLines = memoryDao.getOutput();
225+
assertThat(dataLines.size(), is(4));
226+
JSONObject firstLine = dataLines.get(0);
227+
JSONObject lastLine = dataLines.get(dataLines.size()-1);
228+
JSONObject data = firstLine.getJSONObject("data");
229+
assertThat(data.getString("buildHost"),equalTo("Jenkins"));
230+
assertThat(data.getString("buildLabel"),equalTo("master"));
231+
assertThat(lastLine.getJSONArray("message").get(0).toString(),equalTo("Finished: SUCCESS"));
232+
}
233+
215234
}

0 commit comments

Comments
 (0)