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.

test_compose.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. 'jail.conf': {
  201. 'allow.mount': True,
  202. 'ip6.addr': 'abcd:abcd::0'
  203. }
  204. }
  205. }
  206. spec['test-build-jails-B'] = spec['test-build-jails-A'].copy()
  207. spec['test-build-jails-B']['host.hostname'] = 'test-build-jails-B'
  208. build_jails(spec)
  209. conf = jailconf.load('/etc/jail.conf')
  210. print(conf.values())
  211. blocks = list(filter(lambda a: a['host.hostname'].strip('"\'') in [ 'test-build-jails-A',
  212. 'test-build-jails-B' ], conf.values()))
  213. print(blocks)
  214. assert len(blocks) == 2
  215. assert blocks[0]['host.hostname'] != blocks[1]['host.hostname']
  216. for b in blocks:
  217. name, _ = zfs_find(b['host.hostname'].strip('\'"'), focker_type='jail')
  218. mountpoint = zfs_mountpoint(name)
  219. assert b['path'] == mountpoint
  220. assert b['exec.start'].strip('\'"') == 'test-exec-start'
  221. assert b['exec.stop'].strip('\'"') == 'test-exec-stop'
  222. assert b['ip4.addr'].strip('\'"') == 'test-ip4-addr'
  223. assert b['interface'].strip('\'"') == 'test-interface'
  224. assert b['allow.mount']
  225. assert b['ip6.addr'] == '\'abcd:abcd::0\''
  226. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-A'])
  227. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-B'])
  228. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
  229. for k in list(conf.keys()):
  230. if conf[k]['host.hostname'].strip('\'"') in ['test-build-jails-A', 'test-build-jails-B']:
  231. del conf[k]
  232. conf.write('/etc/jail.conf')
  233. def test_command_compose_build(monkeypatch):
  234. with TemporaryDirectory() as d:
  235. with open(os.path.join(d, 'focker-compose.yml'), 'w') as f:
  236. yaml.dump({
  237. 'exec.prebuild': 'echo exec-prebuild',
  238. 'volumes': {},
  239. 'images': {},
  240. 'jails': {},
  241. 'exec.postbuild': 'echo exec-postbuild'
  242. }, f)
  243. args = lambda: 0
  244. args.filename = os.path.join(d, 'focker-compose.yml')
  245. log = defaultdict(list)
  246. def log_calls(fun):
  247. old = fun.__call__
  248. def inner(*args, **kwargs):
  249. log[fun.__name__].append((args, kwargs))
  250. return old(*args, **kwargs)
  251. return inner
  252. monkeypatch.setattr(focker.compose, 'exec_prebuild', log_calls(exec_prebuild))
  253. monkeypatch.setattr(focker.compose, 'build_volumes', log_calls(build_volumes))
  254. monkeypatch.setattr(focker.compose, 'build_images', log_calls(build_images))
  255. monkeypatch.setattr(focker.compose, 'build_jails', log_calls(build_jails))
  256. monkeypatch.setattr(focker.compose, 'exec_postbuild', log_calls(exec_postbuild))
  257. command_compose_build(args)
  258. assert len(log) == 5
  259. assert 'exec_prebuild' in log
  260. assert 'build_volumes' in log
  261. assert 'build_images' in log
  262. assert 'build_jails' in log
  263. assert 'exec_postbuild' in log
  264. assert log['exec_prebuild'][0][0][0] == 'echo exec-prebuild'
  265. assert log['build_volumes'][0][0][0] == {}
  266. assert log['build_images'][0][0][0] == {}
  267. assert log['build_jails'][0][0][0] == {}
  268. assert log['exec_postbuild'][0][0][0] == 'echo exec-postbuild'