|
| 1 | +import hashlib |
| 2 | +import os |
| 3 | + |
| 4 | +from django.conf import settings |
| 5 | +from django.test import TestCase |
| 6 | + |
| 7 | + |
| 8 | +class FileHashTest(TestCase): |
| 9 | + FILE_TO_CHECK_PATH = os.path.join(settings.BASE_DIR, 'ansible_base', 'feature_flags', 'definitions', 'feature_flags.yaml') |
| 10 | + HASH_ALGORITHM = 'sha256' |
| 11 | + HASH_COMMENT_PREFIX = '# FileHash:' |
| 12 | + |
| 13 | + def _get_last_migration_file(self): |
| 14 | + """ |
| 15 | + Finds the path to the last migration file in the specified Django app. |
| 16 | + """ |
| 17 | + migrations_dir = os.path.join(settings.BASE_DIR, 'ansible_base', 'feature_flags', 'migrations') |
| 18 | + if not os.path.isdir(migrations_dir): |
| 19 | + raise FileNotFoundError(f"Migrations directory not found for app: {self.APP_NAME}") |
| 20 | + |
| 21 | + migration_files = sorted([ |
| 22 | + f for f in os.listdir(migrations_dir) |
| 23 | + if f.endswith('.py') and f != '__init__.py' |
| 24 | + ]) |
| 25 | + |
| 26 | + if not migration_files: |
| 27 | + raise FileNotFoundError(f"No migration files found in {migrations_dir}") |
| 28 | + |
| 29 | + return os.path.join(migrations_dir, migration_files[-1]) |
| 30 | + |
| 31 | + def _extract_hash_from_migration(self, migration_file_path): |
| 32 | + """ |
| 33 | + Extracts the expected hash from a comment in the migration file. |
| 34 | + Assumes the format: '# FileHash: <hash_value>' |
| 35 | + """ |
| 36 | + with open(migration_file_path, 'r') as f: |
| 37 | + for line in f: |
| 38 | + if line.strip().startswith(self.HASH_COMMENT_PREFIX): |
| 39 | + return line.strip().replace(self.HASH_COMMENT_PREFIX, '').strip() |
| 40 | + return None |
| 41 | + |
| 42 | + def _calculate_file_hash(self, file_path): |
| 43 | + """ |
| 44 | + Calculates the hash of the given file. |
| 45 | + """ |
| 46 | + hash_func = getattr(hashlib, self.HASH_ALGORITHM, None) |
| 47 | + if not hash_func: |
| 48 | + raise ValueError(f"Unsupported hash algorithm: {self.HASH_ALGORITHM}") |
| 49 | + |
| 50 | + hasher = hash_func() |
| 51 | + with open(file_path, 'rb') as f: |
| 52 | + for chunk in iter(lambda: f.read(4096), b""): |
| 53 | + hasher.update(chunk) |
| 54 | + return hasher.hexdigest() |
| 55 | + |
| 56 | + def test_file_hash_matches_migration_comment(self): |
| 57 | + """ |
| 58 | + Checks if the hash of a specified file matches the hash commented |
| 59 | + in the last migration file. |
| 60 | + """ |
| 61 | + # 1. Get the last migration file |
| 62 | + try: |
| 63 | + last_migration_file = self._get_last_migration_file() |
| 64 | + except FileNotFoundError as e: |
| 65 | + self.fail(f"Could not find last migration file: {e}") |
| 66 | + |
| 67 | + # 2. Extract the expected hash from the migration file |
| 68 | + expected_hash = self._extract_hash_from_migration(last_migration_file) |
| 69 | + self.assertIsNotNone(expected_hash, |
| 70 | + f"No hash comment '{self.HASH_COMMENT_PREFIX}' found in {last_migration_file}") |
| 71 | + self.assertTrue(expected_hash, "Extracted hash is empty.") |
| 72 | + |
| 73 | + # 3. Calculate the hash of the target file |
| 74 | + self.assertTrue(os.path.exists(self.FILE_TO_CHECK_PATH), |
| 75 | + f"File to check does not exist: {self.FILE_TO_CHECK_PATH}") |
| 76 | + actual_hash = self._calculate_file_hash(self.FILE_TO_CHECK_PATH) |
| 77 | + |
| 78 | + # 4. Compare the hashes |
| 79 | + self.assertEqual(expected_hash, actual_hash, |
| 80 | + f"Hash mismatch for '{os.path.basename(self.FILE_TO_CHECK_PATH)}'. " |
| 81 | + f"Expected: {expected_hash}, Got: {actual_hash} " |
| 82 | + f"If the feature_flags.yaml file changed, generate a new no-op migration file, and correctly set the FileHash.") |
0 commit comments