team-ljyzipelo

공개
149줄6.5 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"], 1), (["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 test_prerm_restores_only_on_remove_for_known_user(self):
61 commands = self.directory / "bin"
62 commands.mkdir()
63 log = self.directory / "calls"
64 for name, content in {
65 "getent": '#!/bin/sh\nprintf "tester:x:1000:1000::%s:/bin/sh\\n" "$TEST_HOME"\n',
66 "runuser": '#!/bin/sh\nprintf "%s\\n" "$*" >> "$TEST_LOG"\n',
67 }.items():
68 executable = commands / name
69 executable.write_text(content)
70 executable.chmod(0o755)
71 env = {**os.environ, "PATH": str(commands) + os.pathsep + os.environ["PATH"],
72 "SUDO_USER": "tester", "TEST_HOME": str(self.directory), "TEST_LOG": str(log)}
73 for action, count in (("upgrade", 0), ("failed-upgrade", 0), ("remove", 1)):
74 with self.subTest(action=action):
75 log.write_text("")
76 subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/prerm"), action],
77 env=env, check=True, capture_output=True)
78 calls = log.read_text().splitlines()
79 self.assertEqual(len(calls), count)
80 if count:
81 self.assertIn("HOME=" + str(self.directory), calls[0])
82 self.assertTrue(calls[0].endswith("/usr/lib/zipelo/default-apps remove"))
83 env.pop("SUDO_USER")
84 log.write_text("")
85 result = subprocess.run(["sh", str(ROOT / "packaging/ubuntu/rust/prerm"), "remove"],
86 env=env, check=True, capture_output=True, text=True)
87 self.assertEqual(log.read_text(), "")
88 self.assertIn("사용자를 확인할 수 없어", result.stdout)
89
90 def check_smoke(self, install_fails=False, purge_fails=False, body_fails=False):
91 calls = []
92
93 def command(arguments, **options):
94 calls.append(arguments)
95 if "-i" in arguments:
96 self.previous.write_bytes(b"zipelo.desktop\n")
97 self.created.write_bytes(b"zipelo.desktop\n")
98 if install_fails:
99 raise subprocess.CalledProcessError(1, arguments)
100 elif purge_fails:
101 raise subprocess.CalledProcessError(2, arguments)
102 return subprocess.CompletedProcess(arguments, 0)
103
104 def exercise():
105 with patch.object(SMOKE.subprocess, "run", side_effect=command):
106 with SMOKE.installed_package(self.directory / "sample.deb"):
107 self.assertFalse(install_fails)
108 if body_fails:
109 raise RuntimeError("창 실행 실패")
110
111 if install_fails or purge_fails or body_fails:
112 with self.assertRaises(ExceptionGroup) as raised:
113 exercise()
114 self.assertEqual(len(raised.exception.exceptions),
115 int(install_fails) + int(purge_fails) + int(body_fails))
116 else:
117 exercise()
118 self.assertEqual(SMOKE.configs(), self.original)
119 self.assertEqual(len(calls), 2)
120 self.assertIn("-P", calls[1])
121
122 def test_success_restores_settings(self):
123 self.check_smoke()
124
125 def test_partial_install_failure_restores_settings(self):
126 self.check_smoke(install_fails=True)
127
128 def test_purge_failure_still_restores_settings(self):
129 self.check_smoke(purge_fails=True)
130
131 def test_both_failures_are_reported(self):
132 self.check_smoke(install_fails=True, purge_fails=True)
133
134 def test_window_failure_restores_settings(self):
135 self.check_smoke(body_fails=True)
136
137 def test_external_change_does_not_skip_other_restores(self):
138 self.previous.write_bytes(b"zipelo.desktop\n")
139 self.created.write_bytes(b"zipelo.desktop\n")
140 after = SMOKE.configs()
141 self.created.write_bytes(b"external-choice.desktop\n")
142 with self.assertRaises(ExceptionGroup):
143 SMOKE.restore_configs(self.original, after)
144 self.assertEqual(self.previous.read_bytes(), self.original[self.previous])
145 self.assertEqual(self.created.read_bytes(), b"external-choice.desktop\n")
146
147
148if __name__ == "__main__":
149 unittest.main()