Skip to content

Commit

Permalink
Fix syslogd tests to listen on localhost, and clean up after themselves.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Nov 20, 2015
1 parent 8abe1ae commit 9ad53bd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 87 deletions.
Expand Up @@ -107,6 +107,7 @@ public void setUp() throws Exception {
"<syslogd-configuration> \n" +
" <configuration \n" +
" syslog-port=\"10514\" \n" +
" listen-address=\"127.0.0.1\" \n" +
" new-suspect-on-message=\"false\" \n" +
" forwarding-regexp=\"^((.+?) (.*))\\n?$\" \n" +
" matching-group-host=\"2\" \n" +
Expand Down Expand Up @@ -204,7 +205,7 @@ private List<Event> doMessageTest(String testPDU, String expectedHost, String ex
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());

final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr("127.0.0.1"));
final DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, testPDU);
final SyslogdConfig config = SyslogdConfigFactory.getInstance();
WaterfallExecutor.waterfall(m_executorServices, new SyslogConnection(pkt, config.getForwardingRegexp(), config.getMatchingGroupHost(), config.getMatchingGroupMessage(), config.getUeiList(), config.getHideMessages(), config.getDiscardUei()));
Expand Down
Expand Up @@ -94,15 +94,19 @@ public class SyslogClient {

/// Creating a Syslog instance is equivalent of the Unix openlog() call.
// @exception SyslogException if there was a problem
public SyslogClient(String ident, int logopt, int facility) throws UnknownHostException {
public SyslogClient(final String ident, final int logopt, final int facility) throws UnknownHostException {
this(ident, logopt, facility, InetAddressUtils.getLocalHostAddress());
}

public SyslogClient(final String ident, final int logopt, final int facility, final InetAddress address) throws UnknownHostException {
if (ident == null) {
this.ident = Thread.currentThread().getName();
} else {
this.ident = ident;
}
this.facility = facility;

address = InetAddressUtils.getLocalHostAddress();
this.address = address;

try {
socket = new DatagramSocket();
Expand All @@ -117,6 +121,7 @@ public SyslogClient(String ident, int logopt, int facility) throws UnknownHostEx
// actually logged.
// @exception SyslogException if there was a problem
public void syslog(int priority, String msg) {
System.err.println("Sending message: " + msg);
final DatagramPacket packet = getPacket(priority, msg);
try {
socket.send(packet);
Expand Down
Expand Up @@ -206,7 +206,7 @@ public void testDefaultSyslogd() throws Exception {

long start = System.currentTimeMillis();
String testPduFormat = "2010-08-19 localhost foo%d: load test %d on tty1";
SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DEBUG);
SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DEBUG, addr("127.0.0.1"));
for (int i = 0; i < eventCount; i++) {
int foo = foos.get(i);
DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, String.format(testPduFormat, foo, foo));
Expand Down
Expand Up @@ -87,7 +87,7 @@
@Transactional
public class SyslogdIT implements InitializingBean {

String m_localhost = "127.0.0.1";
final String m_localhost = "127.0.0.1";

private Syslogd m_syslogd;

Expand All @@ -104,6 +104,7 @@ public void afterPropertiesSet() throws Exception {
@Before
public void setUp() throws Exception {
MockLogAppender.setupLogging();
MockLogAppender.resetEvents();

InputStream stream = null;
try {
Expand Down Expand Up @@ -140,6 +141,11 @@ public void tearDown() throws Exception {
MockLogAppender.assertNoErrorOrGreater();
}

@After
public void stopSyslogd() throws Exception {
m_syslogd.stop();
}

/**
* Send a raw syslog message and expect a given event as a result
*
Expand All @@ -164,7 +170,7 @@ private List<Event> doMessageTest(String testPDU, String expectedHost, String ex
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());

final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
final DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, testPDU);
final SyslogdConfig config = SyslogdConfigFactory.getInstance();
WaterfallExecutor.waterfall(m_executorService, new SyslogConnection(pkt, config.getForwardingRegexp(), config.getMatchingGroupHost(), config.getMatchingGroupMessage(), config.getUeiList(), config.getHideMessages(), config.getDiscardUei()));
Expand Down Expand Up @@ -193,33 +199,24 @@ private List<Event> doMessageTest(String testPDU, String expectedHost, String ex
}

@Test
public void testMessaging() {
public void testMessaging() throws UnknownHostException {
// More of an integrations test
// relies on you reading some of the logging....

SyslogClient s = null;
try {
s = new SyslogClient(null, 0, SyslogClient.LOG_DAEMON);
s.syslog(SyslogClient.LOG_ERR, "Hello.");
} catch (UnknownHostException e) {
//Failures are for weenies
}
final SyslogClient s = new SyslogClient(null, 0, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_ERR, "Hello.");
}

@Test
public void testMyPatternsSyslogNG() {
SyslogClient s = null;
try {
s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 host.domain.com A SyslogNG style message");
} catch (UnknownHostException e) {
//Failures are for weenies
}
public void testMyPatternsSyslogNG() throws UnknownHostException {
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 host.domain.com A SyslogNG style message");
}

@Test
public void testRegexSeverityMatch() throws Exception {
startSyslogdGracefully();

MockLogAppender.setupLogging(true, "TRACE");
String localhost = m_localhost;
final String testPDU = "2007-01-01 127.0.0.1 beer - Not just for dinner anymore";
Expand All @@ -235,13 +232,8 @@ public void testRegexSeverityMatch() throws Exception {
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());

SyslogClient s = null;
try {
s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
s.syslog(SyslogClient.LOG_CRIT, testPDU);
} catch (UnknownHostException uhe) {
//Failures are for weenies
}
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_CRIT, testPDU);

ea.verifyAnticipated(10000, 0, 0, 0, 0);
}
Expand All @@ -268,13 +260,8 @@ public void testRegexFacilitySeverityProcessMatch() throws Exception {
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());

SyslogClient s = null;
try {
s = new SyslogClient("maltd", 10, SyslogClient.LOG_LOCAL1);
s.syslog(SyslogClient.LOG_WARNING, testPDU);
} catch (UnknownHostException uhe) {
//Failures are for weenies
}
final SyslogClient s = new SyslogClient("maltd", 10, SyslogClient.LOG_LOCAL1, addr(m_localhost));
s.syslog(SyslogClient.LOG_WARNING, testPDU);

ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
Expand All @@ -300,13 +287,8 @@ public void testRegexFacilitySeverityMatch() throws Exception {
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());

SyslogClient s = null;
try {
s = new SyslogClient(null, 10, SyslogClient.LOG_LOCAL1);
s.syslog(SyslogClient.LOG_WARNING, testPDU);
} catch (UnknownHostException uhe) {
//Failures are for weenies
}
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_LOCAL1, addr(m_localhost));
s.syslog(SyslogClient.LOG_WARNING, testPDU);

ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
Expand All @@ -331,13 +313,8 @@ public void testRegexFacilityMatch() throws Exception {
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());

SyslogClient s = null;
try {
s = new SyslogClient(null, 10, SyslogClient.LOG_LOCAL0);
s.syslog(SyslogClient.LOG_DEBUG, testPDU);
} catch (UnknownHostException uhe) {
//Failures are for weenies
}
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_LOCAL0, addr(localhost));
s.syslog(SyslogClient.LOG_DEBUG, testPDU);

ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
Expand All @@ -362,37 +339,22 @@ public void testRegexProcessMatch() throws Exception {
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());

SyslogClient s = null;
try {
s = new SyslogClient("beerd", 10, SyslogClient.LOG_DAEMON);
s.syslog(SyslogClient.LOG_DEBUG, testPDU);
} catch (UnknownHostException uhe) {
//Failures are for weenies
}
final SyslogClient s = new SyslogClient("beerd", 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, testPDU);

ea.verifyAnticipated(5000, 0, 0, 0, 0);
}

@Test
public void testIPPatternsSyslogNG() {
SyslogClient s = null;
try {
s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 127.0.0.1 A SyslogNG style message");
} catch (UnknownHostException e) {
//Failures are for weenies
}
public void testIPPatternsSyslogNG() throws UnknownHostException {
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 127.0.0.1 A SyslogNG style message");
}

@Test
public void testResolvePatternsSyslogNG() {
SyslogClient s = null;
try {
s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 www.opennms.org A SyslogNG style message");
} catch (UnknownHostException e) {
//Failures are for weenies
}
public void testResolvePatternsSyslogNG() throws UnknownHostException {
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 www.opennms.org A SyslogNG style message");
}

private void startSyslogdGracefully() {
Expand Down Expand Up @@ -444,8 +406,7 @@ public void testSubstrDiscard() throws Exception {
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);

SyslogClient sc = null;
sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
sc.syslog(SyslogClient.LOG_DEBUG, testPDU);

ea.verifyAnticipated(5000, 0, 0, 0, 0);
Expand All @@ -459,8 +420,7 @@ public void testRegexDiscard() throws Exception {
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);

SyslogClient sc = null;
sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
sc.syslog(SyslogClient.LOG_DEBUG, testPDU);

ea.verifyAnticipated(5000, 0, 0, 0, 0);
Expand All @@ -485,7 +445,7 @@ public void testRegexUEIWithBothKindsOfParameterAssignments() throws Exception {
}

@Test
public void testRegexUEIWithOnlyUserSpecifiedParameterAssignments() throws InterruptedException {
public void testRegexUEIWithOnlyUserSpecifiedParameterAssignments() throws InterruptedException, UnknownHostException {
startSyslogdGracefully();

final String localhost = m_localhost;
Expand All @@ -507,13 +467,8 @@ public void testRegexUEIWithOnlyUserSpecifiedParameterAssignments() throws Inter
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());

SyslogClient s = null;
try {
s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON);
s.syslog(SyslogClient.LOG_DEBUG, testPDU);
} catch (UnknownHostException uhe) {
//Failures are for weenies
}
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, testPDU);

ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
Expand Down
Expand Up @@ -36,7 +36,6 @@
import java.lang.reflect.UndeclaredThrowableException;
import java.net.BindException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -204,7 +203,7 @@ public void testDefaultSyslogd() throws Exception {
m_eventCounter.setAnticipated(eventCount);

String testPduFormat = "2010-08-19 localhost foo%d: load test %d on tty1";
SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_USER);
SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_USER, addr("127.0.0.1"));

// Test by directly invoking the SyslogConnection task
System.err.println("Starting to send packets");
Expand Down
Expand Up @@ -2,6 +2,7 @@
<syslogd-configuration>
<configuration
syslog-port="10514"
listen-address="127.0.0.1"
new-suspect-on-message="false"
forwarding-regexp="^.*\s(19|20)\d\d([-/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])(\s+)(\S+)(\s)(\S.+)"
matching-group-host="6"
Expand Down
Expand Up @@ -2,6 +2,7 @@
<syslogd-configuration>
<configuration
syslog-port="10514"
listen-address="127.0.0.1"
new-suspect-on-message="false"
forwarding-regexp="^.*\s(19|20)\d\d([-/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])(\s+)(\S+)(\s)(\S.+)"
matching-group-host="6"
Expand Down

0 comments on commit 9ad53bd

Please sign in to comment.