Skip to content

Commit

Permalink
Merge branch 'test-script-cleanups' of https://github.com/rhansen/pyt…
Browse files Browse the repository at this point in the history
…hon-gitlab into rhansen-test-script-cleanups
  • Loading branch information
Gauvain Pocentek committed Feb 1, 2016
2 parents 1b5c8f1 + 0024552 commit 01e7c06
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 127 deletions.
121 changes: 97 additions & 24 deletions tools/build_test_env.sh
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
# Copyright (C) 2016 Gauvain Pocentek <gauvain@pocentek.net>
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -14,50 +14,108 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

pecho() { printf %s\\n "$*"; }
log() {
[ "$#" -eq 0 ] || { pecho "$@"; return 0; }
while IFS= read -r log_line || [ -n "${log_line}" ]; do
log "${log_line}"
done
}
error() { log "ERROR: $@" >&2; }
fatal() { error "$@"; exit 1; }
try() { "$@" || fatal "'$@' failed"; }

PY_VER=2
while getopts :p: opt "$@"; do
case $opt in
p)
PY_VER=$OPTARG;;
*)
echo "Unknown option: $opt"
exit 1;;
p) PY_VER=$OPTARG;;
:) fatal "Option -${OPTARG} requires a value";;
'?') fatal "Unknown option: -${OPTARG}";;
*) fatal "Internal error: opt=${opt}";;
esac
done

case $PY_VER in
2) VENV_CMD=virtualenv;;
3) VENV_CMD=pyvenv;;
*)
echo "Wrong python version (2 or 3)"
exit 1;;
*) fatal "Wrong python version (2 or 3)";;
esac

docker run --name gitlab-test --detach --publish 8080:80 --publish 2222:22 gpocentek/test-python-gitlab:latest >/dev/null 2>&1
for req in \
curl \
docker \
"${VENV_CMD}" \
;
do
command -v "${req}" >/dev/null 2>&1 || fatal "${req} is required"
done

VENV=$(pwd)/.venv || exit 1
CONFIG=/tmp/python-gitlab.cfg

cleanup() {
rm -f "${CONFIG}"
log "Stopping gitlab-test docker container..."
docker stop gitlab-test >/dev/null &
docker_stop_pid=$!
log "Waiting for gitlab-test docker container to exit..."
docker wait gitlab-test >/dev/null
wait "${docker_stop_pid}"
log "Removing gitlab-test docker container..."
docker rm gitlab-test >/dev/null
log "Deactivating Python virtualenv..."
command -v deactivate >/dev/null 2>&1 && deactivate || true
log "Deleting python virtualenv..."
rm -rf "$VENV"
log "Done."
}
[ -z "${BUILD_TEST_ENV_AUTO_CLEANUP+set}" ] || {
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
}

try docker run --name gitlab-test --detach --publish 8080:80 \
--publish 2222:22 gpocentek/test-python-gitlab:latest >/dev/null

LOGIN='root'
PASSWORD='5iveL!fe'
CONFIG=/tmp/python-gitlab.cfg
GITLAB() { gitlab --config-file "$CONFIG" "$@"; }
GREEN='\033[0;32m'
NC='\033[0m'
OK="echo -e ${GREEN}OK${NC}"
OK() { printf "${GREEN}OK${NC}\\n"; }
testcase() {
testname=$1; shift
testscript=$1; shift
printf %s "Testing ${testname}... "
eval "${testscript}" || fatal "test failed"
OK
}

echo -n "Waiting for gitlab to come online... "
log "Waiting for gitlab to come online... "
I=0
while :; do
sleep 5
curl -s http://localhost:8080/users/sign_in 2>/dev/null | grep -q "GitLab Community Edition" && break
let I=I+5
[ $I -eq 120 ] && exit 1
sleep 1
docker top gitlab-test >/dev/null 2>&1 || fatal "docker failed to start"
sleep 4
curl -s http://localhost:8080/users/sign_in 2>/dev/null \
| grep -q "GitLab Community Edition" && break
I=$((I+5))
[ "$I" -lt 120 ] || fatal "timed out"
done
sleep 5
$OK

