[][src]Crate kaos

 Chaotic testing harness

Kaos is a chaotic testing harness to test your services against random failures. It allows you to add points to your code to crash sporadically and harness asserts availability and fault tolerance of your services by seeking minimum time between failures, fail points, and randomized runs.

Kaos is equivalent of Chaos Monkey for the Rust ecosystem. But it is more smart to find the closest MTBF based on previous runs. This is dependable system practice. For more information please visit Chaos engineering.

Test Setup

It is better to separate resilience tests. Create a directory that will hold all chaos tests. In our example it will be kaos-tests.

A minimal launcher for kaos setup looks like this:

#[test]
fn chaos_tests() {
    let k = kaos::Runs::new();

    for entry in fs::read_dir("kaos-tests").unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();

        // Every service run should be available at least 2 seconds
        k.available(path, Duration::from_secs(2));
    }
}

and in your Cargo.toml

[[test]]
name = "chaos_tests"
path = "kaos-tests/launcher.rs"

Mind that there two types of tests, first one is: availability test, the latter one is chaotic test which seeks the minimum timing, failure, MTBF combination. The setup shows availability tests as an example. When availability tests run you will see:

Definining flunks

In kaos there is a concept of flunk. Every flunk is a point of failure with panic. This can be redefinable. After adding kaos as dependency you can add flunk points to define fallible operations or crucial points that system should continue its operation.

Basic flunk is like:

use kaos::flunk;
fn vec_check(v: &Vec<usize>) {
  if v.len() == 3 {
    flunk!("fail-when-three-elems");
  }
}

This flunk point will be used later by kaos.

Writing tests

Test harness will execute tests marked by a launcher. An example test for the flunk mentioned above is like this:

use kaos::kaostest;

kaostest!("fail-when-three-elems",
         {
             panic::catch_unwind(|| {
               let mut v = &mut vec![];
               loop {
                  v.push(1);
                  vec_check(v);
               }
             });
         }
);

Chaos Tests

In addition to availability tests mentioned above we can test the software with chaos tests too. For using chaotic measures and finding bare minimum failure, timing and MTBF combination you can configure chaos tests in your launcher:

#[test]
fn chaos_tests() {
    let k = kaos::Runs::new();

    for entry in fs::read_dir("kaos-tests").unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();

        // Let's have 10 varying runs.
        let run_count = 10;

        // Minimum availability to expect as milliseconds for the runs.
        // Which corresponds as maximum surge between service runs.
        // Let's have it 10 seconds.
        let max_surge = 10 * 1000;

        // Run chaotic test.
        k.chaotic(path, run_count, max_surge);
    }
}

This launcher produce multiple results like:

Now you know all the basics, what you have to do is unleash some chaos with cargo test.

Kaos is using the same approach that trybuild has. Instead of being compiler-like test harness, it has diverged to be chaos engineering oriented harness.

Macros

flunk

Macro to define a point to flunk

kaostest

Define kaos tests

Structs

Runs

Chaotic runs test setup