| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,11 +25,13 @@ error() { log "ERROR: $@" >&2; } | |||
| 25 | 25 | fatal() { error "$@"; exit 1; } | |
| 26 | 26 | try() { "$@" || fatal "'$@' failed"; } | |
| 27 | 27 | ||
| 28 | + REUSE_CONTAINER= | ||
| 28 | 29 | NOVENV= | |
| 29 | 30 | PY_VER=3 | |
| 30 | 31 | API_VER=4 | |
| 31 | - while getopts :np:a: opt "$@"; do | ||
| 32 | + while getopts :knp:a: opt "$@"; do | ||
| 32 | 33 | case $opt in | |
| 34 | + k) REUSE_CONTAINER=1;; | ||
| 33 | 35 | n) NOVENV=1;; | |
| 34 | 36 | p) PY_VER=$OPTARG;; | |
| 35 | 37 | a) API_VER=$OPTARG;; | |
@@ -67,18 +69,42 @@ cleanup() { | |||
| 67 | 69 | command -v deactivate >/dev/null 2>&1 && deactivate || true | |
| 68 | 70 | log "Deleting python virtualenv..." | |
| 69 | 71 | 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 | ||
| 72 | 76 | log "Done." | |
| 73 | 77 | } | |
| 74 | 78 | [ -z "${BUILD_TEST_ENV_AUTO_CLEANUP+set}" ] || { | |
| 75 | 79 | trap cleanup EXIT | |
| 76 | 80 | trap 'exit 1' HUP INT TERM | |
| 77 | 81 | } | |
| 78 | 82 | ||
| 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 | ||
| 82 | 108 | ||
| 83 | 109 | LOGIN='root' | |
| 84 | 110 | PASSWORD='5iveL!fe' | |
@@ -106,7 +132,7 @@ if [ -z "$NOVENV" ]; then | |||
| 106 | 132 | try pip install -e . | |
| 107 | 133 | ||
| 108 | 134 | # to run generate_token.py | |
| 109 | - pip install bs4 lxml | ||
| 135 | + pip install requests-html | ||
| 110 | 136 | fi | |
| 111 | 137 | ||
| 112 | 138 | log "Waiting for gitlab to come online... " | |
@@ -115,12 +141,20 @@ while :; do | |||
| 115 | 141 | sleep 1 | |
| 116 | 142 | docker top gitlab-test >/dev/null 2>&1 || fatal "docker failed to start" | |
| 117 | 143 | 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 | ||
| 120 | 151 | I=$((I+5)) | |
| 121 | 152 | [ "$I" -lt 180 ] || fatal "timed out" | |
| 122 | 153 | done | |
| 123 | 154 | ||
| 155 | + log "Pausing to give GitLab some time to finish starting up..." | ||
| 156 | + sleep 200 | ||
| 157 | + | ||
| 124 | 158 | # Get the token | |
| 125 | 159 | TOKEN=$($(dirname $0)/generate_token.py) | |
| 126 | 160 | ||
@@ -138,7 +172,8 @@ EOF | |||
| 138 | 172 | log "Config file content ($CONFIG):" | |
| 139 | 173 | log <$CONFIG | |
| 140 | 174 | ||
| 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 | ||
| 144 | 179 | log "Test environment initialized." | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,64 +1,50 @@ | |||
| 1 | 1 | #!/usr/bin/env python | |
| 2 | 2 | ||
| 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"] | ||
| 54 | 41 | ||
| 55 | 42 | ||
| 56 | 43 | 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")) | ||
| 62 | 48 | ||
| 63 | 49 | ||
| 64 | 50 | if __name__ == "__main__": | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -190,7 +190,9 @@ | |||
| 190 | 190 | ||
| 191 | 191 | new_user.delete() | |
| 192 | 192 | 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 | + ) | ||
| 194 | 196 | ||
| 195 | 197 | # current user mail | |
| 196 | 198 | mail = gl.user.emails.create({"email": "current@user.com"}) | |
@@ -787,9 +789,10 @@ | |||
| 787 | 789 | msg = gl.broadcastmessages.create({"message": "this is the message"}) | |
| 788 | 790 | msg.color = "#444444" | |
| 789 | 791 | msg.save() | |
| 792 | + msg_id = msg.id | ||
| 790 | 793 | msg = gl.broadcastmessages.list(all=True)[0] | |
| 791 | 794 | assert msg.color == "#444444" | |
| 792 | - msg = gl.broadcastmessages.get(1) | ||
| 795 | + msg = gl.broadcastmessages.get(msg_id) | ||
| 793 | 796 | assert msg.color == "#444444" | |
| 794 | 797 | msg.delete() | |
| 795 | 798 | assert len(gl.broadcastmessages.list()) == 0 | |
| Original file line number | Diff line number | Diff 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()) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments