Skip to content

Commit 01e7c06

Browse files
author
Gauvain Pocentek
committed
Merge branch 'test-script-cleanups' of https://github.com/rhansen/python-gitlab into rhansen-test-script-cleanups
2 parents 1b5c8f1 + 0024552 commit 01e7c06

File tree

3 files changed

+169
-127
lines changed

3 files changed

+169
-127
lines changed

tools/build_test_env.sh

+97-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22
# Copyright (C) 2016 Gauvain Pocentek <gauvain@pocentek.net>
33
#
44
# This program is free software: you can redistribute it and/or modify
@@ -14,50 +14,108 @@
1414
# You should have received a copy of the GNU Lesser General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17+
pecho() { printf %s\\n "$*"; }
18+
log() {
19+
[ "$#" -eq 0 ] || { pecho "$@"; return 0; }
20+
while IFS= read -r log_line || [ -n "${log_line}" ]; do
21+
log "${log_line}"
22+
done
23+
}
24+
error() { log "ERROR: $@" >&2; }
25+
fatal() { error "$@"; exit 1; }
26+
try() { "$@" || fatal "'$@' failed"; }
27+
1728
PY_VER=2
1829
while getopts :p: opt "$@"; do
1930
case $opt in
20-
p)
21-
PY_VER=$OPTARG;;
22-
*)
23-
echo "Unknown option: $opt"
24-
exit 1;;
31+
p) PY_VER=$OPTARG;;
32+
:) fatal "Option -${OPTARG} requires a value";;
33+
'?') fatal "Unknown option: -${OPTARG}";;
34+
*) fatal "Internal error: opt=${opt}";;
2535
esac
2636
done
2737

2838
case $PY_VER in
2939
2) VENV_CMD=virtualenv;;
3040
3) VENV_CMD=pyvenv;;
31-
*)
32-
echo "Wrong python version (2 or 3)"
33-
exit 1;;
41+
*) fatal "Wrong python version (2 or 3)";;
3442
esac
3543

36-
docker run --name gitlab-test --detach --publish 8080:80 --publish 2222:22 gpocentek/test-python-gitlab:latest >/dev/null 2>&1
44+
for req in \
45+
curl \
46+
docker \
47+
"${VENV_CMD}" \
48+
;
49+
do
50+
command -v "${req}" >/dev/null 2>&1 || fatal "${req} is required"
51+
done
52+
53+
VENV=$(pwd)/.venv || exit 1
54+
CONFIG=/tmp/python-gitlab.cfg
55+
56+
cleanup() {
57+
rm -f "${CONFIG}"
58+
log "Stopping gitlab-test docker container..."
59+
docker stop gitlab-test >/dev/null &
60+
docker_stop_pid=$!
61+
log "Waiting for gitlab-test docker container to exit..."
62+
docker wait gitlab-test >/dev/null
63+
wait "${docker_stop_pid}"
64+
log "Removing gitlab-test docker container..."
65+
docker rm gitlab-test >/dev/null
66+
log "Deactivating Python virtualenv..."
67+
command -v deactivate >/dev/null 2>&1 && deactivate || true
68+
log "Deleting python virtualenv..."
69+
rm -rf "$VENV"
70+
log "Done."
71+
}
72+
[ -z "${BUILD_TEST_ENV_AUTO_CLEANUP+set}" ] || {
73+
trap cleanup EXIT
74+
trap 'exit 1' HUP INT TERM
75+
}
76+
77+
try docker run --name gitlab-test --detach --publish 8080:80 \
78+
--publish 2222:22 gpocentek/test-python-gitlab:latest >/dev/null
3779

3880
LOGIN='root'
3981
PASSWORD='5iveL!fe'
40-
CONFIG=/tmp/python-gitlab.cfg
82+
GITLAB() { gitlab --config-file "$CONFIG" "$@"; }
4183
GREEN='\033[0;32m'
4284
NC='\033[0m'
43-
OK="echo -e ${GREEN}OK${NC}"
85+
OK() { printf "${GREEN}OK${NC}\\n"; }
86+
testcase() {
87+
testname=$1; shift
88+
testscript=$1; shift
89+
printf %s "Testing ${testname}... "
90+
eval "${testscript}" || fatal "test failed"
91+
OK
92+
}
4493

