Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/301 voidlinux container detection #302

Merged
merged 6 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
> ##### :arrow_up: Hey! Want to migrate from v3.x to v4.x? Check our [**migration guide**](https://github.com/pure-fish/pure/releases/tag/v4.0.0), done with :heart:
> ##### :arrow_up: Hey! Want to migrate from v3.x to v4.x? Check our [**migration guide**](https://github.com/pure-fish/pure/releases/tag/v4.0.0), done with :heart

# pure [![github-ci-badge]][github-ci-link] ![fish-3]

Expand Down Expand Up @@ -54,6 +54,9 @@ Fully **customizable** (colors, symbols and features):
- Display `⇣` when branch is _behind_ (commits to pull) ;
- Async update when [configured with fish-async-prompt](https://github.com/pure-fish/pure/wiki/Async-git-Prompt) ;
- Update terminal title with _current folder_ and _command_ ;
- Detect when running in a container

🏳️: Enabled or disabled via a [feature flag](#-features-flags).

## :paintbrush: Configuration

Expand Down Expand Up @@ -84,6 +87,7 @@ You can tweak `pure` behavior and color by changing [universal variables](https:
| **`pure_begin_prompt_with_current_directory`** | `true` | `true`: _`pwd` `git`, `SSH`, duration_.<br/>`false`: _`SSH` `pwd` `git`, duration_. |
| **`pure_check_for_new_release`** | `false` | `true`: check repo for new release (on every shell start) |
| **`pure_enable_git`** | `true` | Show info about Git repository. |
| **`pure_enable_container_detection`** | `true` | `false`: Do not check if run in container (e.g. `docker`, `podman`, `LXC`/`LXD`, etc.).<br/>:warning: Detection is a bit [tricky across OSes][container-detection]. |
| **`pure_enable_single_line_prompt`** | `false` | `true`: Compact prompt as a single line |
| **`pure_reverse_prompt_symbol_in_vimode`** | `true` | `true`: `❮` indicate a non-insert mode.<br/>`false`: indicate vi mode with `[I]`, `[N]`, `[V]`. |
| **`pure_separate_prompt_on_error`** | `false` | Show last command [exit code as a separate character][exit-code]. |
Expand Down Expand Up @@ -126,9 +130,9 @@ Specify the [`FISH_VERSION`][fish-releases] you want, and the `CMD` executed by

## :clap: Thanks

* [@andreiborisov](https://github.com/andreiborisov) for the [docker images][docker-images] ;
* [@jorgebucaran](https://github.com/jorgebucaran/) for [fishtape](https://github.com/jorgebucaran/fishtape) ;
* [@rafaelrinaldi](https://github.com/pure-fish/pure) for starting the project ;
- [@andreiborisov](https://github.com/andreiborisov) for the [docker images][docker-images] ;
- [@jorgebucaran](https://github.com/jorgebucaran/) for [fishtape](https://github.com/jorgebucaran/fishtape) ;
- [@rafaelrinaldi](https://github.com/pure-fish/pure) for starting the project ;

## :classical_building: License

Expand All @@ -142,3 +146,4 @@ Specify the [`FISH_VERSION`][fish-releases] you want, and the `CMD` executed by
[docker-images]: https://github.com/andreiborisov/docker-fish/
[MIT]: LICENSE.md
[fish-set-color]: https://fishshell.com/docs/current/cmds/set_color.html
[container-detection]: https://stackoverflow.com/q/23513045/802365
3 changes: 3 additions & 0 deletions conf.d/pure.fish
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ _pure_set_default pure_color_prefix_root_prompt pure_color_danger

# Compact mode
_pure_set_default pure_enable_single_line_prompt false

# Detect when running in container (e.g. docker, podman, LXC/LXD)
_pure_set_default pure_enable_container_detection true
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
29 changes: 16 additions & 13 deletions functions/_pure_is_inside_container.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ function _pure_is_inside_container \
--argument-names cgroup_namespace
set --query cgroup_namespace[1]; or set cgroup_namespace /proc/1/cgroup

set --local success 0
if test -n "$container"
return $success
end

set --local os_name (uname -s)
if test $os_name = Linux
if _pure_detect_container_by_pid_method
if set --query pure_enable_container_detection; and test "$pure_enable_container_detection" = true
set --local success 0
if test -n "$container"
return $success
end

if _pure_detect_container_by_cgroup_method $cgroup_namespace
return $success
set --local os_name (uname -s)
# echo $os_name
if test $os_name = Linux
if _pure_detect_container_by_cgroup_method $cgroup_namespace
return $success
end

if _pure_detect_container_by_pid_method
return $success
end
end
end

set --local failure 1
return $failure
set --local failure 1
return $failure
end
end
6 changes: 6 additions & 0 deletions tests/_pure.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,9 @@ end; setup
echo $pure_enable_single_line_prompt
) = false

@test "configure: pure_enable_container_detection" (
set --erase pure_enable_container_detection
source (dirname (status filename))/../conf.d/pure.fish
echo $pure_enable_container_detection
) = true

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
42 changes: 35 additions & 7 deletions tests/_pure_is_inside_container.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,72 @@ function setup
end
setup

function before_each
function cleanup_detection_methods
functions --erase _pure_detect_container_by_pid_method
functions --erase _pure_detect_container_by_cgroup_method
functions --erase uname
set --global container ""
end

function cleanup_spy
functions --erase uname
end

function teardown
rm -rf \
$namespace \
$cgroup_namespace
set --erase cgroup_namespace
set --erase namespace
set --erase called
end

@test "pure_enable_container_detection: feature is disabled" (
set --universal pure_enable_container_detection false
set --universal called "never"
function uname; set called once; echo "fake-os" ; end # spy

_pure_is_inside_container
echo $called
) = never

@test "pure_enable_container_detection: feature is enabled" (
set --universal pure_enable_container_detection true
set --universal called "never"
function uname; set called once; echo "fake-os" ; end # spy

_pure_is_inside_container
echo $called
) = once

cleanup_spy
@test "_pure_is_inside_container: true for Github Action" (
set --universal pure_enable_container_detection true

_pure_is_inside_container
) $status -eq $SUCCESS

@test "_pure_is_inside_container: detect with $container variable" (
set --universal pure_enable_container_detection true
set --global container "fake"

_pure_is_inside_container
) $status -eq $SUCCESS

cleanup_detection_methods
if test (uname -s) = Linux
before_each
@test "_pure_is_inside_container: detect with pid method" (
function _pure_detect_container_by_pid_method; echo "called: "(status function); end # spy
set --universal pure_enable_container_detection true
function _pure_detect_container_by_cgroup_method; false; end # spy
function _pure_detect_container_by_pid_method; echo "called: "(status function); end # spy

_pure_is_inside_container
_pure_is_inside_container
) = "called: _pure_detect_container_by_pid_method"
end

cleanup_detection_methods
if test (uname -s) = Linux
before_each
@test "_pure_is_inside_container: detect with cgroup method" (
function _pure_detect_container_by_pid_method; false; end # spy
set --universal pure_enable_container_detection true
function _pure_detect_container_by_cgroup_method; echo "called: "(status function); end # spy

_pure_is_inside_container
Expand Down
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