//! Migration tests, run against a fixture made by the Koh 2 binary itself. //! //! The fixture is why these are worth having: it is not a guess at Koh 2's //! format, it is the format, including the inlined small files and the object //! store. When `koh` (Koh 2) is not on the machine these tests skip rather //! than pretend. module migrate import json2 import os import src.events import src.history import src.snapshot import src.storage struct Fixture { dir string first string second string message string } fn koh2_binary() ?string { path := os.find_abs_path_of_executable('koh') or { return none } return path } fn sh(dir string, command string) !string { result := os.execute('cd ${dir} && ${command}') if result.exit_code != 0 { return error('${command} failed: ${result.output}') } return result.output } fn make_fixture(koh2 string) !Fixture { dir := os.join_path(os.temp_dir(), 'koh3_migrate_${os.getpid()}') os.rmdir_all(dir) or {} os.mkdir_all(dir, os.MkdirParams{})! sh(dir, '${koh2} init')! // One file big enough for the object store, one small enough to be inlined. os.write_file(os.join_path(dir, 'big.txt'), 'x'.repeat(700))! os.write_file(os.join_path(dir, 'small.txt'), 'tiny\n')! sh(dir, '${koh2} save "first save"')! sh(dir, '${koh2} checkpoint "before something"')! os.write_file(os.join_path(dir, 'big.txt'), 'y'.repeat(700))! sh(dir, '${koh2} save "second save"')! // Identify the first save by its message: koh2 timestamps are whole seconds, // so saves made in a row share one, and a filename is a hash. The head comes // from Koh 2's own state for the same reason. mut first := '' for name in os.ls(os.join_path(dir, '.koh', 'snapshots'))! { if !name.ends_with('.json') { continue } record := json2.decode[Manifest](os.read_file(os.join_path(dir, '.koh', 'snapshots', name))!)! if record.message == 'first save' { first = record.id } } assert first != '' return Fixture{ dir: dir first: first second: koh2_head(dir) message: 'first save' } } // koh2_head reads the lane head Koh 2 recorded. fn koh2_head(dir string) string { db := storage.connect(os.join_path(dir, '.koh', 'koh.db'), 1000) or { return '' } defer { db.close() } mut stmt := db.prepare('SELECT value FROM state WHERE key = ?') or { return '' } defer { stmt.finalize() } stmt.bind_text(1, 'head_main') or { return '' } if stmt.step() or { return '' } { return stmt.text(0) } return '' } fn test_migrating_a_koh2_repo_keeps_every_save_exactly() { koh2 := koh2_binary() or { return } fixture := make_fixture(koh2)! defer { os.rmdir_all(fixture.dir) or {} } // Flag the first save, so a save-level flag has to become an event. sh(fixture.dir, '${koh2} flag ${fixture.first} --reason "sketchy"')! // What the manifests say, read independently of the migration. mut manifests := map[string][]string{} for name in os.ls(os.join_path(fixture.dir, '.koh', 'snapshots'))! { if !name.ends_with('.json') { continue } record := json2.decode[Manifest](os.read_file(os.join_path(fixture.dir, '.koh', 'snapshots', name))!)! mut files := []string{} for file in record.files { files << '${file.path} ${bare_hash(file.hash)}' } manifests[record.id] = files } report := run(fixture.dir, default_lane)! // The fixture lives on main, because that is the lane koh2 starts on. assert report.lane == 'main' assert report.saves == 3 assert report.checkpoints == 1 assert report.flags == 1 assert report.objects >= 3 // The swap happened: `.koh` is a file now and the old data is archived. assert os.is_file(os.join_path(fixture.dir, '.koh')) assert os.is_dir(os.join_path(fixture.dir, '.koh-v2')) assert os.is_file(os.join_path(fixture.dir, '.kohignore')) store := storage.open(fixture.dir)! defer { store.close() } // Every save kept its ID, its files and their hashes. assert history.save_count(store)! == 3 for id, expected in manifests { save := history.by_id(store, id) or { assert false, 'save ${id} did not survive the migration' return } assert save.machine == 'koh2' mut actual := []string{} for file in history.files_of(store, id)! { actual << '${file.path} ${file.hash}' // Every object is present and hashes back to its key. if _ := store.get_object(file.hash) { } else { assert false, 'object ${file.hash} is missing or corrupt' } } assert actual == expected } // The checkpoint mark and the parent chain came across. middle := history.by_id(store, fixture.first) or { panic('the first save did not survive') } assert (history.head(store) or { panic('no head after migrating') }).id == fixture.second assert middle.message == 'first save' // The flag became an event, and the working tree is exactly what Koh 3 // expects once the carried ignore file is saved. assert events.flag_state(store, fixture.first)!.flagged assert events.flag_state(store, fixture.first)!.reason == 'sketchy' entries := snapshot.walk(store, fixture.dir, snapshot.load(fixture.dir))! mut changed := []string{} head_files := snapshot.index_files(history.files_of(store, fixture.second)!) for entry in entries { if entry.path !in head_files { changed << entry.path } else if head_files[entry.path].hash != entry.hash { changed << entry.path } } // The only difference is the ignore file Koh 3 just carried to the root. assert changed == ['.kohignore'] } fn test_migrate_refuses_a_tree_that_does_not_match_the_last_save() { koh2 := koh2_binary() or { return } fixture := make_fixture(koh2)! defer { os.rmdir_all(fixture.dir) or {} } os.write_file(os.join_path(fixture.dir, 'big.txt'), 'unsaved change\n')! if _ := run(fixture.dir, default_lane) { assert false, 'a dirty tree must be refused' } else { assert err.msg().contains('save with koh2 first') } // Nothing was touched: still a Koh 2 repo, no archive, no staging file. assert os.is_dir(os.join_path(fixture.dir, '.koh')) assert !os.exists(os.join_path(fixture.dir, '.koh-v2')) assert !os.exists(os.join_path(fixture.dir, staging_file)) } // A damaged manifest naming a path outside the repo must never be read there. fn test_a_saved_path_is_only_read_inside_the_repo() { root := os.join_path(os.temp_dir(), 'koh3_migrate_paths_${os.getpid()}', 'repo') os.mkdir_all(root)! defer { os.rmdir_all(os.dir(root)) or {} } os.write_file(os.join_path(os.dir(root), 'outside.txt'), 'secret\n')! os.write_file(os.join_path(root, 'inside.txt'), 'fine\n')! assert disk_hash(root, 'inside.txt') != none for path in ['../outside.txt', '/etc/hosts', './inside.txt', 'a//inside.txt'] { assert disk_hash(root, path) == none, path } }