Skip to content

Commit 66d108d

Browse files
committed
test(api): add tests for variables API
1 parent 4492fc4 commit 66d108d

File tree

7 files changed

+288
-41
lines changed

7 files changed

+288
-41
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
"""
2+
GitLab API:
3+
https://docs.gitlab.com/ee/api/instance_level_ci_variables.html
4+
https://docs.gitlab.com/ee/api/project_level_variables.html
5+
https://docs.gitlab.com/ee/api/group_level_variables.html
6+
"""
7+
8+
import re
9+
10+
import pytest
11+
import responses
12+
13+
from gitlab.v4.objects import GroupVariable, ProjectVariable, Variable
14+
15+
16+
key = "TEST_VARIABLE_1"
17+
value = "TEST_1"
18+
new_value = "TEST_2"
19+
20+
variable_content = {
21+
"key": key,
22+
"variable_type": "env_var",
23+
"value": value,
24+
"protected": False,
25+
"masked": True,
26+
}
27+
variables_url = re.compile(
28+
r"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/variables"
29+
)
30+
variables_key_url = re.compile(
31+
rf"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/variables/{key}"
32+
)
33+
34+
35+
@pytest.fixture
36+
def resp_list_variables():
37+
with responses.RequestsMock() as rsps:
38+
rsps.add(
39+
method=responses.GET,
40+
url=variables_url,
41+
json=[variable_content],
42+
content_type="application/json",
43+
status=200,
44+
)
45+
yield rsps
46+
47+
48+
@pytest.fixture
49+
def resp_get_variable():
50+
with responses.RequestsMock() as rsps:
51+
rsps.add(
52+
method=responses.GET,
53+
url=variables_key_url,
54+
json=variable_content,
55+
content_type="application/json",
56+
status=200,
57+
)
58+
yield rsps
59+
60+
61+
@pytest.fixture
62+
def resp_create_variable():
63+
with responses.RequestsMock() as rsps:
64+
rsps.add(
65+
method=responses.POST,
66+
url=variables_url,
67+
json=variable_content,
68+
content_type="application/json",
69+
status=200,
70+
)
71+
yield rsps
72+
73+
74+
@pytest.fixture
75+
def resp_update_variable():
76+
updated_content = dict(variable_content)
77+
updated_content["value"] = new_value
78+
79+
with responses.RequestsMock() as rsps:
80+
rsps.add(
81+
method=responses.PUT,
82+
url=variables_key_url,
83+
json=updated_content,
84+
content_type="application/json",
85+
status=200,
86+
)
87+
yield rsps
88+
89+
90+
@pytest.fixture
91+
def resp_delete_variable(no_content):
92+
with responses.RequestsMock() as rsps:
93+
rsps.add(
94+
method=responses.DELETE,
95+
url=variables_key_url,
96+
json=no_content,
97+
content_type="application/json",
98+
status=204,
99+
)
100+
yield rsps
101+
102+
103+
def test_list_instance_variables(gl, resp_list_variables):
104+
variables = gl.variables.list()
105+
assert isinstance(variables, list)
106+
assert isinstance(variables[0], Variable)
107+
assert variables[0].value == value
108+
109+
110+
def test_get_instance_variable(gl, resp_get_variable):
111+
variable = gl.variables.get(key)
112+
assert isinstance(variable, Variable)
113+
assert variable.value == value
114+
115+
116+
def test_create_instance_variable(gl, resp_create_variable):
117+
variable = gl.variables.create({"key": key, "value": value})
118+
assert isinstance(variable, Variable)
119+
assert variable.value == value
120+
121+
122+
def test_update_instance_variable(gl, resp_update_variable):
123+
variable = gl.variables.get(key, lazy=True)
124+
variable.value = new_value
125+
variable.save()
126+
assert variable.value == new_value
127+
128+
129+
def test_delete_instance_variable(gl, resp_delete_variable):
130+
variable = gl.variables.get(key, lazy=True)
131+
variable.delete()
132+
133+
134+
def test_list_project_variables(project, resp_list_variables):
135+
variables = project.variables.list()
136+
assert isinstance(variables, list)
137+
assert isinstance(variables[0], ProjectVariable)
138+
assert variables[0].value == value
139+
140+
141+
def test_get_project_variable(project, resp_get_variable):
142+
variable = project.variables.get(key)
143+
assert isinstance(variable, ProjectVariable)
144+
assert variable.value == value
145+
146+
147+
def test_create_project_variable(project, resp_create_variable):
148+
variable = project.variables.create({"key": key, "value": value})
149+
assert isinstance(variable, ProjectVariable)
150+
assert variable.value == value
151+
152+
153+
def test_update_project_variable(project, resp_update_variable):
154+
variable = project.variables.get(key, lazy=True)
155+
variable.value = new_value
156+
variable.save()
157+
assert variable.value == new_value
158+
159+
160+
def test_delete_project_variable(project, resp_delete_variable):
161+
variable = project.variables.get(key, lazy=True)
162+
variable.delete()
163+
164+
165+
def test_list_group_variables(group, resp_list_variables):
166+
variables = group.variables.list()
167+
assert isinstance(variables, list)
168+
assert isinstance(variables[0], GroupVariable)
169+
assert variables[0].value == value
170+
171+
172+
def test_get_group_variable(group, resp_get_variable):
173+
variable = group.variables.get(key)
174+
assert isinstance(variable, GroupVariable)
175+
assert variable.value == value
176+
177+
178+
def test_create_group_variable(group, resp_create_variable):
179+
variable = group.variables.create({"key": key, "value": value})
180+
assert isinstance(variable, GroupVariable)
181+
assert variable.value == value
182+
183+
184+
def test_update_group_variable(group, resp_update_variable):
185+
variable = group.variables.get(key, lazy=True)
186+
variable.value = new_value
187+
variable.save()
188+
assert variable.value == new_value
189+
190+
191+
def test_delete_group_variable(group, resp_delete_variable):
192+
variable = group.variables.get(key, lazy=True)
193+
variable.delete()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
GitLab API:
3+
https://docs.gitlab.com/ee/api/instance_level_ci_variables.html
4+
https://docs.gitlab.com/ee/api/project_level_variables.html
5+
https://docs.gitlab.com/ee/api/group_level_variables.html
6+
"""
7+
8+
9+
def test_instance_variables(gl):
10+
variable = gl.variables.create({"key": "key1", "value": "value1"})
11+
assert variable.value == "value1"
12+
assert len(gl.variables.list()) == 1
13+
14+
variable.value = "new_value1"
15+
variable.save()
16+
variable = gl.variables.get(variable.key)
17+
assert variable.value == "new_value1"
18+
19+
variable.delete()
20+
assert len(gl.variables.list()) == 0
21+
22+
23+
def test_group_variables(group):
24+
variable = group.variables.create({"key": "key1", "value": "value1"})
25+
assert variable.value == "value1"
26+
assert len(group.variables.list()) == 1
27+
28+
variable.value = "new_value1"
29+
variable.save()
30+
variable = group.variables.get(variable.key)
31+
assert variable.value == "new_value1"
32+
33+
variable.delete()
34+
assert len(group.variables.list()) == 0
35+
36+
37+
def test_project_variables(project):
38+
variable = project.variables.create({"key": "key1", "value": "value1"})
39+
assert variable.value == "value1"
40+
assert len(project.variables.list()) == 1
41+
42+
variable.value = "new_value1"
43+
variable.save()
44+
variable = project.variables.get(variable.key)
45+
assert variable.value == "new_value1"
46+
47+
variable.delete()
48+
assert len(project.variables.list()) == 0

tools/functional/cli/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def gitlab_cli(script_runner, CONFIG):
6+
"""Wrapper fixture to help make test cases less verbose."""
7+
8+
def _gitlab_cli(subcommands):
9+
"""
10+
Return a script_runner.run method that takes a default gitlab
11+
command, and subcommands passed as arguments inside test cases.
12+
"""
13+
command = ["gitlab", "--config-file", CONFIG]
14+
15+
for subcommand in subcommands:
16+
# ensure we get strings (e.g from IDs)
17+
command.append(str(subcommand))
18+
19+
return script_runner.run(*command)
20+
21+
return _gitlab_cli
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def test_list_instance_variables(gitlab_cli, gl):
2+
cmd = ["variable", "list"]
3+
ret = gitlab_cli(cmd)
4+
5+
assert ret.success
6+
7+
8+
def test_list_group_variables(gitlab_cli, group):
9+
cmd = ["group-variable", "list", "--group-id", group.id]
10+
ret = gitlab_cli(cmd)
11+
12+
assert ret.success
13+
14+
15+
def test_list_project_variables(gitlab_cli, project):
16+
cmd = ["project-variable", "list", "--project-id", project.id]
17+
ret = gitlab_cli(cmd)
18+
19+
assert ret.success

tools/functional/conftest.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import os
2+
import tempfile
13
from random import randint
24

35
import pytest
46

57
import gitlab
68

79

10+
TEMP_DIR = tempfile.gettempdir()
11+
12+
813
def random_id():
914
"""
1015
Helper to ensure new resource creation does not clash with
@@ -17,27 +22,7 @@ def random_id():
1722

