Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions geonode/upload/handlers/common/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def delete_resource(instance):

@staticmethod
def perform_last_step(execution_id):
BaseHandler.perform_last_step(execution_id=execution_id)
_exec = BaseHandler.perform_last_step(execution_id=execution_id)
BaseHandler.remove_temporary_file(_exec)

def extract_resource_to_publish(self, files, action, layer_name, alternate, **kwargs):
if action == exa.COPY.value:
Expand Down Expand Up @@ -564,9 +565,12 @@ def copy_original_file(dataset):
def _import_resource_rollback(self, exec_id, istance_name=None, *args, **kwargs):
"""
In the raster, this step just generate the alternate, no real action
are done on the database
are done on the database. We only remove the uploaded temporary files.
"""
pass
try:
BaseHandler.remove_temporary_file(orchestrator.get_execution_object(exec_id=exec_id))
except Exception as e:
logger.warning(f"Failed to remove temporary files during rollback: {e}")

def _publish_resource_rollback(self, exec_id, instance_name=None, *args, **kwargs):
"""
Expand Down
61 changes: 60 additions & 1 deletion geonode/upload/handlers/common/tests_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
import os
import shutil
import tempfile
from unittest.mock import MagicMock
from django.test import TestCase
from django.test import TestCase, override_settings
from mock import patch
from geonode.upload.handlers.common.raster import BaseRasterFileHandler
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -173,3 +176,59 @@ def test_overwrite_geoserver_resource_success_calls_all_steps(self):
mock_delete_store.assert_called_once_with(resource, mock_catalog, mock_workspace)
mock_publish.assert_called_once_with([resource], mock_catalog, mock_store, mock_workspace)
self.assertTrue(result)

def _create_staging_folder(self, base_dir=None):
"""Create a fake upload staging folder with a raster inside"""
folder = tempfile.mkdtemp(dir=base_dir)
base_file = os.path.join(folder, "sample.tif")
shutil.copy(self.valid_raster, base_file)
return folder, base_file

def _create_exec(self, base_file):
return orchestrator.create_execution_request(
user=self.user,
func_name="funct1",
step="step",
input_params={"files": {"base_file": base_file}, "temporary_files": {"base_file": base_file}},
)

def test_perform_last_step_removes_temporary_files(self):
"""
After a successful upload the staging folder must be removed
"""
folder, base_file = self._create_staging_folder()
exec_id = self._create_exec(base_file)
try:
self.handler.perform_last_step(str(exec_id))
self.assertFalse(os.path.exists(folder))
finally:
shutil.rmtree(folder, ignore_errors=True)
ExecutionRequest.objects.filter(exec_id=exec_id).delete()

def test_perform_last_step_keeps_files_in_assets_root(self):
"""
Files already stored in the ASSETS_ROOT must never be removed
"""
assets_root = tempfile.mkdtemp()
_, base_file = self._create_staging_folder(base_dir=assets_root)
exec_id = self._create_exec(base_file)
try:
with override_settings(ASSETS_ROOT=assets_root):
self.handler.perform_last_step(str(exec_id))
self.assertTrue(os.path.exists(base_file))
finally:
shutil.rmtree(assets_root, ignore_errors=True)
ExecutionRequest.objects.filter(exec_id=exec_id).delete()

def test_import_resource_rollback_removes_temporary_files(self):
"""
If the upload fails, the rollback must remove the staging folder
"""
folder, base_file = self._create_staging_folder()
exec_id = self._create_exec(base_file)
try:
self.handler._import_resource_rollback(str(exec_id))
self.assertFalse(os.path.exists(folder))
finally:
shutil.rmtree(folder, ignore_errors=True)
ExecutionRequest.objects.filter(exec_id=exec_id).delete()
Loading