test_init.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import init as module
  4. from ..test_verbosity import insert_logging_mock
  5. INFO_REPOSITORY_EXISTS_RESPONSE_CODE = 0
  6. INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE = 2
  7. INIT_COMMAND = ('borg', 'init', 'repo', '--encryption', 'repokey')
  8. def insert_info_command_mock(info_response):
  9. subprocess = flexmock(module.subprocess)
  10. subprocess.should_receive('call').and_return(info_response)
  11. def insert_init_command_mock(init_command, **kwargs):
  12. subprocess = flexmock(module.subprocess)
  13. subprocess.should_receive('check_call').with_args(init_command, **kwargs).once()
  14. def test_initialize_repository_calls_borg_with_parameters():
  15. insert_info_command_mock(INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE)
  16. insert_init_command_mock(INIT_COMMAND)
  17. module.initialize_repository(repository='repo', encryption_mode='repokey')
  18. def test_initialize_repository_skips_initialization_when_repository_already_exists():
  19. insert_info_command_mock(INFO_REPOSITORY_EXISTS_RESPONSE_CODE)
  20. flexmock(module.subprocess).should_receive('check_call').never()
  21. module.initialize_repository(repository='repo', encryption_mode='repokey')
  22. def test_initialize_repository_with_append_only_calls_borg_with_append_only_parameter():
  23. insert_info_command_mock(INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE)
  24. insert_init_command_mock(INIT_COMMAND + ('--append-only',))
  25. module.initialize_repository(repository='repo', encryption_mode='repokey', append_only=True)
  26. def test_initialize_repository_with_storage_quota_calls_borg_with_storage_quota_parameter():
  27. insert_info_command_mock(INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE)
  28. insert_init_command_mock(INIT_COMMAND + ('--storage-quota', '5G'))
  29. module.initialize_repository(repository='repo', encryption_mode='repokey', storage_quota='5G')
  30. def test_initialize_repository_with_log_info_calls_borg_with_info_parameter():
  31. insert_info_command_mock(INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE)
  32. insert_init_command_mock(INIT_COMMAND + ('--info',))
  33. insert_logging_mock(logging.INFO)
  34. module.initialize_repository(repository='repo', encryption_mode='repokey')
  35. def test_initialize_repository_with_log_debug_calls_borg_with_debug_parameter():
  36. insert_info_command_mock(INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE)
  37. insert_init_command_mock(INIT_COMMAND + ('--debug',))
  38. insert_logging_mock(logging.DEBUG)
  39. module.initialize_repository(repository='repo', encryption_mode='repokey')
  40. def test_initialize_repository_with_local_path_calls_borg_via_local_path():
  41. insert_info_command_mock(INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE)
  42. insert_init_command_mock(('borg1',) + INIT_COMMAND[1:])
  43. module.initialize_repository(repository='repo', encryption_mode='repokey', local_path='borg1')
  44. def test_initialize_repository_with_remote_path_calls_borg_with_remote_path_parameter():
  45. insert_info_command_mock(INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE)
  46. insert_init_command_mock(INIT_COMMAND + ('--remote-path', 'borg1'))
  47. module.initialize_repository(repository='repo', encryption_mode='repokey', remote_path='borg1')