| 1 | """기본 앱 보존과 설치 실패 정리를 격리 검증함.""" |
| 2 | |
| 3 | import importlib.util |
| 4 | import os |
| 5 | import re |
| 6 | from pathlib import Path |
| 7 | import subprocess |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | from unittest.mock import patch |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parents[2] |
| 13 | SPEC = importlib.util.spec_from_file_location("ubuntu_smoke", Path(__file__).with_name("ubuntu_smoke.py")) |
| 14 | SMOKE = importlib.util.module_from_spec(SPEC) |
| 15 | SPEC.loader.exec_module(SMOKE) |
| 16 | |
| 17 | |
| 18 | def test_work_root(): |
| 19 | job = os.environ.get("ZIPELO_CI_JOB", "rust-local") |
| 20 | if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_-]{0,63}", job): |
| 21 | raise ValueError("안전한 작업명이 필요함") |
| 22 | return SMOKE.work_root() / job / "tmp" |
| 23 | |
| 24 | |
| 25 | class LifecycleTests(unittest.TestCase): |
| 26 | def setUp(self): |
| 27 | work = test_work_root() |
| 28 | work.mkdir(parents=True, exist_ok=True) |
| 29 | temporary = tempfile.TemporaryDirectory(dir=work) |
| 30 | self.addCleanup(temporary.cleanup) |
| 31 | self.directory = Path(temporary.name) |
| 32 | self.previous = self.directory / "mimeapps.list" |
| 33 | self.previous.write_bytes(b"previous-app.desktop\n") |
| 34 | self.created = self.directory / "added-mimeapps.list" |
| 35 | self.original = {self.previous: self.previous.read_bytes()} |
| 36 | settings = patch.object(SMOKE, "CONFIGS", (self.directory,)) |
| 37 | settings.start() |
| 38 | self.addCleanup(settings.stop) |
| 39 | |
| 40 | def test_default_work_path_uses_common_job_directory(self): |
| 41 | with patch.dict(os.environ, {"ZIPELO_CI_JOB": "lifecycle-regression"}): |
| 42 | os.environ.pop("TMPDIR", None) |
| 43 | os.environ.pop("ZIPELO_TEST_WORK_DIR", None) |
| 44 | self.assertEqual( |
| 45 | test_work_root(), SMOKE.work_root() / "lifecycle-regression/tmp" |
| 46 | ) |
| 47 | |
| 48 | def test_only_first_configuration_sets_defaults(self): |
| 49 | commands = self.directory / "bin" |
| 50 | commands.mkdir() |
| 51 | log = self.directory / "calls" |
| 52 | scripts = { |
| 53 | "update-desktop-database": "#!/bin/sh\nexit 0\n", |
| 54 | "gtk-update-icon-cache": "#!/bin/sh\nexit 0\n", |
| 55 | "getent": '#!/bin/sh\nprintf "tester:x:1000:1000::%s:/bin/sh\\n" "$TEST_HOME"\n', |
| 56 | "runuser": '#!/bin/sh\nprintf "%s\\n" "$*" >> "$TEST_LOG"\n', |
| 57 | } |
| 58 | for name, content in scripts.items(): |
| 59 | executable = commands / name |
| 60 | executable.write_text(content) |
| 61 | executable.chmod(0o755) |
| 62 | env = { |
| 63 | **os.environ, |
| 64 | "PATH": str(commands) + os.pathsep + os.environ["PATH"], |
| 65 | "SUDO_USER": "tester", |
| 66 | "TEST_HOME": str(self.directory), |
| 67 | "TEST_LOG": str(log), |
| 68 | } |
| 69 | for arguments, count in ((["configure"], 1), (["configure", "0.1.0"], 0), |
| 70 | (["triggered", "desktop"], 0)): |
| 71 | with self.subTest(arguments=arguments): |
| 72 | log.write_text("") |
| 73 | subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/postinst"), *arguments], |
| 74 | env=env, check=True) |
| 75 | self.assertEqual(len(log.read_text().splitlines()), count) |
| 76 | env["SUDO_USER"] = "root" |
| 77 | log.write_text("") |
| 78 | subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/postinst"), "configure"], |
| 79 | env=env, check=True) |
| 80 | self.assertEqual(log.read_text(), "") |
| 81 | |
| 82 | def test_prerm_restores_only_on_remove_for_known_user(self): |
| 83 | commands = self.directory / "bin" |
| 84 | commands.mkdir() |
| 85 | log = self.directory / "calls" |
| 86 | for name, content in { |
| 87 | "getent": '#!/bin/sh\nprintf "tester:x:1000:1000::%s:/bin/sh\\n" "$TEST_HOME"\n', |
| 88 | "runuser": '#!/bin/sh\nprintf "%s\\n" "$*" >> "$TEST_LOG"\n', |
| 89 | }.items(): |
| 90 | executable = commands / name |
| 91 | executable.write_text(content) |
| 92 | executable.chmod(0o755) |
| 93 | env = {**os.environ, "PATH": str(commands) + os.pathsep + os.environ["PATH"], |
| 94 | "SUDO_USER": "tester", "TEST_HOME": str(self.directory), "TEST_LOG": str(log)} |
| 95 | for action, count in (("upgrade", 0), ("failed-upgrade", 0), ("remove", 1)): |
| 96 | with self.subTest(action=action): |
| 97 | log.write_text("") |
| 98 | subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/prerm"), action], |
| 99 | env=env, check=True, capture_output=True) |
| 100 | calls = log.read_text().splitlines() |
| 101 | self.assertEqual(len(calls), count) |
| 102 | if count: |
| 103 | self.assertIn("HOME=" + str(self.directory), calls[0]) |
| 104 | self.assertTrue(calls[0].endswith("/usr/lib/zipelo/default-apps remove")) |
| 105 | env["SUDO_USER"] = "root" |
| 106 | log.write_text("") |
| 107 | subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/prerm"), "remove"], |
| 108 | env=env, check=True, capture_output=True) |
| 109 | self.assertEqual(log.read_text(), "") |
| 110 | env.pop("SUDO_USER") |
| 111 | log.write_text("") |
| 112 | result = subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/prerm"), "remove"], |
| 113 | env=env, check=True, capture_output=True, text=True) |
| 114 | self.assertEqual(log.read_text(), "") |
| 115 | self.assertIn("사용자를 확인할 수 없어", result.stdout) |
| 116 | |
| 117 | def check_smoke(self, install_fails=False, defaults_fail=False, purge_fails=False, body_fails=False): |
| 118 | calls = [] |
| 119 | home = self.directory / "isolated-home" |
| 120 | home.mkdir() |
| 121 | isolated_config = home / ".config/mimeapps.list" |
| 122 | isolated_config.parent.mkdir() |
| 123 | isolated_config.write_bytes(b"previous-app.desktop\n") |
| 124 | |
| 125 | def command(arguments, **options): |
| 126 | calls.append(arguments) |
| 127 | if arguments[0] == "sudo": |
| 128 | self.assertIn("SUDO_USER=root", arguments) |
| 129 | self.assertIn("HOME=" + str(home), arguments) |
| 130 | if "-i" in arguments and install_fails: |
| 131 | raise subprocess.CalledProcessError(1, arguments) |
| 132 | if "-P" in arguments and purge_fails: |
| 133 | raise subprocess.CalledProcessError(2, arguments) |
| 134 | else: |
| 135 | self.assertEqual(arguments[0], "/usr/lib/zipelo/default-apps") |
| 136 | self.assertEqual(options["env"]["HOME"], str(home)) |
| 137 | self.assertEqual(options["env"]["XDG_CONFIG_HOME"], str(home / ".config")) |
| 138 | self.assertEqual(options["env"]["XDG_DATA_HOME"], str(home / ".local/share")) |
| 139 | self.assertEqual(options["env"]["XDG_STATE_HOME"], str(home / ".local/state")) |
| 140 | if arguments[1] == "install": |
| 141 | isolated_config.write_bytes(b"zipelo.desktop\n") |
| 142 | if defaults_fail: |
| 143 | raise subprocess.CalledProcessError(3, arguments) |
| 144 | else: |
| 145 | isolated_config.write_bytes(b"previous-app.desktop\n") |
| 146 | return subprocess.CompletedProcess(arguments, 0) |
| 147 | |
| 148 | def exercise(): |
| 149 | with patch.object(SMOKE.subprocess, "run", side_effect=command): |
| 150 | with SMOKE.installed_package(self.directory / "sample.deb", home) as isolated: |
| 151 | self.assertEqual(isolated["HOME"], str(home)) |
| 152 | if body_fails: |
| 153 | raise RuntimeError("창 실행 실패") |
| 154 | |
| 155 | if install_fails or defaults_fail or purge_fails or body_fails: |
| 156 | with self.assertRaises(BaseExceptionGroup) as raised: |
| 157 | exercise() |
| 158 | self.assertEqual(len(raised.exception.exceptions), |
| 159 | int(install_fails) + int(defaults_fail) + int(purge_fails) + int(body_fails)) |
| 160 | else: |
| 161 | exercise() |
| 162 | self.assertEqual(SMOKE.configs(), self.original) |
| 163 | self.assertEqual(self.previous.read_bytes(), b"previous-app.desktop\n") |
| 164 | self.assertFalse(self.created.exists()) |
| 165 | if install_fails: |
| 166 | self.assertEqual([arguments[-2:] for arguments in calls], |
| 167 | [["-i", str(self.directory / "sample.deb")], ["-P", "zipelo"]]) |
| 168 | else: |
| 169 | self.assertEqual([arguments[-2:] for arguments in calls if arguments[0] == "sudo"], |
| 170 | [["-i", str(self.directory / "sample.deb")], ["-P", "zipelo"]]) |
| 171 | self.assertEqual([arguments[-1] for arguments in calls if arguments[0] != "sudo"], |
| 172 | ["install", "remove"]) |
| 173 | self.assertEqual(isolated_config.read_bytes(), b"previous-app.desktop\n") |
| 174 | |
| 175 | def test_success_keeps_real_user_settings_unchanged(self): |
| 176 | self.check_smoke() |
| 177 | |
| 178 | def test_partial_install_failure_keeps_real_user_settings_unchanged(self): |
| 179 | self.check_smoke(install_fails=True) |
| 180 | |
| 181 | def test_default_app_failure_still_cleans_isolated_settings(self): |
| 182 | self.check_smoke(defaults_fail=True) |
| 183 | |
| 184 | def test_purge_failure_keeps_real_user_settings_unchanged(self): |
| 185 | self.check_smoke(purge_fails=True) |
| 186 | |
| 187 | def test_install_and_purge_failures_are_reported(self): |
| 188 | self.check_smoke(install_fails=True, purge_fails=True) |
| 189 | |
| 190 | def test_window_failure_keeps_real_user_settings_unchanged(self): |
| 191 | self.check_smoke(body_fails=True) |
| 192 | |
| 193 | def test_host_change_is_detected_without_overwrite(self): |
| 194 | home = self.directory / "isolated-home" |
| 195 | home.mkdir() |
| 196 | calls = [] |
| 197 | |
| 198 | def command(arguments, **options): |
| 199 | calls.append(arguments) |
| 200 | if "-i" in arguments: |
| 201 | self.previous.write_bytes(b"external-choice.desktop\n") |
| 202 | return subprocess.CompletedProcess(arguments, 0) |
| 203 | |
| 204 | with patch.object(SMOKE.subprocess, "run", side_effect=command): |
| 205 | with self.assertRaises(BaseExceptionGroup) as raised: |
| 206 | with SMOKE.installed_package(self.directory / "sample.deb", home): |
| 207 | pass |
| 208 | self.assertTrue(any("실제 사용자 MIME 설정" in str(error) |
| 209 | for error in raised.exception.exceptions)) |
| 210 | self.assertEqual(self.previous.read_bytes(), b"external-choice.desktop\n") |
| 211 | self.assertEqual(len(calls), 2) |
| 212 | |
| 213 | |
| 214 | if __name__ == "__main__": |
| 215 | unittest.main() |