# Get the token
TOKEN=$(curl -s http://localhost:8080/api/v3/session \
-X POST \
--data "login=$LOGIN&password=$PASSWORD" \
| python -c 'import sys, json; print(json.load(sys.stdin)["private_token"])')
log "Getting GitLab token..."
TOKEN_JSON=$(
try curl -s http://localhost:8080/api/v3/session \
-X POST \
--data "login=$LOGIN&password=$PASSWORD"
) || exit 1
TOKEN=$(
pecho "${TOKEN_JSON}" |
try python -c \
'import sys, json; print(json.load(sys.stdin)["private_token"])'
) || exit 1

cat > $CONFIG << EOF
[global]
Expand All @@ -69,5 +127,20 @@ url = http://localhost:8080
private_token = $TOKEN
EOF

echo "Config file content ($CONFIG):"
cat $CONFIG
log "Config file content ($CONFIG):"
log <$CONFIG

log "Creating Python virtualenv..."
try "$VENV_CMD" "$VENV"
. "$VENV"/bin/activate || fatal "failed to activate Python virtual environment"

log "Installing dependencies into virtualenv..."
try pip install -rrequirements.txt

log "Installing into virtualenv..."
try pip install -e .

log "Pausing to give GitLab some time to finish starting up..."
sleep 20

log "Test environment initialized."
147 changes: 67 additions & 80 deletions tools/functional_tests.sh
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
# Copyright (C) 2015 Gauvain Pocentek <gauvain@pocentek.net>
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -14,82 +14,69 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

cleanup() {
rm -f /tmp/python-gitlab.cfg
docker kill gitlab-test >/dev/null 2>&1
docker rm gitlab-test >/dev/null 2>&1
deactivate || true
rm -rf $VENV
}
trap cleanup EXIT

setenv_script=$(dirname $0)/build_test_env.sh

. $setenv_script "$@"

CONFIG=/tmp/python-gitlab.cfg
GITLAB="gitlab --config-file $CONFIG"
GREEN='\033[0;32m'
NC='\033[0m'
OK="echo -e ${GREEN}OK${NC}"

VENV=$(pwd)/.venv

$VENV_CMD $VENV
. $VENV/bin/activate
pip install -rrequirements.txt
pip install -e .

# NOTE(gpocentek): the first call might fail without a little delay
sleep 20

set -e

echo -n "Testing project creation... "
PROJECT_ID=$($GITLAB project create --name test-project1 | grep ^id: | cut -d' ' -f2)
$GITLAB project list | grep -q test-project1
$OK

echo -n "Testing project update... "
$GITLAB project update --id $PROJECT_ID --description "My New Description"
$OK

echo -n "Testing user creation... "
USER_ID=$($GITLAB user create --email fake@email.com --username user1 --name "User One" --password fakepassword | grep ^id: | cut -d' ' -f2)
$OK

echo -n "Testing verbose output... "
$GITLAB -v user list | grep -q avatar-url
$OK

echo -n "Testing CLI args not in output... "
$GITLAB -v user list | grep -qv config-file
$OK

echo -n "Testing adding member to a project... "
$GITLAB project-member create --project-id $PROJECT_ID --user-id $USER_ID --access-level 40 >/dev/null 2>&1
$OK

echo -n "Testing file creation... "
$GITLAB project-file create --project-id $PROJECT_ID --file-path README --branch-name master --content "CONTENT" --commit-message "Initial commit" >/dev/null 2>&1
$OK

echo -n "Testing issue creation... "
ISSUE_ID=$($GITLAB project-issue create --project-id $PROJECT_ID --title "my issue" --description "my issue description" | grep ^id: | cut -d' ' -f2)
$OK

