Skip to content

Commit 8177847

Browse files
committed
Integration tests predicate on source version check
1 parent 8f241dc commit 8177847

File tree

2 files changed

+77
-27
lines changed

2 files changed

+77
-27
lines changed

tests/conftest.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import boto3
66
import pytest
7+
import requests
78

89

910
def get_distribution_url(stack_name):
@@ -30,6 +31,11 @@ def pytest_addoption(parser):
3031
action="store",
3132
help="enable integration tests against this lambda stack",
3233
)
34+
parser.addoption(
35+
"--src-version",
36+
action="store",
37+
help="enable lambda source version validation",
38+
)
3339

3440

3541
@pytest.fixture
@@ -47,6 +53,19 @@ def cdn_test_url(request):
4753
return url
4854

4955

56+
@pytest.fixture
57+
def src_version(request):
58+
# fetch exodus-lambda git version for version check
59+
# if src_version is unknown, version check will be skipped
60+
if request.config.getoption("--src-version"):
61+
version = request.config.getoption("--src-version")
62+
elif os.environ.get("CODEBUILD_RESOLVED_SOURCE_VERSION"):
63+
version = os.environ.get("CODEBUILD_RESOLVED_SOURCE_VERSION")
64+
else:
65+
return None
66+
return version
67+
68+
5069
def mock_conf_file():
5170
temp_file = tempfile.NamedTemporaryFile(prefix="lambda_unittests_")
5271