45-
echo -n "Waiting for gitlab to come online... "
94+
log "Waiting for gitlab to come online... "
4695
I=0
4796
while :; do
48-
sleep 5
49-
curl -s http://localhost:8080/users/sign_in 2>/dev/null | grep -q "GitLab Community Edition" && break
50-
let I=I+5
51-
[ $I -eq 120 ] && exit 1
97+
sleep 1
98+
docker top gitlab-test >/dev/null 2>&1 || fatal "docker failed to start"
99+
sleep 4
100+
curl -s http://localhost:8080/users/sign_in 2>/dev/null \
101+
| grep -q "GitLab Community Edition" && break
102+
I=$((I+5))
103+
[ "$I" -lt 120 ] || fatal "timed out"
52104
done
53105
sleep 5
54-
$OK
55106

56107
# Get the token
57-
TOKEN=$(curl -s http://localhost:8080/api/v3/session \
58-
-X POST \
59-
--data "login=$LOGIN&password=$PASSWORD" \
60-
| python -c 'import sys, json; print(json.load(sys.stdin)["private_token"])')
108+
log "Getting GitLab token..."
109+
TOKEN_JSON=$(
110+
try curl -s http://localhost:8080/api/v3/session \
111+
-X POST \
112+
--data "login=$LOGIN&password=$PASSWORD"
113+
) || exit 1
114+
TOKEN=$(
115+
pecho "${TOKEN_JSON}" |
116+
try python -c \
117+
'import sys, json; print(json.load(sys.stdin)["private_token"])'
118+
) || exit 1
61119

62120
cat > $CONFIG << EOF
63121
[global]
@@ -69,5 +127,20 @@ url = http://localhost:8080
69127
private_token = $TOKEN
70128
EOF
71129

72-
echo "Config file content ($CONFIG):"
73-
cat $CONFIG
130+
log "Config file content ($CONFIG):"
131+
log <$CONFIG
132+
133+
log "Creating Python virtualenv..."
134+
try "$VENV_CMD" "$VENV"
135+
. "$VENV"/bin/activate || fatal "failed to activate Python virtual environment"
136+
137+
log "Installing dependencies into virtualenv..."
138+
try pip install -rrequirements.txt
139+
140+
log "Installing into virtualenv..."
141+
try pip install -e .
142+
143+
log "Pausing to give GitLab some time to finish starting up..."
144+
sleep 20
145+
146+
log "Test environment initialized."

tools/functional_tests.sh

