Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
fix(repos): avoid using temp file context manager for openapi definit…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
aexvir committed Mar 29, 2021
1 parent 74937d9 commit dc46c73
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
37 changes: 36 additions & 1 deletion test/repos/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import tempfile
from pathlib import Path

import fakeredis
import pytest
from gitlab import GitlabGetError, GitlabListError

import zoo.repos.utils as uut
from zoo.repos.exceptions import MissingFilesError, RepositoryNotFoundError
Expand Down Expand Up @@ -89,3 +92,35 @@ def test_download_repository__custom_sha(fake_dir, repo_archive, repository, moc

m_get_project.assert_called_once_with(repository.remote_id)
m_download_archive.assert_called_once_with(mocker.sentinel.project, mocker.ANY, sha)


@pytest.mark.parametrize("repository__provider", [item.value for item in Provider])
def test_openapi_definition(fake_path, repo_archive, repository, mocker):
scm_module = get_scm_module(repository.provider)
m_get_project = mocker.patch.object(
scm_module, "get_project", return_value=mocker.sentinel.project
)
m_download_archive = mocker.patch.object(
scm_module, "download_archive", return_value=repo_archive
)

fake_path.mkdir(parents=True, exist_ok=True)
mocker.patch("tempfile.mkdtemp", return_value=str(fake_path))
mocker.patch("zoo.repos.utils.OPENAPI_SCAN_EXCLUDE", [])

def redis(**kwargs):
return fakeredis.FakeStrictRedis(**kwargs)

mocker.patch("zoo.base.redis.get_connection", redis)

specs = uut.openapi_definition(repository)

m_get_project.assert_called_once_with(repository.remote_id)
m_download_archive.assert_called_once_with(
mocker.sentinel.project, mocker.ANY, None
)

assert len(specs) == 1 # spec is readable
assert specs[0]["info"]["title"] == "Petstore"
assert specs[0]["info"]["version"] == "1.0.0"
assert not fake_path.exists() # cleaned up
38 changes: 23 additions & 15 deletions zoo/repos/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import hashlib
import importlib
import json
import shutil
import tarfile
import tempfile
from pathlib import Path

import structlog
from django.core.serializers.json import DjangoJSONEncoder
from django.http import JsonResponse
from prance import ResolvingParser, ValidationError
from prance.util.formats import ParseError
from prance.util.url import ResolutionError
Expand Down Expand Up @@ -83,23 +83,28 @@ def _parse_file(path, base=None):

def openapi_definition(repository, request=None, repo_path=None):
redis_conn = redis.get_connection(decode_responses=True)
tmp_dir = None
specs = []

if repo_path is None:
log.debug("repos.utils.openapi.download", repo=repository)
with tempfile.TemporaryDirectory() as repo_dir:
try:
repo_path = download_repository(repository, repo_dir)
log.debug(
"repos.utils.openapi.downloaded",
repo=repository,
repo_path=repo_path,
)
except (MissingFilesError, RepositoryNotFoundError) as err:
log.info("repos.utils.openapi.git_error", repo=repository, error=err)
return JsonResponse(
{"error": "error downloading repository"}, status=500
)
try:
log.debug("repos.utils.openapi.download", repo=repository)
tmp_dir = tempfile.mkdtemp()
repo_path = download_repository(repository, tmp_dir)
log.debug(
"repos.utils.openapi.downloaded",
repo=repository,
repo_path=repo_path,
)
except (
FileExistsError,
PermissionError,
MissingFilesError,
RepositoryNotFoundError,
) as err:
log.info("repos.utils.openapi.download_error", repo=repository, error=err)
return []

log.info("repos.utils.openapi.storage", repo_path=repo_path)

for ext in ("json", "yml", "yaml"):
Expand Down Expand Up @@ -147,5 +152,8 @@ def openapi_definition(repository, request=None, repo_path=None):
fingerprint=fingerprint,
)

if tmp_dir is not None:
shutil.rmtree(tmp_dir)

log.info("repos.utils.openapi.done", repo=repository, specs=len(specs))
return list(filter(None, specs))

0 comments on commit dc46c73

Please sign in to comment.