test_config.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from subprocess import CalledProcessError
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks.data_source import config
  5. def test_get_database_option():
  6. data_source = {'option': 'original_value', 'restore_option': 'restore_value'}
  7. connection_params = {'option': 'connection_value'}
  8. assert config.resolve_database_option('option', data_source) == 'original_value'
  9. assert config.resolve_database_option('option', data_source, restore=True) == 'restore_value'
  10. assert (
  11. config.resolve_database_option('option', data_source, connection_params)
  12. == 'connection_value'
  13. )
  14. assert (
  15. config.resolve_database_option('option', data_source, connection_params, restore=True)
  16. == 'connection_value'
  17. )
  18. def test_get_hostname_option_via_container():
  19. data_source = {
  20. 'container': 'original_container',
  21. 'hostname': 'original_hostname',
  22. 'restore_container': 'restore_container',
  23. 'restore_hostname': 'restore_hostname',
  24. }
  25. connection_params = {'container': 'connection_container', 'hostname': 'connection_hostname'}
  26. flexmock(config).should_receive('get_ip_from_container').with_args(
  27. 'original_container'
  28. ).and_return('container_ip_1')
  29. flexmock(config).should_receive('get_ip_from_container').with_args(
  30. 'connection_container'
  31. ).and_return('container_ip_2')
  32. flexmock(config).should_receive('get_ip_from_container').with_args(
  33. 'restore_container'
  34. ).and_return('container_ip_3')
  35. assert config.resolve_database_option('hostname', data_source) == 'container_ip_1'
  36. assert (
  37. config.resolve_database_option('hostname', data_source, connection_params)
  38. == 'container_ip_2'
  39. )
  40. assert config.resolve_database_option('hostname', data_source, restore=True) == 'container_ip_3'
  41. def test_get_container_ip_without_engines():
  42. flexmock(config.shutil).should_receive('which').and_return(None).and_return(None)
  43. with pytest.raises(ValueError):
  44. config.get_ip_from_container('yolo')
  45. def test_get_container_ip_success():
  46. flexmock(config.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')
  47. flexmock(config).should_receive('execute_command_and_capture_output').and_return(
  48. '{"IPAddress": "1.2.3.4"}'
  49. )
  50. addr = config.get_ip_from_container('yolo')
  51. assert addr == '1.2.3.4'
  52. flexmock(config).should_receive('execute_command_and_capture_output').and_return(
  53. '{"Networks": {"my_network": {"IPAddress": "5.6.7.8"}}}'
  54. )
  55. assert config.get_ip_from_container('yolo') == '5.6.7.8'
  56. def test_get_container_ip_container_not_found():
  57. flexmock(config.shutil).should_receive('which').and_return('/usr/bin/podman')
  58. flexmock(config).should_receive('execute_command_and_capture_output').and_raise(
  59. CalledProcessError, 1, ['/usr/bin/podman', 'inspect', 'yolo'], None, 'No such object'
  60. )
  61. with pytest.raises(CalledProcessError):
  62. config.get_ip_from_container('does not exist')
  63. def test_get_container_ip_container_no_network():
  64. flexmock(config.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')
  65. flexmock(config).should_receive('execute_command_and_capture_output').and_return('{}')
  66. with pytest.raises(ValueError) as exc_info:
  67. config.get_ip_from_container('yolo')
  68. assert 'Could not determine ip address for container' in str(exc_info.value)