team-ljyzipelo

공개
119줄4.9 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 "gtk-update-icon-cache": "#!/bin/sh\nexit 0\n",
38 "getent": '#!/bin/sh\nprintf "tester:x:1000:1000::%s:/bin/sh\\n" "$TEST_HOME"\n',
39 "runuser": '#!/bin/sh\nprintf "%s\\n" "$*" >> "$TEST_LOG"\n',
40 }
41 for name, content in scripts.items():
42 executable = commands / name
43 executable.write_text(content)
44 executable.chmod(0o755)
45 env = {
46 **os.environ,
47 "PATH": str(commands) + os.pathsep + os.environ["PATH"],
48 "SUDO_USER": "tester",
49 "TEST_HOME": str(self.directory),
50 "TEST_LOG": str(log),
51 }
52 for arguments, count in ((["configure"], 5), (["configure", "0.1.0"], 0),
53 (["triggered", "desktop"], 0)):
54 with self.subTest(arguments=arguments):
55 log.write_text("")
56 subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/postinst"), *arguments],
57 env=env, check=True)
58 self.assertEqual(len(log.read_text().splitlines()), count)
59
60 def check_smoke(self, install_fails=False, purge_fails=False, body_fails=False):
61 calls = []
62
63 def command(arguments, **options):
64 calls.append(arguments)
65 if "-i" in arguments:
66 self.previous.write_bytes(b"zipelo.desktop\n")
67 self.created.write_bytes(b"zipelo.desktop\n")
68 if install_fails:
69 raise subprocess.CalledProcessError(1, arguments)
70 elif purge_fails:
71 raise subprocess.CalledProcessError(2, arguments)
72 return subprocess.CompletedProcess(arguments, 0)
73
74 def exercise():
75 with patch.object(SMOKE.subprocess, "run", side_effect=command):
76 with SMOKE.installed_package(self.directory / "sample.deb"):
77 self.assertFalse(install_fails)
78 if body_fails:
79 raise RuntimeError("창 실행 실패")
80
81 if install_fails or purge_fails or body_fails:
82 with self.assertRaises(ExceptionGroup) as raised:
83 exercise()
84 self.assertEqual(len(raised.exception.exceptions),
85 int(install_fails) + int(purge_fails) + int(body_fails))
86 else:
87 exercise()
88 self.assertEqual(SMOKE.configs(), self.original)
89 self.assertEqual(len(calls), 2)
90 self.assertIn("-P", calls[1])
91
92 def test_success_restores_settings(self):
93 self.check_smoke()
94
95 def test_partial_install_failure_restores_settings(self):
96 self.check_smoke(install_fails=True)
97
98 def test_purge_failure_still_restores_settings(self):
99 self.check_smoke(purge_fails=True)
100
101 def test_both_failures_are_reported(self):
102 self.check_smoke(install_fails=True, purge_fails=True)
103
104 def test_window_failure_restores_settings(self):
105 self.check_smoke(body_fails=True)
106
107 def test_external_change_does_not_skip_other_restores(self):
108 self.previous.write_bytes(b"zipelo.desktop\n")
109 self.created.write_bytes(b"zipelo.desktop\n")
110 after = SMOKE.configs()
111 self.created.write_bytes(b"external-choice.desktop\n")
112 with self.assertRaises(ExceptionGroup):
113 SMOKE.restore_configs(self.original, after)
114 self.assertEqual(self.previous.read_bytes(), self.original[self.previous])
115 self.assertEqual(self.created.read_bytes(), b"external-choice.desktop\n")
116
117
118if __name__ == "__main__":
119 unittest.main()