applications: report scan errors (#68017)
This commit is contained in:
parent
8986dd9592
commit
f699c76306
|
@ -37,11 +37,19 @@ from .utils import Requests
|
|||
requests = Requests()
|
||||
|
||||
|
||||
class DeploymentError(Exception):
|
||||
class ApplicationError(Exception):
|
||||
def __init__(self, msg):
|
||||
self.msg = msg
|
||||
|
||||
|
||||
class ScanError(ApplicationError):
|
||||
pass
|
||||
|
||||
|
||||
class DeploymentError(ApplicationError):
|
||||
pass
|
||||
|
||||
|
||||
class Application(models.Model):
|
||||
SUPPORTED_MODULES = ('wcs',)
|
||||
|
||||
|
@ -89,6 +97,13 @@ class Application(models.Model):
|
|||
if not dependencies_url:
|
||||
continue
|
||||
response = requests.get(dependencies_url)
|
||||
if not response.ok:
|
||||
raise ScanError(
|
||||
_(
|
||||
'Failed to scan "%s" (type %s, slug %s) dependencies (%s)'
|
||||
% (el.name, el.type, el.slug, response.status_code)
|
||||
)
|
||||
)
|
||||
for dependency in response.json()['data']:
|
||||
if (dependency['type'], dependency['id']) in elements:
|
||||
continue
|
||||
|
@ -289,7 +304,7 @@ class AsyncJob(models.Model):
|
|||
self.version.create_bundle()
|
||||
elif self.action == 'deploy':
|
||||
self.version.deploy()
|
||||
except DeploymentError as e:
|
||||
except ApplicationError as e:
|
||||
self.status = 'failed'
|
||||
self.exception = e.msg
|
||||
if self.raise_exception:
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
{% if object.status == 'failed' %}
|
||||
<div class="pk-error">
|
||||
<p>{% trans "Error running the job." %}</p>
|
||||
{% if object.exception %}<pre>{{ object.exception }}</pre>{% endif %}
|
||||
<p><a class="pk-button" href="{{ view.get_redirect_url }}">{% trans "Back" %}</a></p>
|
||||
</div>
|
||||
{% else %}
|
||||
|
|
|
@ -10,7 +10,15 @@ from httmock import HTTMock
|
|||
from test_manager import login
|
||||
from webtest import Upload
|
||||
|
||||
from hobo.applications.models import Application, AsyncJob, DeploymentError, Element, Relation, Version
|
||||
from hobo.applications.models import (
|
||||
Application,
|
||||
AsyncJob,
|
||||
DeploymentError,
|
||||
Element,
|
||||
Relation,
|
||||
ScanError,
|
||||
Version,
|
||||
)
|
||||
from hobo.environment.models import Authentic, Wcs
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
@ -333,6 +341,49 @@ def test_create_application(app, admin_user, settings, analyze):
|
|||
app.get('/applications/manifest/test/delete/%s/' % application.relation_set.first().pk, status=404)
|
||||
|
||||
|
||||
def test_scandeps_on_unknown_element(app, admin_user, settings):
|
||||
Wcs.objects.create(base_url='https://wcs.example.invalid', slug='foobar', title='Foobar')
|
||||
|
||||
settings.KNOWN_SERVICES = {
|
||||
'wcs': {
|
||||
'foobar': {
|
||||
'title': 'Foobar',
|
||||
'url': 'https://wcs.example.invalid/',
|
||||
'orig': 'example.org',
|
||||
'secret': 'xxx',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
application = Application.objects.create(name='Test', slug='test')
|
||||
element = Element.objects.create(
|
||||
type='forms',
|
||||
slug='unknown',
|
||||
name='Unknown',
|
||||
cache={
|
||||
'urls': {
|
||||
"dependencies": "https://wcs.example.invalid/api/export-import/forms/unknown/dependencies/",
|
||||
}
|
||||
},
|
||||
)
|
||||
Relation.objects.create(application=application, element=element)
|
||||
|
||||
def response_content(url, request):
|
||||
if url.path == '/api/export-import/forms/unknown/dependencies/':
|
||||
return {'status_code': 404}
|
||||
return mocked_http(url, request)
|
||||
|
||||
login(app)
|
||||
|
||||
with HTTMock(response_content):
|
||||
with pytest.raises(ScanError) as e:
|
||||
app.get('/applications/manifest/test/scandeps/').follow()
|
||||
assert str(e.value) == 'Failed to scan "Unknown" (type forms, slug unknown) dependencies (404)'
|
||||
job = AsyncJob.objects.latest('pk')
|
||||
assert job.status == 'failed'
|
||||
assert job.exception == 'Failed to scan "Unknown" (type forms, slug unknown) dependencies (404)'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('editable', [True, False])
|
||||
def test_redirect_application_element(app, admin_user, settings, editable):
|
||||
Wcs.objects.create(base_url='https://wcs.example.invalid', slug='foobar', title='Foobar')
|
||||
|
|
Loading…
Reference in New Issue