|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +job_name=volume_mount |
| 4 | +host_volume_path=/tmp/host_volume/s1 |
| 5 | + |
| 6 | +# test volume_mount |
| 7 | +test_volume_mount_nomad_job() { |
| 8 | + pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example |
| 9 | + |
| 10 | + setup_bind_source |
| 11 | + |
| 12 | + echo "INFO: Starting nomad $job_name job using nomad-driver-containerd." |
| 13 | + nomad job run $job_name.nomad |
| 14 | + |
| 15 | + # Even though $(nomad job status) reports job status as "running" |
| 16 | + # The actual container process might not be running yet. |
| 17 | + # We need to wait for actual container to start running before trying exec. |
| 18 | + echo "INFO: Wait for ${job_name} container to get into RUNNING state, before trying exec." |
| 19 | + is_${job_name}_container_active |
| 20 | + |
| 21 | + echo "INFO: Checking status of $job_name job." |
| 22 | + job_status=$(nomad job status -short $job_name|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ') |
| 23 | + if [ "$job_status" != "running" ];then |
| 24 | + echo "ERROR: Error in getting ${job_name} job status." |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | + |
| 28 | + # Check if bind mount exists. |
| 29 | + echo "INFO: Checking if bind mount exists." |
| 30 | + for mountpoint in t1 read_only_target ; do |
| 31 | + output=$(nomad alloc exec -job ${job_name} cat /tmp/${mountpoint}/bind.txt) |
| 32 | + if [ "$output" != "hello" ]; then |
| 33 | + echo "ERROR: bind mount /tmp/${mountpoint} does not exist in container rootfs." |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + done |
| 37 | + |
| 38 | + # Check read only mount can not write. |
| 39 | + echo "INFO: Checking read only mount is not writable." |
| 40 | + nomad alloc exec -job ${job_name} touch /tmp/read_only_target/writable_test.txt &>/dev/null |
| 41 | + if [ -e ${host_volume_path}/writable_test.txt ];then |
| 42 | + echo "ERROR: Read only bind mount in /tmp/read_only_target should not be writable." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + |
| 46 | + # Check writable mount can write. |
| 47 | + echo "INFO: Checking non read_only mount is writable." |
| 48 | + nomad alloc exec -job ${job_name} touch /tmp/t1/writable_test.txt |
| 49 | + if [ ! -e ${host_volume_path}/writable_test.txt ];then |
| 50 | + echo "ERROR: bind mount in /tmp/t1 should be writable." |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + |
| 54 | + echo "INFO: Stopping nomad ${job_name} job." |
| 55 | + nomad job stop ${job_name} |
| 56 | + job_status=$(nomad job status -short ${job_name}|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ') |
| 57 | + if [ $job_status != "dead(stopped)" ];then |
| 58 | + echo "ERROR: Error in stopping ${job_name} job." |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + |
| 62 | + echo "INFO: purge nomad ${job_name} job." |
| 63 | + nomad job stop -purge ${job_name} |
| 64 | + popd |
| 65 | +} |
| 66 | + |
| 67 | +setup_bind_source() { |
| 68 | + rm -f ${host_volume_path}/bind.txt |
| 69 | + rm -f ${host_volume_path}/writable_test.txt |
| 70 | + |
| 71 | + echo hello > ${host_volume_path}/bind.txt |
| 72 | +} |
| 73 | + |
| 74 | +is_volume_mount_container_active() { |
| 75 | + i="0" |
| 76 | + while test $i -lt 5 |
| 77 | + do |
| 78 | + sudo CONTAINERD_NAMESPACE=nomad ctr task ls|grep -q RUNNING |
| 79 | + if [ $? -eq 0 ]; then |
| 80 | + echo "INFO: ${job_name} container is up and running" |
| 81 | + sleep 5s |
| 82 | + break |
| 83 | + fi |
| 84 | + echo "INFO: ${job_name} container is down, sleep for 4 seconds." |
| 85 | + sleep 4s |
| 86 | + i=$[$i+1] |
| 87 | + done |
| 88 | + |
| 89 | + if [ $i -ge 5 ]; then |
| 90 | + echo "ERROR: ${job_name} container didn't come up. exit 1." |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | +} |
| 94 | + |
| 95 | +test_volume_mount_nomad_job |
0 commit comments