Quick Start

RediB is a Java-based end-to-end testing framework. So, you will need to write your test cases In Java, or languages that can use Java libraries like the ones that can run on JVM, e.g. Scala. RediB can be used alongside the popular testing frameworks in your programming language of choice e.g. JUnit in Java. Here, we use Java and JUnit . We also use Maven as the build system.

Adding dependencies

First, create a simple Maven application and add RediB’s dependency to your pom file.

<dependency>
    <groupId>io.redit</groupId>
    <artifactId>redit</artifactId>
    <version>0.1.0</version>
</dependency>

Also add failsafe plugin to your pom file to be able to run integration tests.

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Creating a Dockerfile

Next, you need to create a Dockerfile for your application and that Dockerfile should add any dependency that may be needed by your application. In case you want to use the network partition capability of RediB, you need to install iptables package as well. Network delay and loss will also need the iproute package to be installed. Here, we assume the application under test is written in Java. So, we create a Dockerfile in the docker/Dockerfile address with the following content:

FROM openjdk:8-stretch

RUN apt update && apt install -y iptables iproute

Important

In case you are using Docker Toolbox (and consequently boot2docker) on Windows or Mac, be aware that your currently installed boot2docker image may be missing sched_netem kernel module which is included in most of the linux distributions and is needed for tc command in the iproute package to work. So, unless you upgrade your boot2docker image (normally through running docker-machine upgrade [machine_name], you won’t be able to use the network operation capabilities of RediB.

Adding a Test Case

Now, create a JUnit integration test case (ending with IT so failsafe picks it up) in the project’s test directory. Here, we provide an example for testing the situation of multithread. You can find the full code in the Redit project.

 1 public class MultithreadTest {
 2    public static final Logger logger = LoggerFactory.getLogger(MultithreadTest.class);
 3
 4    @Test
 5    public void simpleDefinition() throws DeploymentVerificationException, RuntimeEngineException, TimeoutException, WorkspaceException {
 6        Deployment deployment = Deployment.builder("sample-multithread")
 7                // Service Definitions
 8                .withServiceFromJvmClasspath("s1", "target/classes", "**commons-io*.jar")
 9                    .startCommand("java -cp ${REDIT_JVM_CLASSPATH} io.redit.samples.multithread.Main")
10                    .dockerImageName("redit/sample-multithread")
11                    .dockerFileAddress("../sample-multithread/docker/Dockerfile", true)
12                    .logFile("/var/log/sample1")
13                    .logDirectory("/var/log/samples")
14                    .serviceType(ServiceType.JAVA).and()
15                // Node Definitions
16                .withNode("n1", "s1")
17                    .stackTrace("e1", "io.redit.samples.multithread.Main.helloWorld1," +
18                            "io.redit.samples.multithread.Main.hello")
19                    .stackTrace("e2", "io.redit.samples.multithread.Main.helloWorld2," +
20                            "io.redit.samples.multithread.Main.helloWorld")
21                    .stackTrace("e3", "io.redit.samples.multithread.Main.helloWorld3," +
22                            "io.redit.samples.multithread.Main.hello")
23                    .stackTrace("e4", "org.apache.commons.io.FilenameUtils.normalize")
24                    .blockBefore("bbe2", "e2")
25                    .unblockBefore("ubbe2", "e2")
26                    .garbageCollection("g1")
27                    .and()
28                .withNode("n2", "s1").offOnStartup().and()
29                .withNode("n3", "s1").and()
30                .withNode("n4", "s1").and()
31                // Test Case Events
32                .testCaseEvents("x1", "x2")
33                // Run Sequence Definition
34                .runSequence("bbe2 * e1 * ubbe2 * x1 *  e2  * e3 * x2 * e4")
35                .sharedDirectory("/redit")
36                .build();
37
38        ReditRunner runner = ReditRunner.run(deployment);
39        // Starting node n2
40        runner.runtime().enforceOrder("x1",10, () -> runner.runtime().startNode("n2"));
41        // Adding new nodes to the deployed environment
42        runner.addNode(Node.limitedBuilder("n5", "s1"));
43        // Imposing overlapping network partitions
44        NetPart netPart1 = NetPart.partitions("n1","n2").connect(1, NetPart.REST, false).build();
45        NetPart netPart2 = NetPart.partitions("n1","n2,n3").connect(1, NetPart.REST).build();
46        runner.runtime().networkPartition(netPart1);
47        runner.runtime().networkPartition(netPart2);
48        // Imposing 10 secs of clock drift in node n1
49        runner.runtime().clockDrift("n1", -10000);
50        // Applying network delay and loss on node n2 before restarting it
51        runner.runtime().networkOperation("n2", NetOp.delay(50).jitter(10), NetOp.loss(30));
52        // removing the first network partition and restarting node n2
53        runner.runtime().enforceOrder("x2", 10, () -> {
54            runner.runtime().removeNetworkPartition(netPart1);
55            runner.runtime().restartNode("n2", 10);
56        });
57        // removing the second network partition
58        runner.runtime().removeNetworkPartition(netPart2);
59        // Applying different kinds of network operations in different orders
60        runner.runtime().networkOperation("n1", NetOp.delay(100).jitter(10), NetOp.loss(30),
61                NetOp.removeDelay(), NetOp.delay(10).jitter(4), NetOp.removeLoss(),
62                NetOp.removeDelay(), NetOp.loss(20), NetOp.removeLoss());
63        // Waiting for the run sequence to be completed
64        runner.runtime().waitForRunSequenceCompletion(60, 20);
65    }
66}

Each RediB test case should start with defining a new Deployment object. A deployment definition consists of a a set of service and node definitions. A Service is a node template and defines the docker image for the node, the start bash command, required environment variables, common paths, etc. for a specific type of node.

Line 8-14 defines service1 service. Line 9 defines the start command for the node, and in this case, it is using the start.sh bash file and it feeding it with -conf /config.cfg argument. This config file will be provided separately through node definitions later. Line 14 concludes the service definition by marking it as a Java application. If the programming language in use is listed in ServiceType enum, make sure to mark your application with the right ServiceType.

Important

If your program runs on JVM and your programming language in use is not listed in the ServiceType enum, just choose ServiceType.Java as the service type.

Lines 16-30 defines four nodes named n1, n2, n3 and n4 from service1 service and is adding a separate local config file to each of them which will be located at the same target address /config.cfg. Most of the service configuration can be overriden by nodes.

Line 38 starts the defined deployment and line 64 stops the deployment after all tests are executed.

Line 42 shows how to start node while running. In this case, a clock dirft of 100ms will be applied to node n1. Line 44-47 shows how a network partition can be defined and imposed. Here, each of the nodes will be in a separate partition. Line 45 shows an example of imposing network delay and loss on all the interfaces of a specific node. Here, a network delay from a uniform distribution with mean=100 and variance=10 will be applied on n1 and 30% of the packets will be lost.

Logger Configuration

RediB uses SLF4J for logging. As such, you can configure your logging tool of choice. A sample configuration with Logback can be like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>

    <logger name="io.redit" level="DEBUG"/>

    <root level="ERROR">
        <appender-ref ref="Console" />
    </root>
</configuration>

Running the Test Case

Finally, to run the test cases, run the following bash command:

$  mvn clean verify