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!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
11KB

  1. from focker.compose import exec_hook, \
  2. exec_prebuild, \
  3. exec_postbuild, \
  4. build_volumes, \
  5. build_images, \
  6. setup_dependencies, \
  7. build_jails, \
  8. command_compose_build
  9. import focker.compose
  10. from tempfile import TemporaryDirectory
  11. import os
  12. import pytest
  13. import fcntl
  14. from focker.misc import focker_lock, \
  15. focker_unlock
  16. import inspect
  17. import ast
  18. import stat
  19. from focker.zfs import zfs_find, \
  20. zfs_mountpoint, \
  21. zfs_parse_output
  22. import subprocess
  23. import yaml
  24. import jailconf
  25. from focker.jail import backup_file
  26. from collections import defaultdict
  27. def test_exec_hook_01():
  28. spec = [
  29. 'touch test-exec-hook-01',
  30. 'touch test-exec-hook-02'
  31. ]
  32. with TemporaryDirectory() as d:
  33. exec_hook(spec, d, 'test-exec-hook')
  34. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  35. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  36. assert not os.path.exists(d)
  37. def test_exec_hook_02():
  38. spec = 'touch test-exec-hook-01 && touch test-exec-hook-02'
  39. with TemporaryDirectory() as d:
  40. exec_hook(spec, d, 'test-exec-hook')
  41. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  42. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  43. assert not os.path.exists(d)
  44. def test_exec_hook_03a():
  45. spec = 1
  46. with TemporaryDirectory() as d:
  47. with pytest.raises(ValueError):
  48. exec_hook(spec, d, 'test-exec-hook')
  49. def test_exec_hook_03b():
  50. spec = [1]
  51. with TemporaryDirectory() as d:
  52. with pytest.raises(TypeError):
  53. exec_hook(spec, d, 'test-exec-hook')
  54. def test_exec_hook_04():
  55. spec = 'ls'
  56. with pytest.raises(FileNotFoundError):
  57. exec_hook(spec, '/non-existent-directory/wcj20fy103', 'test-exec-hook')
  58. def test_exec_hook_05():
  59. spec = 'ls'
  60. oldwd = os.getcwd()
  61. with TemporaryDirectory() as d:
  62. exec_hook(spec, d, 'test-exec-hook')
  63. assert os.getcwd() == oldwd
  64. def test_exec_hook_06():
  65. spec = '/non-existent-command/hf249h'
  66. with TemporaryDirectory() as d:
  67. with pytest.raises(RuntimeError):
  68. exec_hook(spec, d, 'test-exec-hook')
  69. def test_exec_hook_07():
  70. os.chdir('/')
  71. spec = 'flock --nonblock /var/lock/focker.lock -c ls'
  72. focker_lock()
  73. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  74. with TemporaryDirectory() as d:
  75. exec_hook(spec, d, 'test-exec-hook')
  76. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  77. focker_unlock()
  78. def _test_simple_forward(fun, fwd_fun_name='exec_hook'):
  79. src = inspect.getsource(fun)
  80. mod = ast.parse(src)
  81. assert isinstance(mod.body[0], ast.FunctionDef)
  82. assert isinstance(mod.body[0].body[0], ast.Return)
  83. assert isinstance(mod.body[0].body[0].value, ast.Call)
  84. assert mod.body[0].body[0].value.func.id == fwd_fun_name
  85. def test_exec_prebuild():
  86. _test_simple_forward(exec_prebuild)
  87. def test_exec_postbuild():
  88. _test_simple_forward(exec_postbuild)
  89. def test_build_volumes():
  90. subprocess.check_output(['focker', 'volume', 'remove', '--force', 'test-build-volumes'])
  91. err = False
  92. try:
  93. name, _ = zfs_find('test-build-volumes', focker_type='volume')
  94. except:
  95. err = True
  96. assert err
  97. spec = {
  98. 'test-build-volumes': {
  99. 'chown': '65534:65534',
  100. 'chmod': 0o123,
  101. 'protect': True,
  102. 'zfs': {
  103. 'quota': '1G',
  104. 'readonly': 'on'
  105. }
  106. }
  107. }
  108. build_volumes(spec)
  109. name, _ = zfs_find('test-build-volumes', focker_type='volume')
  110. st = os.stat(zfs_mountpoint(name))
  111. assert st.st_uid == 65534
  112. assert st.st_gid == 65534
  113. assert ('%o' % st.st_mode)[-3:] == '123'
  114. zst = zfs_parse_output(['zfs', 'get', '-H', 'quota,readonly,focker:protect', name])
  115. assert zst[0][2] == '1G'
  116. assert zst[1][2] == 'on'
  117. assert zst[2][2] == 'on'
  118. subprocess.check_output(['zfs', 'destroy', '-r', '-f', name])
  119. def test_build_images():
  120. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
  121. subprocess.check_output(['focker', 'bootstrap', '--empty', '--tags', 'test-focker-bootstrap'])
  122. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-build-images'])
  123. with TemporaryDirectory() as d:
  124. with open(os.path.join(d, 'Fockerfile'), 'w') as f:
  125. yaml.dump({
  126. 'base': 'test-focker-bootstrap',
  127. 'steps': [
  128. { 'copy': [
  129. [ '/bin/sh', '/bin/sh', { 'chmod': 0o777 } ],
  130. [ '/lib/libedit.so.7', '/lib/libedit.so.7' ],
  131. [ '/lib/libncursesw.so.8', '/lib/libncursesw.so.8' ],
  132. [ '/lib/libc.so.7', '/lib/libc.so.7' ],
  133. [ '/usr/bin/touch', '/usr/bin/touch', { 'chmod': 0o777 } ],
  134. [ '/libexec/ld-elf.so.1', '/libexec/ld-elf.so.1', { 'chmod': 0o555 } ]
  135. ] },
  136. { 'run': 'touch /test-build-images' }
  137. ]
  138. }, f)
  139. args = lambda: 0
  140. args.squeeze = False
  141. build_images({
  142. 'test-build-images': '.'
  143. }, d, args)
  144. focker_unlock()
  145. name, _ = zfs_find('test-build-images', focker_type='image')
  146. assert os.path.exists(os.path.join(zfs_mountpoint(name), 'test-build-images'))
  147. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-build-images'])
  148. subprocess.check_output(['focker', 'image', 'prune'])
  149. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
  150. def test_setup_dependencies():
  151. backup_file('/etc/jail.conf')
  152. conf = jailconf.load('/etc/jail.conf')
  153. jail = jailconf.JailBlock()
  154. conf['test-setup-dependencies-A'] = jail
  155. conf['test-setup-dependencies-B'] = jail
  156. conf['test-setup-dependencies-C'] = jail
  157. conf.write('/etc/jail.conf')
  158. setup_dependencies({
  159. 'test-setup-dependencies-A': {},
  160. 'test-setup-dependencies-B': { 'depend': 'test-setup-dependencies-A' },
  161. 'test-setup-dependencies-C': { 'depend': [
  162. 'test-setup-dependencies-A',
  163. 'test-setup-dependencies-B'
  164. ] }
  165. }, {
  166. 'test-setup-dependencies-A': 'test-setup-dependencies-A',
  167. 'test-setup-dependencies-B': 'test-setup-dependencies-B',
  168. 'test-setup-dependencies-C': 'test-setup-dependencies-C'
  169. })
  170. conf = jailconf.load('/etc/jail.conf')
  171. assert 'depend' not in conf['test-setup-dependencies-A']
  172. assert conf['test-setup-dependencies-B']['depend'] == 'test-setup-dependencies-A'
  173. assert conf['test-setup-dependencies-C']['depend'] == [
  174. 'test-setup-dependencies-A',
  175. 'test-setup-dependencies-B'
  176. ]
  177. del conf['test-setup-dependencies-A']
  178. del conf['test-setup-dependencies-B']
  179. del conf['test-setup-dependencies-C']
  180. conf.write('/etc/jail.conf')
  181. def test_build_jails():
  182. backup_file('/etc/jail.conf')
  183. conf = jailconf.load('/etc/jail.conf')
  184. for k in list(conf.keys()):
  185. if conf[k]['host.hostname'].strip('\'"') in ['test-build-jails-A', 'test-build-jails-B']:
  186. del conf[k]
  187. conf.write('/etc/jail.conf')
  188. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-A'])
  189. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-B'])
  190. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-focker-bootstrap'])
  191. subprocess.check_output(['focker', 'bootstrap', '--empty', '-t', 'test-focker-bootstrap'])
  192. spec = {
  193. 'test-build-jails-A': {
  194. 'image': 'test-focker-bootstrap',
  195. 'exec.start': 'test-exec-start',
  196. 'exec.stop': 'test-exec-stop',
  197. 'ip4.addr': 'test-ip4-addr',
  198. 'interface': 'test-interface',
  199. 'host.hostname': 'test-build-jails-A'
  200. }
  201. }
  202. spec['test-build-jails-B'] = spec['test-build-jails-A'].copy()
  203. spec['test-build-jails-B']['host.hostname'] = 'test-build-jails-B'
  204. build_jails(spec)
  205. conf = jailconf.load('/etc/jail.conf')
  206. print(conf.values())
  207. blocks = list(filter(lambda a: a['host.hostname'].strip('"\'') in [ 'test-build-jails-A',
  208. 'test-build-jails-B' ], conf.values()))
  209. print(blocks)
  210. assert len(blocks) == 2
  211. assert blocks[0]['host.hostname'] != blocks[1]['host.hostname']
  212. for b in blocks:
  213. name, _ = zfs_find(b['host.hostname'].strip('\'"'), focker_type='jail')
  214. mountpoint = zfs_mountpoint(name)
  215. assert b['path'] == mountpoint
  216. assert b['exec.start'].strip('\'"') == 'test-exec-start'
  217. assert b['exec.stop'].strip('\'"') == 'test-exec-stop'
  218. assert b['ip4.addr'].strip('\'"') == 'test-ip4-addr'
  219. assert b['interface'].strip('\'"') == 'test-interface'
  220. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-A'])
  221. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-B'])
  222. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
  223. for k in list(conf.keys()):
  224. if conf[k]['host.hostname'].strip('\'"') in ['test-build-jails-A', 'test-build-jails-B']:
  225. del conf[k]
  226. conf.write('/etc/jail.conf')
  227. def test_command_compose_build(monkeypatch):
  228. with TemporaryDirectory() as d:
  229. with open(os.path.join(d, 'focker-compose.yml'), 'w') as f:
  230. yaml.dump({
  231. 'exec.prebuild': 'echo exec-prebuild',
  232. 'volumes': {},
  233. 'images': {},
  234. 'jails': {},
  235. 'exec.postbuild': 'echo exec-postbuild'
  236. }, f)
  237. args = lambda: 0
  238. args.filename = os.path.join(d, 'focker-compose.yml')
  239. log = defaultdict(list)
  240. def log_calls(fun):
  241. old = fun.__call__
  242. def inner(*args, **kwargs):
  243. log[fun.__name__].append((args, kwargs))
  244. return old(*args, **kwargs)
  245. return inner
  246. monkeypatch.setattr(focker.compose, 'exec_prebuild', log_calls(exec_prebuild))
  247. monkeypatch.setattr(focker.compose, 'build_volumes', log_calls(build_volumes))
  248. monkeypatch.setattr(focker.compose, 'build_images', log_calls(build_images))
  249. monkeypatch.setattr(focker.compose, 'build_jails', log_calls(build_jails))
  250. monkeypatch.setattr(focker.compose, 'exec_postbuild', log_calls(exec_postbuild))
  251. command_compose_build(args)
  252. assert len(log) == 5
  253. assert 'exec_prebuild' in log
  254. assert 'build_volumes' in log
  255. assert 'build_images' in log
  256. assert 'build_jails' in log
  257. assert 'exec_postbuild' in log
  258. assert log['exec_prebuild'][0][0][0] == 'echo exec-prebuild'
  259. assert log['build_volumes'][0][0][0] == {}
  260. assert log['build_images'][0][0][0] == {}
  261. assert log['build_jails'][0][0][0] == {}
  262. assert log['exec_postbuild'][0][0][0] == 'echo exec-postbuild'