Skip to content

Instantly share code, notes, and snippets.

@yyogo
Last active January 30, 2022 16:15
Show Gist options
  • Save yyogo/62d41e0013fd38e8cfce4cc5cae04b06 to your computer and use it in GitHub Desktop.
Save yyogo/62d41e0013fd38e8cfce4cc5cae04b06 to your computer and use it in GitHub Desktop.
script for testing Syncthing issue #7924
#!/bin/bash
set -e
# this script starts two ST instances, connects them, adds a shared folder,
# syncs a file, chmods it, and checks that the new permissions are synced
# within a reasonable time frame.
# Set to your local ST build
SYNCTHING=../bin/syncthing
API_KEY=test-xxx
syncthing_cli() {
port=$1; shift
$SYNCTHING cli --gui-address="localhost:$port" --gui-apikey="$API_KEY" "$@"
}
syncthing_serve() {
home_dir=$1
port=$2
rm -rf "${home_dir}"
$SYNCTHING serve \
--home="$home_dir" \
--no-upgrade \
--no-default-folder \
--gui-address="http://localhost:$port" \
--gui-apikey=$API_KEY \
--no-browser \
--no-restart &
}
echo "Starting instances"
FOO_PORT=8183
BAR_PORT=8184
syncthing_serve ./foo-home $FOO_PORT >/dev/null
syncthing_serve ./bar-home $BAR_PORT >/dev/null
trap "echo 'killing instances'; syncthing_cli $FOO_PORT operations shutdown; syncthing_cli $BAR_PORT operations shutdown" EXIT SIGINT SIGTERM ERR
sleep 3
foo_id=$(syncthing_cli $FOO_PORT show system | jq -r '.myID')
bar_id=$(syncthing_cli $BAR_PORT show system | jq -r '.myID')
make_device() {
name=$1; shift;
port=$1; shift;
id=$(syncthing_cli "$port" show system | jq -r '.myID')
addrs=$(syncthing_cli "$port" show system | jq -r '.connectionServiceStatus|to_entries[]|select(.key|startswith("tcp:")).value.lanAddresses')
cat <<EOF
{
"deviceID": "$id",
"name": "$name",
"addresses": $addrs,
"autoAcceptFolders": true
}
EOF
}
echo "connecting"
syncthing_cli $FOO_PORT config devices add-json "$(make_device bar $BAR_PORT)"
syncthing_cli $BAR_PORT config devices add-json "$(make_device foo $FOO_PORT)"
echo "adding folders"
test_foo=$(pwd)/test_foo
test_bar=$(pwd)/test_bar
rm -rf "$test_foo" "$test_bar"
make_folder() {
name=$1; shift
path=$1; shift
remote=$1; shift
cat <<EOF
{
"id": "$name",
"label": "$name",
"path": "$path",
"filesystemType": "basic",
"type": "sendreceive",
"devices": [
{
"deviceID": "$remote"
}
],
"fsWatcherEnabled": true,
"fsWatcherDelayS": 1
}
EOF
}
syncthing_cli $FOO_PORT config folders add-json "$(make_folder test "$test_foo" "$bar_id")"
syncthing_cli $BAR_PORT config folders add-json "$(make_folder test "$test_bar" "$foo_id")"
echo "syncing file..."
umask 022
echo hello > "$test_foo/hello.txt"
while ! [[ -f "$test_bar/hello.txt" ]]; do
sleep 1
done
echo "changing permissions"
chmod 777 "$test_foo/hello.txt"
stat -f '%N: %p' "$test_foo/hello.txt" "$test_bar/hello.txt"
echo "waiting a few seconds to allow sync"
sleep 10
stat -f '%N: %p' "$test_foo/hello.txt" "$test_bar/hello.txt"
diff <(stat -f '%p' "$test_foo/hello.txt") <(stat -f '%p' "$test_bar/hello.txt") && echo success || echo fail
@yyogo
Copy link
Author

yyogo commented Jan 30, 2022

successful output:

Starting instances
connecting
adding folders
syncing file...
changing permissions
test_foo/hello.txt: 100777
test_bar/hello.txt: 100644
waiting a few seconds to allow sync
test_foo/hello.txt: 100777
test_bar/hello.txt: 100777
success
killing instances

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment