riemann.test

Fast, end-to-end, repeatable testing for entire Riemann configs. Provides a tap macro which taps the event stream and (in testing mode) records all events that flow through that stream. Provides a variant of deftest that initiates controlled time and sets up a fresh result set for taps, and a function inject! to apply events to streams and see what each tap received.

*core*

dynamic

The core used in test mode

*results*

dynamic

A map of tap names to atoms of vectors of captured events.

*taps*

dynamic

An atom to a map of tap names to information about the taps; e.g. file and line number, for preventing collisions.

*testing*

dynamic

Are we currently in test mode?

deftest

macro

(deftest name & body)

Like clojure.test deftest, but establishes a fresh time context and a fresh set of tap results for the duration of the body.

(deftest my-tap
  (let [rs (test/inject! [{:time 2 :service "bar"}])]
    (is (= 1 (count (:some-tap rs))))))

fresh-results

(fresh-results taps)

Given a map of tap-names to _, builds a map of tap names to atoms of empty vectors, ready to receive events.

inject!

(inject! events)(inject! streams events)

Takes a sequence of streams, initiates controlled time and resets the scheduler, applies a sequence of events to those streams, and returns a map of tap names to the events each tap received. Absolutely NOT threadsafe; riemann.time.controlled is global. Streams may be omitted, in which case inject! applies events to the streams dynamic var.

io

macro

(io & children)

A stream which suppresses side effects in test mode. When testing is true at compile time, returns a function that discards any incoming events. When testing is false, compiles to (sdo child1 child2 …).

(io
  (fn [e]
    (http/put "http://my.service/callback", {:body (event->json e)})))

lookup

(lookup events host service)

Lookup an event by host/service in a vector of tapped events returned by inject!. If several matching events have passed through the tap, the last one will be returned.

run-stream

macro

(run-stream stream inputs)

Applies inputs to stream, and returns outputs.

run-stream-intervals

macro

(run-stream-intervals stream inputs-and-intervals)

Applies a seq of alternating events and intervals (in seconds) between them to stream, returning outputs.

tap

macro

(tap name & children)

A stream which records inbound events in the results map. Takes a globally unique name which identifies that tap in results.

When testing is false (at compile time!), tap has no effect; it compiles directly into (sdo child-streams).

When testing is true at compile time, tap records any events that arrive in the results map, and passes those events on to its children.

(rate 5 (tap :graph prod-graph-stream))

tap-stream

(tap-stream name child)

Called by tap to construct a stream which records events in results before forwarding to child.

test-stream

macro

(test-stream stream inputs outputs)

Verifies that the given stream, taking inputs, forwards outputs to children.

test-stream-intervals

macro

(test-stream-intervals stream inputs-and-intervals outputs)

Verifies that run-stream-intervals, taking inputs/intervals, forwards outputs to children.

tests

macro

(tests & body)

Declares a new namespace named ns-test, requires some clojure.test and riemann.test helpers, and evaluates body in the context of that namespace. Restores the original namespace afterwards.

with-test-env

macro

(with-test-env & body)

Prepares a fresh set of taps, binds testing to true, initiates controlled time and resets the schedulerand runs body in an implicit do. Wrap your entire test suite (including defining the streams themselves) in this macro. Note that you’ll have to use (eval) or (load-file), etc, in order for this to work because the binding takes effect at run time, not compile time–so make your compile time run time and wow this gets confusing.

(with-test-env
  (eval '(let [s (tap :foo prn)]
           (run-test! [s] [:hi]))))

prints :hi, and returns {:foo :hi}

with-test-stream

macro

(with-test-stream sym stream inputs outputs)

Exposes a fake index, verifies that the given stream, taking inputs, forwards outputs to children