forked from ConsoleCatzirl/jenkins-logstash-plugin
-
Notifications
You must be signed in to change notification settings - Fork 108
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
properly get displayname of node #38
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
src/test/java/jenkins/plugins/logstash/LogstashIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package jenkins.plugins.logstash; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.powermock.api.mockito.PowerMockito.when; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
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; | ||
|
||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Result; | ||
import hudson.model.Slave; | ||
import hudson.model.queue.QueueTaskFuture; | ||
import jenkins.plugins.logstash.persistence.AbstractLogstashIndexerDao; | ||
import jenkins.plugins.logstash.persistence.IndexerDaoFactory; | ||
import jenkins.plugins.logstash.persistence.LogstashIndexerDao.IndexerType; | ||
import net.sf.json.JSONArray; | ||
import net.sf.json.JSONObject; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PowerMockIgnore({"javax.crypto.*"}) | ||
@PrepareForTest(IndexerDaoFactory.class) | ||
public class LogstashIntegrationTest | ||
{ | ||
@Rule | ||
public JenkinsRule jenkins = new JenkinsRule(); | ||
|
||
private Slave slave; | ||
|
||
private FreeStyleProject project; | ||
|
||
private MemoryDao memoryDao; | ||
|
||
@Before | ||
public void setup() throws Exception | ||
{ | ||
PowerMockito.mockStatic(IndexerDaoFactory.class); | ||
LogstashInstallation.Descriptor descriptor = LogstashInstallation.getLogstashDescriptor(); | ||
descriptor.type = IndexerType.SYSLOG; | ||
descriptor.host = "localhost"; | ||
descriptor.port = 1; | ||
descriptor.username = "username"; | ||
descriptor.password = "password"; | ||
descriptor.key = "key"; | ||
|
||
memoryDao = new MemoryDao(); | ||
when(IndexerDaoFactory.getInstance(IndexerType.SYSLOG, descriptor.host, descriptor.port, descriptor.key, | ||
descriptor.username, descriptor.password)).thenReturn(memoryDao); | ||
slave = jenkins.createSlave(); | ||
slave.setLabelString("myLabel"); | ||
project = jenkins.createFreeStyleProject(); | ||
|
||
} | ||
|
||
@Test | ||
public void test_buildWrapperOnMaster() throws Exception | ||
{ | ||
project.getBuildWrappersList().add(new LogstashBuildWrapper()); | ||
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(3)); | ||
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")); | ||
} | ||
|
||
@Test | ||
public void test_buildWrapperOnSlave() throws Exception | ||
{ | ||
project.getBuildWrappersList().add(new LogstashBuildWrapper()); | ||
project.setAssignedNode(slave); | ||
|
||
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(3)); | ||
JSONObject firstLine = dataLines.get(0); | ||
JSONObject lastLine = dataLines.get(dataLines.size()-1); | ||
JSONObject data = firstLine.getJSONObject("data"); | ||
assertThat(data.getString("buildHost"),equalTo(slave.getDisplayName())); | ||
assertThat(data.getString("buildLabel"),equalTo(slave.getLabelString())); | ||
assertThat(lastLine.getJSONArray("message").get(0).toString(),equalTo("Finished: SUCCESS")); | ||
} | ||
|
||
@Test | ||
public void test_buildNotifierOnMaster() throws Exception | ||
{ | ||
project.getPublishersList().add(new LogstashNotifier(10, false)); | ||
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(1)); | ||
JSONObject firstLine = dataLines.get(0); | ||
JSONObject data = firstLine.getJSONObject("data"); | ||
assertThat(data.getString("buildHost"),equalTo("Jenkins")); | ||
assertThat(data.getString("buildLabel"),equalTo("master")); | ||
} | ||
|
||
@Test | ||
public void test_buildNotifierOnSlave() throws Exception | ||
{ | ||
project.getPublishersList().add(new LogstashNotifier(10, false)); | ||
project.setAssignedNode(slave); | ||
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(1)); | ||
JSONObject firstLine = dataLines.get(0); | ||
JSONObject data = firstLine.getJSONObject("data"); | ||
assertThat(data.getString("buildHost"),equalTo(slave.getDisplayName())); | ||
assertThat(data.getString("buildLabel"),equalTo(slave.getLabelString())); | ||
} | ||
|
||
private static class MemoryDao extends AbstractLogstashIndexerDao | ||
{ | ||
List<JSONObject> output = new ArrayList<>(); | ||
|
||
public MemoryDao() | ||
{ | ||
super("localhost", 1, "key", "username", "password"); | ||
} | ||
|
||
@Override | ||
public IndexerType getIndexerType() | ||
{ | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public void push(String data) throws IOException | ||
{ | ||
JSONObject json = JSONObject.fromObject(data); | ||
output.add(json); | ||
} | ||
|
||
public List<JSONObject> getOutput() | ||
{ | ||
return output; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about we try to use
getBuiltOn()
first and fall back togetExecutor().getOwner().getNode()
if the first one isnull
? Just to be on the safe sideThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It must be null if you follow the logic.
In the method execute of class Run you will see that the BuildWrappers are initialized from method createBuildListener. But the builtOn is only set when later in the execute the RunExecution.run is called (which actually is AbstractBuild.AbstractBuildExecution.run)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get that. However this is relying on implementation details. With the current low test coverage I'd prefer to be as conservative as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another way to look at it is this.
It seems pointless for anyone to call
getBuiltOn()
ifgetExecutor().getOwner().getNode()
can give you the same result and do it earlier.I see there is an
@Exported getBuiltOnStr()
method so remote API is the only sane use case.Is
getBuiltOn()
purely a legacy method then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not pointless.
getExecutor()
will only return something when a build is running.getBuiltOn()
will also return the node when the build is finished when the node with this name still exists. Internally only the name of the node is stored and when you callgetBuiltOn()
it searches for the node with the name.Maybe its time to write some tests with JenkinsRule where a real job is executed on a node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation.
Based on that I'd still prefer to keep the fallback logic in case somebody somewhere tries to pass a non-running build here.
Yeah this project definitely needs a heavy makeover (including this class).
I started to port the integ test we do on travis with docker in #36 but there is always something more urgent.
I also need to get more familiar with the jenkins test harness, hopefully #40 will be an opportunity to learn by example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing: how does this compare to the pipeline logic in lines 190-194? Seems to to the same thing but using
displayName
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong, it will return something like
Executor-#0
, but definitely not the host.For pipelines using
getExecutor().getOwner().getNode()
will just get the host where the logstashSend is called, but the build could have run on many different hosts before.Anyway I'm also looking into a more pipeline like logstash that accepts a block
but I have to find a way to inject the host after the logstashwriter is initialized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get that.
One final question: for now, do you think we should change the pipeline handling to use the
getExecutor().getOwner().getNode()
instead of the current executor display name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed also the pipeline handling to display the name of the node and not the executor. I also added some integration tests using JenkinsRule.