echo -n "Testing note creation... "
$GITLAB project-issue-note create --project-id $PROJECT_ID --issue-id $ISSUE_ID --body "the body" >/dev/null 2>&1
$OK

echo -n "Testing branch creation... "
$GITLAB project-branch create --project-id $PROJECT_ID --branch-name branch1 --ref master >/dev/null 2>&1
$OK

echo -n "Testing branch deletion... "
$GITLAB project-branch delete --project-id $PROJECT_ID --name branch1 >/dev/null 2>&1
$OK

echo -n "Testing project deletion... "
$GITLAB project delete --id $PROJECT_ID
$OK
setenv_script=$(dirname "$0")/build_test_env.sh || exit 1
BUILD_TEST_ENV_AUTO_CLEANUP=true
. "$setenv_script" "$@" || exit 1

testcase "project creation" '
OUTPUT=$(try GITLAB project create --name test-project1) || exit 1
PROJECT_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d" " -f2)
OUTPUT=$(try GITLAB project list) || exit 1
pecho "${OUTPUT}" | grep -q test-project1
'

testcase "project update" '
GITLAB project update --id "$PROJECT_ID" --description "My New Description"
'

testcase "user creation" '
OUTPUT=$(GITLAB user create --email fake@email.com --username user1 \
--name "User One" --password fakepassword)
'
USER_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d' ' -f2)

testcase "verbose output" '
OUTPUT=$(try GITLAB -v user list) || exit 1
pecho "${OUTPUT}" | grep -q avatar-url
'

testcase "CLI args not in output" '
OUTPUT=$(try GITLAB -v user list) || exit 1
pecho "${OUTPUT}" | grep -qv config-file
'

testcase "adding member to a project" '
GITLAB project-member create --project-id "$PROJECT_ID" \
--user-id "$USER_ID" --access-level 40 >/dev/null 2>&1
'

testcase "file creation" '
GITLAB project-file create --project-id "$PROJECT_ID" \
--file-path README --branch-name master --content "CONTENT" \
--commit-message "Initial commit" >/dev/null 2>&1
'

testcase "issue creation" '
OUTPUT=$(GITLAB project-issue create --project-id "$PROJECT_ID" \
--title "my issue" --description "my issue description")
'
ISSUE_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d' ' -f2)

testcase "note creation" '
GITLAB project-issue-note create --project-id "$PROJECT_ID" \
--issue-id "$ISSUE_ID" --body "the body" >/dev/null 2>&1
'

testcase "branch creation" '
GITLAB project-branch create --project-id "$PROJECT_ID" \
--branch-name branch1 --ref master >/dev/null 2>&1
'

testcase "branch deletion" '
GITLAB project-branch delete --project-id "$PROJECT_ID" \
--name branch1 >/dev/null 2>&1
'

testcase "project deletion" '
GITLAB project delete --id "$PROJECT_ID"
'
28 changes: 5 additions & 23 deletions tools/py_functional_tests.sh
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
# Copyright (C) 2015 Gauvain Pocentek <gauvain@pocentek.net>
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -14,26 +14,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

cleanup() {
rm -f /tmp/python-gitlab.cfg
docker kill gitlab-test >/dev/null 2>&1
docker rm gitlab-test >/dev/null 2>&1
deactivate || true
rm -rf $VENV
}
trap cleanup EXIT
setenv_script=$(dirname "$0")/build_test_env.sh || exit 1
BUILD_TEST_ENV_AUTO_CLEANUP=true
. "$setenv_script" "$@" || exit 1

setenv_script=$(dirname $0)/build_test_env.sh

. $setenv_script "$@"

VENV=$(pwd)/.venv

$VENV_CMD $VENV
. $VENV/bin/activate
pip install -rrequirements.txt
pip install -e .

sleep 20

python $(dirname $0)/python_test.py
try python "$(dirname "$0")"/python_test.py

0 comments on commit 01e7c06

Please sign in to comment.