Skip to content
Open
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
15 changes: 14 additions & 1 deletion entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When $0 is not the script - bash -s < entry.sh job-42 /var/base, or a wrapper doing bash -c 'source ./entry.sh' job-42 ... - $0 is bash, [ -f bash ] is false and command -v bash succeeds, so self becomes /bin (or /usr/bin on Linux) and judges is invoked as --lib /bin/lib /bin/judges. The command -v judges check 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; }.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set -e turns a failed cd into a bare exit 1 with no output: run with $0=/mnt/job/entry.sh once the job volume is unmounted, or when the directory loses +x, and the swarm run fails with an empty log - judges never starts and nothing says why. Fix: self=$(cd "$(dirname "${self}")" && pwd -P) || { echo >&2 "Cannot enter '$(dirname "${self}")'"; exit 1; }.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Clear CDPATH before resolving the script directory. With an exported CDPATH=. and a relative invocation such as bash swarm/entry.sh job-42 /existing/home, cd swarm prints its destination before pwd -P, so self contains two lines and both paths passed to judges are invalid; a CDPATH pointing elsewhere can also select a different swarm directory. Unset CDPATH inside this command substitution before calling cd, and add a regression test invoking the script through a relative subdirectory with CDPATH set.


judges update --summary --max-cycles=3 --no-log \
--option "id=${id}" --lib "${self}/lib" "${self}/judges" "${home}/base.fb"
72 changes: 64 additions & 8 deletions test/test_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'fileutils'
require 'open3'
require 'pathname'
require 'tmpdir'
require_relative 'test__helper'

Expand All @@ -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

Expand All @@ -31,6 +30,43 @@ def assert_real_path_used(passed)
assert_equal(File.expand_path('../judges', __dir__), passed[passed.index('--lib') + 2])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passed.index('--lib') is nil when the flag is dropped or renamed, so the regression raises NoMethodError: undefined method '+' for nil instead of naming the problem - both assertions here do it. Guard first: i = passed.index('--lib'); refute_nil(i, "--lib not passed: #{passed.inspect}") and index from i.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch the guard exists for is untested: a copy of entry.sh in the cwd and on PATH. An implementation that always prefers the PATH lookup (resolved=$(command -v -- "${self}") || true; self=${resolved:-$0}) passes this test (entry.sh only on PATH) and the test at line 58 (entry.sh only in cwd), yet resolves the wrong directory whenever both exist. Add that case here, with distinct lib/ directories for each copy.


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'))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test cannot fail for the behaviour it names: bin, the only PATH entry prepended in run_entry (line 97), contains just judges, so command -v entry.sh never succeeds here and the [ ! -f "${self}" ] guard at entry.sh:33 is irrelevant - deleting that guard keeps this test green. To lock the rule, put a second entry.sh with its own lib/ inside bin and assert the cwd copy's directory wins (bash opens the literal name first, so the PATH copy is never executed).

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])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test creates judges/ (line 61) but asserts only the --lib value, so the second path the script passes - ${self}/judges from entry.sh:46 - is unverified: a regression passing ./judges or the cwd instead of ${self}/judges stays invisible. Add assert_equal(File.join(File.realpath(dir), 'judges'), passed[passed.index('--lib') + 2]).

end
end

def test_forwards_job_id_to_judges_options
Dir.mktmpdir do |dir|
home = File.join(dir, 'home')
Expand All @@ -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(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The judges stub truncates its output file (File.write at line 16), so a second invocation is invisible: a retry loop, or a pre-flight judges --version added to entry.sh, leaves every test green while only the last call is asserted. Make the stub append (File.open(ENV.fetch('JUDGES_ARGS_FILE'), 'a')) and assert the expected number of invocation blocks.

{ '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; }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stub rejects only -f, so every other GNU-only flag slips through: replacing the readlink call at entry.sh:37 with readlink -e or readlink --canonicalize keeps this test green and re-breaks macOS (issue #298). Reject any option: [ "${1#-}" != "$1" ] && { echo 'readlink: illegal option' >&2; exit 1; }.

exec /usr/bin/readlink "$@"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding /usr/bin/readlink breaks on hosts that ship it as /bin/readlink only (no usr-merge): the stub then exits 127, link in entry.sh is empty, resolution silently stops at the symlink's own directory and the test fails with a wrong path instead of a portability signal. Resolve the real binary once in Ruby (File.realpath('/bin/readlink'), falling back to /usr/bin/readlink) and exec that path in the stub.

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
Loading