test_environment.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.borg.passcommand).should_receive(
  5. 'get_passphrase_from_passcommand'
  6. ).and_return('passphrase')
  7. flexmock(module.os).should_receive('pipe').and_return((3, 4))
  8. flexmock(module.os).should_receive('write')
  9. flexmock(module.os).should_receive('close')
  10. flexmock(module.os).should_receive('set_inheritable')
  11. environment = module.make_environment({'encryption_passcommand': 'command'})
  12. assert not environment.get('BORG_PASSCOMMAND')
  13. assert environment.get('BORG_PASSPHRASE_FD') == '3'
  14. def test_make_environment_with_passphrase_should_set_environment():
  15. flexmock(module.borgmatic.borg.passcommand).should_receive(
  16. 'get_passphrase_from_passcommand'
  17. ).and_return(None)
  18. flexmock(module.os).should_receive('pipe').never()
  19. flexmock(module.os.environ).should_receive('get').and_return(None)
  20. environment = module.make_environment({'encryption_passphrase': 'pass'})
  21. assert environment.get('BORG_PASSPHRASE') == 'pass'
  22. def test_make_environment_with_ssh_command_should_set_environment():
  23. flexmock(module.borgmatic.borg.passcommand).should_receive(
  24. 'get_passphrase_from_passcommand'
  25. ).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({'ssh_command': 'ssh -C'})
  29. assert environment.get('BORG_RSH') == 'ssh -C'
  30. def test_make_environment_without_configuration_sets_certain_environment_variables():
  31. flexmock(module.borgmatic.borg.passcommand).should_receive(
  32. 'get_passphrase_from_passcommand'
  33. ).and_return(None)
  34. flexmock(module.os).should_receive('pipe').never()
  35. flexmock(module.os.environ).should_receive('get').and_return(None)
  36. environment = module.make_environment({})
  37. # Default environment variables.
  38. assert environment == {
  39. 'BORG_EXIT_CODES': 'modern',
  40. 'BORG_RELOCATED_REPO_ACCESS_IS_OK': 'no',
  41. 'BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK': 'no',
  42. }
  43. def test_make_environment_without_configuration_does_not_set_certain_environment_variables_if_already_set():
  44. flexmock(module.borgmatic.borg.passcommand).should_receive(
  45. 'get_passphrase_from_passcommand'
  46. ).and_return(None)
  47. flexmock(module.os).should_receive('pipe').never()
  48. flexmock(module.os.environ).should_receive('get').with_args(
  49. 'BORG_RELOCATED_REPO_ACCESS_IS_OK'
  50. ).and_return('yup')
  51. flexmock(module.os.environ).should_receive('get').with_args(
  52. 'BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK'
  53. ).and_return('nah')
  54. environment = module.make_environment({})
  55. assert environment == {'BORG_EXIT_CODES': 'modern'}
  56. def test_make_environment_with_relocated_repo_access_true_should_set_environment_yes():
  57. flexmock(module.borgmatic.borg.passcommand).should_receive(
  58. 'get_passphrase_from_passcommand'
  59. ).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({'relocated_repo_access_is_ok': True})
  63. assert environment.get('BORG_RELOCATED_REPO_ACCESS_IS_OK') == 'yes'
  64. def test_make_environment_with_relocated_repo_access_false_should_set_environment_no():
  65. flexmock(module.borgmatic.borg.passcommand).should_receive(
  66. 'get_passphrase_from_passcommand'
  67. ).and_return(None)
  68. flexmock(module.os).should_receive('pipe').never()
  69. flexmock(module.os.environ).should_receive('get').and_return(None)
  70. environment = module.make_environment({'relocated_repo_access_is_ok': False})
  71. assert environment.get('BORG_RELOCATED_REPO_ACCESS_IS_OK') == 'no'
  72. def test_make_environment_check_i_know_what_i_am_doing_true_should_set_environment_YES():
  73. flexmock(module.borgmatic.borg.passcommand).should_receive(
  74. 'get_passphrase_from_passcommand'
  75. ).and_return(None)
  76. flexmock(module.os).should_receive('pipe').never()
  77. flexmock(module.os.environ).should_receive('get').and_return(None)
  78. environment = module.make_environment({'check_i_know_what_i_am_doing': True})
  79. assert environment.get('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING') == 'YES'
  80. def test_make_environment_check_i_know_what_i_am_doing_false_should_set_environment_NO():
  81. flexmock(module.borgmatic.borg.passcommand).should_receive(
  82. 'get_passphrase_from_passcommand'
  83. ).and_return(None)
  84. flexmock(module.os).should_receive('pipe').never()
  85. flexmock(module.os.environ).should_receive('get').and_return(None)
  86. environment = module.make_environment({'check_i_know_what_i_am_doing': False})
  87. assert environment.get('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING') == 'NO'
  88. def test_make_environment_with_integer_variable_value():
  89. flexmock(module.borgmatic.borg.passcommand).should_receive(
  90. 'get_passphrase_from_passcommand'
  91. ).and_return(None)
  92. flexmock(module.os).should_receive('pipe').never()
  93. flexmock(module.os.environ).should_receive('get').and_return(None)
  94. environment = module.make_environment({'borg_files_cache_ttl': 40})
  95. assert environment.get('BORG_FILES_CACHE_TTL') == '40'