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!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_focker.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. from focker.focker import *
  2. import focker.focker
  3. from collections import defaultdict
  4. import sys
  5. import pytest
  6. def test_parser_bootstrap():
  7. parser = create_parser()
  8. args = parser.parse_args(['bootstrap', '--dry-run', '--tags', 'a', 'b', 'c'])
  9. assert args.func == command_bootstrap
  10. assert args.tags == ['a', 'b', 'c']
  11. assert args.dry_run
  12. def test_parser_image():
  13. parser = create_parser()
  14. args = parser.parse_args(['image', 'build', '.', '--squeeze', '--tags', 'a', 'b', 'c'])
  15. assert args.func == command_image_build
  16. assert args.focker_dir == '.'
  17. assert args.tags == ['a', 'b', 'c']
  18. assert args.squeeze
  19. args = parser.parse_args(['image', 'tag', 'deadbee', 'a', 'b', 'c'])
  20. assert args.func == command_image_tag
  21. assert args.reference == 'deadbee'
  22. assert args.tags == ['a', 'b', 'c']
  23. args = parser.parse_args(['image', 'untag', 'a', 'b', 'c'])
  24. assert args.func == command_image_untag
  25. assert args.tags == ['a', 'b', 'c']
  26. args = parser.parse_args(['image', 'list', '--full-sha256', '--tagged-only'])
  27. assert args.func == command_image_list
  28. assert args.full_sha256
  29. assert args.tagged_only
  30. args = parser.parse_args(['image', 'prune'])
  31. assert args.func == command_image_prune
  32. args = parser.parse_args(['image', 'remove', 'a', '--remove-dependents', '--force'])
  33. assert args.func == command_image_remove
  34. assert args.reference == 'a'
  35. assert args.remove_dependents
  36. assert args.force
  37. def test_parser_jail(monkeypatch):
  38. parser = create_parser()
  39. counter = [0]
  40. def count_calls(fun, counter):
  41. def inner(*args, **kwargs):
  42. counter[0] += 1
  43. return fun(*args, **kwargs)
  44. return inner
  45. monkeypatch.setattr(parser, 'parse_args', count_calls(parser.parse_args, counter))
  46. args = parser.parse_args(['jail', 'create', 'freebsd-latest', '--command', '/test-command', '--tags', 'a', 'b', 'c', '--env', 'd:1', 'e:2', '--mounts', 'mount-A:/a', 'mount-B:/b', '--hostname', 'xyz'])
  47. assert args.func == command_jail_create
  48. assert args.command == '/test-command'
  49. assert args.tags == ['a', 'b', 'c']
  50. assert args.env == ['d:1', 'e:2']
  51. assert args.mounts == ['mount-A:/a', 'mount-B:/b']
  52. assert args.hostname == 'xyz'
  53. args = parser.parse_args(['jail', 'start', 'jail-A'])
  54. assert args.func == command_jail_start
  55. assert args.reference == 'jail-A'
  56. args = parser.parse_args(['jail', 'stop', 'jail-A'])
  57. assert args.func == command_jail_stop
  58. assert args.reference == 'jail-A'
  59. args = parser.parse_args(['jail', 'remove', 'jail-A', '--force'])
  60. assert args.func == command_jail_remove
  61. assert args.reference == 'jail-A'
  62. assert args.force
  63. args = parser.parse_args(['jail', 'exec', 'jail-A', '/bin/sh', '-c', 'ls', '-al'])
  64. assert args.func == command_jail_exec
  65. assert args.reference == 'jail-A'
  66. assert args.command == ['/bin/sh', '-c', 'ls', '-al']
  67. args = parser.parse_args(['jail', 'oneshot', '--env', 'd:1', 'e:2', '--mounts', 'mount-A:/a', 'mount-B:/b', '--', 'image-A', '/bin/sh', '-c', 'ls', '-al'])
  68. assert args.func == command_jail_oneshot
  69. assert args.image == 'image-A'
  70. assert args.env == ['d:1', 'e:2']
  71. assert args.mounts == ['mount-A:/a', 'mount-B:/b']
  72. assert args.command == ['/bin/sh', '-c', 'ls', '-al']
  73. args = parser.parse_args(['jail', 'list', '--full-sha256'])
  74. assert args.func == command_jail_list
  75. assert args.full_sha256
  76. args = parser.parse_args(['jail', 'tag', 'deadbee', 'a', 'b', 'c'])
  77. assert args.func == command_jail_tag
  78. assert args.reference == 'deadbee'
  79. assert args.tags == ['a', 'b', 'c']
  80. args = parser.parse_args(['jail', 'untag', 'a', 'b', 'c'])
  81. assert args.func == command_jail_untag
  82. assert args.tags == ['a', 'b', 'c']
  83. args = parser.parse_args(['jail', 'prune', '--force'])
  84. assert args.func == command_jail_prune
  85. assert args.force
  86. assert counter[0] == 10
  87. def test_parser_volume(monkeypatch):
  88. parser = create_parser()
  89. counter = [0]
  90. def count_calls(fun, counter):
  91. def inner(*args, **kwargs):
  92. counter[0] += 1
  93. return fun(*args, **kwargs)
  94. return inner
  95. monkeypatch.setattr(parser, 'parse_args', count_calls(parser.parse_args, counter))
  96. args = parser.parse_args(['volume', 'create', '--tags', 'a', 'b', 'c'])
  97. assert args.func == command_volume_create
  98. assert args.tags == ['a', 'b', 'c']
  99. args = parser.parse_args(['volume', 'prune'])
  100. assert args.func == command_volume_prune
  101. args = parser.parse_args(['volume', 'list', '--full-sha256'])
  102. assert args.func == command_volume_list
  103. assert args.full_sha256
  104. args = parser.parse_args(['volume', 'tag', 'deadbee', 'a', 'b', 'c'])
  105. assert args.func == command_volume_tag
  106. assert args.reference == 'deadbee'
  107. assert args.tags == ['a', 'b', 'c']
  108. args = parser.parse_args(['volume', 'untag', 'a', 'b', 'c'])
  109. assert args.func == command_volume_untag
  110. assert args.tags == ['a', 'b', 'c']
  111. args = parser.parse_args(['volume', 'remove', 'volume-A', 'volume-B', '--force'])
  112. assert args.func == command_volume_remove
  113. assert args.references == ['volume-A', 'volume-B']
  114. assert args.force
  115. args = parser.parse_args(['volume', 'set', 'volume-A', 'rdonly=on', 'quota=1G'])
  116. assert args.func == command_volume_set
  117. assert args.reference == 'volume-A'
  118. assert args.properties == ['rdonly=on', 'quota=1G']
  119. args = parser.parse_args(['volume', 'get', 'volume-A', 'rdonly', 'quota'])
  120. assert args.func == command_volume_get
  121. assert args.reference == 'volume-A'
  122. assert args.properties == ['rdonly', 'quota']
  123. assert counter[0] == 8
  124. def test_parser_compose():
  125. parser = create_parser()
  126. args = parser.parse_args(['compose', 'build', '--squeeze', '/a/b/c/focker-compose.yml'])
  127. assert args.func == command_compose_build
  128. assert args.filename == '/a/b/c/focker-compose.yml'
  129. assert args.squeeze
  130. args = parser.parse_args(['compose', 'run', '/a/b/c/focker-compose.yml', 'noop'])
  131. assert args.func == command_compose_run
  132. assert args.filename == '/a/b/c/focker-compose.yml'
  133. assert args.command == 'noop'
  134. def test_focker_main_01(monkeypatch):
  135. log = defaultdict(list)
  136. def log_call(fun):
  137. def inner(*args, **kwargs):
  138. log[fun.__name__].append((args, kwargs))
  139. return fun(*args, **kwargs)
  140. return inner
  141. monkeypatch.setattr(focker.focker, 'focker_lock', log_call(focker_lock))
  142. monkeypatch.setattr(focker.focker, 'zfs_init', log_call(zfs_init))
  143. monkeypatch.setattr(focker.focker, 'create_parser', log_call(create_parser))
  144. monkeypatch.setattr(focker.focker, 'command_image_list', log_call(command_image_list))
  145. monkeypatch.setattr(sys, 'argv', ['focker', 'image', 'list'])
  146. main()
  147. assert len(log) == 4
  148. assert 'focker_lock' in log
  149. assert 'zfs_init' in log
  150. assert 'create_parser' in log
  151. assert 'command_image_list' in log
  152. def test_focker_main_02(monkeypatch):
  153. log = defaultdict(list)
  154. def log_call(fun):
  155. def inner(*args, **kwargs):
  156. log[fun.__name__].append((args, kwargs))
  157. return fun(*args, **kwargs)
  158. return inner
  159. monkeypatch.setattr(sys, 'argv', ['focker'])
  160. monkeypatch.setattr(sys, 'exit', log_call(sys.exit))
  161. with pytest.raises(SystemExit):
  162. main()
  163. assert len(log) == 1
  164. assert 'exit' in log