| 1 |
"""Test cases for resolver.py.""" |
| 2 |
from pathlib import Path |
| 3 |
from unittest import TestCase, main |
| 4 |
|
| 5 |
from in_toto.resolver import RESOLVER_FOR_URI_SCHEME, FileResolver, Resolver |
| 6 |
from tests.common import TmpDirMixin |
| 7 |
|
| 8 |
|
| 9 |
class TestResolver(TestCase): |
| 10 |
"""Test default and custom resolver registration and dispatch.""" |
| 11 |
|
| 12 |
def test_register(self): |
| 13 |
self.assertFalse(RESOLVER_FOR_URI_SCHEME) # assert empty |
| 14 |
|
| 15 |
class Default(Resolver): |
| 16 |
def hash_artifacts(self, uris): |
| 17 |
pass |
| 18 |
|
| 19 |
class Custom(Resolver): |
| 20 |
def hash_artifacts(self, uris): |
| 21 |
pass |
| 22 |
|
| 23 |
# uris with: default scheme, non-registered scheme, no scheme |
| 24 |
uris = ["file:path/to/file", "C:\\path\\to\\file", "path/to/file"] |
| 25 |
|
| 26 |
# Raise if no scheme is registered (default must be registered too) |
| 27 |
for uri in uris: |
| 28 |
with self.assertRaises(KeyError, msg=f"uri={uri}"): |
| 29 |
Resolver.for_uri(uri) |
| 30 |
|
| 31 |
RESOLVER_FOR_URI_SCHEME["file"] = Default() |
| 32 |
RESOLVER_FOR_URI_SCHEME["custom"] = Custom() |
| 33 |
|
| 34 |
# Once registered, all uris w/o (registered) scheme resolve to default. |
| 35 |
for uri in uris: |
| 36 |
self.assertIsInstance(Resolver.for_uri(uri), Default, f"uri={uri}") |
| 37 |
|
| 38 |
self.assertIsInstance(Resolver.for_uri("custom:path"), Custom) |
| 39 |
self.assertIsInstance(Resolver.for_uri("custom"), Default) # no scheme |
| 40 |
|
| 41 |
RESOLVER_FOR_URI_SCHEME.clear() |
| 42 |
|
| 43 |
|
| 44 |
class TestFileResolver(TmpDirMixin, TestCase): |
| 45 |
"""Test hash_artifacts with and without scheme. |
| 46 |
|
| 47 |
See 'test_runlib' for comprehensive tests of file hash recording. |
| 48 |
""" |
| 49 |
|
| 50 |
@classmethod |
| 51 |
def setUpClass(cls): |
| 52 |
cls.set_up_test_dir() # tear_down_test_dir is called implicitly |
| 53 |
Path("foo").touch() |
| 54 |
Path("bar").mkdir() |
| 55 |
Path("bar/baz").touch() |
| 56 |
Path("bar/foo").touch() |
| 57 |
|
| 58 |
def test_hash_artifacts_scheme_no_scheme(self): |
| 59 |
"""Assert that hashes are the same with and without scheme.""" |
| 60 |
resolver = FileResolver() |
| 61 |
uris = {"foo", "file:foo"} |
| 62 |
result = resolver.hash_artifacts(uris) |
| 63 |
self.assertEqual(result.keys(), uris) |
| 64 |
# pylint: disable=no-value-for-parameter |
| 65 |
self.assertEqual(*result.values()) |
| 66 |
|
| 67 |
def test_hash_artifacts_kwags(self): |
| 68 |
"""Assert return values for hash_artifacts with different config.""" |
| 69 |
# Test data: kwargs, uris, expected return value (dict keys) |
| 70 |
test_data = [ |
| 71 |
({"base_path": "bar"}, {"file:baz"}, {"file:baz"}), |
| 72 |
({"lstrip_paths": ["bar/"]}, {"file:bar/baz"}, {"file:baz"}), |
| 73 |
( |
| 74 |
{"exclude_patterns": ["file*", "baz"]}, |
| 75 |
{"file:foo", "file:bar"}, |
| 76 |
{"file:foo", "file:bar/foo"}, |
| 77 |
), |
| 78 |
] |
| 79 |
|
| 80 |
for kwargs, uris, expected_keys in test_data: |
| 81 |
resolver = FileResolver(**kwargs) |
| 82 |
result = resolver.hash_artifacts(uris) |
| 83 |
self.assertEqual(result.keys(), expected_keys) |
| 84 |
|
| 85 |
|
| 86 |
if __name__ == "__main__": |
| 87 |
main() |