test_podman.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import infra.basetest
  2. import json
  3. import os
  4. class PodmanBase(infra.basetest.BRTest):
  5. config = \
  6. """
  7. BR2_arm=y
  8. BR2_cortex_a9=y
  9. BR2_ARM_ENABLE_VFP=y
  10. BR2_TOOLCHAIN_EXTERNAL=y
  11. BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
  12. BR2_PER_PACKAGE_DIRECTORIES=y
  13. BR2_SYSTEM_DHCP="eth0"
  14. BR2_LINUX_KERNEL=y
  15. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  16. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="5.10.202"
  17. BR2_LINUX_KERNEL_DEFCONFIG="vexpress"
  18. BR2_LINUX_KERNEL_DTS_SUPPORT=y
  19. BR2_LINUX_KERNEL_INTREE_DTS_NAME="vexpress-v2p-ca9"
  20. BR2_PACKAGE_PODMAN=y
  21. BR2_PACKAGE_UTIL_LINUX=y
  22. BR2_PACKAGE_UTIL_LINUX_MOUNT=y
  23. BR2_PACKAGE_HOST_GO_BIN=y
  24. BR2_TARGET_ROOTFS_EXT2=y
  25. BR2_TARGET_ROOTFS_EXT2_SIZE="256M"
  26. # BR2_TARGET_ROOTFS_TAR is not set
  27. """
  28. def do_test(self):
  29. class _Emul():
  30. def __init__(self, orig_emulator):
  31. self.emulator = orig_emulator
  32. def run(self, cmd, timeout=-1):
  33. if timeout < 0:
  34. timeout = 60
  35. return self.emulator.run(cmd, timeout)
  36. def stop(self):
  37. self.emulator.stop()
  38. kernel_file = os.path.join(self.builddir, 'images', 'zImage')
  39. dtb_file = os.path.join(self.builddir, 'images', 'vexpress-v2p-ca9.dtb')
  40. ext2_file = os.path.join(self.builddir, 'images', 'rootfs.ext2')
  41. self.emulator.boot(
  42. arch='armv5',
  43. kernel=kernel_file,
  44. kernel_cmdline=[
  45. 'root=/dev/mmcblk0',
  46. 'rootwait',
  47. 'console=ttyAMA0',
  48. ],
  49. options=[
  50. '-M', 'vexpress-a9',
  51. '-dtb', dtb_file,
  52. '-drive', f'file={ext2_file},if=sd,format=raw',
  53. ]
  54. )
  55. self.emulator.login()
  56. # Trick: replace the original emulator with one that always
  57. # adds a timeout
  58. self.emulator = _Emul(self.emulator)
  59. # Do some preparation for rootless use
  60. self.assertRunOk("mount --make-shared /")
  61. self.assertRunOk("chmod 666 /dev/net/tun")
  62. self.assertRunOk("useradd -d /home/foo -m -s /bin/sh -u 1000 foo")
  63. self.assertRunOk("touch /etc/subuid /etc/subgid")
  64. self.assertRunOk("usermod --add-subuids 10000-75535 foo")
  65. self.assertRunOk("usermod --add-subgids 10000-75535 foo")
  66. # First, test podman as root (the current user)
  67. self.do_podman()
  68. # Now, test podman as non-root. We need a bit of setup
  69. # We need to use the same prompts for the user as used for root, so that the
  70. # REPLWrapper still detects the prompts. This means it is going to be a bit
  71. # difficut to directly see that it was a user that executed a command.
  72. self.assertRunOk('su -s /usr/bin/env - foo PS1="${PS1}" PS2="${PS2}" /bin/sh')
  73. output, _ = self.emulator.run("id -u")
  74. self.assertEqual(output[0], "1000", "Could not switch to non-root")
  75. self.do_podman()
  76. def do_podman(self):
  77. # The podman binary is huge, so it takes time to load...
  78. # Next calls will be faster, though, as it is going to be cached.
  79. self.assertRunOk('podman --version')
  80. # Check for an empty image store
  81. output, exit_code = self.emulator.run("podman image ls --format '{{ json }}'")
  82. img_info = json.loads("".join(output))
  83. self.assertEqual(len(img_info), 0, f"{len(img_info)} image(s) already present")
  84. # Pull an image; it can take time: network, hash checksums...
  85. self.assertRunOk('podman image pull busybox:1.37.0')
  86. output, exit_code = self.emulator.run("podman image ls --format '{{ json }}'")
  87. img_info = json.loads("".join(output))
  88. self.assertEqual(len(img_info), 1, f"{len(img_info)} image(s), expecting 1")
  89. self.assertTrue("Id" in img_info[0], '"Id" not in img_info[0]')
  90. self.assertTrue("Digest" in img_info[0], '"Digest" not in img_info[0]')
  91. self.assertEqual(img_info[0]["Names"][0], "docker.io/library/busybox:1.37.0")
  92. output, _ = self.emulator.run('echo ${br_container}')
  93. self.assertEqual(output[0], "", "Already in a container")
  94. # Spawn the container; that can take a bit of time
  95. # Propagate the prompt so that the REPLWrapper detects it
  96. self.assertRunOk(
  97. "podman container run --rm -ti -e PS1 -e br_container=podman busybox:1.37.0",
  98. )
  99. # Twist! The command above is still running, but the shell it
  100. # started exposes the same prompt we expect. This is all what we want.
  101. output, _ = self.emulator.run('echo ${br_container}')
  102. self.assertEqual(output[0], "podman", "Not in a podman container")
  103. # Check that pid1 is the shell
  104. output, _ = self.emulator.run('readlink /proc/1/exe')
  105. self.assertEqual(output[0], "/bin/sh", f"PID1 is {output[0]}, should be /bin/sh")
  106. # Try to get something off the network
  107. # Using http, not https, as busybox' wget does not do https
  108. # Using --spider to just check we can reach the remote.
  109. output, exit_code = self.emulator.run('wget --spider http://google.com/')
  110. self.assertEqual(exit_code, 0, "wget did not succeed to reach google.com")
  111. self.assertEqual(output[-1], "remote file exists", "wget did not succeed to reach google.com")
  112. # Exit the container
  113. self.assertRunOk("exit 0")
  114. # Twist, take two! We are now back to the shell in the VM.
  115. output, _ = self.emulator.run('echo ${br_container}')
  116. self.assertEqual(output[0], "", "Still in a container")
  117. # Spawn a container, round two, but with an injected init this time
  118. self.assertRunOk(
  119. "podman container run --rm -ti -e PS1 --init -e br_container=podman busybox:1.37.0",
  120. )
  121. output, _ = self.emulator.run('echo ${br_container}')
  122. self.assertEqual(output[0], "podman", "Not in a podman container")
  123. # Check that pid1 is the init injected by podman
  124. output, _ = self.emulator.run('readlink /proc/1/exe')
  125. self.assertEqual(output[0], "/run/podman-init", f"PID1 is {output[0]}, should be /run/podman-init")
  126. # Exit the container
  127. self.assertRunOk("exit 0")
  128. output, _ = self.emulator.run('echo ${br_container}')
  129. self.assertEqual(output[0], "", "Still in a container")
  130. # Use an image from another registry, spawn without pulling first
  131. self.assertRunOk(
  132. "podman container run --rm -ti -e PS1 -e br_container=podman quay.io/prometheus/busybox:latest",
  133. )
  134. output, _ = self.emulator.run('echo ${br_container}')
  135. self.assertEqual(output[0], "podman", "Not in a podman container")
  136. self.assertRunOk("exit 0")
  137. output, _ = self.emulator.run('echo ${br_container}')
  138. self.assertEqual(output[0], "", "Still in a container")
  139. # Test networking between two containers
  140. self.assertRunOk("podman network create buz")
  141. self.assertRunOk(
  142. "podman container run --rm -ti --name pod007 --network buz --detach busybox:1.37.0",
  143. )
  144. self.assertRunOk(
  145. "podman container run --rm -ti --name pod006 --network buz --detach busybox:1.37.0",
  146. )
  147. # Ensure each pod can resolv itself and the other
  148. # (not using itertools.matrix() just for those trivial combinations)
  149. for pod1, pod2 in [
  150. ("pod006", "pod006"),
  151. ("pod006", "pod007"),
  152. ("pod007", "pod007"),
  153. ("pod007", "pod006"),
  154. ]:
  155. output, exit_code = self.emulator.run(
  156. f"podman container exec {pod1} nslookup {pod2}",
  157. )
  158. self.assertEqual(exit_code, 0)
  159. self.assertTrue(output[0].startswith("Server:"))
  160. self.assertTrue(output[1].startswith("Address:"))
  161. # Busybox' nslookup emits one "Non-authoritative answer" per
  162. # supported address familly: IPv4 and IPv6.
  163. self.assertEqual(
  164. len([line for line in output[2:] if line == "Non-authoritative answer:"]),
  165. 2,
  166. )
  167. # But only IPv4 is available on this network
  168. self.assertEqual(
  169. len([line for line in output[2:] if line.startswith("Address:")]),
  170. 1,
  171. )
  172. self.assertRunOk("podman container kill --all")
  173. output, _ = self.emulator.run("podman container ls --format '{{ json }}'")
  174. pod_info = json.loads("".join(output))
  175. self.assertEqual(len(pod_info), 0, f"{len(pod_info)} container(s) still present, expecting 0")
  176. # Remove the offical image
  177. self.assertRunOk('podman image rm busybox:1.37.0')
  178. output, _ = self.emulator.run("podman image ls --format '{{ json }}'")
  179. img_info = json.loads("".join(output))
  180. # There is still one image(the unofficial one from quay.io)
  181. self.assertEqual(len(img_info), 1, f"{len(img_info)} image(s) still present, expecting 1")
  182. # Remove all remaining images
  183. self.assertRunOk('podman image prune -af')
  184. output, exit_code = self.emulator.run("podman image ls --format '{{ json }}'")
  185. img_info = json.loads("".join(output))
  186. self.assertEqual(len(img_info), 0, f"{len(img_info)} image(s) still present, expecting 0")
  187. class TestPodmanIptables(PodmanBase):
  188. def test_run(self):
  189. self.do_test()
  190. class TestPodmanNftables(PodmanBase):
  191. config = PodmanBase.config + """
  192. BR2_PACKAGE_NFTABLES=y
  193. """
  194. def test_run(self):
  195. self.do_test()
  196. class TestPodmanTini(PodmanBase):
  197. config = PodmanBase.config + """
  198. BR2_PACKAGE_PODMAN_INIT_TINI=y
  199. """
  200. def test_run(self):
  201. self.do_test()
  202. class TestPodmanSlirpIptables(PodmanBase):
  203. config = PodmanBase.config + """
  204. BR2_PACKAGE_PODMAN_NET_SLIRP4NETNS=y
  205. """
  206. def test_run(self):
  207. self.do_test()
  208. class TestPodmanSlirpNftables(PodmanBase):
  209. config = PodmanBase.config + """
  210. BR2_PACKAGE_NFTABLES=y
  211. BR2_PACKAGE_PODMAN_NET_SLIRP4NETNS=y
  212. """
  213. def test_run(self):
  214. self.do_test()