#!/bin/bash set -ex # Turn on bash job control mode. This makes the t2usm become a pgrp. # That's important, because if t2usm wants to shut down it, sends # SIGHUP to its whbole process group. set -m rm -rf tmp mkdir tmp cd tmp : "${DGIT_TEST_T2USM_PROGRAM:=../target/debug/tag2upload-service-manager}" mkdir scratch archive fake_https_dir="$PWD" repo_url="file://${fake_https_dir}/localhost/repo" test_files=../src/test hook_body_file=$test_files/hook_body_gitlab.json tag_ref=$(jq <$hook_body_file -r .ref) tag_name=${tag_ref#refs/tags/} start_t2usm () { trap 'kill -9 $pid; exit 16' 0 rm -f port mkfifo -m 600 port exec 3<>port $DGIT_TEST_T2USM_PROGRAM -c Example.toml \ -C ' rocket.port = 0 [files] scratch_dir = "scratch" port_report_file = "port" archive_dir = "archive" [timeouts] socket_stat_interval = "30 s" [testing] fake_https_dir = "'"$fake_https_dir"'" time_offset = -'$rewind_time' [[t2u.forges]] kind = "gitlab" host = "localhost" allow = ["127.0.0.1/32"] ' & pid=$! exec 4hook.json curl -d @hook.json http://127.0.0.1:"$port"/hook/gitlab \ | tee hook.response grep 'job received' hook.response } setup_api_response () { tag_name_urlenc=$(jq -nr '"'"$tag_name"'" | @uri') api_path=localhost/api/v4/projects/42/repository/tags/$tag_name_urlenc mkdir -p "${api_path%/*}" jq <$test_files/tag_api_resp.json >"$api_path" ' . ' } setup_project_repo () { tar --no-same-owner -axf "$test_files"/dgit-test-dummy.git.tar.xz mkdir -p localhost mv dgit-test-dummy.git localhost/repo } calculate_time_offset () { tag_date=$( set -e cd localhost/repo git for-each-ref --'format=%(taggerdate:raw)' "[r]efs/tags/$tag_name" ) tag_date=${tag_date%% *} now_date=$(date +%s) rewind_time=$(( now_date - tag_date - 1000 )) } setup_project_repo calculate_time_offset start_t2usm send_webhook 40 stop_t2usm_wait start_t2usm setup_api_response send_webhook 42 mkfifo -m600 nc-w mkfifo -m600 nc-r exec 3<>nc-w exec 4<>nc-r nc.openbsd -U o2m.s nc-r & exec 5nc-w exec 3<&- exec 4<&- tx () { echo >&6 "$1" } expect () { read -r <&5 m test "$m" = "$1" } expect "t2u-manager-ready" tx "t2u-oracle-version 1" tx "worker-id test-worker" expect "ayt" while true; do tx "ack" read -r <&5 m case "$m" in ayt) continue;; 'job '*) break;; *) echo >&2 "wrong: $m"; exit 16;; esac done test "${m#job }" = "$repo_url" mkfifo -m600 forever for _attempt in 1 2; do # This shouldn't kill the manager yet! kill $pid # But this should get ECONNREFUSED, or an quickly-dropped connectdion nc.openbsd <>forever -U o2m.s ||: done tx "message Simulation complete!" tx "uploaded" # Now it should be happy wait_for_t2usm_stop echo ok.