From ba1a8c111b3610a1ed67c03b8b4ba671b6d06ed3 Mon Sep 17 00:00:00 2001 From: tarilabs Date: Tue, 17 Dec 2024 16:20:48 +0100 Subject: [PATCH] test: add additional basics tests Signed-off-by: tarilabs --- olot/basics.py | 4 ++-- tests/basic_test.py | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/olot/basics.py b/olot/basics.py index c63aca3..00bd139 100644 --- a/olot/basics.py +++ b/olot/basics.py @@ -41,13 +41,13 @@ def get_file_hash(path) -> str: def oci_layers_on_top(ocilayout: Path, model_files): check_ocilayout(ocilayout) ocilayout_root_index = read_ocilayout_root_index(ocilayout) + ocilayout_indexes: Dict[str, OCIImageIndex] = crawl_ocilayout_indexes(ocilayout, ocilayout_root_index) + ocilayout_manifests: Dict[str, OCIImageManifest] = crawl_ocilayout_manifests(ocilayout, ocilayout_indexes) new_layers = [] for model in model_files: model = Path(model) new_layer = tar_into_ocilayout(ocilayout, model) new_layers.append(new_layer) - ocilayout_indexes: Dict[str, OCIImageIndex] = crawl_ocilayout_indexes(ocilayout, ocilayout_root_index) - ocilayout_manifests: Dict[str, OCIImageManifest] = crawl_ocilayout_manifests(ocilayout, ocilayout_indexes) new_ocilayout_manifests: Dict[str, str] = {} for manifest_hash, manifest in ocilayout_manifests.items(): print(manifest_hash, manifest.mediaType) diff --git a/tests/basic_test.py b/tests/basic_test.py index 6edc62f..0a7fc27 100644 --- a/tests/basic_test.py +++ b/tests/basic_test.py @@ -6,7 +6,7 @@ import pytest -from olot.basics import HashingWriter, get_file_hash, check_ocilayout, read_ocilayout_root_index, crawl_ocilayout_indexes, crawl_ocilayout_manifests +from olot.basics import HashingWriter, get_file_hash, check_ocilayout, read_ocilayout_root_index, crawl_ocilayout_indexes, crawl_ocilayout_manifests, compute_hash_of_str, tar_into_ocilayout from olot.oci.oci_image_index import OCIImageIndex from olot.oci.oci_image_manifest import OCIImageManifest @@ -19,6 +19,14 @@ def test_get_file_hash(): assert checksum_from_disk == "d91aa8aa7b56706b89e4a9aa27d57f45785082ba40e8a67e58ede1ed5709afd8" +def test_compute_hash_of_str(): + """Basis compute_hash_of_str() fn testing + """ + my_string = "Hello, World! Thanks Andrew, Roland, Syed and Quay team for the idea :)" + actual = compute_hash_of_str(my_string) + assert actual == "8695589abd30ec83cde42fabc3b4f6fd7da629eca94cf146c7920c6b067f4087" # can use echo -n "Hello, World! Thanks Andrew, Roland, Syed and Quay team for the idea :)" | shasum -a 256 + + def test_bespoke_single_file_tar(tmp_path): """Example bespoke use of HashingWriter for .tar """ @@ -48,6 +56,22 @@ def test_bespoke_single_file_tar(tmp_path): print(file) +def test_tar_into_ocilayout(tmp_path): + """Test tar_into_ocilayout() function is able to produce the expected tar (uncompressed) layer blob in the oci-layout + """ + model_path = Path(__file__).parent / "data" / "model.joblib" + sha256_path = tmp_path / "blobs" / "sha256" + sha256_path.mkdir(parents=True, exist_ok=True) + digest = tar_into_ocilayout(tmp_path, model_path) # forcing it into a partial temp directory with blobs subdir for tests + + for file in tmp_path.rglob('*'): + if file.is_file(): + print(file) + + checksum_from_disk = get_file_hash(sha256_path / digest) # read the file + assert digest == checksum_from_disk # filename should match its digest + + def test_bespoke_single_file_gz(tmp_path): """Example bespoke use of HashingWriter for .tar.gz """ @@ -150,3 +174,4 @@ def test_crawl_ocilayout_manifests(): assert layer0.digest == "sha256:1933e30a3373776d5c7155591a6dacbc205cf6a2665b6dced682c6d2ea7b000f" assert layer0.size == 1949749 assert layer0.mediaType == "application/vnd.oci.image.layer.v1.tar+gzip" +