@@ -105,3 +124,31 @@ def dummy_private_key():
105124
K8PZxUBy9cZ0KOEpAkA1b7cZpW40ZowMvAH6sF+7Ok1NFd+08AMXLiSJ6z7Sk29s
106125
UrfAc2T6ZnfNC4qLIaDyo87CzVG/wk1Upr21z0YD
107126
-----END RSA PRIVATE KEY-----"""
127+
128+
129+
class VersionCheckingAdapter(requests.adapters.HTTPAdapter):
130+
def __init__(self, expected_version):
131+
self.expected_version = expected_version
132+
super().__init__()
133+
134+
def send(self, request, *args, **kwargs):
135+
request.headers["X-Exodus-Query"] = "1"
136+
response = super().send(request, *args, **kwargs)
137+
138+
version = response.headers["X-Exodus-Version"]
139+
if self.expected_version not in version:
140+
raise AssertionError(
141+
"Expected to run against version %s but server sent X-Exodus-Version: %s"
142+
% (self.expected_version, version)
143+
)
144+
return response
145+
146+
147+
@pytest.fixture
148+
def requests_session(src_version):
149+
session = requests.Session()
150+
if src_version:
151+
# we have an expected version, mount an adapter which
152+
# will check for that version
153+
session.mount("https://", VersionCheckingAdapter(src_version))
154+
return session

tests/integration/test_exodus.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,30 @@
22
import re
33

44
import pytest
5-
import requests
65

76
from ..test_utils.utils import generate_test_cookies
87

98
TEST_COOKIES = generate_test_cookies()
109

1110

12-
def test_exodus_basic(cdn_test_url):
11+
def test_exodus_basic(cdn_test_url, requests_session):
1312
url = (
1413
cdn_test_url
1514
+ "/content/aus/rhel/server/6/6.5/x86_64/os/Packages/c/cpio-2.10-12.el6_5.x86_64.rpm"
1615
)
1716

18-
r = requests.get(url, cookies=TEST_COOKIES)
17+
r = requests_session.get(url, cookies=TEST_COOKIES)
1918
print(json.dumps(dict(r.headers), indent=2))
2019
assert r.status_code == 200
2120
assert "cache-control" not in r.headers
2221

2322

24-
def test_header_not_exist_file(cdn_test_url):
23+
def test_header_not_exist_file(cdn_test_url, requests_session):
2524
url = (
2625
cdn_test_url
2726
+ "/content/aus/rhel/server/6/6.5/x86_64/os/Packages/c/cpio-2.10-12.el6_5.x86_64.rpm_not_exist" # noqa: E501
2827
)
29-
r = requests.get(url, cookies=TEST_COOKIES)
28+
r = requests_session.get(url, cookies=TEST_COOKIES)
3029
print(json.dumps(dict(r.headers), indent=2))
3130
assert r.status_code == 404
3231
assert "cache-control" not in r.headers
@@ -42,21 +41,21 @@ def test_header_not_exist_file(cdn_test_url):
4241

4342

4443
@pytest.mark.parametrize("testdata_path", testdata_cache_control_path)
45-
def test_header_cache_control(cdn_test_url, testdata_path):
44+
def test_header_cache_control(cdn_test_url, requests_session, testdata_path):
4645
url = cdn_test_url + testdata_path
47-
r = requests.get(url, cookies=TEST_COOKIES)
46+
r = requests_session.get(url, cookies=TEST_COOKIES)
4847
print(json.dumps(dict(r.headers), indent=2))
4948
assert r.status_code == 200
5049
assert re.match("^max-age=[0-9]+$", r.headers["cache-control"])
5150

5251

53-
def test_header_want_digest_GET(cdn_test_url):
52+
def test_header_want_digest_GET(cdn_test_url, requests_session):
5453
headers = {"want-digest": "id-sha-256"}
5554
url = (
5655
cdn_test_url
5756
+ "/content/dist/rhel/server/7/7.2/x86_64/rhev-mgmt-agent/3/os/repodata/repomd.xml"
5857
)
59-
r = requests.get(url, headers=headers, cookies=TEST_COOKIES)
58+
r = requests_session.get(url, headers=headers, cookies=TEST_COOKIES)
6059
print(json.dumps(dict(r.headers), indent=2))
6160
assert r.status_code == 200
6261
assert (
@@ -65,13 +64,13 @@ def test_header_want_digest_GET(cdn_test_url):
6564
)
6665

6766

68-
def test_header_want_digest_HEAD(cdn_test_url):
67+
def test_header_want_digest_HEAD(cdn_test_url, requests_session):
6968
headers = {"want-digest": "id-sha-256"}
7069
url = (
7170
cdn_test_url
7271
+ "/content/dist/rhel/server/7/7.2/x86_64/rhev-mgmt-agent/3/os/repodata/repomd.xml"
7372
)
74-
r = requests.head(url, headers=headers, cookies=TEST_COOKIES)
73+
r = requests_session.head(url, headers=headers, cookies=TEST_COOKIES)
7574
print(json.dumps(dict(r.headers), indent=2))
7675
assert r.status_code == 200
7776
assert (
@@ -101,18 +100,22 @@ def assert_content_type(url, content_type):
101100

102101

103102
@pytest.mark.parametrize("testdata_path", testdata_content_type_path)
104-
def test_content_type_header_GET(cdn_test_url, testdata_path):
103+
def test_content_type_header_GET(
104+
cdn_test_url, requests_session, testdata_path
105+
):
105106
url = cdn_test_url + testdata_path
106-
r = requests.get(url, cookies=TEST_COOKIES)
107+
r = requests_session.get(url, cookies=TEST_COOKIES)
107108
print(json.dumps(dict(r.headers), indent=2))
108109
assert r.status_code == 200
109110
assert_content_type(url, r.headers["Content-Type"])
110111

111112

112113
@pytest.mark.parametrize("testdata_path", testdata_content_type_path)
113-
def test_content_type_header_HEAD(cdn_test_url, testdata_path):
114+
def test_content_type_header_HEAD(
115+
cdn_test_url, requests_session, testdata_path
116+
):
114117
url = cdn_test_url + testdata_path
115-
r = requests.head(url, cookies=TEST_COOKIES)
118+
r = requests_session.head(url, cookies=TEST_COOKIES)
116119
print(json.dumps(dict(r.headers), indent=2))
117120
assert r.status_code == 200
118121
assert_content_type(url, r.headers["Content-Type"])
@@ -128,10 +131,10 @@ def test_content_type_header_HEAD(cdn_test_url, testdata_path):
128131

129132
# use Want-Digest/Digest to check if alias take effect
130133
@pytest.mark.parametrize("testdata_path", testdata_origin_alias_path)
131-
def test_origin_path_alias(cdn_test_url, testdata_path):
134+
def test_origin_path_alias(cdn_test_url, requests_session, testdata_path):
132135
headers = {"want-digest": "id-sha-256"}
133136
url = cdn_test_url + testdata_path
134-
r = requests.head(url, headers=headers, cookies=TEST_COOKIES)
137+
r = requests_session.head(url, headers=headers, cookies=TEST_COOKIES)
135138
print(json.dumps(dict(r.headers), indent=2))
136139
assert r.status_code == 200
137140
assert (
@@ -147,10 +150,10 @@ def test_origin_path_alias(cdn_test_url, testdata_path):
147150

148151

149152
@pytest.mark.parametrize("testdata_path", testdata_rhui_alias_path_aus)
150-
def test_rhui_path_alias_aus(cdn_test_url, testdata_path):
153+
def test_rhui_path_alias_aus(cdn_test_url, requests_session, testdata_path):
151154
headers = {"want-digest": "id-sha-256"}
152155
url = cdn_test_url + testdata_path
153-
r = requests.head(url, headers=headers, cookies=TEST_COOKIES)
156+
r = requests_session.head(url, headers=headers, cookies=TEST_COOKIES)
154157
print(json.dumps(dict(r.headers), indent=2))
155158
assert r.status_code == 200
156159
assert (
@@ -166,10 +169,10 @@ def test_rhui_path_alias_aus(cdn_test_url, testdata_path):
166169

167170

168171
@pytest.mark.parametrize("testdata_path", testdata_rhui_alias_path_rhel8)
169-
def test_rhui_path_alias_rhel8(cdn_test_url, testdata_path):
172+
def test_rhui_path_alias_rhel8(cdn_test_url, requests_session, testdata_path):
170173
headers = {"want-digest": "id-sha-256"}
171174
url = cdn_test_url + testdata_path
172-
r = requests.head(url, headers=headers, cookies=TEST_COOKIES)
175+
r = requests_session.head(url, headers=headers, cookies=TEST_COOKIES)
173176
print(json.dumps(dict(r.headers), indent=2))
174177
assert r.status_code == 200
175178
assert (
@@ -185,10 +188,10 @@ def test_rhui_path_alias_rhel8(cdn_test_url, testdata_path):
185188

186189

187190
@pytest.mark.parametrize("testdata_path", testdata_releasever_alias_rhel6)
188-
def test_releasever_alias_rhel6(cdn_test_url, testdata_path):
191+
def test_releasever_alias_rhel6(cdn_test_url, requests_session, testdata_path):
189192
headers = {"want-digest": "id-sha-256"}
190193
url = cdn_test_url + testdata_path
191-
r = requests.head(url, headers=headers, cookies=TEST_COOKIES)
194+
r = requests_session.head(url, headers=headers, cookies=TEST_COOKIES)
192195
print(json.dumps(dict(r.headers), indent=2))
193196
assert r.status_code == 200
194197
assert (
@@ -203,9 +206,9 @@ def test_releasever_alias_rhel6(cdn_test_url, testdata_path):
203206

204207

205208
@pytest.mark.parametrize("testdata_path", testdata_no_content_type)
206-
def test_no_content_type(cdn_test_url, testdata_path):
209+
def test_no_content_type(cdn_test_url, requests_session, testdata_path):
207210
url = cdn_test_url + testdata_path
208-
r = requests.get(url, cookies=TEST_COOKIES)
211+
r = requests_session.get(url, cookies=TEST_COOKIES)
209212
print(json.dumps(dict(r.headers), indent=2))
210213
assert r.status_code == 200
211214
assert r.headers["Content-Type"] == "application/octet-stream"
@@ -217,8 +220,8 @@ def test_no_content_type(cdn_test_url, testdata_path):
217220

218221

219222
@pytest.mark.parametrize("testdata_path", testdata_absent_item)
220-
def test_absent_item(cdn_test_url, testdata_path):
223+
def test_absent_item(cdn_test_url, requests_session, testdata_path):
221224
url = cdn_test_url + testdata_path
222-
r = requests.get(url, cookies=TEST_COOKIES)
225+
r = requests_session.get(url, cookies=TEST_COOKIES)
223226
print(json.dumps(dict(r.headers), indent=2))
224227
assert r.status_code == 404

0 commit comments

Comments
 (0)