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