Skip to content

Commit

Permalink
refactor: restrict container detection by pid only when required file…
Browse files Browse the repository at this point in the history
… exists
  • Loading branch information
edouard-lopez committed Oct 14, 2022
1 parent 0027f5b commit 3616f36
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 24 deletions.
15 changes: 9 additions & 6 deletions functions/_pure_detect_container_by_pid_method.fish
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
function _pure_detect_container_by_pid_method \
--description "Linux method to detect container using /proc. see https://stackoverflow.com/a/37015387/802365"
--description "Linux method to detect container using /proc. see https://stackoverflow.com/a/37015387/802365" \
--argument-names proc_sched

set --query proc_sched[1]; or set proc_sched /proc/1/sched

head -n 1 $proc_sched \
| string match \
--quiet \
--invert \
--regex 'init|systemd'
if test -e $proc_sched
head -n 1 $proc_sched \
| string match \
--quiet \
--invert \
--regex 'init|systemd'
end
end
68 changes: 50 additions & 18 deletions tests/_pure_detect_container_by_pid_method.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,67 @@ end
setup

function teardown
functions --erase uname
functions --erase uname
end

if test (uname -s) = "Linux"
@test "_pure_detect_container_by_pid_method: true for init" (
set --local proc_sched /proc/1/sched
echo "init (1, #threads: 1)" >$proc_sched
set proc_sched /tmp/1/sched
function before_each
functions --erase head
end

_pure_detect_container_by_pid_method $proc_sched
) $status -eq $SUCCESS
function _create_proc_sched_file --argument-names proc_sched
mkdir -p (dirname $proc_sched)
touch $proc_sched
end

if test (uname -s) = "Linux"
@test "_pure_detect_container_by_pid_method: true for systemd" (
set --local proc_sched /proc/1/sched
echo "systemd (1, #threads: 1)" >$proc_sched
before_each
@test "_pure_detect_container_by_pid_method: ignored when /proc/*/scheg is missing" (
rm -rf $proc_sched
function head; echo (status function) > /tmp/called; end # spy

_pure_detect_container_by_pid_method $proc_sched
) $status -eq $SUCCESS
end
_has_called head
) $status -eq $FAILURE


if test (uname -s) = "Linux"
@test "_pure_detect_container_by_pid_method: true for Github Action" (
set --local proc_sched /proc/1/sched
echo "systemd (1, #threads: 1)" >$proc_sched
before_each
@test "_pure_detect_container_by_pid_method: when /proc/*/scheg exists" (
_create_proc_sched_file $proc_sched
function head; echo (status function) > /tmp/called; end # spy

_pure_detect_container_by_pid_method $proc_sched
_has_called head
) $status -eq $SUCCESS


if test (uname -s) = Linux
_create_proc_sched_file $proc_sched
before_each
@test "_pure_detect_container_by_pid_method: false when detecting init in /proc/1/sched" (
echo "init (1, #threads: 1)" >$proc_sched

_pure_detect_container_by_pid_method $proc_sched
) $status -eq $FAILURE
end

if test (uname -s) = Linux
before_each
@test "_pure_detect_container_by_pid_method: false when detecting systemd in /proc/1/sched" (
_create_proc_sched_file $proc_sched
echo "systemd (1, #threads: 1)" >$proc_sched

_pure_detect_container_by_pid_method $proc_sched
) $status -eq $FAILURE
end

if test (uname -s) = Linux
before_each
@test "_pure_detect_container_by_pid_method: true when 1st process is neither systemd nor init in /proc/1/sched" (
_create_proc_sched_file $proc_sched
echo "fish (1, #threads: 1)" >$proc_sched

_pure_detect_container_by_pid_method $proc_sched
) $status -eq $SUCCESS
end

teardown
# teardown
11 changes: 11 additions & 0 deletions tests/fixtures/constants.fish
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ end
function _print_filename --argument-names filename
echo (set_color cyan)$filename(set_color normal)
end

function _has_called \
--description "check spy method XYZ write to the /tmp/called file when called" \
--argument-names spy # name of the method

if test -r /tmp/called
grep -c -q $spy /tmp/called # check spy was called
else
return $FAILURE
end
end

0 comments on commit 3616f36

Please sign in to comment.