Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Here are some test examples and/or templates.

Test without run_tests()

Here is a simple test that runs a command and checks the output if it was successfully.

Code Block
languagebash
#!/usr/bin/env bash
source `dirname "$0"`/lib.sh && init || exit 1

check_if_tested softhsm && exit 0
start_test softhsm

test_ok=0
(
    log_this softhsm-init-token softhsm --init-token --slot 0 --label OpenDNSSEC --pin 1234 --so-pin 1234 &&
    log_grep softhsm-init-token stdout "The token has been initialized."
) &&
test_ok=1

if [ "$test_ok" -eq 1 ]; then
    log_cleanup
    set_test_ok softhsm || exit 1
    exit 0
fi

exit 1

Tests with run_tests()

Using run_tests() is good if you have many tests for a project. This example gives the tests additionally functions to use that are directly related to the project.

Main test-<project.sh>

Code Block
languagebash
#!/usr/bin/env bash
source `dirname "$0"`/lib.sh && init || exit 1

ods_reset_env ()
{
    echo "ods_reset_env: reseting opendnssec environment"
    echo "y" | ods-ksmutil setup &&
    log_this softhsm-init-token softhsm --init-token --slot 0 --label OpenDNSSEC --pin 1234 --so-pin 1234 ||
    return 1
    
    if ! log_grep softhsm-init-token stdout "The token has been initialized."; then
        return 1
    fi
}

require opendnssec

check_if_tested opendnssec && exit 0
start_test opendnssec

test_ok=0
(
    run_tests test.d
) &&
test_ok=1

if [ "$test_ok" -eq 1 ]; then
    set_test_ok opendnssec || exit 1
    exit 0
fi

exit 1

Simple test.d/00-start-stop/test.sh

This simple test starts and stop the program and checks the syslog for the appropriated messages that the start and stop was successful.

Code Block
languagebash
#!/usr/bin/env bash

ods_reset_env &&
log_this ods-control-start ods-control start &&
syslog_waitfor 60 'ods-signerd: \[engine\] signer started' &&
syslog_grep 'ods-enforcerd: Sleeping for' &&
log_this ods-control-stop ods-control stop &&
syslog_waitfor 60 'ods-signerd: \[engine\] signer shutdown' &&
syslog_grep 'ods-enforcerd: all done'