diff --git a/e2e/storage_test.py b/e2e/storage_test.py index ed74dd0f3..e73cfa662 100644 --- a/e2e/storage_test.py +++ b/e2e/storage_test.py @@ -60,118 +60,6 @@ class StorageTests(unittest.TestCase): os.utime(path, (old, old)) return path - # exclusive: restarts the daemon around offline maintenance - @exclusive - def test_maintenance_refuses_startup_and_second_cleanup_then_releases(self): - with Albedo(providers={}) as app: - backup = self.backup(app) - - def maintenance(app): - database = app.home / "albedo.sqlite" - before = hashlib.sha256(database.read_bytes()).digest() - helper = self.helper(app, [backup]) - refused = subprocess.run( - [app.env["ALBEDO_DAEMON"]], - env=dict(app.env, ALBEDO_TOKEN="x" * 32), - cwd=ROOT, - capture_output=True, - timeout=20, - ) - self.assertNotEqual(refused.returncode, 0) - self.assertIn(b"storage is in use", refused.stdout + refused.stderr) - self.assertEqual(hashlib.sha256(database.read_bytes()).digest(), before) - cleanup = subprocess.run( - [ - str(ROOT / "cli/bin/albedo"), - "storage", - "prune", - "--all", - "--yes", - ], - env=app.env, - cwd=ROOT, - capture_output=True, - timeout=20, - ) - self.assertNotEqual(cleanup.returncode, 0) - self.assertIn(b"storage is in use", cleanup.stderr) - self.assertEqual(backup.read_bytes(), b"approved backup") - self.close_helper(helper) - self.assertEqual(helper.returncode, 0) - - app.restart(prepare=maintenance) - self.assertEqual(backup.read_bytes(), b"approved backup") - self.assertEqual(app.api("/server").status, 200) - - # exclusive: suspends the daemon and inspects global storage - @exclusive - @unittest.skipUnless(os.path.isdir("/proc/self"), "requires Linux process status") - def test_unreachable_daemon_keeps_cleanup_out(self): - with Albedo(providers={}) as app: - backup = self.backup(app) - pid = app.connection["pid"] - with closing(sqlite3.connect(app.home / "albedo.sqlite")) as db, db: - db.execute("CREATE TABLE reclaim(data BLOB)") - db.execute("INSERT INTO reclaim VALUES(zeroblob(262144))") - db.execute("DELETE FROM reclaim") - os.kill(pid, signal.SIGSTOP) - try: - deadline = time.monotonic() + 10 - while True: - with open(f"/proc/{pid}/status") as status: - if "State:\tT" in status.read(): - break - self.assertLess( - time.monotonic(), deadline, "daemon did not suspend" - ) - # Freeze before measuring: the normal checkpoint can backfill - # the main database while an offline report is being assembled. - before = app.cli("storage", "--offline", "--json") - database_files = [ - app.home / name for name in ("albedo.sqlite", "albedo.sqlite-wal") - ] - before_bytes = [ - hashlib.sha256(path.read_bytes()).digest() - for path in database_files - ] - unavailable = subprocess.run( - [str(ROOT / "cli/bin/albedo"), "storage", "--json"], - env=dict(app.env, ALBEDO_DAEMON="/nonexistent/no-launch"), - cwd=ROOT, - capture_output=True, - timeout=30, - ) - self.assertNotEqual(unavailable.returncode, 0) - self.assertEqual(unavailable.stdout, b"") - cleanup = subprocess.run( - [ - str(ROOT / "cli/bin/albedo"), - "storage", - "prune", - "--all", - "--yes", - ], - env=app.env, - cwd=ROOT, - capture_output=True, - timeout=20, - ) - self.assertNotEqual(cleanup.returncode, 0) - self.assertEqual(backup.read_bytes(), b"approved backup") - self.assertEqual( - json.loads(app.cli("storage", "--offline", "--json")), - json.loads(before), - ) - self.assertEqual( - [ - hashlib.sha256(path.read_bytes()).digest() - for path in database_files - ], - before_bytes, - ) - finally: - os.kill(pid, signal.SIGCONT) - # exclusive: restarts the daemon around offline maintenance @exclusive @unittest.skipUnless(hasattr(os, "pidfd_open"), "requires Linux pidfds") @@ -244,64 +132,170 @@ class StorageTests(unittest.TestCase): app.restart(prepare=maintenance) - # exclusive: restarts the daemon and races startup against offline cleanup + # exclusive: restarts the daemon around offline maintenance, races its + # startup against cleanup, and suspends it @exclusive - def test_daemon_started_after_preview_refuses_cleanup_before_mutation(self): + @unittest.skipUnless(os.path.isdir("/proc/self"), "requires Linux process status") + def test_offline_maintenance_and_the_daemon_exclude_each_other(self): + """The home lock, shared by the daemon's startup, the maintenance + helper and the CLI's cleanup, is a contract between three binaries + only the assembled system has. With the daemon stopped: a helper that + holds the lock refuses startup and a second cleanup and changes + nothing; a cleanup that previewed before the daemon booted is refused + before it removes anything. With the daemon suspended, so it cannot + answer: cleanup stays out and the offline report changes nothing.""" with Albedo(providers={}) as app: backup = self.backup(app) def maintenance(app): - ready = app.home / "acquisition-ready" - release = app.home / "acquisition-release" - os.mkfifo(ready) - os.mkfifo(release) - descriptor = os.open(ready, os.O_RDWR | os.O_NONBLOCK) - self.addCleanup(os.close, descriptor) - tools = app.home / "test-tools" - tools.mkdir() - wrapper = tools / "albedo-daemon" - wrapper.write_text( - f"#!{sys.executable}\n" - "import os, sys\n" - "if sys.argv[1:3] == ['storage', 'maintain']:\n" - f" with open({str(ready)!r}, 'w') as pipe:\n" - " pipe.write('acquiring')\n" - f" with open({str(release)!r}) as pipe:\n" - " pipe.read(1)\n" - f"os.execv({app.env['ALBEDO_DAEMON']!r}, [{app.env['ALBEDO_DAEMON']!r}, *sys.argv[1:]])\n" - ) - wrapper.chmod(0o700) - cleanup = subprocess.Popen( - [ - str(ROOT / "cli/bin/albedo"), - "storage", - "prune", - "--all", - "--yes", - ], - env=dict(app.env, ALBEDO_DAEMON=str(wrapper)), - cwd=ROOT, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - self.addCleanup(self.close_helper, cleanup) - readable, _, _ = select.select([descriptor], [], [], 20) - self.assertEqual( - readable, [descriptor], "cleanup never approved its preview" - ) - self.assertEqual(os.read(descriptor, 1024), b"acquiring") - # The CLI has previewed and approved storage, but has not claimed - # ownership. Boot the runner's real daemon before releasing it. - app.daemon.boot() - with release.open("w") as pipe: - pipe.write("x") - _, stderr = cleanup.communicate(timeout=20) - self.assertNotEqual(cleanup.returncode, 0) - self.assertIn(b"storage is in use", stderr) - self.assertEqual(backup.read_bytes(), b"approved backup") - self.close_helper(cleanup) + self.helper_holds_the_lock(app, backup) + # Boots the daemon before the cleanup claims the lock. + self.startup_wins_over_an_approved_cleanup(app, backup) app.restart(prepare=maintenance) + self.assertEqual(backup.read_bytes(), b"approved backup") + self.assertEqual(app.api("/server").status, 200) + self.suspended_daemon_keeps_cleanup_out(app, backup) + + def helper_holds_the_lock(self, app, backup): + database = app.home / "albedo.sqlite" + before = hashlib.sha256(database.read_bytes()).digest() + helper = self.helper(app, [backup]) + refused = subprocess.run( + [app.env["ALBEDO_DAEMON"]], + env=dict(app.env, ALBEDO_TOKEN="x" * 32), + cwd=ROOT, + capture_output=True, + timeout=20, + ) + self.assertNotEqual(refused.returncode, 0) + self.assertIn(b"storage is in use", refused.stdout + refused.stderr) + self.assertEqual(hashlib.sha256(database.read_bytes()).digest(), before) + cleanup = subprocess.run( + [ + str(ROOT / "cli/bin/albedo"), + "storage", + "prune", + "--all", + "--yes", + ], + env=app.env, + cwd=ROOT, + capture_output=True, + timeout=20, + ) + self.assertNotEqual(cleanup.returncode, 0) + self.assertIn(b"storage is in use", cleanup.stderr) + self.assertEqual(backup.read_bytes(), b"approved backup") + self.close_helper(helper) + self.assertEqual(helper.returncode, 0) + + def startup_wins_over_an_approved_cleanup(self, app, backup): + ready = app.home / "acquisition-ready" + release = app.home / "acquisition-release" + os.mkfifo(ready) + os.mkfifo(release) + descriptor = os.open(ready, os.O_RDWR | os.O_NONBLOCK) + self.addCleanup(os.close, descriptor) + tools = app.home / "test-tools" + tools.mkdir() + wrapper = tools / "albedo-daemon" + wrapper.write_text( + f"#!{sys.executable}\n" + "import os, sys\n" + "if sys.argv[1:3] == ['storage', 'maintain']:\n" + f" with open({str(ready)!r}, 'w') as pipe:\n" + " pipe.write('acquiring')\n" + f" with open({str(release)!r}) as pipe:\n" + " pipe.read(1)\n" + f"os.execv({app.env['ALBEDO_DAEMON']!r}, [{app.env['ALBEDO_DAEMON']!r}, *sys.argv[1:]])\n" + ) + wrapper.chmod(0o700) + cleanup = subprocess.Popen( + [ + str(ROOT / "cli/bin/albedo"), + "storage", + "prune", + "--all", + "--yes", + ], + env=dict(app.env, ALBEDO_DAEMON=str(wrapper)), + cwd=ROOT, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + self.addCleanup(self.close_helper, cleanup) + readable, _, _ = select.select([descriptor], [], [], 20) + self.assertEqual(readable, [descriptor], "cleanup never approved its preview") + self.assertEqual(os.read(descriptor, 1024), b"acquiring") + # The CLI has previewed and approved storage, but has not claimed + # ownership. Boot the runner's real daemon before releasing it. + app.daemon.boot() + with release.open("w") as pipe: + pipe.write("x") + _, stderr = cleanup.communicate(timeout=20) + self.assertNotEqual(cleanup.returncode, 0) + self.assertIn(b"storage is in use", stderr) + self.assertEqual(backup.read_bytes(), b"approved backup") + self.close_helper(cleanup) + + def suspended_daemon_keeps_cleanup_out(self, app, backup): + pid = app.connection["pid"] + with closing(sqlite3.connect(app.home / "albedo.sqlite")) as db, db: + db.execute("CREATE TABLE reclaim(data BLOB)") + db.execute("INSERT INTO reclaim VALUES(zeroblob(262144))") + db.execute("DELETE FROM reclaim") + os.kill(pid, signal.SIGSTOP) + try: + deadline = time.monotonic() + 10 + while True: + with open(f"/proc/{pid}/status") as status: + if "State:\tT" in status.read(): + break + self.assertLess(time.monotonic(), deadline, "daemon did not suspend") + # Freeze before measuring: the normal checkpoint can backfill + # the main database while an offline report is being assembled. + before = app.cli("storage", "--offline", "--json") + database_files = [ + app.home / name for name in ("albedo.sqlite", "albedo.sqlite-wal") + ] + before_bytes = [ + hashlib.sha256(path.read_bytes()).digest() for path in database_files + ] + unavailable = subprocess.run( + [str(ROOT / "cli/bin/albedo"), "storage", "--json"], + env=dict(app.env, ALBEDO_DAEMON="/nonexistent/no-launch"), + cwd=ROOT, + capture_output=True, + timeout=30, + ) + self.assertNotEqual(unavailable.returncode, 0) + self.assertEqual(unavailable.stdout, b"") + cleanup = subprocess.run( + [ + str(ROOT / "cli/bin/albedo"), + "storage", + "prune", + "--all", + "--yes", + ], + env=app.env, + cwd=ROOT, + capture_output=True, + timeout=20, + ) + self.assertNotEqual(cleanup.returncode, 0) + self.assertEqual(backup.read_bytes(), b"approved backup") + self.assertEqual( + json.loads(app.cli("storage", "--offline", "--json")), + json.loads(before), + ) + self.assertEqual( + [hashlib.sha256(path.read_bytes()).digest() for path in database_files], + before_bytes, + ) + finally: + os.kill(pid, signal.SIGCONT) # The private home catches filename encoding regressions and Python invocation. def test_offline_inspection_and_prune_do_not_invoke_python(self):