Skip to content

Commit eefceac

Browse files
committed
feat(ci): improve functionnal tests
1 parent f5b4a11 commit eefceac

File tree

4 files changed

+114
-70
lines changed

4 files changed

+114
-70
lines changed

tools/build_test_env.sh

+47-12
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ error() { log "ERROR: $@" >&2; }
2525
fatal() { error "$@"; exit 1; }
2626
try() { "$@" || fatal "'$@' failed"; }
2727

28+
REUSE_CONTAINER=
2829
NOVENV=
2930
PY_VER=3
3031
API_VER=4
31-
while getopts :np:a: opt "$@"; do
32+
while getopts :knp:a: opt "$@"; do
3233
case $opt in
34+
k) REUSE_CONTAINER=1;;
3335
n) NOVENV=1;;
3436
p) PY_VER=$OPTARG;;
3537
a) API_VER=$OPTARG;;
@@ -67,18 +69,42 @@ cleanup() {
6769
command -v deactivate >/dev/null 2>&1 && deactivate || true
6870
log "Deleting python virtualenv..."
6971
rm -rf "$VENV"
70-
log "Stopping gitlab-test docker container..."
71-
docker rm -f gitlab-test >/dev/null
72+
if [ -z "$REUSE_CONTAINER" ]; then
73+
log "Stopping gitlab-test docker container..."
74+
docker rm -f gitlab-test >/dev/null
75+
fi
7276
log "Done."
7377
}
7478
[ -z "${BUILD_TEST_ENV_AUTO_CLEANUP+set}" ] || {
7579
trap cleanup EXIT
7680
trap 'exit 1' HUP INT TERM
7781
}
7882

79-
try docker pull registry.gitlab.com/python-gitlab/python-gitlab:test >/dev/null
80-
try docker run --name gitlab-test --detach --publish 8080:80 \
81-
--publish 2222:22 registry.gitlab.com/python-gitlab/python-gitlab:test >/dev/null
83+
if [ -z "$REUSE_CONTAINER" ] || ! docker top gitlab-test >/dev/null 2>&1; then
84+
GITLAB_OMNIBUS_CONFIG="external_url 'http://gitlab.test'
85+
gitlab_rails['initial_root_password'] = '5iveL!fe'
86+
gitlab_rails['initial_shared_runners_registration_token'] = 'sTPNtWLEuSrHzoHP8oCU'
87+
registry['enable'] = false
88+
nginx['redirect_http_to_https'] = false
89+
nginx['listen_port'] = 80
90+
nginx['listen_https'] = false
91+
pages_external_url 'http://pages.gitlab.lxd'
92+
gitlab_pages['enable'] = true
93+
gitlab_pages['inplace_chroot'] = true
94+
prometheus['enable'] = false
95+
alertmanager['enable'] = false
96+
node_exporter['enable'] = false
97+
redis_exporter['enable'] = false
98+
postgres_exporter['enable'] = false
99+
pgbouncer_exporter['enable'] = false
100+
gitlab_exporter['enable'] = false
101+
grafana['enable'] = false
102+
letsencrypt['enable'] = false
103+
"
104+
try docker run --name gitlab-test --detach --publish 8080:80 \
105+
--publish 2222:22 --env "GITLAB_OMNIBUS_CONFIG=$GITLAB_OMNIBUS_CONFIG" \
106+
gitlab/gitlab-ce:latest >/dev/null
107+
fi
82108

83109
LOGIN='root'
84110
PASSWORD='5iveL!fe'
@@ -106,7 +132,7 @@ if [ -z "$NOVENV" ]; then
106132
try pip install -e .
107133

108134
# to run generate_token.py
109-
pip install bs4 lxml
135+
pip install requests-html
110136
fi
111137

112138
log "Waiting for gitlab to come online... "
@@ -115,12 +141,20 @@ while :; do
115141
sleep 1
116142
docker top gitlab-test >/dev/null 2>&1 || fatal "docker failed to start"
117143
sleep 4
118-
curl -s http://localhost:8080/users/sign_in 2>/dev/null \
119-
| grep -q "GitLab Community Edition" && break
144+
# last command started by the container is "gitlab-ctl tail"
145+
docker exec gitlab-test pgrep -f 'gitlab-ctl tail' &>/dev/null \
146+
&& docker exec gitlab-test curl http://localhost/-/health 2>/dev/null \
147+
| grep -q 'GitLab OK' \
148+
&& curl -s http://localhost:8080/users/sign_in 2>/dev/null \
149+
| grep -q "GitLab Community Edition" \
150+
&& break
120151
I=$((I+5))
121152
[ "$I" -lt 180 ] || fatal "timed out"
122153
done
123154

155+
log "Pausing to give GitLab some time to finish starting up..."
156+
sleep 200
157+
124158
# Get the token
125159
TOKEN=$($(dirname $0)/generate_token.py)
126160

@@ -138,7 +172,8 @@ EOF
138172
log "Config file content ($CONFIG):"
139173
log <$CONFIG
140174

141-
log "Pausing to give GitLab some time to finish starting up..."
142-
sleep 200
143-
175+
if [ ! -z "$REUSE_CONTAINER" ]; then
176+
echo reset gitlab
177+
$(dirname $0)/reset_gitlab.py
178+
fi
144179
log "Test environment initialized."

tools/generate_token.py

+42-56
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,50 @@
11
#!/usr/bin/env python
22

