12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
"""Importer tests."""
15
+ import contextlib
15
16
import datetime
16
17
import os
17
18
import shutil
22
23
import threading
23
24
24
25
from unittest import mock
26
+ from urllib3 .exceptions import SystemTimeWarning
25
27
import warnings
26
28
27
29
from google .cloud import ndb
@@ -66,6 +68,7 @@ def setUp(self):
66
68
self .tmp_dir = tempfile .mkdtemp ()
67
69
68
70
tests .mock_datetime (self )
71
+ warnings .filterwarnings ('ignore' , category = SystemTimeWarning )
69
72
self .mock_repo = tests .mock_repository (self )
70
73
71
74
storage_patcher = mock .patch ('google.cloud.storage.Client' )
@@ -407,6 +410,7 @@ def setUp(self):
407
410
self .tmp_dir = tempfile .mkdtemp ()
408
411
409
412
tests .mock_datetime (self )
413
+ warnings .filterwarnings ('ignore' , category = SystemTimeWarning )
410
414
411
415
self .source_repo = osv .SourceRepository (
412
416
type = osv .SourceRepositoryType .BUCKET ,
@@ -821,7 +825,7 @@ def setUp(self):
821
825
self .tmp_dir = tempfile .mkdtemp ()
822
826
823
827
tests .mock_datetime (self )
824
- warnings .filterwarnings (" ignore" , "unclosed" , ResourceWarning )
828
+ warnings .filterwarnings (' ignore' , category = SystemTimeWarning )
825
829
826
830
storage_patcher = mock .patch ('google.cloud.storage.Client' )
827
831
self .addCleanup (storage_patcher .stop )
@@ -841,79 +845,97 @@ def setUp(self):
841
845
842
846
def tearDown (self ):
843
847
shutil .rmtree (self .tmp_dir , ignore_errors = True )
844
- self .httpd .shutdown ()
848
+
849
+ @contextlib .contextmanager
850
+ def server (self , handler_class ):
851
+ """REST mock server context manager."""
852
+ httpd = http .server .HTTPServer (SERVER_ADDRESS , handler_class )
853
+ thread = threading .Thread (target = httpd .serve_forever )
854
+ thread .start ()
855
+ try :
856
+ yield httpd
857
+ finally :
858
+ httpd .shutdown ()
859
+ httpd .server_close ()
860
+ thread .join ()
845
861
846
862
@mock .patch ('google.cloud.pubsub_v1.PublisherClient.publish' )
847
863
@mock .patch ('time.time' , return_value = 12345.0 )
848
864
def test_all_updated (self , unused_mock_time : mock .MagicMock ,
849
865
mock_publish : mock .MagicMock ):
850
866
"""Testing basic rest endpoint import"""
851
867
data_handler = MockDataHandler
868
+ data_handler .last_modified = 'Mon, 01 Jan 2024 00:00:00 GMT'
852
869
data_handler .load_file (data_handler , 'rest_test.json' )
853
- self .httpd = http .server .HTTPServer (SERVER_ADDRESS , data_handler )
854
- thread = threading .Thread (target = self .httpd .serve_forever )
855
- thread .start ()
856
870
self .source_repo .last_update_date = datetime .datetime (2020 , 1 , 1 )
857
- self .source_repo .put ()
871
+ repo = self .source_repo .put ()
858
872
imp = importer .Importer ('fake_public_key' , 'fake_private_key' , self .tmp_dir ,
859
873
importer .DEFAULT_PUBLIC_LOGGING_BUCKET , 'bucket' ,
860
874
False , False )
861
- imp .run ()
875
+ with self .server (data_handler ):
876
+ imp .run ()
862
877
self .assertEqual (mock_publish .call_count , data_handler .cve_count )
878
+ self .assertEqual (
879
+ repo .get ().last_update_date ,
880
+ datetime .datetime (2024 , 1 , 1 ),
881
+ msg = 'Expected last_update_date to equal REST Last-Modified date' )
863
882
864
883
@mock .patch ('google.cloud.pubsub_v1.PublisherClient.publish' )
865
884
@mock .patch ('time.time' , return_value = 12345.0 )
866
885
def test_last_update_ignored (self , unused_mock_time : mock .MagicMock ,
867
886
mock_publish : mock .MagicMock ):
868
887
"""Testing last update ignored"""
869
888
data_handler = MockDataHandler
889
+ data_handler .last_modified = 'Mon, 01 Jan 2024 00:00:00 GMT'
870
890
data_handler .load_file (data_handler , 'rest_test.json' )
871
- self .httpd = http .server .HTTPServer (SERVER_ADDRESS , data_handler )
872
- thread = threading .Thread (target = self .httpd .serve_forever )
873
- thread .start ()
874
891
self .source_repo .last_update_date = datetime .datetime (2023 , 6 , 6 )
875
892
self .source_repo .ignore_last_import_time = True
876
- self .source_repo .put ()
893
+ repo = self .source_repo .put ()
877
894
imp = importer .Importer ('fake_public_key' , 'fake_private_key' , self .tmp_dir ,
878
895
importer .DEFAULT_PUBLIC_LOGGING_BUCKET , 'bucket' ,
879
896
False , False )
880
- imp .run ()
897
+ with self .server (data_handler ):
898
+ imp .run ()
881
899
self .assertEqual (mock_publish .call_count , data_handler .cve_count )
900
+ self .assertEqual (
901
+ repo .get ().last_update_date ,
902
+ datetime .datetime (2024 , 1 , 1 ),
903
+ msg = 'Expected last_update_date to equal REST Last-Modified date' )
882
904
883
905
@mock .patch ('google.cloud.pubsub_v1.PublisherClient.publish' )
884
906
@mock .patch ('time.time' , return_value = 12345.0 )
885
907
def test_no_updates (self , unused_mock_time : mock .MagicMock ,
886
908
mock_publish : mock .MagicMock ):
887
909
"""Testing none last modified"""
888
910
MockDataHandler .last_modified = 'Fri, 01 Jan 2021 00:00:00 GMT'
889
- self .httpd = http .server .HTTPServer (SERVER_ADDRESS , MockDataHandler )
890
- thread = threading .Thread (target = self .httpd .serve_forever )
891
- thread .start ()
892
- self .source_repo .last_update_date = datetime .datetime (2024 , 1 , 1 )
893
- self .source_repo .put ()
911
+ self .source_repo .last_update_date = datetime .datetime (2024 , 2 , 1 )
912
+ repo = self .source_repo .put ()
894
913
imp = importer .Importer ('fake_public_key' , 'fake_private_key' , self .tmp_dir ,
895
914
importer .DEFAULT_PUBLIC_LOGGING_BUCKET , 'bucket' ,
896
915
True , False )
897
- with self .assertLogs () as logs :
916
+ with self .assertLogs () as logs , self . server ( MockDataHandler ) :
898
917
imp .run ()
899
918
mock_publish .assert_not_called ()
900
919
self .assertIn ('INFO:root:No changes since last update.' , logs .output [1 ])
920
+ self .assertEqual (
921
+ repo .get ().last_update_date ,
922
+ datetime .datetime (2024 , 2 , 1 ),
923
+ msg = 'last_update_date should not have been updated' )
901
924
902
925
@mock .patch ('google.cloud.pubsub_v1.PublisherClient.publish' )
903
926
@mock .patch ('time.time' , return_value = 12345.0 )
904
927
def test_few_updates (self , unused_mock_time : mock .MagicMock ,
905
928
mock_publish : mock .MagicMock ):
906
929
"""Testing from date between entries -
907
930
only entries after 6/6/2023 should be called"""
908
- self .httpd = http .server .HTTPServer (SERVER_ADDRESS , MockDataHandler )
909
- thread = threading .Thread (target = self .httpd .serve_forever )
910
- thread .start ()
931
+ MockDataHandler .last_modified = 'Mon, 01 Jan 2024 00:00:00 GMT'
911
932
self .source_repo .last_update_date = datetime .datetime (2023 , 6 , 6 )
912
- self .source_repo .put ()
933
+ repo = self .source_repo .put ()
913
934
imp = importer .Importer ('fake_public_key' , 'fake_private_key' , self .tmp_dir ,
914
935
importer .DEFAULT_PUBLIC_LOGGING_BUCKET , 'bucket' ,
915
936
False , False )
916
- imp .run ()
937
+ with self .server (MockDataHandler ):
938
+ imp .run ()
917
939
mock_publish .assert_has_calls ([
918
940
mock .call (
919
941
self .tasks_topic ,
@@ -976,6 +998,10 @@ def test_few_updates(self, unused_mock_time: mock.MagicMock,
976
998
deleted = 'false' ,
977
999
req_timestamp = '12345' )
978
1000
])
1001
+ self .assertEqual (
1002
+ repo .get ().last_update_date ,
1003
+ datetime .datetime (2024 , 1 , 1 ),
1004
+ msg = 'Expected last_update_date to equal REST Last-Modified date' )
979
1005
980
1006
981
1007
@mock .patch ('importer.utcnow' , lambda : datetime .datetime (2024 , 1 , 1 ))
@@ -986,6 +1012,7 @@ def setUp(self):
986
1012
tests .reset_emulator ()
987
1013
988
1014
tests .mock_datetime (self )
1015
+ warnings .filterwarnings ('ignore' , category = SystemTimeWarning )
989
1016
990
1017
def test_add_finding (self ):
991
1018
"""Test that creating an import finding works."""
0 commit comments