forked from ConsoleCatzirl/jenkins-logstash-plugin
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
properly get displayname of node (#38)
* properly get displayname of node use the executor of the build to get the node. builtOn is set too late for us to get the node and we end up using master * displayname of node in pipeline properly get the node when BuildData is initilaized from a pipeline pump mockito to latest version add integration test, that use Jenkins test harness
- Loading branch information
1 parent
db3b9d4
commit cc4225b
Showing
7 changed files
with
226 additions
and
29 deletions.
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.