From 7d412c3093cf7abed5873f9523cd6c3420976dd7 Mon Sep 17 00:00:00 2001 From: nprimo Date: Wed, 8 Feb 2023 14:35:34 +0100 Subject: [PATCH] feat(job-regist): add new bash exercise - are the current test cases enough? --- sh/tests/job-regist_test.sh | 59 ++++++++++++++++++++++++++++ sh/tests/solutions/job-regist.sh | 11 ++++++ subjects/devops/job-regist/README.md | 33 ++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 sh/tests/job-regist_test.sh create mode 100644 sh/tests/solutions/job-regist.sh create mode 100644 subjects/devops/job-regist/README.md diff --git a/sh/tests/job-regist_test.sh b/sh/tests/job-regist_test.sh new file mode 100644 index 00000000..aded4d1d --- /dev/null +++ b/sh/tests/job-regist_test.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +# create a function to be called everytime the process exit +abort () { + rm exp.log job.log +} + +trap 'abort' EXIT + + +set -euo pipefail +IFS=' +' +script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd) + +SUBMITTED='student/job-regist.sh' +EXPECTED='solutions/job-regist.sh' + +wait_bg_jobs () { + while [ -n "$(jobs | grep -i running)" ]; do + echo -n "." + sleep 1 + done + echo +} + +# test cases +one_process () { + sleep 2 & + source $script_dirS/$1 +} + +two_processes () { + sleep 3 & + sleep 4 & + source $script_dirS/$1 +} + +one_process_and_kill () { + echo do something +} + +one_process_and_suspend () { + echo do something +} +# end of test cases + +challenge () { + echo "testing $1 case" + $1 $SUBMITTED & + $1 $EXPECTED & + + wait_bg_jobs + + diff <(cat exp.log) <(cat job.log) +} + +challenge one_process +challenge two_processes diff --git a/sh/tests/solutions/job-regist.sh b/sh/tests/solutions/job-regist.sh new file mode 100644 index 00000000..8c1c99ed --- /dev/null +++ b/sh/tests/solutions/job-regist.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +IFS=' +' +PID=$(jobs -l %1 | grep -Eo '[\+\-] [0-9]+' | grep -Eo '[0-9]+') +LOG_FILE="exp.log" + +while kill -0 "$PID" 2> /dev/null; do + echo $(date +"%F %T") - $(jobs %1) >> "$LOG_FILE" + sleep 1 +done diff --git a/subjects/devops/job-regist/README.md b/subjects/devops/job-regist/README.md new file mode 100644 index 00000000..0bff4976 --- /dev/null +++ b/subjects/devops/job-regist/README.md @@ -0,0 +1,33 @@ +## job-regist + +### Instruction + +Create a script, `job-regist.sh`, that will be able to monitor a specific background job. + +The script needs to track the status of the first jobs spawned by the current terminal and periodically save the status into a `job.log` file. It must append the new update without modifying the previous content. + +Each update needs to be appended to the file with the current format: `YYYY-MM-DD hh:mm:ss - ` + +The script should stop running as soon as the job it is tracking ends. + +### Usage + +Here an example of how the script should behave: + +```console +$ sleep 10 & +$ sleep 12 & +$ source job-regist.sh +$ cat job.log +2023-02-08 10:37:50 - [1]+ Running sleep 10 & +2023-02-08 10:37:51 - [1]+ Running sleep 10 & +2023-02-08 10:37:52 - [1]+ Running sleep 10 & +2023-02-08 10:37:53 - [1]+ Running sleep 10 & +2023-02-08 10:37:54 - [1]+ Running sleep 10 & +2023-02-08 10:37:55 - [1]+ Running sleep 10 & +2023-02-08 10:37:56 - [1]+ Running sleep 10 & +2023-02-08 10:37:57 - [1]+ Running sleep 10 & +2023-02-08 10:37:58 - [1]+ Running sleep 10 & +2023-02-08 10:37:59 - [1]+ Running sleep 10 & +$ +```