-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlivetests.py
executable file
·195 lines (158 loc) · 7.49 KB
/
livetests.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The MIT License
#
# Copyright 2018 David Pursehouse. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Live server tests."""
import base64
import pytest
import unittest
from pygerrit2 import GerritRestAPI, GerritReview, HTTPBasicAuth, Anonymous
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_container_is_ready
TEST_TOPIC = "test-topic"
class GerritContainer(DockerContainer):
"""Gerrit container."""
def __init__(self, version):
"""Construct a GerritContainer with the given version."""
image = "gerritcodereview/gerrit:" + version
super(GerritContainer, self).__init__(image)
self.with_exposed_ports(8080)
@wait_container_is_ready()
def _initialize(api):
api.get("/changes/")
@pytest.fixture(scope="module", params=["3.6.6", "3.7.4", "3.8.1"])
def gerrit_api(request):
"""Create a Gerrit container for the given version and return an API."""
with GerritContainer(request.param) as gerrit:
port = gerrit.get_exposed_port(8080)
url = "http://localhost:%s" % port
api = GerritRestAPI(url=url, auth=Anonymous())
_initialize(api)
auth = HTTPBasicAuth("admin", "secret")
api = GerritRestAPI(url=url, auth=auth)
yield api
class TestGerritAgainstLiveServer(object):
"""Run tests against a live server."""
def _get_test_change(self, gerrit_api, topic=TEST_TOPIC):
results = gerrit_api.get("/changes/?q=topic:" + topic)
assert len(results) == 1
return results[0]
def test_put_with_json_dict(self, gerrit_api):
"""Test a PUT request passing data as a dict to `json`.
Tests that the PUT request works as expected when data is passed
via the `json` argument as a `dict`.
Creates the test project which is used by subsequent tests.
"""
projectinput = {"create_empty_commit": "true"}
gerrit_api.put("/projects/test-project", json=projectinput)
gerrit_api.get("/projects/test-project")
def test_put_with_data_dict(self, gerrit_api):
"""Test a PUT request passing data as a dict to `data`.
Tests that the PUT request works as expected when data is passed
via the `data` argument as a `dict`.
"""
description = {"description": "New Description"}
gerrit_api.put("/projects/test-project/description", data=description)
project = gerrit_api.get("/projects/test-project")
assert project["description"] == "New Description"
def test_post_with_data_dict_and_no_data(self, gerrit_api):
"""Test a POST request passing data as a dict to `data`.
Tests that the POST request works as expected when data is passed
via the `data` argument as a `dict`.
"""
changeinput = {
"project": "test-project",
"subject": "subject",
"branch": "master",
"topic": "post-with-data",
}
result = gerrit_api.post("/changes/", data=changeinput)
change = self._get_test_change(gerrit_api, "post-with-data")
assert change["id"] == result["id"]
# Subsequent post without data or json should not have the Content-Type
# json header, and should succeed.
result = gerrit_api.post("/changes/" + change["id"] + "/abandon")
assert result["status"] == "ABANDONED"
def test_post_with_json_dict(self, gerrit_api):
"""Test a POST request passing data as a dict to `json`.
Tests that the POST request works as expected when data is passed
via the `json` argument as a `dict`.
Creates the change which is used by subsequent tests.
"""
changeinput = {
"project": "test-project",
"subject": "subject",
"branch": "master",
"topic": TEST_TOPIC,
}
result = gerrit_api.post("/changes/", json=changeinput)
change = self._get_test_change(gerrit_api)
assert change["id"] == result["id"]
def test_put_with_data_as_string(self, gerrit_api):
"""Test a PUT request passing data as a string to `data`.
Tests that the PUT request works as expected when data is passed
via the `data` parameter as a string.
Creates a change edit that is checked in the subsequent test.
"""
change_id = self._get_test_change(gerrit_api)["id"]
gerrit_api.put(
"/changes/" + change_id + "/edit/foo",
data="Content with non base64 valid chars åäö",
)
def test_put_json_content(self, gerrit_api):
"""Test a PUT request with a json file content (issue #54)."""
change_id = self._get_test_change(gerrit_api)["id"]
content = """{"foo" : "bar"}"""
gerrit_api.put("/changes/" + change_id + "/edit/file.json", data=content)
def test_get_base64_data(self, gerrit_api):
"""Test a GET request on an API that returns base64 encoded response.
Tests that the headers can be overridden on the GET call, resulting
in the data being returned as text/plain, and that the content of the
response can be base64 decoded.
"""
change_id = self._get_test_change(gerrit_api)["id"]
response = gerrit_api.get(
"/changes/" + change_id + "/edit/foo", headers={"Accept": "text/plain"}
)
# Will raise binascii.Error if content is not properly encoded
base64.b64decode(response)
def test_get_patch_zip(self, gerrit_api):
"""Test a GET request to get a patch file (issue #19)."""
change_id = self._get_test_change(gerrit_api)["id"]
gerrit_api.get("/changes/" + change_id + "/revisions/current/patch?zip")
def test_put_with_no_content(self, gerrit_api):
"""Test a PUT request with no content."""
change_id = self._get_test_change(gerrit_api)["id"]
gerrit_api.put("/changes/" + change_id + "/edit/foo")
def test_review(self, gerrit_api):
"""Test that a review can be posted by the review API."""
change_id = self._get_test_change(gerrit_api)["id"]
review = GerritReview()
review.set_message("Review from live test")
review.add_labels({"Code-Review": 1})
review.set_tag("a_test_tag")
result = gerrit_api.review(change_id, "current", review)
assert "labels" in result
assert "Code-Review" in result["labels"]
assert result["labels"]["Code-Review"] == 1
if __name__ == "__main__":
unittest.main()