53 lines
2.6 KiB
Python
53 lines
2.6 KiB
Python
"""Migration inventory and runtime must resolve the same config and state."""
|
|
import contextlib
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from harness import paths
|
|
from scripts import migration_inventory
|
|
|
|
|
|
class MigrationPaths(unittest.TestCase):
|
|
def test_config_precedence_and_state_match_runtime_without_writes(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
root = Path(temp).resolve()
|
|
override = root / 'override.yaml'; override.write_text('paths: {}\n')
|
|
explicit = root / 'explicit.yaml'; explicit.write_text('paths: {}\n')
|
|
env = {'BENCH_ROOT': str(root), 'BENCH_CONFIG': str(override),
|
|
'BENCH_STATE_DIR': str(root / 'not-created')}
|
|
with patch.dict(os.environ, env, clear=True):
|
|
resolved = paths.resolve_installation()
|
|
self.assertEqual(resolved, (root, override, root / 'not-created'))
|
|
with contextlib.redirect_stdout(io.StringIO()) as output:
|
|
self.assertEqual(migration_inventory.main([]), 0)
|
|
report = json.loads(output.getvalue())
|
|
self.assertEqual(report['config'], str(override))
|
|
self.assertEqual(report['state'], str(root / 'not-created'))
|
|
storage = next(x for x in report['checks'] if x['name'] == 'storage.state')
|
|
self.assertEqual(storage['resolved'], report['state'])
|
|
with contextlib.redirect_stdout(io.StringIO()) as output:
|
|
self.assertEqual(migration_inventory.main(['--config', str(explicit)]), 0)
|
|
self.assertEqual(json.loads(output.getvalue())['config'], str(explicit))
|
|
self.assertFalse((root / 'not-created').exists())
|
|
|
|
def test_explicit_root_and_relative_paths_use_documented_cwd(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
root = Path(temp).resolve()
|
|
with patch.dict(os.environ, {'BENCH_ROOT': '/unused', 'BENCH_CONFIG': 'alternate.yaml',
|
|
'BENCH_STATE_DIR': 'scratch-state'}, clear=True):
|
|
actual = paths.resolve_installation(root=root)
|
|
self.assertEqual(actual, (root, (Path.cwd() / 'alternate.yaml').resolve(),
|
|
(Path.cwd() / 'scratch-state').resolve()))
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
self.assertEqual(paths.resolve_installation(root=root),
|
|
(root, root / 'config.yaml', root / 'state'))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|