3-
import sys
4-
5-
try:
6-
from urllib.parse import urljoin
7-
except ImportError:
8-
from urlparse import urljoin
9-
10-
from bs4 import BeautifulSoup
11-
import requests
12-
13-
endpoint = "http://localhost:8080"
14-
root_route = urljoin(endpoint, "/")
15-
sign_in_route = urljoin(endpoint, "/users/sign_in")
16-
pat_route = urljoin(endpoint, "/profile/personal_access_tokens")
17-
18-
login = "root"
19-
password = "5iveL!fe"
20-
21-
22-
def find_csrf_token(text):
23-
soup = BeautifulSoup(text, "lxml")
24-
token = soup.find(attrs={"name": "csrf-token"})
25-
param = soup.find(attrs={"name": "csrf-param"})
26-
data = {param.get("content"): token.get("content")}
27-
return data
28-
29-
30-
def obtain_csrf_token():
31-
r = requests.get(root_route)
32-
token = find_csrf_token(r.text)
33-
return token, r.cookies
34-
35-
36-
def sign_in(csrf, cookies):
37-
data = {"user[login]": login, "user[password]": password}
38-
data.update(csrf)
39-
r = requests.post(sign_in_route, data=data, cookies=cookies)
40-
token = find_csrf_token(r.text)
41-
return token, r.history[0].cookies
42-
43-
44-
def obtain_personal_access_token(name, csrf, cookies):
45-
data = {
46-
"personal_access_token[name]": name,
47-
"personal_access_token[scopes][]": ["api", "sudo"],
48-
}
49-
data.update(csrf)
50-
r = requests.post(pat_route, data=data, cookies=cookies)
51-
soup = BeautifulSoup(r.text, "lxml")
52-
token = soup.find("input", id="created-personal-access-token").get("value")
53-
return token
3+
from six.moves.urllib.parse import urljoin
4+
from requests_html import HTMLSession
5+
6+
ENDPOINT = "http://localhost:8080"
7+
LOGIN = "root"
8+
PASSWORD = "5iveL!fe"
9+
10+
11+
class GitlabSession(HTMLSession):
12+
def __init__(self, endpoint, *args, **kwargs):
13+
super().__init__(*args, **kwargs)
14+
self.endpoint = endpoint
15+
self.csrf = None
16+
17+
def find_csrf_token(self, html):
18+
param = html.find("meta[name=csrf-param]")[0].attrs["content"]
19+
token = html.find("meta[name=csrf-token]")[0].attrs["content"]
20+
self.csrf = {param: token}
21+
22+
def obtain_csrf_token(self):
23+
r = self.get(urljoin(self.endpoint, "/"))
24+
self.find_csrf_token(r.html)
25+
26+
def sign_in(self, login, password):
27+
data = {"user[login]": login, "user[password]": password, **self.csrf}
28+
r = self.post(urljoin(self.endpoint, "/users/sign_in"), data=data)
29+
self.find_csrf_token(r.html)
30+
31+
def obtain_personal_access_token(self, name):
32+
data = {
33+
"personal_access_token[name]": name,
34+
"personal_access_token[scopes][]": ["api", "sudo"],
35+
**self.csrf,
36+
}
37+
r = self.post(
38+
urljoin(self.endpoint, "/profile/personal_access_tokens"), data=data
39+
)
40+
return r.html.find("#created-personal-access-token")[0].attrs["value"]
5441

5542

5643
def main():
57-
csrf1, cookies1 = obtain_csrf_token()
58-
csrf2, cookies2 = sign_in(csrf1, cookies1)
59-
60-
token = obtain_personal_access_token("default", csrf2, cookies2)
61-
print(token)
44+
with GitlabSession(ENDPOINT) as s:
45+
s.obtain_csrf_token()
46+
s.sign_in(LOGIN, PASSWORD)
47+
print(s.obtain_personal_access_token("default"))
6248

6349

6450
if __name__ == "__main__":

tools/python_test_v4.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@
190190

191191
new_user.delete()
192192
foobar_user.delete()
193-
assert len(gl.users.list()) == 3
193+
assert len(gl.users.list()) == 3 + len(
194+
[u for u in gl.users.list() if u.username == "ghost"]
195+
)
194196

195197
# current user mail
196198
mail = gl.user.emails.create({"email": "current@user.com"})
@@ -787,9 +789,10 @@
787789
msg = gl.broadcastmessages.create({"message": "this is the message"})
788790
msg.color = "#444444"
789791
msg.save()
792+
msg_id = msg.id
790793
msg = gl.broadcastmessages.list(all=True)[0]
791794
assert msg.color == "#444444"
792-
msg = gl.broadcastmessages.get(1)
795+
msg = gl.broadcastmessages.get(msg_id)
793796
assert msg.color == "#444444"
794797
msg.delete()
795798
assert len(gl.broadcastmessages.list()) == 0

tools/reset_gitlab.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
5+
from gitlab import Gitlab
6+
7+
8+
def main():
9+
with Gitlab.from_config(config_files=["/tmp/python-gitlab.cfg"]) as gl:
10+
for project in gl.projects.list():
11+
project.delete()
12+
for group in gl.groups.list():
13+
group.delete()
14+
for user in gl.users.list():
15+
if user.username != "root":
16+
user.delete()
17+
18+
19+
if __name__ == "__main__":
20+
sys.exit(main())

0 commit comments

Comments
 (0)