Monitors external service endpoints from integration tests by automatically sending Nagios passive service checks.
If you are using JUnit v4.7 or later, you can use the JUnit Rule API to
write integration tests in the following style:
public class IntegrationSmokeTests {
private String nscaServer = "nagios.box";
private String nscaPassword = "password";
private int nscaPort = 5667;
private int nscaConnectionTimeout = 5000; // ms
private int nscaResponseTimeout = 15000; // ms
private NagiosNotifier notifier = new NagiosNotifier(nscaServer,
nscaPassword, nscaPort, nscaConnectionTimeout,
nscaResponseTimeout);
private String serviceHost = "service.box";
@Rule ServiceMonitor monitor = new ServiceMonitor(serviceHost,
notifier);
@Test
@MonitorsService(name = "AwesomeService")
public void awesomeServiceShouldBeAvailable() throws Exception {
final Service awesome = new AwesomeService();
smokeTest(awesome);
assertEquals(Service.AVAILABLE, awesome.getStatus());
assertEquals(Service.CORRECT, awesome.getResponse());
}
@Test
@MonitorsService(name = "Google", onHost = "google.com",
failureMessage = "Google should never be down, so something must be terribly wrong!")
public void googleShouldNeverBeDown() throws Exception {
final Service google = new GoogleService();
assertEquals(Service.AVAILABLE, google.ping());
}
@Ignore
@Test
@MonitorsService(name = "UndocumentedService",
ignoredMessage = "ignoring until we finish reverse-engineering this undocumented API")
public void awesomeServiceShouldBeAvailable() throws Exception {
final Service undocumented = new UndocumentedService();
// TODO: figure out the correct order of these calls:
/*
undocumented.setListener(Environments.INTEGRATION_TEST);
undocumented.initialize();
final Token token = undocumented.getRequestToken();
final Service.Response response = undocumented.invoke(token);
*/
assertEquals(Service.CORRECT, response.validate());
}
@Test
@MonitorsService(name = "UnstableService",
successMessage = "I can't believe it actually worked!")
public void awesomeServiceShouldBeAvailable() throws Exception {
final Service unstable = new UnstableService();
final Response response = unstable.invoke();
assertEquals(Service.AVAILABLE, response.getStatus());
}
@Test
@MonitorsService(name = "UberService",
failureMessage = "can't connect!",
ignoredMessage = "fiddling with something",
successMessage = "all systems are go!")
public void awesomeServiceShouldBeAvailable() throws Exception {
final Service uber = new UberService();
smokeTest(uber);
assertEquals(Service.AVAILABLE, uber.getStatus());
assertEquals(Service.CORRECT, uber.getResponse());
}
}
If you are using JUnit v4.6 or earlier then you must use the
hippie
runner, instead (which also works with JUnit v4.7+,
but is not preferred):
@RunWith(NagiosAwareRunner.class)
public class IntegrationSmokeTests {
static {
// Ideally, you would set these properties via your
// build tool (i.e. Ant, Maven, etc.), in your IDE, or
// at the command line.
System.setProperty("hippie.nsca.server", "localhost");
System.setProperty("hippie.nsca.password", "ABC123");
// Optional JVM/System properties you can also set if
// you need to override the defaults:
// - hippie.nsca.port (default: 5667)
// - hippie.nsca.timeout.connection (default: 5000 ms)
// - hippie.nsca.timeout.response (default: 15000 ms)
}
@Test
@MonitorsService(name = "AwesomeService", onHost = "qa.env")
public void awesomeServiceShouldBeAvailable() throws Exception {
final Service awesome = new AwesomeService();
smokeTest(awesome);
assertEquals(Service.AVAILABLE, awesome.getStatus());
assertEquals(Service.CORRECT, awesome.getResponse());
}
@Test
@MonitorsService(name = "Google", onHost = "google.com",
failureMessage = "Google should never be down, so something must be terribly wrong!")
public void googleShouldNeverBeDown() throws Exception {
final Service google = new GoogleService();
assertEquals(Service.AVAILABLE, google.ping());
}
@Ignore
@Test
@MonitorsService(name = "UndocumentedService", onHost = "dev",
ignoredMessage = "ignoring until we finish reverse-engineering this undocumented API")
public void awesomeServiceShouldBeAvailable() throws Exception {
final Service undocumented = new UndocumentedService();
// TODO: figure out the correct order of these calls:
/*
undocumented.setListener(Environments.INTEGRATION_TEST);
undocumented.initialize();
final Token token = undocumented.getRequestToken();
final Service.Response response = undocumented.invoke(token);
*/
assertEquals(Service.CORRECT, response.validate());
}
@Test
@MonitorsService(name = "UnstableService", onHost = "test",
successMessage = "I can't believe it actually worked!")
public void awesomeServiceShouldBeAvailable() throws Exception {
final Service unstable = new UnstableService();
final Response response = unstable.invoke();
assertEquals(Service.AVAILABLE, response.getStatus());
}
@Test
@MonitorsService(name = "UberService", onHost = "production.env",
failureMessage = "can't connect!",
ignoredMessage = "fiddling with something",
successMessage = "all systems are go!")
public void awesomeServiceShouldBeAvailable() throws Exception {
final Service uber = new UberService();
smokeTest(uber);
assertEquals(Service.AVAILABLE, uber.getStatus());
assertEquals(Service.CORRECT, uber.getResponse());
}
}
Copyright © 2009, Kent R. Spillner <kspillner@acm.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- Kent R. Spillner <kspillner@acm.org>
- Paul Hammant
- Jimmy Staggs
- Lejo Varughese
- Greg Warren – whose dogged commitment to integration tests, and advocacy of their inherent value as a cross-team communication tool, was the inspiration for this project
hippie
is another ThoughtWorks innovation.