From 550218b96d6505bed5481d5385fa4684ebd71016 Mon Sep 17 00:00:00 2001 From: Ed Smith Date: Sat, 14 Aug 2021 13:19:57 +0100 Subject: [PATCH] Add exit code test to validate logic is as expected --- tests/exit_test/bad.yaml | 5 ++ tests/exit_test/exit_test.sh | 88 +++++++++++++++++++++++ tests/exit_test/garbled.yaml | 4 ++ tests/exit_test/good.yaml | 5 ++ tests/exit_test/post-machine-failure.yaml | 6 ++ tests/exit_test/pre-machine-failure.yaml | 8 +++ tests/exit_test/verify-fail.yaml | 6 ++ 7 files changed, 122 insertions(+) create mode 100644 tests/exit_test/bad.yaml create mode 100755 tests/exit_test/exit_test.sh create mode 100644 tests/exit_test/garbled.yaml create mode 100644 tests/exit_test/good.yaml create mode 100644 tests/exit_test/post-machine-failure.yaml create mode 100644 tests/exit_test/pre-machine-failure.yaml create mode 100644 tests/exit_test/verify-fail.yaml diff --git a/tests/exit_test/bad.yaml b/tests/exit_test/bad.yaml new file mode 100644 index 0000000..7e7da77 --- /dev/null +++ b/tests/exit_test/bad.yaml @@ -0,0 +1,5 @@ +architecture: amd64 + +actions: + - action: run + command: exit 1 diff --git a/tests/exit_test/exit_test.sh b/tests/exit_test/exit_test.sh new file mode 100755 index 0000000..dc6c74a --- /dev/null +++ b/tests/exit_test/exit_test.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +TEST=0 +FAILURES=0 +VERBOSE=1 + +function test_failed { + local MSG="$1" + TEST=$(($TEST + 1)) + FAILURES=$(($FAILURES + 1)) + echo "Test ${TEST}: ${MSG}" +} + +function test_passed { + TEST=$(($TEST + 1)) + echo "Test ${TEST}: PASS" +} + +function run_cmd { + local CMD="$@" + echo + echo "Running ${CMD}" + if [[ $VERBOSE == 0 ]]; then + $CMD &>/dev/null + else + $CMD + fi + return $? +} + +function expect_success { + local CMD="$@" + run_cmd $CMD && test_passed || test_failed "${CMD} failed with exitcode $?, expected success" +} + +function expect_failure { + local CMD="$@" + run_cmd $CMD && test_failed "${CMD} succeeded, failure expected." || test_passed +} + +function rename_command { + newname="$1" + shift + (exec -a "$newname" "$@") + return $? +} + +if [ -v sudo ]; then + SUDO=sudo +else + SUDO= +fi + +expect_success debos --help +expect_failure debos --not-a-valid-option +expect_failure debos +expect_failure debos good.yaml good.yaml +expect_failure debos --disable-fakemachine --fakemachine-backend=uml good.yaml +expect_failure debos non-existent-file.yaml +expect_failure debos garbled.yaml +expect_failure debos --fakemachine-backend=kvm good.yaml +expect_failure debos verify-fail.yaml +expect_success debos --dry-run good.yaml +expect_failure debos --memory=NotANumber good.yaml +expect_failure debos --scratchsize=NotANumber good.yaml +expect_success debos good.yaml +expect_failure debos bad.yaml +expect_failure debos pre-machine-failure.yaml +expect_failure debos post-machine-failure.yaml +expect_failure rename_command NOT_DEBOS debos good.yaml + +expect_failure $SUDO debos non-existent-file.yaml --disable-fakemachine +expect_failure $SUDO debos garbled.yaml --disable-fakemachine +expect_failure $SUDO debos verify-fail.yaml --disable-fakemachine +expect_success $SUDO debos --dry-run good.yaml --disable-fakemachine +expect_success $SUDO debos good.yaml --disable-fakemachine +expect_failure $SUDO debos bad.yaml --disable-fakemachine +expect_failure $SUDO debos pre-machine-failure.yaml --disable-fakemachine +expect_failure $SUDO debos post-machine-failure.yaml --disable-fakemachine + +echo +if [[ $FAILURES -ne 0 ]]; then + SUCCESSES=$(( $TEST - $FAILURES )) + echo "Error: Only $SUCCESSES/$TEST tests passed" + exit 1 +fi + +echo "All tests passed" diff --git a/tests/exit_test/garbled.yaml b/tests/exit_test/garbled.yaml new file mode 100644 index 0000000..a42aa05 --- /dev/null +++ b/tests/exit_test/garbled.yaml @@ -0,0 +1,4 @@ +This isn't + ::: + + very good YAML diff --git a/tests/exit_test/good.yaml b/tests/exit_test/good.yaml new file mode 100644 index 0000000..4900556 --- /dev/null +++ b/tests/exit_test/good.yaml @@ -0,0 +1,5 @@ +architecture: amd64 + +actions: + - action: run + command: exit 0 diff --git a/tests/exit_test/post-machine-failure.yaml b/tests/exit_test/post-machine-failure.yaml new file mode 100644 index 0000000..1a73a59 --- /dev/null +++ b/tests/exit_test/post-machine-failure.yaml @@ -0,0 +1,6 @@ +architecture: amd64 + +actions: + - action: run + command: exit 1 + postprocess: true diff --git a/tests/exit_test/pre-machine-failure.yaml b/tests/exit_test/pre-machine-failure.yaml new file mode 100644 index 0000000..96743a5 --- /dev/null +++ b/tests/exit_test/pre-machine-failure.yaml @@ -0,0 +1,8 @@ +architecture: amd64 + +actions: + - action: image-partition + imagename: test + imagesize: 500000000000000TB + partitiontype: gpt + diff --git a/tests/exit_test/verify-fail.yaml b/tests/exit_test/verify-fail.yaml new file mode 100644 index 0000000..4f90077 --- /dev/null +++ b/tests/exit_test/verify-fail.yaml @@ -0,0 +1,6 @@ +architecture: amd64 + +actions: + - action: unpack + compression: gz + file: ~