|
1 | 1 | import unittest |
2 | | -import tempfile |
3 | 2 | from pathlib import Path |
4 | 3 | from unittest.mock import patch, mock_open |
5 | 4 | from sagemaker.serve.validations.check_integrity import ( |
6 | | - generate_secret_key, |
7 | 5 | compute_hash, |
8 | 6 | perform_integrity_check |
9 | 7 | ) |
10 | 8 |
|
11 | 9 |
|
12 | 10 | class TestCheckIntegrity(unittest.TestCase): |
13 | | - def test_generate_secret_key(self): |
14 | | - key = generate_secret_key() |
15 | | - self.assertIsInstance(key, str) |
16 | | - self.assertEqual(len(key), 64) |
17 | | - |
18 | | - def test_generate_secret_key_custom_bytes(self): |
19 | | - key = generate_secret_key(nbytes=16) |
20 | | - self.assertEqual(len(key), 32) |
21 | | - |
22 | 11 | def test_compute_hash(self): |
23 | 12 | buffer = b"test data" |
24 | | - secret_key = "test_secret" |
25 | | - hash_value = compute_hash(buffer, secret_key) |
| 13 | + hash_value = compute_hash(buffer) |
26 | 14 | self.assertIsInstance(hash_value, str) |
27 | 15 | self.assertEqual(len(hash_value), 64) |
28 | 16 |
|
29 | 17 | def test_compute_hash_consistency(self): |
30 | 18 | buffer = b"test data" |
31 | | - secret_key = "test_secret" |
32 | | - hash1 = compute_hash(buffer, secret_key) |
33 | | - hash2 = compute_hash(buffer, secret_key) |
| 19 | + hash1 = compute_hash(buffer) |
| 20 | + hash2 = compute_hash(buffer) |
34 | 21 | self.assertEqual(hash1, hash2) |
35 | 22 |
|
36 | | - @patch.dict("os.environ", {"SAGEMAKER_SERVE_SECRET_KEY": "test_key"}) |
| 23 | + def test_compute_hash_different_data(self): |
| 24 | + hash1 = compute_hash(b"data1") |
| 25 | + hash2 = compute_hash(b"data2") |
| 26 | + self.assertNotEqual(hash1, hash2) |
| 27 | + |
37 | 28 | @patch("pathlib.Path.exists") |
38 | 29 | @patch("builtins.open", new_callable=mock_open, read_data=b'{"sha256_hash": "test_hash"}') |
39 | 30 | @patch("sagemaker.serve.validations.check_integrity._MetaData.from_json") |
40 | 31 | def test_perform_integrity_check_failure(self, mock_metadata, mock_file, mock_exists): |
41 | 32 | mock_exists.return_value = True |
42 | 33 | mock_meta = type("obj", (object,), {"sha256_hash": "wrong_hash"})() |
43 | 34 | mock_metadata.return_value = mock_meta |
44 | | - |
| 35 | + |
45 | 36 | with self.assertRaises(ValueError): |
46 | 37 | perform_integrity_check(b"test", Path("/tmp/metadata.json")) |
47 | 38 |
|
| 39 | + def test_perform_integrity_check_missing_metadata(self): |
| 40 | + with self.assertRaises(ValueError, msg="Path to metadata.json does not exist"): |
| 41 | + perform_integrity_check(b"test", Path("/nonexistent/metadata.json")) |
| 42 | + |
48 | 43 |
|
49 | 44 | if __name__ == "__main__": |
50 | 45 | unittest.main() |
0 commit comments