11import hashlib
22import os
3- from unittest .mock import Mock , patch
3+ from unittest .mock import Mock
44
55import pytest
66
@@ -43,9 +43,7 @@ def requests_head_call_return(*args, **kwargs): # pylint: disable=unused-argume
4343 return HTTPResult (status_code )
4444
4545
46- @patch ("requests.head" )
47- @patch ("requests.put" )
48- def test_check_wal_archive_integrity (requests_put_mock , requests_head_mock , tmpdir ):
46+ def test_check_wal_archive_integrity (tmpdir ):
4947 from pghoard .archive_sync import ArchiveSync , SyncError
5048
5149 # Instantiate a fake PG data directory
@@ -57,59 +55,57 @@ def test_check_wal_archive_integrity(requests_put_mock, requests_head_mock, tmpd
5755 write_json_file (config_file , {"http_port" : 8080 , "backup_sites" : {"foo" : {"pg_data_directory" : pg_data_directory }}})
5856 arsy = ArchiveSync ()
5957 arsy .set_config (config_file , site = "foo" )
60- requests_put_mock . return_value = HTTPResult (201 ) # So the backup requests succeeds
61- requests_head_mock . side_effect = requests_head_call_return
58+ arsy . session . put = Mock ( return_value = HTTPResult (201 ) ) # So the backup requests succeeds
59+ arsy . session . head = Mock ( side_effect = requests_head_call_return )
6260
6361 # Check integrity within same timeline
6462 arsy .get_current_wal_file = Mock (return_value = "00000005000000000000008F" )
6563 arsy .get_first_required_wal_segment = Mock (return_value = ("00000005000000000000008C" , 90300 ))
6664 assert arsy .check_wal_archive_integrity (new_backup_on_failure = False ) == 0
67- assert requests_head_mock .call_count == 3
68- assert requests_put_mock .call_count == 0
65+ assert arsy . session . head .call_count == 3
66+ assert arsy . session . put .call_count == 0
6967
7068 # Check integrity when timeline has changed
71- requests_head_mock .call_count = 0
72- requests_put_mock .call_count = 0
69+ arsy . session . head .call_count = 0
70+ arsy . session . put .call_count = 0
7371 arsy .get_current_wal_file = Mock (return_value = "000000090000000000000008" )
7472 arsy .get_first_required_wal_segment = Mock (return_value = ("000000080000000000000005" , 90300 ))
7573 assert arsy .check_wal_archive_integrity (new_backup_on_failure = False ) == 0
76- assert requests_head_mock .call_count == 4
74+ assert arsy . session . head .call_count == 4
7775
78- requests_head_mock .call_count = 0
79- requests_put_mock .call_count = 0
76+ arsy . session . head .call_count = 0
77+ arsy . session . put .call_count = 0
8078 arsy .get_current_wal_file = Mock (return_value = "000000030000000000000008" )
8179 arsy .get_first_required_wal_segment = Mock (return_value = ("000000030000000000000005" , 90300 ))
8280 with pytest .raises (SyncError ):
8381 arsy .check_wal_archive_integrity (new_backup_on_failure = False )
84- assert requests_put_mock .call_count == 0
82+ assert arsy . session . put .call_count == 0
8583 assert arsy .check_wal_archive_integrity (new_backup_on_failure = True ) == 0
86- assert requests_put_mock .call_count == 1
84+ assert arsy . session . put .call_count == 1
8785
88- requests_head_mock .call_count = 0
89- requests_put_mock .call_count = 0
86+ arsy . session . head .call_count = 0
87+ arsy . session . put .call_count = 0
9088 arsy .get_current_wal_file = Mock (return_value = "000000070000000000000002" )
9189 arsy .get_first_required_wal_segment = Mock (return_value = ("000000060000000000000001" , 90300 ))
9290 assert arsy .check_wal_archive_integrity (new_backup_on_failure = False ) == 0
93- assert requests_put_mock .call_count == 0
91+ assert arsy . session . put .call_count == 0
9492
95- requests_head_mock .call_count = 0
96- requests_put_mock .call_count = 0
93+ arsy . session . head .call_count = 0
94+ arsy . session . put .call_count = 0
9795 arsy .get_current_wal_file = Mock (return_value = "000000020000000B00000000" )
9896 arsy .get_first_required_wal_segment = Mock (return_value = ("000000020000000A000000FD" , 90200 ))
9997 assert arsy .check_wal_archive_integrity (new_backup_on_failure = False ) == 0
100- assert requests_put_mock .call_count == 0
98+ assert arsy . session . put .call_count == 0
10199
102- requests_head_mock .call_count = 0
103- requests_put_mock .call_count = 0
100+ arsy . session . head .call_count = 0
101+ arsy . session . put .call_count = 0
104102 arsy .get_current_wal_file = Mock (return_value = "000000020000000B00000000" )
105103 arsy .get_first_required_wal_segment = Mock (return_value = ("000000020000000A000000FD" , 90300 ))
106104 assert arsy .check_wal_archive_integrity (new_backup_on_failure = True ) == 0
107- assert requests_put_mock .call_count == 1
105+ assert arsy . session . put .call_count == 1
108106
109107
110- @patch ("requests.head" )
111- @patch ("requests.put" )
112- def test_check_and_upload_missing_local_files (requests_put_mock , requests_head_mock , tmpdir ):
108+ def test_check_and_upload_missing_local_files (tmpdir ):
113109 from pghoard .archive_sync import ArchiveSync
114110
115111 data_dir = str (tmpdir )
@@ -157,8 +153,8 @@ def requests_put(*args, **kwargs): # pylint: disable=unused-argument
157153 write_json_file (config_file , {"http_port" : 8080 , "backup_sites" : {"foo" : {"pg_data_directory" : data_dir }}})
158154 arsy = ArchiveSync ()
159155 arsy .set_config (config_file , site = "foo" )
160- requests_put_mock . side_effect = requests_put
161- requests_head_mock . side_effect = requests_head
156+ arsy . session . put = Mock ( side_effect = requests_put )
157+ arsy . session . head = Mock ( side_effect = requests_head )
162158 arsy .get_current_wal_file = Mock (return_value = "00000000000000000000001A" )
163159 arsy .get_first_required_wal_segment = Mock (return_value = ("000000000000000000000001" , 90300 ))
164160
0 commit comments