-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_cache_requests.py
253 lines (208 loc) · 9.33 KB
/
test_cache_requests.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Test HTTP(S) requests caching."""
from email.utils import formatdate
import pook
import mock
from pook.interceptors.urllib3 import io
import pytest
import claranet_tfwrapper as tfwrapper
@pytest.fixture
def terraform_releases_html_after_v0_13_0():
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="release-header">
<a href="/hashicorp/terraform/releases/tag/v0.12.19">v0.12.19</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.18">v0.12.18</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.17">v0.12.17</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.16">v0.12.16</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.15">v0.12.15</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.14">v0.12.14</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.13">v0.12.13</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.12">v0.12.12</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.11">v0.12.11</a>
<a href="/hashicorp/terraform/releases/tag/v0.12.10">v0.12.10</a>
</div>
</body>
</html>
"""
# This test uses pook instead of requests_mock because:
# - requests_mock intercepts requests at the Adapter level and thus prevents using
# CacheControl at the same time as it also works at the Adapter level.
# - pook intercepts requests at a lower level by replacing urllib3.urlopen() by its
# own handler. This allows to actually verify what network requests would happen.
# See https://github.com/h2non/pook/blob/v1.0.2/pook/interceptors/urllib3.py#L24-L27
class AutoclosingBytesIO(io.BytesIO):
"""
AutoclosingBytesIO extends io.BytesIO to autoclose itself.
CacheControl only flushes its buffer into its cache when the underlying IO
is closed. pook uses io.BytesIO which does not automatically close itself when
consumed so we need to subclass it and just do this.
"""
def __init__(self, *args, **kwargs):
"""Initialize the bytes I/O stream."""
return super(io.BytesIO, self).__init__(*args, **kwargs)
def read(self, size=-1):
"""Read and, if at end, close the bytes I/O stream."""
data = super(AutoclosingBytesIO, self).read(size)
pos = self.tell()
if not super(AutoclosingBytesIO, self).read():
# If there is no more data to read, close the stream
self.close()
else:
# Otherwise restore position
self.seek(pos)
return data
def test_search_on_github_cache_terraform_releases_200(
tmp_working_dir,
terraform_releases_html_after_v0_13_0,
):
with mock.patch("io.BytesIO", AutoclosingBytesIO):
with pook.use():
repo = "hashicorp/terraform"
releases_url = "https://github.com/{}/tags?after=v0.13.0".format(repo)
# A volatile mock that can only be invoked once
pook.get(
releases_url,
reply=200,
response_type="text/plain",
response_body=terraform_releases_html_after_v0_13_0,
response_headers={
"Status": "200 OK",
"ETag": 'W/"df0474ebd25f223a95926ba58e11e77b"',
"Cache-Control": "max-age=0, private, must-revalidate",
"Date": formatdate(usegmt=True),
},
times=1,
)
patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
assert patch == "14"
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "")
assert patch == "19"
assert pook.isdone()
assert not pook.pending_mocks()
assert not pook.unmatched_requests()
assert not pook.isactive()
def test_search_on_github_cache_terraform_releases_does_not_cache_error_429(
tmp_working_dir, terraform_releases_html_after_v0_13_0
):
with mock.patch("io.BytesIO", AutoclosingBytesIO):
with pook.use():
repo = "hashicorp/terraform"
releases_url = "https://github.com/{}/tags?after=v0.13.0".format(repo)
# volatile mocks that can only be invoked once each
pook.get(
releases_url,
reply=429,
response_headers={"Status": "429 Too Many Requests", "Date": formatdate(usegmt=True), "Retry-After": "120"},
times=1,
)
pook.get(
releases_url,
reply=200,
response_type="text/plain",
response_body=terraform_releases_html_after_v0_13_0,
response_headers={
"Status": "200 OK",
"ETag": 'W/"df0474ebd25f223a95926ba58e11e77b"',
"Cache-Control": "max-age=0, private, must-revalidate",
"Date": formatdate(usegmt=True),
},
times=1,
)
patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"
# pook does not implement urllib3's retry logic so this first request will always return None during tests
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
assert patch is None
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
assert patch == "14"
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "")
assert patch == "19"
assert pook.isdone()
assert not pook.pending_mocks()
assert not pook.unmatched_requests()
assert not pook.isactive()
def test_search_on_github_cache_terraform_releases_does_not_cache_error_403(
tmp_working_dir,
terraform_releases_html_after_v0_13_0,
):
with mock.patch("io.BytesIO", AutoclosingBytesIO):
with pook.use():
repo = "hashicorp/terraform"
releases_url = "https://github.com/{}/tags?after=v0.13.0".format(repo)
# volatile mocks that can only be invoked once each
pook.get(
releases_url,
reply=403,
response_headers={"Status": "403 Forbidden", "Date": formatdate(usegmt=True)},
times=1,
)
pook.get(
releases_url,
reply=200,
response_type="text/plain",
response_body=terraform_releases_html_after_v0_13_0,
response_headers={
"Status": "200 OK",
"ETag": 'W/"df0474ebd25f223a95926ba58e11e77b"',
"Cache-Control": "max-age=0, private, must-revalidate",
"Date": formatdate(usegmt=True),
},
times=1,
)
patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"
# pook does not implement urllib3's retry logic so this first request will always return None during tests
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
assert patch is None
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
assert patch == "14"
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "")
assert patch == "19"
assert pook.isdone()
assert not pook.pending_mocks()
assert not pook.unmatched_requests()
assert not pook.isactive()
def test_search_on_github_cache_terraform_releases_does_not_cache_error_404(
tmp_working_dir,
terraform_releases_html_after_v0_13_0,
):
with mock.patch("io.BytesIO", AutoclosingBytesIO):
with pook.use():
repo = "hashicorp/terraform"
releases_url = "https://github.com/{}/tags?after=v0.13.0".format(repo)
# volatile mocks that can only be invoked once each
pook.get(
releases_url,
reply=404,
response_headers={"Status": "404 Not found", "Date": formatdate(usegmt=True)},
times=1,
)
pook.get(
releases_url,
reply=200,
response_type="text/plain",
response_body=terraform_releases_html_after_v0_13_0,
response_headers={
"Status": "200 OK",
"ETag": 'W/"df0474ebd25f223a95926ba58e11e77b"',
"Cache-Control": "max-age=0, private, must-revalidate",
"Date": formatdate(usegmt=True),
},
times=1,
)
patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"
# pook does not implement urllib3's retry logic so this first request will always return None during tests
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
assert patch is None
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
assert patch == "14"
patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "")
assert patch == "19"
assert pook.isdone()
assert not pook.pending_mocks()
assert not pook.unmatched_requests()
assert not pook.isactive()