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_image.py 7.7KB

4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import pytest
  2. from focker.image import validate_spec, \
  3. build_squeeze, \
  4. build, \
  5. command_image_build, \
  6. command_image_tag, \
  7. command_image_untag, \
  8. command_image_list
  9. import subprocess
  10. from tempfile import TemporaryDirectory
  11. import focker.image
  12. import os
  13. from focker.zfs import zfs_find, \
  14. zfs_mountpoint, \
  15. zfs_exists_snapshot_sha256, \
  16. zfs_parse_output
  17. from focker.misc import focker_unlock
  18. import yaml
  19. def test_validate_spec_01():
  20. spec = { 'base': 'base', 'steps': 'steps' }
  21. validate_spec(spec)
  22. def test_validate_spec_02():
  23. spec = { 'steps': 'steps' }
  24. with pytest.raises(ValueError):
  25. validate_spec(spec)
  26. def test_validate_spec_03():
  27. spec = { 'base': 'base' }
  28. with pytest.raises(ValueError):
  29. validate_spec(spec)
  30. def test_validate_spec_04():
  31. spec = {}
  32. with pytest.raises(ValueError):
  33. validate_spec(spec)
  34. def test_build_squeeze(monkeypatch):
  35. focker_unlock()
  36. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-build-squeeze-base'])
  37. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-build-squeeze-base'])
  38. spec = dict(base='test-build-squeeze-base', steps=[
  39. dict(copy=['/etc/localtime', '/etc/localtime']),
  40. dict(copy=['/etc/hosts', '/etc/hosts'])
  41. ])
  42. _, base_sha256 = zfs_find('test-build-squeeze-base', focker_type='image')
  43. def fail(sha256, *args, **kwargs):
  44. if sha256 != base_sha256:
  45. raise RuntimeError('No pre-existing layers expected apart from base')
  46. monkeypatch.setattr(focker.image, 'zfs_snapshot_by_sha256', fail)
  47. with TemporaryDirectory() as d:
  48. args = lambda: 0
  49. args.focker_dir = d
  50. name, _ = build_squeeze(spec, args)
  51. focker_unlock()
  52. mountpoint = zfs_mountpoint(name.split('@')[0])
  53. print('name:', name, 'mountpoint:', mountpoint)
  54. assert os.path.exists(os.path.join(mountpoint, 'etc/localtime'))
  55. assert os.path.exists(os.path.join(mountpoint, 'etc/hosts'))
  56. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-build-squeeze-base'])
  57. assert not os.path.exists(mountpoint)
  58. def test_build(monkeypatch):
  59. focker_unlock()
  60. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-build-squeeze-base'])
  61. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-build-squeeze-base'])
  62. spec = dict(base='test-build-squeeze-base', steps=[
  63. dict(copy=['/etc/localtime', '/etc/localtime']),
  64. dict(copy=['/etc/hosts', '/etc/hosts'])
  65. ])
  66. _, base_sha256 = zfs_find('test-build-squeeze-base', focker_type='image')
  67. counter = 0
  68. def count_calls(*args, **kwargs):
  69. nonlocal counter
  70. counter += 1
  71. return zfs_exists_snapshot_sha256(*args, **kwargs)
  72. monkeypatch.setattr(focker.image, 'zfs_exists_snapshot_sha256', count_calls)
  73. with TemporaryDirectory() as d:
  74. args = lambda: 0
  75. args.focker_dir = d
  76. name, _ = build(spec, args)
  77. assert counter == 2
  78. focker_unlock()
  79. mountpoint = zfs_mountpoint(name.split('@')[0])
  80. print('name:', name, 'mountpoint:', mountpoint)
  81. assert os.path.exists(os.path.join(mountpoint, 'etc/localtime'))
  82. assert os.path.exists(os.path.join(mountpoint, 'etc/hosts'))
  83. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-build-squeeze-base'])
  84. assert not os.path.exists(mountpoint)
  85. def test_command_image_build():
  86. focker_unlock()
  87. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-command-image-build-base'])
  88. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-command-image-build-base'])
  89. with TemporaryDirectory() as d:
  90. args = lambda: 0
  91. with open(os.path.join(d, 'Fockerfile'), 'w') as f:
  92. yaml.dump(dict(base='test-command-image-build-base', steps=[
  93. dict(copy=['/etc/localtime', '/etc/localtime']),
  94. dict(copy=['/etc/hosts', '/etc/hosts'])
  95. ]), f)
  96. args.focker_dir = d
  97. args.squeeze = False
  98. args.tags = [ 'test-command-image-build' ]
  99. command_image_build(args)
  100. focker_unlock()
  101. name, _ = zfs_find('test-command-image-build', focker_type='image')
  102. mountpoint = zfs_mountpoint(name)
  103. assert os.path.exists(os.path.join(mountpoint, 'etc/localtime'))
  104. assert os.path.exists(os.path.join(mountpoint, 'etc/hosts'))
  105. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-command-image-build-base'])
  106. assert not os.path.exists(mountpoint)
  107. def test_command_image_tag():
  108. focker_unlock()
  109. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-command-image-tag'])
  110. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-command-image-tag'])
  111. name_1, sha256_1 = zfs_find('test-command-image-tag', focker_type='image')
  112. args = lambda: 0
  113. args.reference = sha256_1
  114. args.tags = ['test-a', 'test-b', 'test-c']
  115. command_image_tag(args)
  116. for t in args.tags:
  117. name_2, sha256_2 = zfs_find(t, focker_type='image')
  118. assert name_2 == name_1
  119. assert sha256_2 == sha256_1
  120. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-command-image-tag'])
  121. for t in args.tags:
  122. with pytest.raises(ValueError):
  123. zfs_find(t, focker_type='image')
  124. with pytest.raises(ValueError):
  125. zfs_find('test-command-image-tag', focker_type='image')
  126. def test_command_image_untag():
  127. focker_unlock()
  128. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-command-image-untag'])
  129. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-command-image-untag', 'test-command-image-untag-1', 'test-command-image-untag-2'])
  130. name, sha256 = zfs_find('test-command-image-untag', focker_type='image')
  131. args = lambda: 0
  132. args.tags = ['test-command-image-untag-1', 'test-command-image-untag-2']
  133. command_image_untag(args)
  134. tags = zfs_parse_output(['zfs', 'get', '-H', 'focker:tags', name])
  135. tags = tags[0][2].split(',')
  136. assert tags == ['test-command-image-untag']
  137. with pytest.raises(ValueError):
  138. zfs_find('test-command-image-untag-1', focker_type='image')
  139. with pytest.raises(ValueError):
  140. zfs_find('test-command-image-untag-2', focker_type='image')
  141. subprocess.check_output(['focker', 'image', 'remove', 'test-command-image-untag'])
  142. def test_command_image_list(monkeypatch):
  143. focker_unlock()
  144. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-command-image-list'])
  145. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-command-image-list', 'test-command-image-list-1', 'test-command-image-list-2'])
  146. name, sha256 = zfs_find('test-command-image-list', focker_type='image')
  147. args = lambda: 0
  148. args.tagged_only = True
  149. args.full_sha256 = True
  150. lst = None
  151. headers = None
  152. def fake_tabulate(*args, **kwargs):
  153. nonlocal lst
  154. nonlocal headers
  155. lst = args[0]
  156. headers = kwargs['headers']
  157. monkeypatch.setattr(focker.image, 'tabulate', fake_tabulate)
  158. command_image_list(args)
  159. assert lst is not None
  160. assert headers == ['Tags', 'Size', 'SHA256', 'Base']
  161. assert len(lst) >= 3
  162. match = list(filter(lambda a: sorted(a[0].split(' ')) == ['test-command-image-list', 'test-command-image-list-1', 'test-command-image-list-2'], lst))
  163. assert len(match) == 1
  164. match = match[0]
  165. assert match[2] == sha256
  166. assert match[3] == '-'
  167. subprocess.check_output(['focker', 'image', 'remove', 'test-command-image-list'])