diff --git a/tests/001-test-redis.sh b/tests/001-test-redis.sh index baf322e..1e56019 100755 --- a/tests/001-test-redis.sh +++ b/tests/001-test-redis.sh @@ -1,5 +1,7 @@ #!/bin/bash +source $SRCDIR/utils.sh + test_redis_nomad_job() { pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example @@ -16,7 +18,7 @@ test_redis_nomad_job() { # The actual container process might not be running yet. # We need to wait for actual container to start running before trying exec. echo "INFO: Wait for redis container to get into RUNNING state, before trying exec." - is_redis_container_active + is_container_active redis false echo "INFO: Inspecting redis job." redis_status=$(nomad job inspect redis|jq -r '.Job .Status') @@ -53,24 +55,4 @@ test_redis_nomad_job() { popd } -is_redis_container_active() { - i="0" - while test $i -lt 5 - do - sudo CONTAINERD_NAMESPACE=nomad ctr task ls|grep -q RUNNING - if [ $? -eq 0 ]; then - echo "INFO: redis container is up and running" - break - fi - echo "INFO: redis container is down, sleep for 4 seconds." - sleep 4s - i=$[$i+1] - done - - if [ $i -ge 5 ]; then - echo "ERROR: redis container didn't come up. exit 1." - exit 1 - fi -} - test_redis_nomad_job diff --git a/tests/002-test-signal-handler.sh b/tests/002-test-signal-handler.sh index d1e01e8..eeccf7d 100755 --- a/tests/002-test-signal-handler.sh +++ b/tests/002-test-signal-handler.sh @@ -1,5 +1,7 @@ #!/bin/bash +source $SRCDIR/utils.sh + test_signal_handler_nomad_job() { pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example @@ -24,7 +26,7 @@ test_signal_handler_nomad_job() { # The actual container process might not be running yet. # We need to wait for actual container to start running before trying to send invalid signal. echo "INFO: Wait for signal container to get into RUNNING state, before trying to send invalid signal." - is_signal_container_active + is_container_active signal false echo "INFO: Test invalid signal." alloc_id=$(nomad job status signal|awk 'END{print}'|cut -d ' ' -f 1) @@ -55,24 +57,4 @@ cleanup() { rm $tmpfile > /dev/null 2>&1 } -is_signal_container_active() { - i="0" - while test $i -lt 5 - do - sudo CONTAINERD_NAMESPACE=nomad ctr task ls|grep -q RUNNING - if [ $? -eq 0 ]; then - echo "INFO: signal container is up and running" - break - fi - echo "INFO: signal container is down, sleep for 4 seconds." - sleep 4s - i=$[$i+1] - done - - if [ $i -ge 5 ]; then - echo "ERROR: signal container didn't come up. exit 1." - exit 1 - fi -} - test_signal_handler_nomad_job diff --git a/tests/003-test-capabilities.sh b/tests/003-test-capabilities.sh index 3895652..3f853d3 100755 --- a/tests/003-test-capabilities.sh +++ b/tests/003-test-capabilities.sh @@ -1,5 +1,7 @@ #!/bin/bash +source $SRCDIR/utils.sh + # readonly_rootfs, cap_add and cap_drop flags are tested as part of this test. test_capabilities_nomad_job() { pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example @@ -18,7 +20,7 @@ test_capabilities_nomad_job() { # The actual container process might not be running yet. # We need to wait for actual container to start running before trying exec. echo "INFO: Wait for capabilities container to get into RUNNING state, before trying exec." - is_capabilities_container_active + is_container_active capabilities true echo "INFO: Inspecting capabilities job." cap_status=$(nomad job inspect capabilities|jq -r '.Job .Status') @@ -74,25 +76,4 @@ cleanup() { rm $tmpfile > /dev/null 2>&1 } -is_capabilities_container_active() { - i="0" - while test $i -lt 5 - do - sudo CONTAINERD_NAMESPACE=nomad ctr task ls|grep -q RUNNING - if [ $? -eq 0 ]; then - echo "INFO: capabilities container is up and running" - sleep 5s - break - fi - echo "INFO: capabilities container is down, sleep for 4 seconds." - sleep 4s - i=$[$i+1] - done - - if [ $i -ge 5 ]; then - echo "ERROR: capabilities container didn't come up. exit 1." - exit 1 - fi -} - test_capabilities_nomad_job diff --git a/tests/004-test-privileged.sh b/tests/004-test-privileged.sh index 5103fea..ab5bcfa 100755 --- a/tests/004-test-privileged.sh +++ b/tests/004-test-privileged.sh @@ -1,5 +1,7 @@ #!/bin/bash +source $SRCDIR/utils.sh + # privileged mode, devices and mounts are tested as part of this test. test_privileged_nomad_job() { pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example @@ -20,7 +22,7 @@ test_privileged_nomad_job() { # The actual container process might not be running yet. # We need to wait for actual container to start running before trying exec. echo "INFO: Wait for privileged container to get into RUNNING state, before trying exec." - is_privileged_container_active + is_container_active privileged true echo "INFO: Inspecting privileged job." job_status=$(nomad job inspect privileged|jq -r '.Job .Status') @@ -73,25 +75,4 @@ setup_bind_source() { echo hello > /tmp/s1/bind.txt } -is_privileged_container_active() { - i="0" - while test $i -lt 5 - do - sudo CONTAINERD_NAMESPACE=nomad ctr task ls|grep -q RUNNING - if [ $? -eq 0 ]; then - echo "INFO: privileged container is up and running" - sleep 5s - break - fi - echo "INFO: privileged container is down, sleep for 4 seconds." - sleep 4s - i=$[$i+1] - done - - if [ $i -ge 5 ]; then - echo "ERROR: privileged container didn't come up. exit 1." - exit 1 - fi -} - test_privileged_nomad_job diff --git a/tests/005-test-volume_mount.sh b/tests/005-test-volume_mount.sh index f2e64d6..2521c3f 100755 --- a/tests/005-test-volume_mount.sh +++ b/tests/005-test-volume_mount.sh @@ -1,5 +1,7 @@ #!/bin/bash +source $SRCDIR/utils.sh + job_name=volume_mount host_volume_path=/tmp/host_volume/s1 @@ -16,7 +18,7 @@ test_volume_mount_nomad_job() { # The actual container process might not be running yet. # We need to wait for actual container to start running before trying exec. echo "INFO: Wait for ${job_name} container to get into RUNNING state, before trying exec." - is_${job_name}_container_active + is_container_active ${job_name} true echo "INFO: Checking status of $job_name job." job_status=$(nomad job status -short $job_name|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ') @@ -71,25 +73,4 @@ setup_bind_source() { echo hello > ${host_volume_path}/bind.txt } -is_volume_mount_container_active() { - i="0" - while test $i -lt 5 - do - sudo CONTAINERD_NAMESPACE=nomad ctr task ls|grep -q RUNNING - if [ $? -eq 0 ]; then - echo "INFO: ${job_name} container is up and running" - sleep 5s - break - fi - echo "INFO: ${job_name} container is down, sleep for 4 seconds." - sleep 4s - i=$[$i+1] - done - - if [ $i -ge 5 ]; then - echo "ERROR: ${job_name} container didn't come up. exit 1." - exit 1 - fi -} - test_volume_mount_nomad_job diff --git a/tests/006-test-dns.sh b/tests/006-test-dns.sh index d455c6b..2169f80 100755 --- a/tests/006-test-dns.sh +++ b/tests/006-test-dns.sh @@ -1,5 +1,7 @@ #!/bin/bash +source $SRCDIR/utils.sh + job_name=dns test_dns_nomad_job() { @@ -12,7 +14,7 @@ test_dns_nomad_job() { # The actual container process might not be running yet. # We need to wait for actual container to start running before trying exec. echo "INFO: Wait for ${job_name} container to get into RUNNING state, before trying exec." - is_${job_name}_container_active + is_container_active ${job_name} true echo "INFO: Checking status of $job_name job." job_status=$(nomad job status -short $job_name|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ') @@ -58,25 +60,4 @@ test_dns_nomad_job() { popd } -is_dns_container_active() { - i="0" - while test $i -lt 5 - do - sudo CONTAINERD_NAMESPACE=nomad ctr task ls|grep -q RUNNING - if [ $? -eq 0 ]; then - echo "INFO: ${job_name} container is up and running" - sleep 5s - break - fi - echo "INFO: ${job_name} container is down, sleep for 4 seconds." - sleep 4s - i=$[$i+1] - done - - if [ $i -ge 5 ]; then - echo "ERROR: ${job_name} container didn't come up. exit 1." - exit 1 - fi -} - test_dns_nomad_job diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 763a635..a1891ec 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -12,6 +12,8 @@ else export GOPATH=$HOME/go fi export GO_VERSION=1.14.3 +export SRCDIR=`dirname $0` +source $SRCDIR/utils.sh # Keeps track of overall pass/failure status of tests. Even if single test # fails, PASS_STATUS will be set to 1 and returned to caller when all @@ -47,11 +49,10 @@ run_test () { } run_tests() { - local srcdir=`dirname $0` if [ $# -gt 0 ]; then local files=$@ else - local files="$srcdir/[0-9][0-9][0-9]-test-*" + local files="$SRCDIR/[0-9][0-9][0-9]-test-*" fi for t in $files;do run_test ./$t diff --git a/tests/utils.sh b/tests/utils.sh new file mode 100755 index 0000000..b49552a --- /dev/null +++ b/tests/utils.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +is_container_active() { + local job_name=$1 + local is_sleep=$2 + + i="0" + while test $i -lt 5 + do + sudo CONTAINERD_NAMESPACE=nomad ctr task ls|grep -q RUNNING + if [ $? -eq 0 ]; then + echo "INFO: ${job_name} container is up and running" + if [ "$is_sleep" = true ]; then + sleep 5s + fi + break + fi + echo "INFO: ${job_name} container is down, sleep for 4 seconds." + sleep 4s + i=$[$i+1] + done + + if [ $i -ge 5 ]; then + echo "ERROR: ${job_name} container didn't come up. exit 1." + exit 1 + fi +}