fake_btrfs.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import argparse
  2. import json
  3. import os
  4. import shutil
  5. import sys
  6. def parse_arguments(*unparsed_arguments):
  7. global_parser = argparse.ArgumentParser(add_help=False)
  8. action_parsers = global_parser.add_subparsers(dest='action')
  9. subvolume_parser = action_parsers.add_parser('subvolume')
  10. subvolume_subparser = subvolume_parser.add_subparsers(dest='subaction')
  11. show_parser = subvolume_subparser.add_parser('show')
  12. show_parser.add_argument('subvolume_path')
  13. snapshot_parser = subvolume_subparser.add_parser('snapshot')
  14. snapshot_parser.add_argument('subvolume_path')
  15. snapshot_parser.add_argument('snapshot_path')
  16. delete_parser = subvolume_subparser.add_parser('delete')
  17. delete_parser.add_argument('snapshot_path')
  18. ensure_deleted_parser = subvolume_subparser.add_parser('ensure_deleted')
  19. ensure_deleted_parser.add_argument('snapshot_path')
  20. property_parser = action_parsers.add_parser('property')
  21. property_subparser = property_parser.add_subparsers(dest='subaction')
  22. get_parser = property_subparser.add_parser('get')
  23. get_parser.add_argument('-t', dest='type')
  24. get_parser.add_argument('subvolume_path')
  25. get_parser.add_argument('property_name')
  26. return (global_parser, global_parser.parse_args(unparsed_arguments))
  27. def load_snapshots():
  28. try:
  29. return json.load(open('/tmp/fake_btrfs.json'))
  30. except FileNotFoundError:
  31. return []
  32. def save_snapshots(snapshot_paths):
  33. json.dump(snapshot_paths, open('/tmp/fake_btrfs.json', 'w'))
  34. def print_subvolume_show(arguments):
  35. assert arguments.subvolume_path == '/e2e/mnt/subvolume'
  36. # borgmatic doesn't currently parse the output of "btrfs subvolume show"—it's just checking the
  37. # exit code—so what we print in response doesn't matter in this test.
  38. print('Totally legit btrfs subvolume!')
  39. def main():
  40. (global_parser, arguments) = parse_arguments(*sys.argv[1:])
  41. snapshot_paths = load_snapshots()
  42. if not hasattr(arguments, 'subaction'):
  43. global_parser.print_help()
  44. sys.exit(1)
  45. if arguments.subaction == 'show':
  46. print_subvolume_show(arguments)
  47. elif arguments.subaction == 'snapshot':
  48. snapshot_paths.append(arguments.snapshot_path)
  49. save_snapshots(snapshot_paths)
  50. subdirectory = os.path.join(arguments.snapshot_path, 'subdir')
  51. os.makedirs(subdirectory, mode=0o700, exist_ok=True)
  52. test_file = open(os.path.join(subdirectory, 'file.txt'), 'w')
  53. test_file.write('contents')
  54. test_file.close()
  55. elif arguments.subaction == 'delete':
  56. subdirectory = os.path.join(arguments.snapshot_path, 'subdir')
  57. shutil.rmtree(subdirectory)
  58. snapshot_paths = [
  59. snapshot_path
  60. for snapshot_path in snapshot_paths
  61. if snapshot_path.endswith('/' + arguments.snapshot_path)
  62. ]
  63. save_snapshots(snapshot_paths)
  64. # Not a real btrfs subcommand.
  65. elif arguments.subaction == 'ensure_deleted':
  66. assert arguments.snapshot_path not in snapshot_paths
  67. elif arguments.action == 'property' and arguments.subaction == 'get':
  68. print(f'{arguments.property_name}=false')
  69. if __name__ == '__main__':
  70. main()