IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

203 lines
6.8KB

  1. from focker.compose import exec_hook, \
  2. exec_prebuild, \
  3. exec_postbuild, \
  4. build_volumes, \
  5. build_images, \
  6. setup_dependencies
  7. from tempfile import TemporaryDirectory
  8. import os
  9. import pytest
  10. import fcntl
  11. from focker.misc import focker_lock, \
  12. focker_unlock
  13. import inspect
  14. import ast
  15. import stat
  16. from focker.zfs import zfs_find, \
  17. zfs_mountpoint, \
  18. zfs_parse_output
  19. import subprocess
  20. import yaml
  21. import jailconf
  22. from focker.jail import backup_file
  23. def test_exec_hook_01():
  24. spec = [
  25. 'touch test-exec-hook-01',
  26. 'touch test-exec-hook-02'
  27. ]
  28. with TemporaryDirectory() as d:
  29. exec_hook(spec, d, 'test-exec-hook')
  30. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  31. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  32. assert not os.path.exists(d)
  33. def test_exec_hook_02():
  34. spec = 'touch test-exec-hook-01 && touch test-exec-hook-02'
  35. with TemporaryDirectory() as d:
  36. exec_hook(spec, d, 'test-exec-hook')
  37. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  38. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  39. assert not os.path.exists(d)
  40. def test_exec_hook_03a():
  41. spec = 1
  42. with TemporaryDirectory() as d:
  43. with pytest.raises(ValueError):
  44. exec_hook(spec, d, 'test-exec-hook')
  45. def test_exec_hook_03b():
  46. spec = [1]
  47. with TemporaryDirectory() as d:
  48. with pytest.raises(TypeError):
  49. exec_hook(spec, d, 'test-exec-hook')
  50. def test_exec_hook_04():
  51. spec = 'ls'
  52. with pytest.raises(FileNotFoundError):
  53. exec_hook(spec, '/non-existent-directory/wcj20fy103', 'test-exec-hook')
  54. def test_exec_hook_05():
  55. spec = 'ls'
  56. oldwd = os.getcwd()
  57. with TemporaryDirectory() as d:
  58. exec_hook(spec, d, 'test-exec-hook')
  59. assert os.getcwd() == oldwd
  60. def test_exec_hook_06():
  61. spec = '/non-existent-command/hf249h'
  62. with TemporaryDirectory() as d:
  63. with pytest.raises(RuntimeError):
  64. exec_hook(spec, d, 'test-exec-hook')
  65. def test_exec_hook_07():
  66. os.chdir('/')
  67. spec = 'flock --nonblock /var/lock/focker.lock -c ls'
  68. focker_lock()
  69. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  70. with TemporaryDirectory() as d:
  71. exec_hook(spec, d, 'test-exec-hook')
  72. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  73. focker_unlock()
  74. def _test_simple_forward(fun, fwd_fun_name='exec_hook'):
  75. src = inspect.getsource(fun)
  76. mod = ast.parse(src)
  77. assert isinstance(mod.body[0], ast.FunctionDef)
  78. assert isinstance(mod.body[0].body[0], ast.Return)
  79. assert isinstance(mod.body[0].body[0].value, ast.Call)
  80. assert mod.body[0].body[0].value.func.id == fwd_fun_name
  81. def test_exec_prebuild():
  82. _test_simple_forward(exec_prebuild)
  83. def test_exec_postbuild():
  84. _test_simple_forward(exec_postbuild)
  85. def test_build_volumes():
  86. subprocess.check_output(['focker', 'volume', 'remove', '--force', 'test-build-volumes'])
  87. err = False
  88. try:
  89. name, _ = zfs_find('test-build-volumes', focker_type='volume')
  90. except:
  91. err = True
  92. assert err
  93. spec = {
  94. 'test-build-volumes': {
  95. 'chown': '65534:65534',
  96. 'chmod': 0o123,
  97. 'zfs': {
  98. 'quota': '1G',
  99. 'readonly': 'on'
  100. }
  101. }
  102. }
  103. build_volumes(spec)
  104. name, _ = zfs_find('test-build-volumes', focker_type='volume')
  105. st = os.stat(zfs_mountpoint(name))
  106. assert st.st_uid == 65534
  107. assert st.st_gid == 65534
  108. assert ('%o' % st.st_mode)[-3:] == '123'
  109. zst = zfs_parse_output(['zfs', 'get', '-H', 'quota,readonly', name])
  110. assert zst[0][2] == '1G'
  111. assert zst[1][2] == 'on'
  112. subprocess.check_output(['zfs', 'destroy', '-r', '-f', name])
  113. def test_build_images():
  114. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
  115. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '--tags', 'test-focker-bootstrap'])
  116. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-build-images'])
  117. with TemporaryDirectory() as d:
  118. with open(os.path.join(d, 'Fockerfile'), 'w') as f:
  119. yaml.dump({
  120. 'base': 'test-focker-bootstrap',
  121. 'steps': [
  122. { 'copy': [
  123. [ '/bin/sh', '/bin/sh', { 'chmod': 0o777 } ],
  124. [ '/lib/libedit.so.7', '/lib/libedit.so.7' ],
  125. [ '/lib/libncursesw.so.8', '/lib/libncursesw.so.8' ],
  126. [ '/lib/libc.so.7', '/lib/libc.so.7' ],
  127. [ '/usr/bin/touch', '/usr/bin/touch', { 'chmod': 0o777 } ],
  128. [ '/libexec/ld-elf.so.1', '/libexec/ld-elf.so.1', { 'chmod': 0o555 } ]
  129. ] },
  130. { 'run': 'touch /test-build-images' }
  131. ]
  132. }, f)
  133. args = lambda: 0
  134. args.squeeze = False
  135. build_images({
  136. 'test-build-images': '.'
  137. }, d, args)
  138. focker_unlock()
  139. name, _ = zfs_find('test-build-images', focker_type='image')
  140. assert os.path.exists(os.path.join(zfs_mountpoint(name), 'test-build-images'))
  141. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-build-images'])
  142. subprocess.check_output(['focker', 'image', 'prune'])
  143. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
  144. def test_setup_dependencies():
  145. backup_file('/etc/jail.conf')
  146. conf = jailconf.load('/etc/jail.conf')
  147. jail = jailconf.JailBlock()
  148. conf['test-setup-dependencies-A'] = jail
  149. conf['test-setup-dependencies-B'] = jail
  150. conf['test-setup-dependencies-C'] = jail
  151. conf.write('/etc/jail.conf')
  152. setup_dependencies({
  153. 'test-setup-dependencies-A': {},
  154. 'test-setup-dependencies-B': { 'depend': 'test-setup-dependencies-A' },
  155. 'test-setup-dependencies-C': { 'depend': [
  156. 'test-setup-dependencies-A',
  157. 'test-setup-dependencies-B'
  158. ] }
  159. }, {
  160. 'test-setup-dependencies-A': 'test-setup-dependencies-A',
  161. 'test-setup-dependencies-B': 'test-setup-dependencies-B',
  162. 'test-setup-dependencies-C': 'test-setup-dependencies-C'
  163. })
  164. conf = jailconf.load('/etc/jail.conf')
  165. assert 'depend' not in conf['test-setup-dependencies-A']
  166. assert conf['test-setup-dependencies-B']['depend'] == 'test-setup-dependencies-A'
  167. assert conf['test-setup-dependencies-C']['depend'] == [
  168. 'test-setup-dependencies-A',
  169. 'test-setup-dependencies-B'
  170. ]
  171. del conf['test-setup-dependencies-A']
  172. del conf['test-setup-dependencies-B']
  173. del conf['test-setup-dependencies-C']
  174. conf.write('/etc/jail.conf')