Skip to content

Commit bc467e4

Browse files
committed
tests: verify hash value of sample images
1 parent 58d3a62 commit bc467e4

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

ci/docker/linux/jenkins/Dockerfile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
ARG CONAN_USER_HOME=/conan
22
ARG CONAN_HOME=${CONAN_USER_HOME}/.conan2
33

4+
ARG SAMPLE_IMAGES_URL="https://nexus.library.illinois.edu/repository/sample-data/images/sample_images.tar.gz"
5+
46
ARG PYTHON_VERSION=latest
57

68
ARG CONAN_CENTER_PROXY_V2_URL=https://center2.conan.io
@@ -33,6 +35,12 @@ RUN mkdir -p /.cache/pip && \
3335
#==============================================================================
3436

3537

38+
FROM base_image AS sample_files_downloader
39+
ARG SAMPLE_IMAGES_URL
40+
ENV EXPECTED_SHA256="0461f57db3806ca47d9063151eec4bc0720c66a83153dda04e230f838b64f063"
41+
RUN mkdir /sampledata && wget $SAMPLE_IMAGES_URL -P /sampledata && echo "$EXPECTED_SHA256 /sampledata/sample_images.tar.gz" | sha256sum --check
42+
43+
3644
FROM base_image AS sonar_builder
3745
ARG SONAR_USER_HOME
3846

@@ -141,11 +149,14 @@ COPY --from=dr_memory_builder /opt/drmemory /opt/drmemory/
141149
RUN ln -s /opt/drmemory/bin64/drmemory /usr/local/bin/drmemory && \
142150
drmemory -version
143151

152+
COPY --from=sample_files_downloader /sampledata /sampledata
153+
144154
ARG CONAN_USER_HOME
145155
ARG CONAN_HOME
146156
COPY --from=conan_builder --chmod=777 ${CONAN_HOME} ${CONAN_HOME}
147157
ENV CONAN_USER_HOME=${CONAN_USER_HOME}\
148-
CONAN_HOME=${CONAN_HOME}
158+
CONAN_HOME=${CONAN_HOME}\
159+
SAMPLE_IMAGES_ARCHIVE=/sampledata
149160

150161
# To help mark the image as a CI image so it can be cleaned up more easily
151162
LABEL purpose=ci

tests/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ include(FetchContent)
22
if (BUILD_TESTING AND Catch2_FOUND)
33
include(Catch)
44
FetchContent_Declare(test_images
5-
URL https://nexus.library.illinois.edu/repository/sample-data/images/sample_images.tar.gz
6-
SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/sample_images
7-
)
5+
URL https://nexus.library.illinois.edu/repository/sample-data/images/sample_images.tar.gz
6+
URL_HASH SHA256=0461f57db3806ca47d9063151eec4bc0720c66a83153dda04e230f838b64f063
7+
SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/sample_images
8+
)
89
FetchContent_GetProperties(test_images)
910
FetchContent_MakeAvailable(test_images)
1011
find_path(TEST_IMAGE_PATH dummy.jp2

tests/conftest.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
import os
22
import shutil
33
import tarfile
4+
import hashlib
45

56
import pytest
67
import urllib.request
78

9+
SAMPLE_IMAGES_SHA256 = "0461f57db3806ca47d9063151eec4bc0720c66a83153dda04e230f838b64f063"
10+
SAMPLE_IMAGES_URL = "https://nexus.library.illinois.edu/repository/sample-data/images/sample_images.tar.gz"
811

9-
def download_images(url, destination, download_path):
12+
# To save time from redownloading the sample_images.tar.gz file every time,
13+
# download it locally and set the environment variable SAMPLE_IMAGES_ARCHIVE
14+
# to the path of the downloaded file
1015

11-
print("Downloading {}".format(url))
12-
urllib.request.urlretrieve(url,
13-
filename=os.path.join(download_path, "sample_images.tar.gz"))
14-
if not os.path.exists(os.path.join(download_path, "sample_images.tar.gz")):
16+
def download_images(url, download_path):
17+
18+
print(f"Downloading {url}")
19+
output = os.path.join(download_path, "sample_images.tar.gz")
20+
urllib.request.urlretrieve(url, filename=output)
21+
if not os.path.exists(output):
1522
raise FileNotFoundError("sample images not download")
16-
print("Extracting images")
17-
with tarfile.open(os.path.join(download_path, "sample_images.tar.gz"), "r:gz") as archive_file:
18-
for item in archive_file.getmembers():
19-
print("Extracting {}".format(item.name))
20-
archive_file.extract(item, path=destination)
21-
pass
23+
return output
24+
def extract_images(path, destination):
25+
print("Extracting images")
26+
with tarfile.open(os.path.join(path, "sample_images.tar.gz"), "r:gz") as archive_file:
27+
for item in archive_file.getmembers():
28+
print("Extracting {}".format(item.name))
29+
archive_file.extract(item, path=destination)
30+
31+
def verify_hash(path, sha256_hash):
32+
with open(path, "rb") as f:
33+
file_hash = hashlib.sha256(f.read()).hexdigest()
34+
assert file_hash == sha256_hash
2235

2336

2437
@pytest.fixture(scope="session")
@@ -28,16 +41,21 @@ def sample_images_readonly(tmpdir_factory):
2841
sample_images_path = os.path.join(test_path, "sample_images")
2942
download_path = tmpdir_factory.mktemp("downloaded_archives", numbered=False)
3043
if os.path.exists(sample_images_path):
31-
print("{} already exits".format(sample_images_path))
44+
print(f"{sample_images_path} already exits")
3245

3346
else:
34-
print("Downloading sample images")
35-
download_images(url="https://nexus.library.illinois.edu/repository/sample-data/images/sample_images.tar.gz",
36-
destination=test_path,
37-
download_path=download_path)
38-
47+
archive = os.getenv('SAMPLE_IMAGES_ARCHIVE')
48+
if not archive:
49+
print("Downloading sample images")
50+
archive = download_images(
51+
url=SAMPLE_IMAGES_URL,
52+
download_path=download_path
53+
)
54+
verify_hash(archive, sha256_hash=SAMPLE_IMAGES_SHA256)
55+
extract_images(path=download_path, destination=test_path)
3956
yield sample_images_path
40-
shutil.rmtree(test_path)
57+
if os.path.exists(test_path):
58+
shutil.rmtree(test_path)
4159

4260

4361
@pytest.fixture

0 commit comments

Comments
 (0)