team-ljyzipelo

공개
118줄4.8 KB
1"""기본 앱 보존과 설치 실패 정리를 격리 검증함."""
2
3import importlib.util
4import os
5from pathlib import Path
6import subprocess
7import tempfile
8import unittest
9from unittest.mock import patch
10
11ROOT = Path(__file__).resolve().parents[2]
12SPEC = importlib.util.spec_from_file_location("ubuntu_smoke", Path(__file__).with_name("ubuntu_smoke.py"))
13SMOKE = importlib.util.module_from_spec(SPEC)
14SPEC.loader.exec_module(SMOKE)
15
16
17class LifecycleTests(unittest.TestCase):
18 def setUp(self):
19 (ROOT / "tests/release-packages/runs").mkdir(parents=True, exist_ok=True)
20 temporary = tempfile.TemporaryDirectory(dir=ROOT / "tests/release-packages/runs")
21 self.addCleanup(temporary.cleanup)
22 self.directory = Path(temporary.name)
23 self.previous = self.directory / "mimeapps.list"
24 self.previous.write_bytes(b"previous-app.desktop\n")
25 self.created = self.directory / "added-mimeapps.list"
26 self.original = {self.previous: self.previous.read_bytes()}
27 settings = patch.object(SMOKE, "CONFIGS", (self.directory,))
28 settings.start()
29 self.addCleanup(settings.stop)
30
31 def test_only_first_configuration_sets_defaults(self):
32 commands = self.directory / "bin"
33 commands.mkdir()
34 log = self.directory / "calls"
35 scripts = {
36 "update-desktop-database": "#!/bin/sh\nexit 0\n",
37 "getent": '#!/bin/sh\nprintf "tester:x:1000:1000::%s:/bin/sh\\n" "$TEST_HOME"\n',
38 "runuser": '#!/bin/sh\nprintf "%s\\n" "$*" >> "$TEST_LOG"\n',
39 }
40 for name, content in scripts.items():
41 executable = commands / name
42 executable.write_text(content)
43 executable.chmod(0o755)
44 env = {
45 **os.environ,
46 "PATH": str(commands) + os.pathsep + os.environ["PATH"],
47 "SUDO_USER": "tester",
48 "TEST_HOME": str(self.directory),
49 "TEST_LOG": str(log),
50 }
51 for arguments, count in ((["configure"], 5), (["configure", "0.1.0"], 0),
52 (["triggered", "desktop"], 0)):
53 with self.subTest(arguments=arguments):
54 log.write_text("")
55 subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/postinst"), *arguments],
56 env=env, check=True)
57 self.assertEqual(len(log.read_text().splitlines()), count)
58
59 def check_smoke(self, install_fails=False, purge_fails=False, body_fails=False):
60 calls = []
61
62 def command(arguments, **options):
63 calls.append(arguments)
64 if "-i" in arguments:
65 self.previous.write_bytes(b"zipelo.desktop\n")
66 self.created.write_bytes(b"zipelo.desktop\n")
67 if install_fails:
68 raise subprocess.CalledProcessError(1, arguments)
69 elif purge_fails:
70 raise subprocess.CalledProcessError(2, arguments)
71 return subprocess.CompletedProcess(arguments, 0)
72
73 def exercise():
74 with patch.object(SMOKE.subprocess, "run", side_effect=command):
75 with SMOKE.installed_package(self.directory / "sample.deb"):
76 self.assertFalse(install_fails)
77 if body_fails:
78 raise RuntimeError("창 실행 실패")
79
80 if install_fails or purge_fails or body_fails:
81 with self.assertRaises(ExceptionGroup) as raised:
82 exercise()
83 self.assertEqual(len(raised.exception.exceptions),
84 int(install_fails) + int(purge_fails) + int(body_fails))
85 else:
86 exercise()
87 self.assertEqual(SMOKE.configs(), self.original)
88 self.assertEqual(len(calls), 2)
89 self.assertIn("-P", calls[1])
90
91 def test_success_restores_settings(self):
92 self.check_smoke()
93
94 def test_partial_install_failure_restores_settings(self):
95 self.check_smoke(install_fails=True)
96
97 def test_purge_failure_still_restores_settings(self):
98 self.check_smoke(purge_fails=True)
99
100 def test_both_failures_are_reported(self):
101 self.check_smoke(install_fails=True, purge_fails=True)
102
103 def test_window_failure_restores_settings(self):
104 self.check_smoke(body_fails=True)
105
106 def test_external_change_does_not_skip_other_restores(self):
107 self.previous.write_bytes(b"zipelo.desktop\n")
108 self.created.write_bytes(b"zipelo.desktop\n")
109 after = SMOKE.configs()
110 self.created.write_bytes(b"external-choice.desktop\n")
111 with self.assertRaises(ExceptionGroup):
112 SMOKE.restore_configs(self.original, after)
113 self.assertEqual(self.previous.read_bytes(), self.original[self.previous])
114 self.assertEqual(self.created.read_bytes(), b"external-choice.desktop\n")
115
116
117if __name__ == "__main__":
118 unittest.main()