Repository navigation
fix(#298): resolve entry.sh path without GNU readlink #324
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,20 @@ if ! command -v judges &>/dev/null; then | |
| exit 1 | ||
| fi | ||
|
|
||
| self=$(dirname "$(readlink -f "$0")") | ||
| self=$0 | ||
| # bash passes a slashless $0 when the script is found via PATH; resolve it | ||
| # unless a same-named file in cwd is what bash actually opened. | ||
| if [ ! -f "${self}" ] && resolved=$(command -v -- "${self}"); then | ||
| self=${resolved} | ||
| fi | ||
| while [ -L "${self}" ]; do | ||
| link=$(readlink "${self}") | ||
| case "${link}" in | ||
| /*) self=${link} ;; | ||
| *) self=$(dirname "${self}")/${link} ;; | ||
| esac | ||
| done | ||
| self=$(cd "$(dirname "${self}")" && pwd -P) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Clear CDPATH before resolving the script directory. With an exported |
||
|
|
||
| judges update --summary --max-cycles=3 --no-log \ | ||
| --option "id=${id}" --lib "${self}/lib" "${self}/judges" "${home}/base.fb" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| require 'fileutils' | ||
| require 'open3' | ||
| require 'pathname' | ||
| require 'tmpdir' | ||
| require_relative 'test__helper' | ||
|
|
||
|
|
@@ -20,9 +21,7 @@ def test_resolves_real_path_when_invoked_via_symlink | |
| home = File.join(dir, 'home') | ||
| FileUtils.mkdir_p(home) | ||
|
|
||
| assert_real_path_used( | ||
| run_entry(judges_stub(dir), home, File.join(dir, 'args.txt'), script: symlink_entry(dir)) | ||
| ) | ||
| assert_real_path_used(run_entry(judges_stub(dir), home, File.join(dir, 'args.txt'), script: symlink_entry(dir))) | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -31,6 +30,43 @@ def assert_real_path_used(passed) | |
| assert_equal(File.expand_path('../judges', __dir__), passed[passed.index('--lib') + 2]) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| end | ||
|
|
||
| def test_resolves_relative_symlink_chain_without_gnu_readlink | ||
| Dir.mktmpdir do |dir| | ||
| FileUtils.mkdir_p(File.join(dir, 'home')) | ||
| bin = judges_stub(dir) | ||
| bsd_readlink_stub(bin) | ||
| script = relative_chain_entry(dir) | ||
| passed = run_entry(bin, File.join(dir, 'home'), File.join(dir, 'args.txt'), script: script, chdir: dir) | ||
|
|
||
| assert_real_path_used(passed) | ||
| end | ||
| end | ||
|
|
||
| def test_resolves_slashless_script_name_found_through_path | ||
| Dir.mktmpdir do |dir| | ||
| bin = judges_stub(dir) | ||
| FileUtils.ln_s(File.expand_path('../entry.sh', __dir__), File.join(bin, 'entry.sh')) | ||
| home = File.join(dir, 'home') | ||
| cwd = File.join(dir, 'cwd') | ||
| FileUtils.mkdir_p([home, cwd]) | ||
| passed = run_entry(bin, home, File.join(dir, 'args.txt'), script: %w[bash entry.sh], chdir: cwd) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The branch the guard exists for is untested: a copy of |
||
|
|
||
| assert_real_path_used(passed) | ||
| end | ||
| end | ||
|
|
||
| def test_keeps_own_directory_when_slashless_script_is_not_in_path | ||
| Dir.mktmpdir do |dir| | ||
| FileUtils.cp(File.expand_path('../entry.sh', __dir__), File.join(dir, 'entry.sh')) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test cannot fail for the behaviour it names: |
||
| FileUtils.mkdir_p(%w[lib judges home].map { |name| File.join(dir, name) }) | ||
| passed = run_entry( | ||
| judges_stub(dir), "#{dir}/home", "#{dir}/args.txt", script: %w[bash entry.sh], chdir: dir | ||
| ) | ||
|
|
||
| assert_equal(File.join(File.realpath(dir), 'lib'), passed[passed.index('--lib') + 1]) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test creates |
||
| end | ||
| end | ||
|
|
||
| def test_forwards_job_id_to_judges_options | ||
| Dir.mktmpdir do |dir| | ||
| home = File.join(dir, 'home') | ||
|
|
@@ -56,21 +92,41 @@ def judges_stub(dir) | |
| bin | ||
| end | ||
|
|
||
| def run_entry(bin, home, args, script: './entry.sh') | ||
| def run_entry(bin, home, args, script: './entry.sh', chdir: Dir.pwd) | ||
| stdout, stderr, status = Open3.capture3( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| { 'PATH' => "#{bin}:#{ENV.fetch('PATH')}", 'JUDGES_ARGS_FILE' => args }, | ||
| script, 'job-42', home | ||
| *script, 'job-42', home, chdir: chdir | ||
| ) | ||
|
|
||
| assert_predicate(status, :success?, "#{stdout}\n#{stderr}") | ||
| File.readlines(args, chomp: true) | ||
| end | ||
|
|
||
| def symlink_entry(dir) | ||
| link_dir = File.join(dir, 'linkdir') | ||
| FileUtils.mkdir_p(link_dir) | ||
| link = File.join(link_dir, 'entry.sh') | ||
| link = File.join(dir, 'linkdir', 'entry.sh') | ||
| FileUtils.mkdir_p(File.dirname(link)) | ||
| FileUtils.ln_s(File.expand_path('../entry.sh', __dir__), link) | ||
| link | ||
| end | ||
|
|
||
| def bsd_readlink_stub(bin) | ||
| stub = File.join(bin, 'readlink') | ||
| File.write(stub, <<~SH) | ||
| #!/bin/sh | ||
| [ "$1" = '-f' ] && { echo 'readlink: illegal option -- f' >&2; exit 1; } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The stub rejects only |
||
| exec /usr/bin/readlink "$@" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding |
||
| SH | ||
| FileUtils.chmod(0o755, stub) | ||
| end | ||
|
|
||
| def relative_chain_entry(dir) | ||
| first = File.join(dir, 'first') | ||
| second = File.join(dir, 'second') | ||
| FileUtils.mkdir_p([first, second]) | ||
| FileUtils.ln_s('../second/entry.sh', File.join(first, 'entry.sh')) | ||
| real = File.realpath(second) | ||
| rel = Pathname.new(File.expand_path('../entry.sh', __dir__)).relative_path_from(Pathname.new(real)) | ||
| FileUtils.ln_s(rel, File.join(second, 'entry.sh')) | ||
| File.join(first, 'entry.sh') | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
$0is not the script -bash -s < entry.sh job-42 /var/base, or a wrapper doingbash -c 'source ./entry.sh' job-42 ...-$0isbash,[ -f bash ]is false andcommand -v bashsucceeds, soselfbecomes/bin(or/usr/binon Linux) and judges is invoked as--lib /bin/lib /bin/judges. Thecommand -v judgescheck at line 25 already passed, so the job dies inside judges with a message that never mentions$0. Minimal fix, after the loop:[ -d "${self}/judges" ] || { echo >&2 "Cannot locate the swarm directory for '$0' (resolved to '${self}')"; exit 1; }.