+67-80
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22
# Copyright (C) 2015 Gauvain Pocentek <gauvain@pocentek.net>
33
#
44
# This program is free software: you can redistribute it and/or modify
@@ -14,82 +14,69 @@
1414
# You should have received a copy of the GNU Lesser General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
cleanup() {
18-
rm -f /tmp/python-gitlab.cfg
19-
docker kill gitlab-test >/dev/null 2>&1
20-
docker rm gitlab-test >/dev/null 2>&1
21-
deactivate || true
22-
rm -rf $VENV
23-
}
24-
trap cleanup EXIT
25-
26-
setenv_script=$(dirname $0)/build_test_env.sh
27-
28-
. $setenv_script "$@"
29-
30-
CONFIG=/tmp/python-gitlab.cfg
31-
GITLAB="gitlab --config-file $CONFIG"
32-
GREEN='\033[0;32m'
33-
NC='\033[0m'
34-
OK="echo -e ${GREEN}OK${NC}"
35-
36-
VENV=$(pwd)/.venv
37-
38-
$VENV_CMD $VENV
39-
. $VENV/bin/activate
40-
pip install -rrequirements.txt
41-
pip install -e .
42-
43-
# NOTE(gpocentek): the first call might fail without a little delay
44-
sleep 20
45-
46-
set -e
47-
48-
echo -n "Testing project creation... "
49-
PROJECT_ID=$($GITLAB project create --name test-project1 | grep ^id: | cut -d' ' -f2)
50-
$GITLAB project list | grep -q test-project1
51-
$OK
52-
53-
echo -n "Testing project update... "
54-
$GITLAB project update --id $PROJECT_ID --description "My New Description"
55-
$OK
56-
57-
echo -n "Testing user creation... "
58-
USER_ID=$($GITLAB user create --email fake@email.com --username user1 --name "User One" --password fakepassword | grep ^id: | cut -d' ' -f2)
59-
$OK
60-
61-
echo -n "Testing verbose output... "
62-
$GITLAB -v user list | grep -q avatar-url
63-
$OK
64-
65-
echo -n "Testing CLI args not in output... "
66-
$GITLAB -v user list | grep -qv config-file
67-
$OK
68-
69-
echo -n "Testing adding member to a project... "
70-
$GITLAB project-member create --project-id $PROJECT_ID --user-id $USER_ID --access-level 40 >/dev/null 2>&1
71-
$OK
72-
73-
echo -n "Testing file creation... "
74-
$GITLAB project-file create --project-id $PROJECT_ID --file-path README --branch-name master --content "CONTENT" --commit-message "Initial commit" >/dev/null 2>&1
75-
$OK
76-
77-
echo -n "Testing issue creation... "
78-
ISSUE_ID=$($GITLAB project-issue create --project-id $PROJECT_ID --title "my issue" --description "my issue description" | grep ^id: | cut -d' ' -f2)
79-
$OK
80-
81-
echo -n "Testing note creation... "
82-
$GITLAB project-issue-note create --project-id $PROJECT_ID --issue-id $ISSUE_ID --body "the body" >/dev/null 2>&1
83-
$OK
84-
85-
echo -n "Testing branch creation... "
86-
$GITLAB project-branch create --project-id $PROJECT_ID --branch-name branch1 --ref master >/dev/null 2>&1
87-
$OK
88-
89-
echo -n "Testing branch deletion... "
90-
$GITLAB project-branch delete --project-id $PROJECT_ID --name branch1 >/dev/null 2>&1
91-
$OK
92-
93-
echo -n "Testing project deletion... "
94-
$GITLAB project delete --id $PROJECT_ID
95-
$OK
17+
setenv_script=$(dirname "$0")/build_test_env.sh || exit 1
18+
BUILD_TEST_ENV_AUTO_CLEANUP=true
19+
. "$setenv_script" "$@" || exit 1
20+
21+
testcase "project creation" '
22+
OUTPUT=$(try GITLAB project create --name test-project1) || exit 1
23+
PROJECT_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d" " -f2)
24+
OUTPUT=$(try GITLAB project list) || exit 1
25+
pecho "${OUTPUT}" | grep -q test-project1
26+
'
27+
28+
testcase "project update" '
29+
GITLAB project update --id "$PROJECT_ID" --description "My New Description"
30+
'
31+
32+
testcase "user creation" '
33+
OUTPUT=$(GITLAB user create --email fake@email.com --username user1 \
34+
--name "User One" --password fakepassword)
35+
'
36+
USER_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d' ' -f2)
37+
38+
testcase "verbose output" '
39+
OUTPUT=$(try GITLAB -v user list) || exit 1
40+
pecho "${OUTPUT}" | grep -q avatar-url
41+
'
42+
43+
testcase "CLI args not in output" '
44+
OUTPUT=$(try GITLAB -v user list) || exit 1
45+
pecho "${OUTPUT}" | grep -qv config-file
46+
'
47+
48+
testcase "adding member to a project" '
49+
GITLAB project-member create --project-id "$PROJECT_ID" \
50+
--user-id "$USER_ID" --access-level 40 >/dev/null 2>&1
51+
'
52+
53+
testcase "file creation" '
54+
GITLAB project-file create --project-id "$PROJECT_ID" \
55+
--file-path README --branch-name master --content "CONTENT" \
56+
--commit-message "Initial commit" >/dev/null 2>&1
57+
'
58+
59+
testcase "issue creation" '
60+
OUTPUT=$(GITLAB project-issue create --project-id "$PROJECT_ID" \
61+
--title "my issue" --description "my issue description")
62+
'
63+
ISSUE_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d' ' -f2)
64+
65+
testcase "note creation" '
66+
GITLAB project-issue-note create --project-id "$PROJECT_ID" \
67+
--issue-id "$ISSUE_ID" --body "the body" >/dev/null 2>&1
68+
'
69+
70+
testcase "branch creation" '
71+
GITLAB project-branch create --project-id "$PROJECT_ID" \
72+
--branch-name branch1 --ref master >/dev/null 2>&1
73+
'
74+
75+
testcase "branch deletion" '
76+
GITLAB project-branch delete --project-id "$PROJECT_ID" \
77+
--name branch1 >/dev/null 2>&1
78+
'
79+
80+
testcase "project deletion" '
81+
GITLAB project delete --id "$PROJECT_ID"
82+
'

tools/py_functional_tests.sh

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22
# Copyright (C) 2015 Gauvain Pocentek <gauvain@pocentek.net>
33
#
44
# This program is free software: you can redistribute it and/or modify
@@ -14,26 +14,8 @@
1414
# You should have received a copy of the GNU Lesser General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

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

26-
setenv_script=$(dirname $0)/build_test_env.sh
27-
28-
. $setenv_script "$@"
29-
30-
VENV=$(pwd)/.venv
31-
32-
$VENV_CMD $VENV
33-
. $VENV/bin/activate
34-
pip install -rrequirements.txt
35-
pip install -e .
36-
37-
sleep 20
38-
39-
python $(dirname $0)/python_test.py
21+
try python "$(dirname "$0")"/python_test.py

0 commit comments

Comments
 (0)