Skip to content

Commit 13fea45

Browse files
committed
RHOAIENG-20088: chore(tests/containers): check images size change
Adds a new test to our pytest set to check the given image size compared to the expected value. In this test we are checking the uncompressed image size by summing up all the layeres of the image. This image is usually downloaded on the machine where this is being run already. https://issues.redhat.com/browse/RHOAIENG-20088
1 parent 92c2dde commit 13fea45

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/containers/base_image_test.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,67 @@ def test_fn(container: testcontainers.core.container.DockerContainer):
242242
self._run_test(image=image, test_fn=test_fn)
243243

244244

245+
# There are two ways how the image is being updated
246+
# 1. A change to the image is being done (e.g. package update, Dockerfile update etc.). This is what this test does.
247+
# In this case, we need to check the size of the build image that contains these updates. We're checking the compressed image size.
248+
# 2. A change is done to the params.env file or runtimes images definitions, where we update manifest references to a new image.
249+
# Check for this scenario is being done in 'ci/[check-params-env.sh|check-runtime-images.sh]'.
250+
size_treshold: int = 100 # in MBs
251+
percent_treshold: int = 10
252+
def test_image_size_change(self, image: str):
253+
"""Checks the image size didn't change extensively based on the size and percent defined tresholds."""
254+
255+
# Map of image label names with expected size in MBs.
256+
expected_image_name_size_map = {
257+
"odh-notebook-code-server-ubi9-python-3.11": 2820,
258+
"odh-notebook-cuda-jupyter-tensorflow-ubi9-python-3.11": 14813,
259+
"odh-notebook-jupyter-cuda-minimal-ubi9-python-3.11": 8920,
260+
"odh-notebook-jupyter-cuda-pytorch-ubi9-python-3.11": 15540,
261+
"odh-notebook-jupyter-datascience-ubi9-python-3.11": 3068,
262+
"odh-notebook-jupyter-minimal-ubi9-python-3.11": 1676,
263+
"odh-notebook-jupyter-rocm-minimal-ubi9-python-3.11": 28675,
264+
"odh-notebook-jupyter-rocm-pytorch-ubi9-python-3.11": 34321,
265+
"odh-notebook-jupyter-rocm-tensorflow-ubi9-python-3.11": 32166,
266+
"odh-notebook-jupyter-trustyai-ubi9-python-3.11": 8785,
267+
"odh-notebook-rstudio-server-c9s-python-3.11": 3480,
268+
"odh-notebook-rstudio-server-cuda-c9s-python-3.11": 12250,
269+
"odh-notebook-runtime-datascience-ubi9-python-3.11": 2750,
270+
"odh-notebook-runtime-minimal-ubi9-python-3.11": 1561,
271+
"odh-notebook-runtime-pytorch-ubi9-python-3.11": 15234,
272+
"odh-notebook-cuda-runtime-tensorflow-ubi9-python-3.11": 14488,
273+
"odh-notebook-runtime-rocm-pytorch-ubi9-python-3.11": 34009,
274+
"odh-notebook-rocm-runtime-tensorflow-ubi9-python-3.11": 31834,
275+
}
276+
277+
import docker
278+
client = testcontainers.core.container.DockerClient()
279+
try:
280+
image_metadata = client.client.images.get(image)
281+
except docker.errors.ImageNotFound:
282+
image_metadata = client.client.images.pull(image)
283+
assert isinstance(image_metadata, docker.models.images.Image)
284+
285+
actual_img_size = image_metadata.attrs["Size"]
286+
actual_img_size = round(actual_img_size / 1024 / 1024)
287+
logging.info(f"The size of the image is {actual_img_size} MBs.")
288+
logging.debug(f"The image metadata: {image_metadata}")
289+
290+
img_label_name = image_metadata.labels["name"]
291+
if img_label_name in expected_image_name_size_map:
292+
expected_img_size = expected_image_name_size_map[img_label_name]
293+
logging.debug(f"Expected size of the '{img_label_name}' image is {expected_img_size} MBs.")
294+
else:
295+
pytest.fail(f"Image name label '{img_label_name}' is not in the expected image size map {expected_image_name_size_map}")
296+
297+
# Check the size change constraints now
298+
# 1. Percentual size change
299+
abs_percent_change = abs(actual_img_size / expected_img_size * 100 - 100)
300+
assert abs_percent_change < self.percent_treshold, f"Image size of '{img_label_name}' changed by {abs_percent_change}% (expected: {expected_img_size} MB; actual: {actual_img_size} MB; treshold: {self.percent_treshold}%)."
301+
# 2. Absolute size change
302+
abs_size_difference = abs(actual_img_size - expected_img_size)
303+
assert abs_size_difference < self.size_treshold, f"Image size of '{img_label_name}' changed by {abs_size_difference} MB (expected: {expected_img_size} MB; actual: {actual_img_size} MB; treshold: {self.size_treshold} MB)."
304+
305+
245306
def encode_python_function_execution_command_interpreter(
246307
python: str, function: Callable[..., Any], *args: list[Any]
247308
) -> list[str]:

0 commit comments

Comments
 (0)