summaryrefslogtreecommitdiffstats
path: root/fluent-bit/tests/lib/shunit2/examples
diff options
context:
space:
mode:
Diffstat (limited to 'fluent-bit/tests/lib/shunit2/examples')
-rwxr-xr-xfluent-bit/tests/lib/shunit2/examples/equality_test.sh9
-rwxr-xr-xfluent-bit/tests/lib/shunit2/examples/lineno_test.sh15
-rw-r--r--fluent-bit/tests/lib/shunit2/examples/math.inc17
-rwxr-xr-xfluent-bit/tests/lib/shunit2/examples/math_test.sh25
-rwxr-xr-xfluent-bit/tests/lib/shunit2/examples/mkdir_test.sh79
-rwxr-xr-xfluent-bit/tests/lib/shunit2/examples/mock_file.sh80
-rwxr-xr-xfluent-bit/tests/lib/shunit2/examples/mock_file_test.sh33
-rwxr-xr-xfluent-bit/tests/lib/shunit2/examples/party_test.sh16
-rwxr-xr-xfluent-bit/tests/lib/shunit2/examples/suite_test.sh32
9 files changed, 306 insertions, 0 deletions
diff --git a/fluent-bit/tests/lib/shunit2/examples/equality_test.sh b/fluent-bit/tests/lib/shunit2/examples/equality_test.sh
new file mode 100755
index 00000000..7b566403
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/equality_test.sh
@@ -0,0 +1,9 @@
+#! /bin/sh
+# file: examples/equality_test.sh
+
+testEquality() {
+ assertEquals 1 1
+}
+
+# Load and run shUnit2.
+. ../shunit2
diff --git a/fluent-bit/tests/lib/shunit2/examples/lineno_test.sh b/fluent-bit/tests/lib/shunit2/examples/lineno_test.sh
new file mode 100755
index 00000000..11ddfc8a
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/lineno_test.sh
@@ -0,0 +1,15 @@
+#! /bin/sh
+# file: examples/lineno_test.sh
+
+testLineNo() {
+ # This assert will have line numbers included (e.g. "ASSERT:[123] ...") if
+ # they are supported.
+ echo "_ASSERT_EQUALS_ macro value: ${_ASSERT_EQUALS_}"
+ ${_ASSERT_EQUALS_} '"not equal"' 1 2
+
+ # This assert will not have line numbers included (e.g. "ASSERT: ...").
+ assertEquals 'not equal' 1 2
+}
+
+# Load and run shUnit2.
+. ../shunit2
diff --git a/fluent-bit/tests/lib/shunit2/examples/math.inc b/fluent-bit/tests/lib/shunit2/examples/math.inc
new file mode 100644
index 00000000..40971066
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/math.inc
@@ -0,0 +1,17 @@
+# available as examples/math.inc
+
+add_generic()
+{
+ num_a=$1
+ num_b=$2
+
+ expr $1 + $2
+}
+
+add_bash()
+{
+ num_a=$1
+ num_b=$2
+
+ echo $(($1 + $2))
+}
diff --git a/fluent-bit/tests/lib/shunit2/examples/math_test.sh b/fluent-bit/tests/lib/shunit2/examples/math_test.sh
new file mode 100755
index 00000000..c6d00299
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/math_test.sh
@@ -0,0 +1,25 @@
+#! /bin/sh
+# file: examples/math_test.sh
+
+testAdding() {
+ result=`add_generic 1 2`
+ assertEquals \
+ "the result of '${result}' was wrong" \
+ 3 "${result}"
+
+ # Disable non-generic tests.
+ [ -z "${BASH_VERSION:-}" ] && startSkipping
+
+ result=`add_bash 1 2`
+ assertEquals \
+ "the result of '${result}' was wrong" \
+ 3 "${result}"
+}
+
+oneTimeSetUp() {
+ # Load include to test.
+ . ./math.inc
+}
+
+# Load and run shUnit2.
+. ../shunit2
diff --git a/fluent-bit/tests/lib/shunit2/examples/mkdir_test.sh b/fluent-bit/tests/lib/shunit2/examples/mkdir_test.sh
new file mode 100755
index 00000000..a608e3aa
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/mkdir_test.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+# vim:et:ft=sh:sts=2:sw=2
+#
+# Copyright 2008-2019 Kate Ward. All Rights Reserved.
+# Released under the Apache 2.0 license.
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# shUnit2 -- Unit testing framework for Unix shell scripts.
+# https://github.com/kward/shunit2
+#
+# Author: kate.ward@forestent.com (Kate Ward)
+#
+# Example unit test for the mkdir command.
+#
+# There are times when an existing shell script needs to be tested. In this
+# example, we will test several aspects of the the mkdir command, but the
+# techniques could be used for any existing shell script.
+
+testMissingDirectoryCreation() {
+ ${mkdirCmd} "${testDir}" >${stdoutF} 2>${stderrF}
+ rtrn=$?
+ th_assertTrueWithNoOutput ${rtrn} "${stdoutF}" "${stderrF}"
+
+ assertTrue 'directory missing' "[ -d '${testDir}' ]"
+}
+
+testExistingDirectoryCreationFails() {
+ # Create a directory to test against.
+ ${mkdirCmd} "${testDir}"
+
+ # Test for expected failure while trying to create directory that exists.
+ ${mkdirCmd} "${testDir}" >${stdoutF} 2>${stderrF}
+ rtrn=$?
+ assertFalse 'expecting return code of 1 (false)' ${rtrn}
+ assertNull 'unexpected output to stdout' "`cat ${stdoutF}`"
+ assertNotNull 'expected error message to stderr' "`cat ${stderrF}`"
+
+ assertTrue 'directory missing' "[ -d '${testDir}' ]"
+}
+
+testRecursiveDirectoryCreation() {
+ testDir2="${testDir}/test2"
+
+ ${mkdirCmd} -p "${testDir2}" >${stdoutF} 2>${stderrF}
+ rtrn=$?
+ th_assertTrueWithNoOutput ${rtrn} "${stdoutF}" "${stderrF}"
+
+ assertTrue 'first directory missing' "[ -d '${testDir}' ]"
+ assertTrue 'second directory missing' "[ -d '${testDir2}' ]"
+}
+
+th_assertTrueWithNoOutput() {
+ th_return_=$1
+ th_stdout_=$2
+ th_stderr_=$3
+
+ assertFalse 'unexpected output to STDOUT' "[ -s '${th_stdout_}' ]"
+ assertFalse 'unexpected output to STDERR' "[ -s '${th_stderr_}' ]"
+
+ unset th_return_ th_stdout_ th_stderr_
+}
+
+oneTimeSetUp() {
+ outputDir="${SHUNIT_TMPDIR}/output"
+ mkdir "${outputDir}"
+ stdoutF="${outputDir}/stdout"
+ stderrF="${outputDir}/stderr"
+
+ mkdirCmd='mkdir' # save command name in variable to make future changes easy
+ testDir="${SHUNIT_TMPDIR}/some_test_dir"
+}
+
+tearDown() {
+ rm -fr "${testDir}"
+}
+
+# Load and run shUnit2.
+[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
+. ../shunit2
diff --git a/fluent-bit/tests/lib/shunit2/examples/mock_file.sh b/fluent-bit/tests/lib/shunit2/examples/mock_file.sh
new file mode 100755
index 00000000..812e4488
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/mock_file.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+#
+# shUnit2 example for mocking files.
+#
+# This example demonstrates two different mechanisms for mocking files on the
+# system. The first method is preferred for testing specific aspects of a file,
+# and the second method is preferred when multiple tests need access to the
+# same mock data.
+#
+# When mocking files, the key thing of importance is providing the code under
+# test with the correct file to read. The best practice for writing code where
+# files need to be mocked is either:
+# - Pass the filename to be tested into a function and test that function, or
+# - Provide a function that returns the name of the filename to be read.
+#
+# The first case is preferred whenever possible as it allows the unit test to
+# be explicit about what is being tested. The second case is useful when the
+# first case is not achievable.
+#
+# For the second case, there are two common methods to mock the filename
+# returned by the function:
+# - Provide a special value (e.g. a mock variable) that is only available
+# during testing, or
+# - Override something (e.g. the constant) in the test script.
+#
+# The first case is preferred as it doesn't require the unit test to alter code
+# in any way. Yes, it means that the code itself knows that it is under test,
+# and it behaves slightly differently than under normal conditions, but a
+# visual inspection of the code by the developer should be sufficient to
+# validate proper functionality of such a simple function.
+
+# Treat unset variables as an error.
+set -u
+
+PASSWD='/etc/passwd'
+
+# Read the root UID from the passwd filename provided as the first argument.
+root_uid_from_passed_filename() {
+ filename=$1
+ root_uid "${filename}"
+ unset filename
+}
+
+
+# Read the root UID from the passwd filename derived by call to the
+# passwd_filename() function.
+root_uid_from_derived_filename() {
+ root_uid "$(passwd_filename)"
+}
+
+passwd_filename() {
+ if [ -n "${MOCK_PASSWD:-}" ]; then
+ echo "${MOCK_PASSWD}" # Mock file for testing.
+ return
+ fi
+ echo "${PASSWD}"
+}
+
+
+# Extract the root UID.
+root_uid() { awk -F: 'u==$1{print $3}' u=root "$1"; }
+
+
+main() {
+ echo "root_uid_from_passed_filename:"
+ root_uid_from_passed_filename "${PASSWD}"
+
+ echo
+
+ echo "root_uid_from_derived_filename:"
+ root_uid_from_derived_filename
+}
+
+
+# Execute main() if this is run in standalone mode (i.e. not in a unit test).
+ARGV0="$(basename "$0")"
+argv0="$(echo "${ARGV0}" |sed 's/_test$//;s/_test\.sh$//')"
+if [ "${ARGV0}" = "${argv0}" ]; then
+ main "$@"
+fi
diff --git a/fluent-bit/tests/lib/shunit2/examples/mock_file_test.sh b/fluent-bit/tests/lib/shunit2/examples/mock_file_test.sh
new file mode 100755
index 00000000..1da8dd2a
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/mock_file_test.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+#
+# shUnit2 example for mocking files.
+
+MOCK_PASSWD='' # This will be overridden in oneTimeSetUp().
+
+test_root_uid_from_passed_filename() {
+ result="$(root_uid_from_passed_filename "${MOCK_PASSWD}")"
+ assertEquals 'unexpected root uid' '0' "${result}"
+}
+
+test_root_uid_from_derived_filename() {
+ result="$(root_uid_from_derived_filename)"
+ assertEquals 'unexpected root uid' '0' "${result}"
+}
+
+oneTimeSetUp() {
+ # Provide a mock passwd file for testing. This will be cleaned up
+ # automatically by shUnit2.
+ MOCK_PASSWD="${SHUNIT_TMPDIR}/passwd"
+ cat <<EOF >"${MOCK_PASSWD}"
+nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
+root:*:0:0:System Administrator:/var/root:/bin/sh
+daemon:*:1:1:System Services:/var/root:/usr/bin/false
+EOF
+
+ # Load script under test.
+ . './mock_file.sh'
+}
+
+# Load and run shUnit2.
+[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
+. ../shunit2
diff --git a/fluent-bit/tests/lib/shunit2/examples/party_test.sh b/fluent-bit/tests/lib/shunit2/examples/party_test.sh
new file mode 100755
index 00000000..41bd124f
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/party_test.sh
@@ -0,0 +1,16 @@
+#! /bin/sh
+# file: examples/party_test.sh
+#
+# This test is mostly for fun. Technically, it is a bad example of a unit test
+# because of the temporal requirement, namely that the year be 1999. A better
+# test would have been to pass in both a known-bad and known-good year into a
+# function, and test for the expected result.
+
+testPartyLikeItIs1999() {
+ year=`date '+%Y'`
+ assertEquals "It's not 1999 :-(" \
+ '1999' "${year}"
+}
+
+# Load and run shUnit2.
+. ../shunit2
diff --git a/fluent-bit/tests/lib/shunit2/examples/suite_test.sh b/fluent-bit/tests/lib/shunit2/examples/suite_test.sh
new file mode 100755
index 00000000..278c3a01
--- /dev/null
+++ b/fluent-bit/tests/lib/shunit2/examples/suite_test.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+# file: examples/suite_test.sh
+#
+# This test demonstrates the use of suites. Note: the suite functionality is
+# deprecated as of v2.1.0, and will be removed in a future major release.
+
+# suite is a special function called by shUnit2 to setup a suite of tests. It
+# enables a developer to call a set of functions that contain tests without
+# needing to rename the functions to start with "test".
+#
+# Tests that are to be called from within `suite()` are added to the list of
+# executable tests by means of the `suite_addTest()` function.
+suite() {
+ # Add the suite_test_one() function to the list of executable tests.
+ suite_addTest suite_test_one
+
+ # Call the suite_test_two() function, but note that the test results will not
+ # be added to the global stats, and therefore not reported at the end of the
+ # unit test execution.
+ suite_test_two
+}
+
+suite_test_one() {
+ assertEquals 1 1
+}
+
+suite_test_two() {
+ assertNotEquals 1 2
+}
+
+# Load and run shUnit2.
+. ../shunit2