-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_gerrit.py
161 lines (110 loc) · 4.51 KB
/
test_gerrit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
CAVEAT EMPTOR: Tests require to be launched top-down!
Notes:
If you inspect Gerrit manually after running these
"""
from __future__ import print_function
import pytest
import fixtures
from gerrit import Client, AuthenticationError
from gerrit.model import ChangeDetails, Patch
def setup_module():
fixtures.setup()
def gerrit():
return Client(fixtures.GERRIT_URL)
def authenticated_gerrit():
client = gerrit()
client.authenticate(method='become', username=fixtures.USERNAME)
return client
def first_line(text):
return text.strip().split('\n')[0].strip()
def first_para(text):
lines = [line.strip() for line in text.strip().split('\n')]
result = []
for line in lines:
if not line:
return ' '.join(result)
result.append(line)
return ' '.join(result)
def test_projects():
client = gerrit()
projects = client.projects()
assert any(project.name == fixtures.TEST_PROJECT for project in projects)
def test_changes():
client = gerrit()
changes = client.changes(search={'project': fixtures.TEST_PROJECT})
assert set(change.name for change in changes) == \
set((first_para(fixtures.COMMIT_i),
first_para(fixtures.COMMIT_b1_2),
first_para(fixtures.COMMIT_b2),))
assert changes[0].project_name == fixtures.TEST_PROJECT
def test_change_details():
client = gerrit()
# We need to get the id at a cost of a slightly more brittle test
changes = client.changes(search={'project': fixtures.TEST_PROJECT})
change = [ch for ch in changes
if ch.name == first_para(fixtures.COMMIT_b2)][0]
change = client.change_details(change)
print(change)
assert change.name == first_para(fixtures.COMMIT_b2)
assert change.message == fixtures.COMMIT_b2
assert change.status == ChangeDetails.IN_PROGRESS
assert len(change.messages) == 0
def test_patchset_details():
client = gerrit()
# We need to get the id at a cost of a slightly more brittle test
changes = client.changes(search={'project': fixtures.TEST_PROJECT})
change = [ch for ch in changes
if ch.name == first_para(fixtures.COMMIT_b1_2)][0]
change = client.change_details(change)
patchset = change.last_patchset_details
assert len(patchset.patches) == 2
assert patchset.patches[-1].change_type == Patch.MODIFIED
assert patchset.patches[-1].insertions == 5
assert patchset.patches[-1].deletions == 1
def test_review():
client = authenticated_gerrit()
REVIEW_COMMENT = "YES, THIS IS COMMENT"
# We need to get the id at a cost of a slightly more brittle test
changes = client.changes(search={'project': fixtures.TEST_PROJECT})
change = [ch for ch in changes
if ch.name == first_para(fixtures.COMMIT_b2)][0]
change = client.change_details(change)
patchset = change.last_patchset_details
client.publish_review(patchset, REVIEW_COMMENT)
change = client.change_details(change)
assert len(change.messages) == 1
assert change.messages[0].message.strip().endswith(REVIEW_COMMENT)
def test_commenting():
client = authenticated_gerrit()
# We need to get the id at a cost of a slightly more brittle test
changes = client.changes(search={'project': fixtures.TEST_PROJECT})
change = [ch for ch in changes
if ch.name == first_para(fixtures.COMMIT_b2)][0]
change = client.change_details(change)
patchset = change.last_patchset_details
client.save_comment(patchset.patches[-1], 2, 'Hello!')
return # FIXME: actually do some testing here!
# skipping - if someone has any idea how to create a simple LDAP server,
# leave me a note
def _test_invalid_authentication():
client = gerrit()
with pytest.raises(AuthenticationError):
client.authenticate(method='user_pass',
username=fixtures.USERNAME,
password=fixtures.PASSWORD + '___')
# skipping - see note above
def _test_authentication():
client = gerrit()
client.authenticate(method='user_pass',
username=fixtures.USERNAME,
password=fixtures.PASSWORD)
def test_account_without_signing_in():
client = gerrit()
assert client.account() is None
def test_dev_authentication():
client = gerrit()
client.account()
client.authenticate(method='become',
username=fixtures.USERNAME)
assert client.account().user_name == fixtures.USERNAME