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!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

165 lignes
6.5KB

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