test_systemd.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import infra.basetest
  2. import re
  3. from tests.init.base import InitSystemBase as InitSystemBase
  4. # In the following tests, the read-only cases use the default settings,
  5. # which historically used both a factory to populate a tmpfs on /var,
  6. # and pre-populated /var at buildtime. Since these are the default
  7. # settings, and they proved to generate a system that ultimately boots,
  8. # we still want to keep testing that. See later, below, for the
  9. # specialised test cases.
  10. class InitSystemSystemdBase(InitSystemBase):
  11. config = \
  12. """
  13. BR2_arm=y
  14. BR2_cortex_a9=y
  15. BR2_ARM_ENABLE_VFP=y
  16. BR2_TOOLCHAIN_EXTERNAL=y
  17. BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
  18. BR2_INIT_SYSTEMD=y
  19. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  20. # BR2_TARGET_ROOTFS_TAR is not set
  21. BR2_PER_PACKAGE_DIRECTORIES=y
  22. """
  23. def check_systemd(self, fs):
  24. if "BR2_LINUX_KERNEL=y" in self.config:
  25. self.start_emulator(fs, "zImage", "vexpress-v2p-ca9")
  26. else:
  27. self.start_emulator(fs)
  28. self.check_init("/lib/systemd/systemd")
  29. # Test all units are OK
  30. output, _ = self.emulator.run("systemctl --no-pager --failed --no-legend")
  31. self.assertEqual(len(output), 0)
  32. # Test we can reach the DBus daemon
  33. self.assertRunOk("busctl --no-pager")
  34. # Test we can read at least one line from the journal
  35. output, _ = self.emulator.run("journalctl --no-pager --lines 1 --quiet")
  36. self.assertEqual(len(output), 1)
  37. # Check the network is up
  38. self.check_network("eth0")
  39. class TestInitSystemSystemdRoNetworkd(InitSystemSystemdBase):
  40. config = InitSystemSystemdBase.config + \
  41. """
  42. BR2_SYSTEM_DHCP="eth0"
  43. # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
  44. BR2_TARGET_ROOTFS_SQUASHFS=y
  45. """
  46. def test_run(self):
  47. self.check_systemd("squashfs")
  48. class TestInitSystemSystemdRwNetworkd(InitSystemSystemdBase):
  49. config = InitSystemSystemdBase.config + \
  50. """
  51. BR2_SYSTEM_DHCP="eth0"
  52. BR2_TARGET_ROOTFS_EXT2=y
  53. """
  54. def test_run(self):
  55. self.check_systemd("ext2")
  56. class TestInitSystemSystemdRoIfupdown(InitSystemSystemdBase):
  57. config = InitSystemSystemdBase.config + \
  58. """
  59. BR2_SYSTEM_DHCP="eth0"
  60. # BR2_PACKAGE_SYSTEMD_NETWORKD is not set
  61. # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
  62. BR2_TARGET_ROOTFS_SQUASHFS=y
  63. """
  64. def test_run(self):
  65. self.check_systemd("squashfs")
  66. class TestInitSystemSystemdRoIfupdownDbusbroker(TestInitSystemSystemdRoIfupdown):
  67. config = TestInitSystemSystemdRoIfupdown.config + \
  68. """
  69. BR2_PACKAGE_DBUS_BROKER=y
  70. """
  71. def test_run(self):
  72. # Parent class' test_run() method does exactly that, no more:
  73. self.check_systemd("squashfs")
  74. # Check that the dbus-broker daemon is running as non-root
  75. cmd = "find /proc/$(pidof dbus-broker) -maxdepth 1 -name exe -user dbus"
  76. out, _ = self.emulator.run(cmd)
  77. self.assertEqual(len(out), 1)
  78. class TestInitSystemSystemdRoIfupdownDbusbrokerDbus(TestInitSystemSystemdRoIfupdownDbusbroker):
  79. config = TestInitSystemSystemdRoIfupdownDbusbroker.config + \
  80. """
  81. BR2_PACKAGE_DBUS=y
  82. """
  83. class TestInitSystemSystemdRwIfupdown(InitSystemSystemdBase):
  84. config = InitSystemSystemdBase.config + \
  85. """
  86. BR2_SYSTEM_DHCP="eth0"
  87. # BR2_PACKAGE_SYSTEMD_NETWORKD is not set
  88. BR2_TARGET_ROOTFS_EXT2=y
  89. """
  90. def test_run(self):
  91. self.check_systemd("ext2")
  92. class TestInitSystemSystemdRwIfupdownDbusbroker(TestInitSystemSystemdRwIfupdown):
  93. config = TestInitSystemSystemdRwIfupdown.config + \
  94. """
  95. BR2_PACKAGE_DBUS_BROKER=y
  96. """
  97. def test_run(self):
  98. # Parent class' test_run() method does exactly that, no more:
  99. self.check_systemd("ext2")
  100. # Check that the dbus-broker daemon is running as non-root
  101. cmd = "find /proc/$(pidof dbus-broker) -maxdepth 1 -name exe -user dbus"
  102. out, _ = self.emulator.run(cmd)
  103. self.assertEqual(len(out), 1)
  104. class TestInitSystemSystemdRwIfupdownDbusbrokerDbus(TestInitSystemSystemdRwIfupdownDbusbroker):
  105. config = TestInitSystemSystemdRwIfupdownDbusbroker.config + \
  106. """
  107. BR2_PACKAGE_DBUS=y
  108. """
  109. class TestInitSystemSystemdRoFull(InitSystemSystemdBase):
  110. config = InitSystemSystemdBase.config + \
  111. """
  112. BR2_SYSTEM_DHCP="eth0"
  113. # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
  114. BR2_PACKAGE_SYSTEMD_JOURNAL_REMOTE=y
  115. BR2_PACKAGE_SYSTEMD_BACKLIGHT=y
  116. BR2_PACKAGE_SYSTEMD_BINFMT=y
  117. BR2_PACKAGE_SYSTEMD_COREDUMP=y
  118. BR2_PACKAGE_SYSTEMD_FIRSTBOOT=y
  119. BR2_PACKAGE_SYSTEMD_HIBERNATE=y
  120. BR2_PACKAGE_SYSTEMD_IMPORTD=y
  121. BR2_PACKAGE_SYSTEMD_LOCALED=y
  122. BR2_PACKAGE_SYSTEMD_LOGIND=y
  123. BR2_PACKAGE_SYSTEMD_MACHINED=y
  124. BR2_PACKAGE_SYSTEMD_POLKIT=y
  125. BR2_PACKAGE_SYSTEMD_QUOTACHECK=y
  126. BR2_PACKAGE_SYSTEMD_RANDOMSEED=y
  127. BR2_PACKAGE_SYSTEMD_RFKILL=y
  128. BR2_PACKAGE_SYSTEMD_SMACK_SUPPORT=y
  129. BR2_PACKAGE_SYSTEMD_SYSUSERS=y
  130. BR2_PACKAGE_SYSTEMD_VCONSOLE=y
  131. BR2_TARGET_ROOTFS_SQUASHFS=y
  132. """
  133. def test_run(self):
  134. self.check_systemd("squashfs")
  135. class TestInitSystemSystemdRwFull(InitSystemSystemdBase):
  136. config = InitSystemSystemdBase.config + \
  137. """
  138. BR2_SYSTEM_DHCP="eth0"
  139. BR2_PACKAGE_SYSTEMD_JOURNAL_REMOTE=y
  140. BR2_PACKAGE_SYSTEMD_BACKLIGHT=y
  141. BR2_PACKAGE_SYSTEMD_BINFMT=y
  142. BR2_PACKAGE_SYSTEMD_COREDUMP=y
  143. BR2_PACKAGE_SYSTEMD_FIRSTBOOT=y
  144. BR2_PACKAGE_SYSTEMD_HIBERNATE=y
  145. BR2_PACKAGE_SYSTEMD_IMPORTD=y
  146. BR2_PACKAGE_SYSTEMD_LOCALED=y
  147. BR2_PACKAGE_SYSTEMD_LOGIND=y
  148. BR2_PACKAGE_SYSTEMD_MACHINED=y
  149. BR2_PACKAGE_SYSTEMD_POLKIT=y
  150. BR2_PACKAGE_SYSTEMD_QUOTACHECK=y
  151. BR2_PACKAGE_SYSTEMD_RANDOMSEED=y
  152. BR2_PACKAGE_SYSTEMD_RFKILL=y
  153. BR2_PACKAGE_SYSTEMD_SMACK_SUPPORT=y
  154. BR2_PACKAGE_SYSTEMD_SYSUSERS=y
  155. BR2_PACKAGE_SYSTEMD_VCONSOLE=y
  156. BR2_TARGET_ROOTFS_EXT2=y
  157. """
  158. def test_run(self):
  159. self.check_systemd("ext2")
  160. # The following tests are all about read-only rootfs, and exercise either
  161. # using an un-populated factory for /var, or an overlaysfs on top of a
  162. # pre-populated /var. They all specialise the TestInitSystemSystemdRo*
  163. # test cases above.
  164. # Helper class for factory-based tests
  165. class InitSystemSystemdBaseFactory():
  166. config = \
  167. """
  168. # BR2_INIT_SYSTEMD_POPULATE_TMPFILES is not set
  169. BR2_ROOTFS_OVERLAY="{}"
  170. """.format(infra.filepath("tests/init/systemd-factory"))
  171. def test_run(self):
  172. super().test_run()
  173. # This one must be executed on the target, to check that
  174. # the factory feature works as expected
  175. out, exit_code = self.emulator.run("cat /var/foo/bar")
  176. self.assertEqual(exit_code, 0)
  177. self.assertEqual(out[0], "foobar")
  178. # /var/foo/bar is from the /var factory
  179. _, exit_code = self.emulator.run("test -e /usr/share/factory/var/foo/bar")
  180. self.assertEqual(exit_code, 0)
  181. # We can write in /var/foo/bar
  182. _, exit_code = self.emulator.run("echo barfoo >/var/foo/bar")
  183. self.assertEqual(exit_code, 0)
  184. # ... and it contains the new content
  185. out, exit_code = self.emulator.run("cat /var/foo/bar")
  186. self.assertEqual(exit_code, 0)
  187. self.assertEqual(out[0], "barfoo")
  188. # ... but the factory is umodified
  189. out, exit_code = self.emulator.run("cat /usr/share/factory/var/foo/bar")
  190. self.assertEqual(exit_code, 0)
  191. self.assertEqual(out[0], "foobar")
  192. class TestInitSystemSystemdRoNetworkdFactory(
  193. InitSystemSystemdBaseFactory,
  194. TestInitSystemSystemdRoNetworkd,
  195. ):
  196. config = InitSystemSystemdBaseFactory.config + \
  197. TestInitSystemSystemdRoNetworkd.config
  198. class TestInitSystemSystemdRoIfupdownFactory(
  199. InitSystemSystemdBaseFactory,
  200. TestInitSystemSystemdRoIfupdown,
  201. ):
  202. config = InitSystemSystemdBaseFactory.config + \
  203. TestInitSystemSystemdRoIfupdown.config
  204. class TestInitSystemSystemdRoIfupdownDbusbrokerFactory(
  205. InitSystemSystemdBaseFactory,
  206. TestInitSystemSystemdRoIfupdownDbusbroker,
  207. ):
  208. config = InitSystemSystemdBaseFactory.config + \
  209. TestInitSystemSystemdRoIfupdownDbusbroker.config
  210. class TestInitSystemSystemdRoIfupdownDbusbrokerDbusFactory(
  211. InitSystemSystemdBaseFactory,
  212. TestInitSystemSystemdRoIfupdownDbusbrokerDbus,
  213. ):
  214. config = InitSystemSystemdBaseFactory.config + \
  215. TestInitSystemSystemdRoIfupdownDbusbrokerDbus.config
  216. class TestInitSystemSystemdRoFullFactory(
  217. InitSystemSystemdBaseFactory,
  218. TestInitSystemSystemdRoFull,
  219. ):
  220. config = InitSystemSystemdBaseFactory.config + \
  221. TestInitSystemSystemdRoFull.config
  222. # Helper class for overlayfs-based tests
  223. class InitSystemSystemdBaseOverlayfs():
  224. config = \
  225. """
  226. # BR2_INIT_SYSTEMD_VAR_FACTORY is not set
  227. BR2_INIT_SYSTEMD_VAR_OVERLAYFS=y
  228. BR2_ROOTFS_OVERLAY="{}"
  229. BR2_LINUX_KERNEL=y
  230. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  231. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="5.10.202"
  232. BR2_LINUX_KERNEL_DEFCONFIG="vexpress"
  233. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{}"
  234. BR2_LINUX_KERNEL_DTS_SUPPORT=y
  235. BR2_LINUX_KERNEL_INTREE_DTS_NAME="vexpress-v2p-ca9"
  236. """.format(infra.filepath("tests/init/systemd-factory"),
  237. infra.filepath("conf/overlayfs-kernel-fragment.config"))
  238. def test_run(self):
  239. super().test_run()
  240. # This one must be executed on the target, to check that
  241. # the tmpfiles pre-populate works as expected
  242. out, exit_code = self.emulator.run("cat /var/foo/bar")
  243. self.assertEqual(exit_code, 0)
  244. self.assertEqual(out[0], "foobar")
  245. # /var/foo/bar is from the pre-populated /var, so it should
  246. # not be present in the upper of the overlay
  247. _, exit_code = self.emulator.run("test -e /run/buildroot/mounts/var/upper/foo/bar")
  248. self.assertNotEqual(exit_code, 0)
  249. # We can write in /var/foo/bar
  250. _, exit_code = self.emulator.run("echo barfoo >/var/foo/bar")
  251. self.assertEqual(exit_code, 0)
  252. # ... and it contains the new content
  253. out, exit_code = self.emulator.run("cat /var/foo/bar")
  254. self.assertEqual(exit_code, 0)
  255. self.assertEqual(out[0], "barfoo")
  256. # ... and it to appears in the upper
  257. _, exit_code = self.emulator.run("test -e /run/buildroot/mounts/var/upper/foo/bar")
  258. self.assertEqual(exit_code, 0)
  259. # ... with the new content
  260. out, exit_code = self.emulator.run("cat /run/buildroot/mounts/var/upper/foo/bar")
  261. self.assertEqual(exit_code, 0)
  262. self.assertEqual(out[0], "barfoo")
  263. # ... while the lower still has the oldcontent
  264. out, exit_code = self.emulator.run("cat /run/buildroot/mounts/var/lower/foo/bar")
  265. self.assertEqual(exit_code, 0)
  266. self.assertEqual(out[0], "foobar")
  267. class TestInitSystemSystemdRoNetworkdOverlayfs(
  268. InitSystemSystemdBaseOverlayfs,
  269. TestInitSystemSystemdRoNetworkd,
  270. ):
  271. config = InitSystemSystemdBaseOverlayfs.config + \
  272. TestInitSystemSystemdRoNetworkd.config
  273. class TestInitSystemSystemdRoIfupdownOverlayfs(
  274. InitSystemSystemdBaseOverlayfs,
  275. TestInitSystemSystemdRoIfupdown,
  276. ):
  277. config = InitSystemSystemdBaseOverlayfs.config + \
  278. TestInitSystemSystemdRoIfupdown.config
  279. class TestInitSystemSystemdRoIfupdownDbusbrokerOverlayfs(
  280. InitSystemSystemdBaseOverlayfs,
  281. TestInitSystemSystemdRoIfupdownDbusbroker,
  282. ):
  283. config = InitSystemSystemdBaseOverlayfs.config + \
  284. TestInitSystemSystemdRoIfupdownDbusbroker.config
  285. class TestInitSystemSystemdRoIfupdownDbusbrokerDbusOverlayfs(
  286. InitSystemSystemdBaseOverlayfs,
  287. TestInitSystemSystemdRoIfupdownDbusbrokerDbus,
  288. ):
  289. config = InitSystemSystemdBaseOverlayfs.config + \
  290. TestInitSystemSystemdRoIfupdownDbusbrokerDbus.config
  291. class TestInitSystemSystemdRoFullOverlayfs(
  292. InitSystemSystemdBaseOverlayfs,
  293. TestInitSystemSystemdRoFull,
  294. ):
  295. config = InitSystemSystemdBaseOverlayfs.config + \
  296. TestInitSystemSystemdRoFull.config
  297. class InitSystemSystemdBaseOverlayfsVarBacking(InitSystemBase):
  298. @classmethod
  299. def gen_config(cls, overlaydir: str) -> str:
  300. return re.sub(
  301. r'^\s*BR2_ROOTFS_OVERLAY="(.*)"$',
  302. 'BR2_ROOTFS_OVERLAY="\\1 {}"'.format(infra.filepath(overlaydir)),
  303. TestInitSystemSystemdRoFullOverlayfs.config,
  304. flags=re.MULTILINE,
  305. )
  306. def check_var_mounted(self):
  307. self.assertRunOk("grep '^other-var-backing-store /run/buildroot/mounts/var tmpfs' /proc/mounts")
  308. class TestInitSystemSystemdRoFullOverlayfsVarBackingMountUnit(
  309. TestInitSystemSystemdRoFullOverlayfs,
  310. InitSystemSystemdBaseOverlayfsVarBacking,
  311. ):
  312. config = InitSystemSystemdBaseOverlayfsVarBacking.gen_config(
  313. 'tests/init/systemd-overlay-mount-unit',
  314. )
  315. def test_run(self):
  316. super().test_run()
  317. self.check_var_mounted()
  318. class TestInitSystemSystemdRoFullOverlayfsVarBackingFstab(
  319. TestInitSystemSystemdRoFullOverlayfs,
  320. InitSystemSystemdBaseOverlayfsVarBacking,
  321. ):
  322. config = InitSystemSystemdBaseOverlayfsVarBacking.gen_config(
  323. 'tests/init/systemd-overlay-fstab',
  324. )
  325. def test_run(self):
  326. super().test_run()
  327. self.check_var_mounted()