| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + -r requirements.txt | ||
| 2 | + -r test-requirements.txt | ||
| 3 | + pytest-console-scripts | ||
| 4 | + pytest-docker | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + """ | ||
| 2 | + Temporary module to run legacy tests as a single pytest test case | ||
| 3 | + as they're all plain asserts at module level. | ||
| 4 | + """ | ||
| 5 | + | ||
| 6 | + | ||
| 7 | + def test_api_v4(gl): | ||
| 8 | + from tools.functional import python_test_v4 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,15 +2,15 @@ | |||
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | 4 | @pytest.fixture | |
| 5 | - def gitlab_cli(script_runner, CONFIG): | ||
| 5 | + def gitlab_cli(script_runner, gitlab_config): | ||
| 6 | 6 | """Wrapper fixture to help make test cases less verbose.""" | |
| 7 | 7 | ||
| 8 | 8 | def _gitlab_cli(subcommands): | |
| 9 | 9 | """ | |
| 10 | 10 | Return a script_runner.run method that takes a default gitlab | |
| 11 | 11 | command, and subcommands passed as arguments inside test cases. | |
| 12 | 12 | """ | |
| 13 | - command = ["gitlab", "--config-file", CONFIG] | ||
| 13 | + command = ["gitlab", "--config-file", gitlab_config] | ||
| 14 | 14 | ||
| 15 | 15 | for subcommand in subcommands: | |
| 16 | 16 | # ensure we get strings (e.g from IDs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,13 +1,16 @@ | |||
| 1 | + import time | ||
| 1 | 2 | import tempfile | |
| 2 | 3 | from pathlib import Path | |
| 3 | 4 | from random import randint | |
| 5 | + from subprocess import check_output | ||
| 4 | 6 | ||
| 5 | 7 | import pytest | |
| 6 | 8 | ||
| 7 | 9 | import gitlab | |
| 8 | 10 | ||
| 9 | 11 | ||
| 10 | 12 | TEMP_DIR = tempfile.gettempdir() | |
| 13 | + TEST_DIR = Path(__file__).resolve().parent | ||
| 11 | 14 | ||
| 12 | 15 | ||
| 13 | 16 | def random_id(): | |
@@ -20,15 +23,103 @@ def random_id(): | |||
| 20 | 23 | return randint(9, 9999) | |
| 21 | 24 | ||
| 22 | 25 | ||
| 26 | + def reset_gitlab(gl): | ||
| 27 | + # previously tools/reset_gitlab.py | ||
| 28 | + for project in gl.projects.list(): | ||
| 29 | + project.delete() | ||
| 30 | + for group in gl.groups.list(): | ||
| 31 | + group.delete() | ||
| 32 | + for variable in gl.variables.list(): | ||
| 33 | + variable.delete() | ||
| 34 | + for user in gl.users.list(): | ||
| 35 | + if user.username != "root": | ||
| 36 | + user.delete() | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + def set_token(container): | ||
| 40 | + set_token_rb = TEST_DIR / "fixtures" / "set_token.rb" | ||
| 41 | + | ||
| 42 | + with open(set_token_rb, "r") as f: | ||
| 43 | + set_token_command = f.read().strip() | ||
| 44 | + | ||
| 45 | + rails_command = [ | ||
| 46 | + "docker", | ||
| 47 | + "exec", | ||
| 48 | + container, | ||
| 49 | + "gitlab-rails", | ||
| 50 | + "runner", | ||
| 51 | + set_token_command, | ||
| 52 | + ] | ||
| 53 | + output = check_output(rails_command).decode().strip() | ||
| 54 | + | ||
| 55 | + return output | ||
| 56 | + | ||
| 57 | + | ||
| 23 | 58 | @pytest.fixture(scope="session") | |
| 24 | - def CONFIG(): | ||
| 25 | - return Path(TEMP_DIR) / "python-gitlab.cfg" | ||
| 59 | + def docker_compose_file(): | ||
| 60 | + return TEST_DIR / "fixtures" / "docker-compose.yml" | ||
| 26 | 61 | ||
| 27 | 62 | ||
| 28 | 63 | @pytest.fixture(scope="session") | |
| 29 | - def gl(CONFIG): | ||
| 64 | + def check_is_alive(request): | ||
| 65 | + """ | ||
| 66 | + Return a healthcheck function fixture for the GitLab container spinup. | ||
| 67 | + """ | ||
| 68 | + start = time.time() | ||
| 69 | + | ||
| 70 | + # Temporary manager to disable capsys in a session-scoped fixture | ||
| 71 | + # so people know it takes a while for GitLab to spin up | ||
| 72 | + # https://github.com/pytest-dev/pytest/issues/2704 | ||
| 73 | + capmanager = request.config.pluginmanager.getplugin("capturemanager") | ||
| 74 | + | ||
| 75 | + def _check(container): | ||
| 76 | + delay = int(time.time() - start) | ||
| 77 | + | ||
| 78 | + with capmanager.global_and_fixture_disabled(): | ||
| 79 | + print(f"Waiting for GitLab to reconfigure.. (~{delay}s)") | ||
| 80 | + | ||
| 81 | + logs = ["docker", "logs", container] | ||
| 82 | + output = check_output(logs).decode() | ||
| 83 | + | ||
| 84 | + return "gitlab Reconfigured!" in output | ||
| 85 | + | ||
| 86 | + return _check | ||
| 87 | + | ||
| 88 | + | ||
| 89 | + @pytest.fixture(scope="session") | ||
| 90 | + def gitlab_config(check_is_alive, docker_ip, docker_services): | ||
| 91 | + config_file = Path(TEMP_DIR) / "python-gitlab.cfg" | ||
| 92 | + port = docker_services.port_for("gitlab", 80) | ||
| 93 | + | ||
| 94 | + docker_services.wait_until_responsive( | ||
| 95 | + timeout=180, pause=5, check=lambda: check_is_alive("gitlab-test") | ||
| 96 | + ) | ||
| 97 | + | ||
| 98 | + token = set_token("gitlab-test") | ||
| 99 | + | ||
| 100 | + config = f"""[global] | ||
| 101 | + default = local | ||
| 102 | + timeout = 60 | ||
| 103 | + | ||
| 104 | + [local] | ||
| 105 | + url = http://{docker_ip}:{port} | ||
| 106 | + private_token = {token} | ||
| 107 | + api_version = 4""" | ||
| 108 | + | ||
| 109 | + with open(config_file, "w") as f: | ||
| 110 | + f.write(config) | ||
| 111 | + | ||
| 112 | + return config_file | ||
| 113 | + | ||
| 114 | + | ||
| 115 | + @pytest.fixture(scope="session") | ||
| 116 | + def gl(gitlab_config): | ||
| 30 | 117 | """Helper instance to make fixtures and asserts directly via the API.""" | |
| 31 | - return gitlab.Gitlab.from_config("local", [CONFIG]) | ||
| 118 | + | ||
| 119 | + instance = gitlab.Gitlab.from_config("local", [gitlab_config]) | ||
| 120 | + reset_gitlab(instance) | ||
| 121 | + | ||
| 122 | + return instance | ||
| 32 | 123 | ||
| 33 | 124 | ||
| 34 | 125 | @pytest.fixture(scope="module") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,31 @@ | |||
| 1 | + version: '3' | ||
| 2 | + services: | ||
| 3 | + gitlab: | ||
| 4 | + image: 'gitlab/gitlab-ce:latest' | ||
| 5 | + container_name: 'gitlab-test' | ||
| 6 | + hostname: 'gitlab.test' | ||
| 7 | + privileged: true # Just in case https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/1350 | ||
| 8 | + environment: | ||
| 9 | + GITLAB_OMNIBUS_CONFIG: | | ||
| 10 | + external_url 'http://gitlab.test' | ||
| 11 | + gitlab_rails['initial_root_password'] = '5iveL!fe' | ||
| 12 | + gitlab_rails['initial_shared_runners_registration_token'] = 'sTPNtWLEuSrHzoHP8oCU' | ||
| 13 | + registry['enable'] = false | ||
| 14 | + nginx['redirect_http_to_https'] = false | ||
| 15 | + nginx['listen_port'] = 80 | ||
| 16 | + nginx['listen_https'] = false | ||
| 17 | + pages_external_url 'http://pages.gitlab.lxd' | ||
| 18 | + gitlab_pages['enable'] = true | ||
| 19 | + gitlab_pages['inplace_chroot'] = true | ||
| 20 | + prometheus['enable'] = false | ||
| 21 | + alertmanager['enable'] = false | ||
| 22 | + node_exporter['enable'] = false | ||
| 23 | + redis_exporter['enable'] = false | ||
| 24 | + postgres_exporter['enable'] = false | ||
| 25 | + pgbouncer_exporter['enable'] = false | ||
| 26 | + gitlab_exporter['enable'] = false | ||
| 27 | + grafana['enable'] = false | ||
| 28 | + letsencrypt['enable'] = false | ||
| 29 | + ports: | ||
| 30 | + - '8080:80' | ||
| 31 | + - '2222:22' | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,9 @@ | |||
| 1 | + # https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#programmatically-creating-a-personal-access-token | ||
| 2 | + | ||
| 3 | + user = User.find_by_username('root') | ||
| 4 | + | ||
| 5 | + token = user.personal_access_tokens.create(scopes: [:api, :sudo], name: 'default'); | ||
| 6 | + token.set_token('python-gitlab-token'); | ||
| 7 | + token.save! | ||
| 8 | + | ||
| 9 | + puts token.token | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments