Skip to main content

Tests

Baz tests are code that captures the desired state of IT infrastructure. Every invocation of the Baz system essentially boils down to running a test or tests with a specified set of "actors" and reporting if the tests pass or fail. If the tests pass, the actors are in their desired state as described by the code.

Actors

Actors are specified as input to invocating the Baz system through an actors file. This file is a TOML file with the following structure:

[actor_name]
plugin = "actor_plugin"
url = "localhost:<predefined-port>"

[actor_name.params]
host = "ldaps://XXXX.com"
cacert = "file://XXXX/XXXX/XXXX.pem"
auth = "simpleBind"
dn = "cn=XXXX,dc=XXXX,dc=XXXX,dc=XXX"
password = "secret://XXXX.XXXX.XXXX"

The above structure defines a single actor with the name actor_name. Now in the ES6 test code, actor_name is an available object. Depending on the plugin, the object will have a set of functions and/or properties that can be used in the tests.

The plugins currently supported are Windows, Okta, and LDAP. See Plugins documentation for details of the supported parameters for each plugin.

When an actor is defined as above and passed using the --actor input flag of bazcli, for instance, the Baz system automatically initializes the plugin with the details from the params. The plugin, in turn, talks to the end system, authenticate, and sets up connections/credentials as required.

Test Code

Test code can be a single file or a collection of files. A simple single-file test with no actors is given below:

// test_template.js
// Sample baz test script

function setup() {
let life = 42;
console.log("Hello, BazLang World!");
console.log("The answer to life, the universe, and everything");
}

function testAssertion() {
let b = life;
console.log("Meaning of life is " + b);
verify(b).to.eql(life);
}

function testMeaning() {
console.log("Meaning of life is " + life);
console.log("This test will fail as b is not in scope.");
verify(b).to.eql(life);
}

function tear_down() {
console.log("Bye!");
}

There are three major parts in the above file:

  1. The setup() function.
  2. The tear_down() function.
  3. The test* functions, aka Assertions.

setup()

The setup() function should be present at most once in a test invocation. If tests are distributed across files/directories, then one of the files should have exactly one implementation of the setup(). This function, if present, is run first, and any variables declared within it are available in all assertions.

tear_down()

The tear_down() function is similar to the setup() function. It should be present at most once in a test invocation. If present, it is run once after all assertions are run. As the name indicates, any form of cleanup is expected to be done via this.

Assertions

Any function that starts with the [T|t]est string is an assertion. All assertions presented to the Baz system as input are run at least once by default. Each function is a test case/assertion in Baz. They can be run multiple times via the code itself or will be run automatically once. Any test function that throws an exception is deemed a failed test. This is accordingly captured when reporting.

In the example given above, note the use of verify. This is a wrapper around chaijs's expect with better error handling. Refer to the example scripts for more details on verify and verifyEach.