Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Panaetius committed Jun 20, 2022
1 parent 20c4e32 commit 7a3c84e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
10 changes: 5 additions & 5 deletions renku/ui/service/controllers/cache_files_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ def process_chunked_upload(self):
if not completed:
return {}

file_path = user_cache_dir / self.file.filename
target_file_path = user_cache_dir / self.file.filename

if file_path.exists():
if target_file_path.exists():
if self.response_builder.get("override_existing", False):
file_path.unlink()
target_file_path.unlink()
else:
raise IntermittentFileExistsError(file_name=self.file.filename)

with open(file_path, "wb") as f:
with open(target_file_path, "wb") as f:
for file_number in range(total_chunks):
f.write((chunks_dir / str(file_number)).read_bytes())
shutil.rmtree(chunks_dir)
self.cache.invalidate_chunks(self.user, chunked_id)

self.response_builder["is_archive"] = self.response_builder.get("chunked_content_type") in SUPPORTED_ARCHIVES

return self.postprocess_file(file_path, user_cache_dir)
return self.postprocess_file(target_file_path, user_cache_dir)

def process_file(self):
"""Process uploaded file."""
Expand Down
2 changes: 1 addition & 1 deletion renku/ui/service/jobs/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def cache_files_cleanup():
chunk_folders.add(chunk.abs_path.parent)

for chunk_folder in chunk_folders:
shutil.rmtree(chunk_folder)
shutil.rmtree(chunk_folder, ignore_errors=True)


def cache_project_cleanup():
Expand Down
2 changes: 1 addition & 1 deletion start-telepresence.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ then
mkdir temp/service_cache
fi

POD_NAME="${DEV_NAMESPACE}-renku-core-${CORE_VERSION}"
POD_NAME="${DEV_NAMESPACE}-core-${CORE_VERSION}"
echo -e ""
echo -e "Context: ${COLOR_RED}${CURRENT_CONTEXT}${COLOR_RESET}, target: ${COLOR_RED}${POD_NAME}${COLOR_RESET}"
echo "Starting telepresence..."
Expand Down
15 changes: 15 additions & 0 deletions tests/service/jobs/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,29 @@ def test_cleanup_files_old_keys(svc_client_with_user, service_job, tmp_path):
}
cache.set_file(user, file_upload)

chunk_id = uuid.uuid4().hex
chunk_folder = tmp_path / chunk_id
chunk_folder.mkdir()
chunk_data = chunk_folder / "0"
chunk_data.write_text("abcdefg")

chunk = {
"chunked_id": chunk_id,
"file_name": "0",
"relative_path": "0",
}
cache.set_file_chunk(user, chunk)

response = svc_client.get("/cache.files_list", headers=headers)
assert_rpc_response(response)
assert 1 == len(response.json["result"]["files"])
assert 1 == len(list(cache.get_chunks(user, chunk_id)))

cache_files_cleanup()
response = svc_client.get("/cache.files_list", headers=headers)
assert_rpc_response(response)
assert 0 == len(response.json["result"]["files"])
assert 0 == len(list(cache.get_chunks(user, chunk_id)))


@pytest.mark.service
Expand Down
4 changes: 4 additions & 0 deletions tests/service/views/test_cache_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
UserProjectTemplateReferenceError,
UserRepoUrlInvalidError,
)
from renku.ui.service.jobs.cleanup import cache_files_cleanup
from renku.ui.service.serializers.headers import JWT_TOKEN_SECRET
from tests.utils import assert_rpc_response, retry_failed

Expand Down Expand Up @@ -144,6 +145,9 @@ def test_file_chunked_upload(svc_client, identity_headers, svc_cache_dir):
assert {"result"} == set(response.json.keys())
assert "files" not in response.json["result"]

# NOTE: force cleanup to ensure that chunks aren't prematurely cleaned up
cache_files_cleanup()

response = svc_client.post(
"/cache.files_upload",
data=dict(
Expand Down

0 comments on commit 7a3c84e

Please sign in to comment.