1823
@pytest.fixture(scope="session")
1924
def CONFIG():
20-
return "/tmp/python-gitlab.cfg"
21-
22-
23-
@pytest.fixture
24-
def gitlab_cli(script_runner, CONFIG):
25-
"""Wrapper fixture to help make test cases less verbose."""
26-
27-
def _gitlab_cli(subcommands):
28-
"""
29-
Return a script_runner.run method that takes a default gitlab
30-
command, and subcommands passed as arguments inside test cases.
31-
"""
32-
command = ["gitlab", "--config-file", CONFIG]
33-
34-
for subcommand in subcommands:
35-
# ensure we get strings (e.g from IDs)
36-
command.append(str(subcommand))
37-
38-
return script_runner.run(*command)
39-
40-
return _gitlab_cli
25+
return os.path.join(TEMP_DIR, "python-gitlab.cfg")
4126

4227

4328
@pytest.fixture(scope="session")

tools/functional_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ setenv_script=$(dirname "$0")/build_test_env.sh || exit 1
1818
BUILD_TEST_ENV_AUTO_CLEANUP=true
1919
. "$setenv_script" "$@" || exit 1
2020

21-
pytest "$(dirname "$0")/functional/cli"
21+
pytest --script-launch-mode=subprocess "$(dirname "$0")/functional/cli"

