test_environment.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from flexmock import flexmock
  2. from borgmatic.borg import environment as module
  3. def test_make_environment_with_passcommand_should_call_it_and_set_passphrase_file_descriptor_in_environment():
  4. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return('passphrase')
  5. flexmock(module.os).should_receive('pipe').and_return((3, 4))
  6. flexmock(module.os).should_receive('write')
  7. flexmock(module.os).should_receive('close')
  8. flexmock(module.os).should_receive('set_inheritable')
  9. environment = module.make_environment({'encryption_passcommand': 'command'})
  10. assert not environment.get('BORG_PASSCOMMAND')
  11. assert environment.get('BORG_PASSPHRASE_FD') == '3'
  12. def test_make_environment_with_passphrase_should_set_environment():
  13. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  14. flexmock(module.os).should_receive('pipe').never()
  15. flexmock(module.os.environ).should_receive('get').and_return(None)
  16. environment = module.make_environment({'encryption_passphrase': 'pass'})
  17. assert environment.get('BORG_PASSPHRASE') == 'pass'
  18. def test_make_environment_with_ssh_command_should_set_environment():
  19. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  20. flexmock(module.os).should_receive('pipe').never()
  21. flexmock(module.os.environ).should_receive('get').and_return(None)
  22. environment = module.make_environment({'ssh_command': 'ssh -C'})
  23. assert environment.get('BORG_RSH') == 'ssh -C'
  24. def test_make_environment_without_configuration_sets_certain_environment_variables():
  25. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  26. flexmock(module.os).should_receive('pipe').never()
  27. flexmock(module.os.environ).should_receive('get').and_return(None)
  28. environment = module.make_environment({})
  29. # Default environment variables.
  30. assert environment == {
  31. 'BORG_EXIT_CODES': 'modern',
  32. 'BORG_RELOCATED_REPO_ACCESS_IS_OK': 'no',
  33. 'BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK': 'no',
  34. }
  35. def test_make_environment_without_configuration_does_not_set_certain_environment_variables_if_already_set():
  36. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  37. flexmock(module.os).should_receive('pipe').never()
  38. flexmock(module.os.environ).should_receive('get').with_args(
  39. 'BORG_RELOCATED_REPO_ACCESS_IS_OK'
  40. ).and_return('yup')
  41. flexmock(module.os.environ).should_receive('get').with_args(
  42. 'BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK'
  43. ).and_return('nah')
  44. environment = module.make_environment({})
  45. assert environment == {'BORG_EXIT_CODES': 'modern'}
  46. def test_make_environment_with_relocated_repo_access_true_should_set_environment_yes():
  47. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  48. flexmock(module.os).should_receive('pipe').never()
  49. flexmock(module.os.environ).should_receive('get').and_return(None)
  50. environment = module.make_environment({'relocated_repo_access_is_ok': True})
  51. assert environment.get('BORG_RELOCATED_REPO_ACCESS_IS_OK') == 'yes'
  52. def test_make_environment_with_relocated_repo_access_false_should_set_environment_no():
  53. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  54. flexmock(module.os).should_receive('pipe').never()
  55. flexmock(module.os.environ).should_receive('get').and_return(None)
  56. environment = module.make_environment({'relocated_repo_access_is_ok': False})
  57. assert environment.get('BORG_RELOCATED_REPO_ACCESS_IS_OK') == 'no'
  58. def test_make_environment_check_i_know_what_i_am_doing_true_should_set_environment_YES():
  59. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  60. flexmock(module.os).should_receive('pipe').never()
  61. flexmock(module.os.environ).should_receive('get').and_return(None)
  62. environment = module.make_environment({'check_i_know_what_i_am_doing': True})
  63. assert environment.get('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING') == 'YES'
  64. def test_make_environment_check_i_know_what_i_am_doing_false_should_set_environment_NO():
  65. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  66. flexmock(module.os).should_receive('pipe').never()
  67. flexmock(module.os.environ).should_receive('get').and_return(None)
  68. environment = module.make_environment({'check_i_know_what_i_am_doing': False})
  69. assert environment.get('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING') == 'NO'
  70. def test_make_environment_with_integer_variable_value():
  71. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(None)
  72. flexmock(module.os).should_receive('pipe').never()
  73. flexmock(module.os.environ).should_receive('get').and_return(None)
  74. environment = module.make_environment({'borg_files_cache_ttl': 40})
  75. assert environment.get('BORG_FILES_CACHE_TTL') == '40'