Skip to content

Commit 5dcceb8

Browse files
committed
improve error handling
Break up pipelines and check the exit status of non-basic commands to ensure that any problems cause the scripts/testcases to fail right away.
1 parent 57f1ad5 commit 5dcceb8

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

tools/build_test_env.sh

+16-11
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ do
4848
command -v "${req}" >/dev/null 2>&1 || fatal "${req} is required"
4949
done
5050

51-
VENV=$(pwd)/.venv
51+
VENV=$(pwd)/.venv || exit 1
5252

5353
cleanup() {
5454
rm -f /tmp/python-gitlab.cfg
@@ -62,7 +62,7 @@ cleanup() {
6262
trap 'exit 1' HUP INT TERM
6363
}
6464

65-
docker run --name gitlab-test --detach --publish 8080:80 \
65+
try docker run --name gitlab-test --detach --publish 8080:80 \
6666
--publish 2222:22 gpocentek/test-python-gitlab:latest >/dev/null 2>&1
6767

6868
LOGIN='root'
@@ -87,11 +87,16 @@ done
8787
sleep 5
8888

8989
# Get the token
90-
TOKEN=$(curl -s http://localhost:8080/api/v3/session \
91-
-X POST \
92-
--data "login=$LOGIN&password=$PASSWORD" \
93-
| python -c \
94-
'import sys, json; print(json.load(sys.stdin)["private_token"])')
90+
TOKEN_JSON=$(
91+
try curl -s http://localhost:8080/api/v3/session \
92+
-X POST \
93+
--data "login=$LOGIN&password=$PASSWORD"
94+
) || exit 1
95+
TOKEN=$(
96+
pecho "${TOKEN_JSON}" |
97+
try python -c \
98+
'import sys, json; print(json.load(sys.stdin)["private_token"])'
99+
) || exit 1
95100

96101
cat > $CONFIG << EOF
97102
[global]
@@ -106,9 +111,9 @@ EOF
106111
log "Config file content ($CONFIG):"
107112
log <$CONFIG
108113

109-
"$VENV_CMD" "$VENV"
110-
. "$VENV"/bin/activate
111-
pip install -rrequirements.txt
112-
pip install -e .
114+
try "$VENV_CMD" "$VENV"
115+
. "$VENV"/bin/activate || fatal "failed to activate Python virtual environment"
116+
try pip install -rrequirements.txt
117+
try pip install -e .
113118

114119
sleep 20

tools/functional_tests.sh

+27-21
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,73 @@
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-
setenv_script=$(dirname "$0")/build_test_env.sh
17+
setenv_script=$(dirname "$0")/build_test_env.sh || exit 1
1818
BUILD_TEST_ENV_AUTO_CLEANUP=true
19-
. "$setenv_script" "$@"
20-
21-
set -e
19+
. "$setenv_script" "$@" || exit 1
2220

2321
printf %s "Testing project creation... "
24-
PROJECT_ID=$(GITLAB project create --name test-project1 \
25-
| grep ^id: | cut -d' ' -f2)
26-
GITLAB project list | grep -q test-project1
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 || fatal "test failed"
2726
OK
2827

2928
printf %s "Testing project update... "
30-
GITLAB project update --id "$PROJECT_ID" --description "My New Description"
29+
GITLAB project update --id "$PROJECT_ID" --description "My New Description" \
30+
|| fatal "test failed"
3131
OK
3232

3333
printf %s "Testing user creation... "
34-
USER_ID=$(GITLAB user create --email fake@email.com --username user1 \
35-
--name "User One" --password fakepassword | grep ^id: | cut -d' ' -f2)
34+
OUTPUT=$(GITLAB user create --email fake@email.com --username user1 \
35+
--name "User One" --password fakepassword) || fatal "test failed"
3636
OK
37+
USER_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d' ' -f2)
3738

3839
printf %s "Testing verbose output... "
39-
GITLAB -v user list | grep -q avatar-url
40+
OUTPUT=$(try GITLAB -v user list) || exit 1
41+
pecho "${OUTPUT}" | grep -q avatar-url || fatal "test failed"
4042
OK
4143

4244
printf %s "Testing CLI args not in output... "
43-
GITLAB -v user list | grep -qv config-file
45+
OUTPUT=$(try GITLAB -v user list) || exit 1
46+
pecho "${OUTPUT}" | grep -qv config-file || fatal "test failed"
4447
OK
4548

4649
printf %s "Testing adding member to a project... "
4750
GITLAB project-member create --project-id "$PROJECT_ID" \
48-
--user-id "$USER_ID" --access-level 40 >/dev/null 2>&1
51+
--user-id "$USER_ID" --access-level 40 >/dev/null 2>&1 \
52+
|| fatal "test failed"
4953
OK
5054

5155
printf %s "Testing file creation... "
5256
GITLAB project-file create --project-id "$PROJECT_ID" \
5357
--file-path README --branch-name master --content "CONTENT" \
54-
--commit-message "Initial commit" >/dev/null 2>&1
58+
--commit-message "Initial commit" >/dev/null 2>&1 || fatal "test failed"
5559
OK
5660

5761
printf %s "Testing issue creation... "
58-
ISSUE_ID=$(GITLAB project-issue create --project-id "$PROJECT_ID" \
59-
--title "my issue" --description "my issue description" \
60-
| grep ^id: | cut -d' ' -f2)
62+
OUTPUT=$(GITLAB project-issue create --project-id "$PROJECT_ID" \
63+
--title "my issue" --description "my issue description") \
64+
|| fatal "test failed"
6165
OK
66+
ISSUE_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d' ' -f2)
6267

6368
printf %s "Testing note creation... "
6469
GITLAB project-issue-note create --project-id "$PROJECT_ID" \
65-
--issue-id "$ISSUE_ID" --body "the body" >/dev/null 2>&1
70+
--issue-id "$ISSUE_ID" --body "the body" >/dev/null 2>&1 \
71+
|| fatal "test failed"
6672
OK
6773

6874
printf %s "Testing branch creation... "
6975
GITLAB project-branch create --project-id "$PROJECT_ID" \
70-
--branch-name branch1 --ref master >/dev/null 2>&1
76+
--branch-name branch1 --ref master >/dev/null 2>&1 || fatal "test failed"
7177
OK
7278

7379
printf %s "Testing branch deletion... "
7480
GITLAB project-branch delete --project-id "$PROJECT_ID" \
75-
--name branch1 >/dev/null 2>&1
81+
--name branch1 >/dev/null 2>&1 || fatal "test failed"
7682
OK
7783

7884
printf %s "Testing project deletion... "
79-
GITLAB project delete --id "$PROJECT_ID"
85+
GITLAB project delete --id "$PROJECT_ID" || fatal "test failed"
8086
OK

tools/py_functional_tests.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +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-
setenv_script=$(dirname "$0")/build_test_env.sh
17+
setenv_script=$(dirname "$0")/build_test_env.sh || exit 1
1818
BUILD_TEST_ENV_AUTO_CLEANUP=true
19-
. "$setenv_script" "$@"
19+
. "$setenv_script" "$@" || exit 1
2020

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

0 commit comments

Comments
 (0)