def test_get_annotation(self, mock_cloud_interface): """ Tests getting the value of a single annotation. Because cloud storage only cares about keys, and because we mock the cloud interface to return a specific response for a specific key, we do not have separate tests here for getting one of multiple annotations. """ mock_cloud_interface.path = None annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server") mock_cloud_interface.list_bucket.return_value = iter( [ "test_server/base/%s/annotations/test_annotation" % test_backup_id, ] ) mock_cloud_interface.remote_open.return_value = io.BytesIO( "annotation_value".encode("utf-8") ) assert ( annotation_manager.get_annotation(test_backup_id, "test_annotation") == "annotation_value" ) mock_cloud_interface.remote_open.assert_called_once_with( "test_server/base/%s/annotations/test_annotation" % (test_backup_id) )
def test_get_missing_annotation_bypass_cache_optimisation( self, mock_cloud_interface): """ Tests that we bypass the cache optimisation for missing annotations by default. """ mock_cloud_interface.path = None annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server") # Deliberately try to fetch an annotation which isn't there without using the # cache optimization annotation_manager.get_annotation(test_backup_id, "test_annotation_2", use_cache=False) mock_cloud_interface.remote_open.assert_called_once_with( "test_server/base/%s/annotations/test_annotation_2" % test_backup_id) # The AnnotationManager did not have to list the bucket to populate the cache mock_cloud_interface.list_bucket.assert_not_called()
def test_get_missing_annotation(self, mock_cloud_interface): """ Getting a missing annotation returns None. Because cloud storage only cares about keys and has no hard notion of directories we do not need a separate test case for missing backups. """ annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server") mock_cloud_interface.remote_open.return_value = None assert (annotation_manager.get_annotation(test_backup_id, "test_annotation") is None)
def test_get_missing_annotation_cache_optimisation(self, mock_cloud_interface): """ Tests that we avoid remote_open calls for missing annotations. """ mock_cloud_interface.path = None annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server") mock_cloud_interface.list_bucket.return_value = iter([ "test_server/base/%s/annotations/test_annotation" % test_backup_id, ]) # Deliberately try to fetch an annotation which isn't there assert (annotation_manager.get_annotation(test_backup_id, "test_annotation_2") is None) # The AnnotationManager did not have to open the annotation to determine it # was missing mock_cloud_interface.remote_open.assert_not_called()