fake_btrfs.py 3.2 KB

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