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.

145 lines
5.4KB

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