Skip to content

Commit

Permalink
Modularize tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
shishir-a412ed committed Jan 28, 2021
1 parent 797cbc8 commit e423a7a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 132 deletions.
24 changes: 3 additions & 21 deletions tests/001-test-redis.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

source $SRCDIR/utils.sh

test_redis_nomad_job() {
pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example

Expand All @@ -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')
Expand Down Expand Up @@ -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
24 changes: 3 additions & 21 deletions tests/002-test-signal-handler.sh
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
25 changes: 3 additions & 22 deletions tests/003-test-capabilities.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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
25 changes: 3 additions & 22 deletions tests/004-test-privileged.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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
25 changes: 3 additions & 22 deletions tests/005-test-volume_mount.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

source $SRCDIR/utils.sh

job_name=volume_mount
host_volume_path=/tmp/host_volume/s1

Expand All @@ -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 ' ')
Expand Down Expand Up @@ -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
25 changes: 3 additions & 22 deletions tests/006-test-dns.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

source $SRCDIR/utils.sh

job_name=dns

test_dns_nomad_job() {
Expand All @@ -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 ' ')
Expand Down Expand Up @@ -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
5 changes: 3 additions & 2 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/utils.sh
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e423a7a

Please sign in to comment.