| 1 |
#!/usr/bin/env python |
| 2 |
# coding=utf-8 |
| 3 |
|
| 4 |
# Copyright New York University and the in-toto contributors |
| 5 |
# SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
""" |
| 8 |
<Program Name> |
| 9 |
test_ostree_resolver.py |
| 10 |
|
| 11 |
<Author> |
| 12 |
Aditya Sirish A Yelgundhalli <[email protected]> |
| 13 |
|
| 14 |
<Started> |
| 15 |
May 04, 2023 |
| 16 |
|
| 17 |
<Copyright> |
| 18 |
See LICENSE for licensing information. |
| 19 |
|
| 20 |
<Purpose> |
| 21 |
Test OSTree resolver. |
| 22 |
|
| 23 |
""" |
| 24 |
|
| 25 |
import os |
| 26 |
import unittest |
| 27 |
from pathlib import Path |
| 28 |
|
| 29 |
from in_toto.resolver import OSTreeResolver |
| 30 |
|
| 31 |
|
| 32 |
class TestOSTreeResolver(unittest.TestCase): |
| 33 |
"""Test OSTree resolver implementation.""" |
| 34 |
|
| 35 |
def test_hash_for_branch_with_base_path(self): |
| 36 |
"""Verify hash of test-branch with base_path.""" |
| 37 |
|
| 38 |
resolver = OSTreeResolver( |
| 39 |
base_path=str(Path(__file__).parent / "resolver" / "ostree_repo") |
| 40 |
) |
| 41 |
|
| 42 |
uri = "ostree:test-branch" |
| 43 |
|
| 44 |
expected_artifact_dict = { |
| 45 |
uri: { |
| 46 |
"sha256": "cf3e103a6aed64aec261e2161d1026aa26349d422d238b1ad9e2e2a7eeed8591" |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
artifact_dict = resolver.hash_artifacts([uri]) |
| 51 |
self.assertEqual(artifact_dict, expected_artifact_dict) |
| 52 |
|
| 53 |
def test_hash_for_branch_with_cwd(self): |
| 54 |
"""Verify hash of test-branch from ostree_repo in current dir.""" |
| 55 |
|
| 56 |
current_dir = os.getcwd() |
| 57 |
|
| 58 |
os.chdir(Path(__file__).parent / "resolver" / "ostree_repo") |
| 59 |
resolver = OSTreeResolver() |
| 60 |
|
| 61 |
uri = "ostree:test-branch" |
| 62 |
|
| 63 |
expected_artifact_dict = { |
| 64 |
uri: { |
| 65 |
"sha256": "cf3e103a6aed64aec261e2161d1026aa26349d422d238b1ad9e2e2a7eeed8591" |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
artifact_dict = resolver.hash_artifacts([uri]) |
| 70 |
self.assertEqual(artifact_dict, expected_artifact_dict) |
| 71 |
|
| 72 |
os.chdir(current_dir) |
| 73 |
|
| 74 |
def test_non_existent_ref(self): |
| 75 |
"""Verify expected exception for non existent ref.""" |
| 76 |
|
| 77 |
resolver = OSTreeResolver( |
| 78 |
base_path=str(Path(__file__).parent / "resolver" / "ostree_repo") |
| 79 |
) |
| 80 |
uri = "ostree:invalid" |
| 81 |
with self.assertRaises(FileNotFoundError): |
| 82 |
resolver.hash_artifacts([uri]) |
| 83 |
|
| 84 |
def test_non_existent_ostree_repo(self): |
| 85 |
"""Verify expected exception for non existent repo.""" |
| 86 |
|
| 87 |
resolver = OSTreeResolver( |
| 88 |
base_path=str(Path(__file__).parent / "resolver" / "not_a_repo") |
| 89 |
) |
| 90 |
|
| 91 |
with self.assertRaises(FileNotFoundError): |
| 92 |
resolver.hash_artifacts(["ostree:some-ref"]) |
| 93 |
|
| 94 |
|
| 95 |
if __name__ == "__main__": |
| 96 |
unittest.main() |