Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit79489c7

Browse files
committed
test(env): replace custom scripts with pytest and docker-compose
1 parent0d89a6e commit79489c7

15 files changed

+163
-287
lines changed

‎docker-requirements.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-r requirements.txt
2+
-r test-requirements.txt
3+
pytest-console-scripts
4+
pytest-docker

‎tools/build_test_env.sh‎

Lines changed: 0 additions & 158 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
deftest_api_v4(gl):
8+
fromtools.functionalimportpython_test_v4

‎tools/functional/cli/conftest.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33

44
@pytest.fixture
5-
defgitlab_cli(script_runner,CONFIG):
5+
defgitlab_cli(script_runner,gitlab_config):
66
"""Wrapper fixture to help make test cases less verbose."""
77

88
def_gitlab_cli(subcommands):
99
"""
1010
Return a script_runner.run method that takes a default gitlab
1111
command, and subcommands passed as arguments inside test cases.
1212
"""
13-
command= ["gitlab","--config-file",CONFIG]
13+
command= ["gitlab","--config-file",gitlab_config]
1414

1515
forsubcommandinsubcommands:
1616
# ensure we get strings (e.g from IDs)

‎tools/functional/conftest.py‎

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
importtime
12
importtempfile
23
frompathlibimportPath
34
fromrandomimportrandint
5+
fromsubprocessimportcheck_output
46

57
importpytest
68

79
importgitlab
810

911

1012
TEMP_DIR=tempfile.gettempdir()
13+
TEST_DIR=Path(__file__).resolve().parent
1114

1215

1316
defrandom_id():
@@ -20,15 +23,103 @@ def random_id():
2023
returnrandint(9,9999)
2124

2225

26+
defreset_gitlab(gl):
27+
# previously tools/reset_gitlab.py
28+
forprojectingl.projects.list():
29+
project.delete()
30+
forgroupingl.groups.list():
31+
group.delete()
32+
forvariableingl.variables.list():
33+
variable.delete()
34+
foruseringl.users.list():
35+
ifuser.username!="root":
36+
user.delete()
37+
38+
39+
defset_token(container):
40+
set_token_rb=TEST_DIR/"fixtures"/"set_token.rb"
41+
42+
withopen(set_token_rb,"r")asf:
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+
returnoutput
56+
57+
2358
@pytest.fixture(scope="session")
24-
defCONFIG():
25-
returnPath(TEMP_DIR)/"python-gitlab.cfg"
59+
defdocker_compose_file():
60+
returnTEST_DIR/"fixtures"/"docker-compose.yml"
2661

2762

2863
@pytest.fixture(scope="session")
29-
defgl(CONFIG):
64+
defcheck_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+
withcapmanager.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!"inoutput
85+
86+
return_check
87+
88+
89+
@pytest.fixture(scope="session")
90+
defgitlab_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+
withopen(config_file,"w")asf:
110+
f.write(config)
111+
112+
returnconfig_file
113+
114+
115+
@pytest.fixture(scope="session")
116+
defgl(gitlab_config):
30117
"""Helper instance to make fixtures and asserts directly via the API."""
31-
returngitlab.Gitlab.from_config("local", [CONFIG])
118+
119+
instance=gitlab.Gitlab.from_config("local", [gitlab_config])
120+
reset_gitlab(instance)
121+
122+
returninstance
32123

33124

34125
@pytest.fixture(scope="module")
File renamed without changes.
File renamed without changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff 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'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
putstoken.token

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp