# Shell library for running git.liw.fi ACL tests. # We create ssh keys in the test suite. The temporary directory, # $DATADIR, must be made inaccessible to others before that happens. # We do it here, so it gets done before any of the code from an # IMPLEMENTS actually runs. chmod 0700 "$DATADIR" # This is handy for giving an error message and aborting. die() { echo "$@" 1>&2 exit 1 } # Attempt to run something, which may fail. Store the stdout, # stderr, and exit code in $DATADIR. attempt() { if "$@" > "$DATADIR/attempt.stdout" 2> "$DATADIR/attempt.stderr" then echo 0 > "$DATADIR/attempt.exit" else echo $? > "$DATADIR/attempt.exit" fi } # Run gitano on the server using a desired ssh key. The key is # either the admin key (i.e., they key of whoever invoked the # test suite), or a test key we've created in $DATADIR. run_gitano_as() { local keyname="$1" shift if [ "$keyname" = "admin" ] then ssh "$GITANO@$GITHOST" "$@" else SSH_AUTH_SOCK= ssh -F "ssh.conf" -i "$DATADIR/$keyname.key" \ "$GITANO@$GITHOST" "$@" fi } # Does a user exist on the server? user_exists() { ssh "$GITANO@$GITHOST" user | grep "^$1:" } # Create a user, including setting their ssh key. user_add() { run_gitano_as "$1" user add "$2" name foo@example.com run_gitano_as "$1" as "$2" sshkey add somekey < "$DATADIR/$2.key.pub" } # A helper function to do a two-step destruction on gitano. With the # first step, gitano outputs a token, which is the last word on the # last line of the output. The second step has the same command line # as the first step, but with the token appended. two_step() { if "$@" 2> "$DATADIR/temp" then secret=$(awk '{ s = $2 } END { print s }' "$DATADIR/temp") "$@" "$secret" else cat "$DATADIR/temp" 1>&2 return 1 fi } # Remove a user from the server. This is a two-step process. user_del() { two_step run_gitano_as "$1" user del "$2" } # Create a group on the server. group_add() { run_gitano_as "$1" group add "$2" group for testing } # Add a user to a group. group_adduser() { run_gitano_as "$1" group adduser "$2" "$3" } # Remove a group. group_del() { two_step run_gitano_as "$1" group del "$2" } # Does a repository exist? repo_exists() { run_gitano_as "$1" ls | awk -v "r=$2" '$NF == r' | grep . } # Clone repo over ssh. clone_with_ssh() { localdir="$DATADIR/$1/$2" rm -rf "$localdir" mkdir -p "$localdir" url="ssh://$GITANO@$GITHOST/$2" if [ "$1" = admin ] then git clone "$url" "$localdir" else KEYFILE="$DATADIR/$1.key" \ PATH="$SRCDIR:$PATH" git clone "$url" "$localdir" fi } # Push repo over ssh. push_with_ssh() { local user="$1" shift if [ "$user" = admin ] then git push "$@" else KEYFILE="$DATADIR/$user.key" \ PATH="$SRCDIR:$PATH" git push "$@" fi } # Remove a repository from the server. This is a two-step process. destroy_repo() { two_step run_gitano_as "$1" destroy "$2" } # Does cgit show a repository? cgit_shows() { local tempfile="$(mktemp)" wget -q -O- "http://$GITHOST/cgi-bin/cgit/cgit.cgi/$1/" \ > "$tempfile" 2>&1 if grep 'Repository seems to be empty' "$tempfile" then ret=0 elif grep 'Commit message' "$tempfile" then ret=0 else ret=1 fi rm -f "$tempfile" return $ret }