tools/python_test_v4.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,6 @@
367367
assert len(gm1.issues()) == 0
368368
assert len(gm1.merge_requests()) == 0
369369

370-
# group variables
371-
group1.variables.create({"key": "foo", "value": "bar"})
372-
g_v = group1.variables.get("foo")
373-
assert g_v.value == "bar"
374-
g_v.value = "baz"
375-
g_v.save()
376-
g_v = group1.variables.get("foo")
377-
assert g_v.value == "baz"
378-
assert len(group1.variables.list()) == 1
379-
g_v.delete()
380-
assert len(group1.variables.list()) == 0
381370

382371
# group labels
383372
# group1.labels.create({"name": "foo", "description": "bar", "color": "#112233"})
@@ -856,14 +845,6 @@
856845
assert len(admin_project.triggers.list()) == 1
857846
tr1.delete()
858847

859-
# variables
860-
v1 = admin_project.variables.create({"key": "key1", "value": "value1"})
861-
assert len(admin_project.variables.list()) == 1
862-
v1.value = "new_value1"
863-
v1.save()
864-
v1 = admin_project.variables.get(v1.key)
865-
assert v1.value == "new_value1"
866-
v1.delete()
867848

868849
# branches and merges
869850
to_merge = admin_project.branches.create({"branch": "branch1", "ref": "master"})

0 commit comments

Comments
 (0)