eclipse/test/eclipse.bats

215 lines
4.8 KiB
Plaintext
Raw Permalink Normal View History

2024-01-24 04:38:37 +00:00
# Copyright (C) 2024 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
bats_require_minimum_version 1.7.0
bats_load_library bats-support
bats_load_library bats-assert
bats_load_library bats-file
teardown() {
if [[ -n "${httpd_pid:-}" ]]; then
kill "$httpd_pid"
wait || true
fi
if [[ -n "${tmpdir:-}" ]]; then
rm -rf -- "$tmpdir"
fi
}
run_httpd() {
eclipse-httpd --config="$tmpdir/leader.yml" --socket=tcp:127.0.0.1:0 &>"$tmpdir/httpd.log" &
httpd_pid=$!
echo "httpd_pid: $httpd_pid"
httpd_port=''
while [[ -z "$httpd_port" ]]; do
httpd_port=$(sed -n -E 's/.* Serving on 127\.0\.0\.1:([0-9]+)\.\.\.$/\1/p' <"$tmpdir/httpd.log")
if ! [[ -d "/proc/$httpd_pid" ]]; then
cat "$tmpdir/httpd.log"
return 1
fi
done
echo "httpd_port: $httpd_port"
}
@test "httpd fails if dirs don't exist" {
tmpdir="$(temp_make)"
cat >"$tmpdir/leader.yml" <<-EOF
state_dir: ${tmpdir}/state
cache_dir: ${tmpdir}/cache
actions_dir: ${tmpdir}/actions.d
cache_git_for: 5s
parallel_downloads: 1
EOF
r=0
run_httpd || r=$?
[[ $r != 0 ]]
}
@test "httpd starts" {
tmpdir="$(temp_make)"
cat >"$tmpdir/leader.yml" <<-EOF
state_dir: ${tmpdir}/state
cache_dir: ${tmpdir}/cache
actions_dir: ${tmpdir}/actions.d
cache_git_for: 5s
parallel_downloads: 1
EOF
mkdir "$tmpdir"/{state,cache,actions.d}
run_httpd
}
@test "runs jobs" {
tmpdir="$(temp_make)"
cat >"$tmpdir/leader.yml" <<-EOF
state_dir: ${tmpdir}/state
cache_dir: ${tmpdir}/cache
actions_dir: ${tmpdir}/actions.d
cache_git_for: 5s
parallel_downloads: 1
EOF
mkdir "$tmpdir"/{state,cache,actions.d}
cat >"$tmpdir/actions.d/test.yml" <<-EOF
- triggers:
- repo_url: "${tmpdir}/upstream"
rev: HEAD
run: "echo foo"
EOF
git init "$tmpdir/upstream"
pushd "$tmpdir/upstream"
echo 'Hello, World!' >hello.txt
git add hello.txt
git commit -m 'initial commit'
hash=$(git rev-parse --verify HEAD)
popd
run_httpd
cat >"$tmpdir/follower.yml" <<-EOF
leader_url: http://127.0.0.1:${httpd_port}
EOF
run curl --no-progress-meter "http://127.0.0.1:${httpd_port}/jobs/"
assert_success
assert_output 'null'
eclipse-trigger --config="$tmpdir/leader.yml"
run curl --no-progress-meter "http://127.0.0.1:${httpd_port}/jobs/"
assert_success
jq <<<"$output" >"$tmpdir/actual.json"
jq >"$tmpdir/expected.json" <<-EOF
[
{
"ID": 1,
"TriggerExpressions": [
"{\"repo_url\":\"${tmpdir}/upstream\",\"rev\":\"HEAD\"}"
],
"TriggerValues": [
"${hash}"
],
"Command": "echo foo",
"Status": "new",
"CreatedAt": "2024-01-23T21:16:13-07:00",
"UpdatedAt": "2024-01-23T21:16:13-07:00"
}
]
EOF
sed -i -e /CreatedAt/d -e /UpdatedAt/d -- "$tmpdir/actual.json" "$tmpdir/expected.json"
diff -u "$tmpdir/expected.json" "$tmpdir/actual.json"
job_url=$(eclipse-pick --config="$tmpdir/follower.yml")
assert_equal "$job_url" "http://127.0.0.1:${httpd_port}/jobs/1/"
eclipse-run --config="$tmpdir/follower.yml" -- "$job_url"
run curl --no-progress-meter "http://127.0.0.1:${httpd_port}/jobs/"
assert_success
jq <<<"$output" >"$tmpdir/actual.json"
jq >"$tmpdir/expected.json" <<-EOF
[
{
"ID": 1,
"TriggerExpressions": [
"{\"repo_url\":\"${tmpdir}/upstream\",\"rev\":\"HEAD\"}"
],
"TriggerValues": [
"${hash}"
],
"Command": "echo foo",
"Status": "succeeded",
"CreatedAt": "2024-01-23T21:16:13-07:00",
"UpdatedAt": "2024-01-23T21:16:13-07:00"
}
]
EOF
sed -i -e /CreatedAt/d -e /UpdatedAt/d -- "$tmpdir/actual.json" "$tmpdir/expected.json"
diff -u "$tmpdir/expected.json" "$tmpdir/actual.json"
echo foo >"$tmpdir/expected.txt"
diff -u "$tmpdir/expected.txt" "$tmpdir/state/jobfiles/1/log.txt"
}
2024-01-25 09:08:33 +00:00
@test "pick waits for jobs" {
tmpdir="$(temp_make)"
cat >"$tmpdir/leader.yml" <<-EOF
state_dir: ${tmpdir}/state
cache_dir: ${tmpdir}/cache
actions_dir: ${tmpdir}/actions.d
cache_git_for: 5s
parallel_downloads: 1
EOF
mkdir "$tmpdir"/{state,cache,actions.d}
cat >"$tmpdir/actions.d/test.yml" <<-EOF
- triggers:
- repo_url: "${tmpdir}/upstream"
rev: HEAD
run: "echo foo"
EOF
git init "$tmpdir/upstream"
pushd "$tmpdir/upstream"
echo 'Hello, World!' >hello.txt
git add hello.txt
git commit -m 'initial commit'
hash=$(git rev-parse --verify HEAD)
popd
run_httpd
cat >"$tmpdir/follower.yml" <<-EOF
leader_url: http://127.0.0.1:${httpd_port}
EOF
run curl --no-progress-meter "http://127.0.0.1:${httpd_port}/jobs/"
assert_success
assert_output 'null'
echo 'starting pick...'
eclipse-pick --config="$tmpdir/follower.yml" >"$tmpdir/output.txt" &
pick_pid=$!
echo 'sleeping...'
sleep 2
2024-01-25 09:08:33 +00:00
echo 'triggering job...'
eclipse-trigger --config="$tmpdir/leader.yml"
echo 'waiting for pick to notice...'
wait "$pick_pid"
output=$(cat "$tmpdir/output.txt")
assert_output "http://127.0.0.1:${httpd_port}/jobs/1/"
}