Skip to content

Commit ae019d9

Browse files
committed
tests: sample images files can run from a locally sourced file archive
1 parent ca051cb commit ae019d9

File tree

4 files changed

+78
-36
lines changed

4 files changed

+78
-36
lines changed

Jenkinsfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ pipeline {
536536
-Wdev \
537537
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON \
538538
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=true \
539+
-DSAMPLE_IMAGES_ARCHIVE=${SAMPLE_IMAGES_ARCHIVE} \
539540
-DBUILD_TESTING:BOOL=true \
540541
-DCMAKE_CXX_FLAGS="-fno-inline -fno-omit-frame-pointer -fprofile-arcs -ftest-coverage -Wall -Wextra" \
541542
-DMEMORYCHECK_COMMAND=$(which drmemory) \

ci/docker/linux/jenkins/Dockerfile

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ ARG CONAN_USER_HOME=/conan
22

33
ARG CONAN_HOME=${CONAN_USER_HOME}/.conan2
44

5+
56
ARG CONAN_CENTER_PROXY_V2_URL=https://center2.conan.io
67
# If you want to use a diffrent remote for Conan, such as a proxy. Set the CONAN_CENTER_PROXY_V2_URL
78
# Not this is only for building the image. The actual conan center proxy URL is set in the remotes.json file.
@@ -12,6 +13,8 @@ ARG PIP_DOWNLOAD_CACHE=/.cache/pip
1213
ARG UV_CACHE_DIR=/.cache/uv
1314
# UV Cache directory. Change this only if you need to override the default location.
1415

16+
ARG SAMPLE_IMAGES_URL=https://nexus.library.illinois.edu/repository/sample-data/images/metadata_test_images.tar.gz
17+
1518
FROM ghcr.io/astral-sh/uv:latest AS uv_builder
1619

1720
FROM ubuntu:24.04 AS base_image
@@ -112,6 +115,21 @@ RUN --mount=type=cache,target=${PIP_DOWNLOAD_CACHE} \
112115

113116
# shared files are needed to run the Build C++ Tests stage
114117

118+
#==============================================================================
119+
120+
121+
FROM base_image AS sample_files_downloader
122+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
123+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
124+
apt-get update && \
125+
apt-get install -y --no-install-recommends wget
126+
127+
ARG SAMPLE_IMAGES_URL
128+
ENV EXPECTED_SHA256="eee5fa3641628c7365e14de3f58da069cb332dc13a4b739dc168a1617109ebbf"
129+
RUN mkdir /sampledata && wget $SAMPLE_IMAGES_URL -P /sampledata && echo "$EXPECTED_SHA256 /sampledata/metadata_test_images.tar.gz" | sha256sum --check
130+
131+
132+
115133
FROM base_image
116134
COPY --from=uv_builder /uv /uvx /bin/
117135
# -----------------------------------------------------------------------------
@@ -136,14 +154,15 @@ RUN ln -s /opt/drmemory/bin64/drmemory /usr/local/bin/drmemory && \
136154

137155
ARG PIP_EXTRA_INDEX_URL
138156

139-
ENV PIP_NO_CACHE_DIR=1
140-
141157
ARG CONAN_USER_HOME
142-
ENV CONAN_USER_HOME=${CONAN_USER_HOME}
143158
ARG CONAN_HOME
144-
ENV CONAN_HOME=${CONAN_HOME}
145159
COPY --from=conan_builder --chmod=777 ${CONAN_HOME} ${CONAN_HOME}
146-
ENV TZ=UTC
160+
COPY --from=sample_files_downloader /sampledata /sampledata
161+
ENV TZ=UTC \
162+
PIP_NO_CACHE_DIR=1 \
163+
CONAN_USER_HOME=${CONAN_USER_HOME} \
164+
SAMPLE_IMAGES_ARCHIVE=/sampledata/metadata_test_images.tar.gz \
165+
CONAN_HOME=${CONAN_HOME}
147166

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

tests/CMakeLists.txt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
if (BUILD_TESTING AND Catch2_FOUND)
22
include(FetchContent)
33
include(Catch)
4+
set(SAMPLE_IMAGES_ARCHIVE "" CACHE FILEPATH "Path to sample images archive. If not set, it will be downloaded from the internet.")
45

56
# Get sample images
6-
if(NOT EXISTS ${PROJECT_BINARY_DIR}/metadata_test_images.tar.gz)
7-
file(DOWNLOAD https://nexus.library.illinois.edu/repository/sample-data/images/metadata_test_images.tar.gz ${PROJECT_BINARY_DIR}/metadata_test_images.tar.gz SHOW_PROGRESS)
8-
endif()
9-
FetchContent_Declare(
10-
test_images
11-
URL ${PROJECT_BINARY_DIR}/metadata_test_images.tar.gz
12-
)
13-
FetchContent_MakeAvailable(test_images)
7+
if(SAMPLE_IMAGES_ARCHIVE)
8+
set(TEST_IMAGE_PATH "${CMAKE_CURRENT_BINARY_DIR}/metadata_test_images/")
9+
file(ARCHIVE_EXTRACT
10+
INPUT "${SAMPLE_IMAGES_ARCHIVE}"
11+
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/"
12+
)
13+
else ()
14+
FetchContent_Declare(
15+
test_images
16+
URL ${PROJECT_BINARY_DIR}/metadata_test_images.tar.gz
17+
)
18+
FetchContent_MakeAvailable(test_images)
19+
set(TEST_IMAGE_PATH ${test_images_SOURCE_DIR})
20+
endif ()
1421
add_executable(tester test-basic.cpp test_exceptions.cpp)
1522
target_include_directories(tester PRIVATE ${PROJECT_SOURCE_DIR}/uiucprescon/imagevalidate)
1623

17-
target_compile_definitions(tester PRIVATE TEST_IMAGE_PATH="${test_images_SOURCE_DIR}")
24+
target_compile_definitions(tester PRIVATE TEST_IMAGE_PATH="${TEST_IMAGE_PATH}")
1825
target_link_libraries(tester PRIVATE Catch2::Catch2WithMain imagevalidate::glue)
1926
catch_discover_tests(tester
2027
TEST_PREFIX imagevalidate.
@@ -24,7 +31,7 @@ if (BUILD_TESTING AND Catch2_FOUND)
2431

2532
add_executable(tester-internal tester-internal.cpp)
2633
target_compile_definitions(tester-internal
27-
PRIVATE TEST_IMAGE_PATH="${test_images_SOURCE_DIR}")
34+
PRIVATE TEST_IMAGE_PATH="${TEST_IMAGE_PATH}")
2835

2936

3037
target_link_libraries(tester-internal

tests/test_integration.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hashlib
12
import shutil
23

34
import pytest
@@ -11,25 +12,20 @@
1112

1213
# TEST_PATH = os.path.join(os.path.dirname(__file__), "henrytestmetadata")
1314
SAMPLE_IMAGES = "https://nexus.library.illinois.edu/repository/sample-data/images/metadata_test_images.tar.gz"
15+
SAMPLE_IMAGES_SHA256 = "eee5fa3641628c7365e14de3f58da069cb332dc13a4b739dc168a1617109ebbf"
1416

17+
# To save time from redownloading the metadata_test_images.tar.gz file every time,
18+
# download it locally and set the environment variable SAMPLE_IMAGES_ARCHIVE
19+
# to the path of the downloaded file
1520

16-
def download_images(url, destination):
17-
with TemporaryDirectory() as download_path:
18-
print("Downloading {}".format(url), flush=True)
19-
urllib.request.urlretrieve(url,
20-
filename=os.path.join(download_path,
21-
"sample_images.tar.gz"))
22-
if not os.path.exists(
23-
os.path.join(download_path, "sample_images.tar.gz")):
24-
raise FileNotFoundError("sample images not download")
25-
print("Extracting images")
26-
with tarfile.open(os.path.join(download_path, "sample_images.tar.gz"),
27-
"r:gz") as archive_file:
28-
for item in archive_file.getmembers():
29-
print("Extracting {}".format(item.name))
30-
archive_file.extract(item, path=destination)
31-
pass
21+
def download_images(url, download_path):
3222

23+
print(f"Downloading {url}")
24+
output = os.path.join(download_path, "sample_images.tar.gz")
25+
urllib.request.urlretrieve(url, filename=output)
26+
if not os.path.exists(output):
27+
raise FileNotFoundError("sample images not download")
28+
return output
3329

3430
@pytest.fixture(scope="session")
3531
def sample_data():
@@ -40,14 +36,33 @@ def sample_data():
4036
if os.path.exists(sample_images_path):
4137
print("{} already exits".format(sample_images_path))
4238
else:
43-
print("Downloading sample images")
44-
if not os.path.exists(sample_images_path):
45-
download_images(
46-
url=SAMPLE_IMAGES,
47-
destination=test_path)
39+
archive = os.getenv('SAMPLE_IMAGES_ARCHIVE')
40+
if not archive:
41+
print("Downloading sample images")
42+
if not os.path.exists(sample_images_path):
43+
archive = download_images(
44+
url=SAMPLE_IMAGES,
45+
download_path=test_path
46+
)
47+
if not os.path.exists(archive):
48+
raise FileNotFoundError(f"sample image archive not found. {archive} does not exist.")
49+
verify_hash(archive, sha256_hash=SAMPLE_IMAGES_SHA256)
50+
extract_images(path=archive, destination=test_path)
4851
yield sample_images_path
4952
shutil.rmtree(sample_images_path)
5053

54+
def extract_images(path, destination):
55+
print("Extracting images")
56+
with tarfile.open(path, "r:gz") as archive_file:
57+
for item in archive_file.getmembers():
58+
print("Extracting {}".format(item.name))
59+
archive_file.extract(item, path=destination)
60+
61+
def verify_hash(path, sha256_hash):
62+
with open(path, "rb") as f:
63+
file_hash = hashlib.sha256(f.read()).hexdigest()
64+
assert file_hash == sha256_hash
65+
5166

5267
@pytest.mark.integration
5368
@pytest.mark.parametrize("test_file,profile_name", [

0 commit comments

Comments
 (0)