forked from kernelci/kcidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
224 lines (203 loc) · 7.33 KB
/
test_main.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
"""main.py tests"""
import os
import subprocess
import unittest
from importlib import import_module
from urllib.parse import quote
import time
import yaml
import requests
import kcidb
@unittest.skipIf(os.environ.get("KCIDB_DEPLOYMENT"), "local-only")
def test_google_credentials_are_not_specified():
"""Check Google Application credentials are not specified"""
assert os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") is None, \
"Local tests must run without " \
"GOOGLE_APPLICATION_CREDENTIALS " \
"environment variable"
def test_import():
"""Check main.py can be loaded"""
# Load deployment environment variables
file_dir = os.path.dirname(os.path.abspath(__file__))
cloud_path = os.path.join(file_dir, "cloud")
env = yaml.safe_load(
subprocess.check_output([
cloud_path,
"env", "kernelci-production", "", "0",
"--log-level=DEBUG"
])
)
env["GCP_PROJECT"] = "TEST_PROJECT"
orig_env = dict(os.environ)
try:
os.environ.update(env)
import_module("main")
finally:
os.environ.clear()
os.environ.update(orig_env)
def check_url_in_cache(url):
"""Check whether the URL is sourced from storage or not."""
url_encoded = quote(url)
cache_redirector_url = os.environ["KCIDB_CACHE_REDIRECTOR_URL"]
response = requests.get(
f"{cache_redirector_url}?{url_encoded}",
timeout=10, # Time in secs
allow_redirects=False
)
if response.status_code == 302:
# Check if the redirect URL matches the blob storage URL pattern
if (
response.headers.get("Location", "")
.startswith('https://storage.googleapis.com/')
):
return True
return False
def test_url_caching(empty_deployment):
"""kcidb cache client workflow test"""
# Make empty_deployment appear used to silence pylint warning
assert empty_deployment is None
client = kcidb.Client(
project_id=os.environ["GCP_PROJECT"],
topic_name=os.environ["KCIDB_LOAD_QUEUE_TOPIC"]
)
data = {
"version": {
"major": 4,
"minor": 0
},
"checkouts": [
{
"id": "_:1",
"origin": "_",
"log_url":
"https://github.com/kernelci/kcidb/blob/main"
"/.github/workflows/deploy.yml?padding=4821",
"patchset_files": [
{
"name": "file",
"url":
"https://github.com/kernelci/kcidb/"
"blob/main/doc/installation.md?padding=1505"
}
],
},
],
"builds": [
{
"id": "_:1",
"origin": "_",
"input_files": [
{
"name": "kernel_image1",
"url":
"https://github.com/kernelci/kcidb/blob/"
"main/doc/_index.md?padding=7230"
}
],
"log_url":
"https://github.com/kernelci/kcidb/blob/main"
"/.pylintrc?padding=247",
"config_url":
"https://github.com/kernelci/kcidb/blob/main"
"/.gitignore?padding=547",
"output_files": [
{
"name": "kernel_image",
"url":
"https://github.com/kernelci/kcidb/"
"blob/main/Dockerfile"
},
{
"name": "kernel",
"url": "https://kernelcdn.kernel.org/pub/linux/"
}
],
"checkout_id": "_:1",
},
],
"tests": [
{
"build_id": "kernelci:kernelci.org:64147283e6021132258c86c0",
"id": "_:1",
"origin": "_",
"log_url":
"https://cdn.kernel.org/pub/linux/"
"kernel/v6.x/linux-6.4.11.tar.xz",
},
{
"build_id": "kernelci:kernelci.org:64147283e6021132258c86c0",
"id": "_:1",
"origin": "_",
"output_files": [
{
"name": "x86_64_4_console.log",
"url":
"https://github.com/kernelci/kcidb/"
"blob/main/setup.py?padding=4673"
},
{
"name": "x86_64_4_IOMMU_boot_test_dmesg.log",
"url":
"https://github.com/kernelci/kcidb/blob"
"/main/requirements.txt?padding=1649"
},
],
"environment": {
"comment": "meson-s905d-p230 in lab-baylibre",
"misc": {
"rootfs_url":
"https://github.com/kernelci/kcidb/tree/main/"
"kcidb/?padding=4165"
}
},
},
],
}
# Submit data to submission queue
client.submit(data)
current_time = time.time()
# The time when caching system should be done with all our data
deadline_time = current_time + 180
# URLs to be check, as they should be cached
urls_expected = [
"https://github.com/kernelci/kcidb/blob/main/setup.py?padding=4673",
"https://github.com/kernelci/kcidb/blob/main/requirements.txt?"
"padding=1649",
"https://github.com/kernelci/kcidb/blob/main/.gitignore?"
"padding=547",
"https://github.com/kernelci/kcidb/blob/main/.pylintrc?padding=247",
"https://github.com/kernelci/kcidb/blob/main/doc/_index.md?"
"padding=7230",
"https://github.com/kernelci/kcidb/blob/main/.github/workflows/"
"deploy.yml?padding=4821",
"https://github.com/kernelci/kcidb/blob/main/doc/installation.md?"
"padding=1505"
]
# Retry checking URLs in the cache for a minute
retry_interval = 5 # seconds
for url in urls_expected:
while True:
if check_url_in_cache(url):
break
if time.time() > deadline_time:
raise AssertionError(f"URL '{url}' not found in the cache")
time.sleep(retry_interval)
current_time = time.time()
if current_time < deadline_time:
time.sleep(deadline_time - current_time)
# URL cases not to be cached
urls_not_expected = [
# Invalid url
'https://non-existing-name.kernel.org/pub/linux/',
# Larger-than-maximum size URL
"https://cdn.kernel.org/pub/linux/kernel/v6.x/"
"linux-6.4.11.tar.xz",
# Wrong hash URL
"https://github.com/kernelci/kcidb/blob/main/Dockerfile",
# URL from a wrong field
"https://github.com/kernelci/kcidb/tree/main/kcidb/?padding=4165"
]
for url_not_expected in urls_not_expected:
if check_url_in_cache(url_not_expected):
raise AssertionError(f"Unexpected URL '{url_not_expected}' \
found in the cache")