From 3bb0a0c3ca8f911af736aa77ab1f3c9345ac5939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Mon, 20 Feb 2023 14:23:23 +0100 Subject: [PATCH] applications: declare an app to services on version generation (#74659) --- hobo/applications/models.py | 25 ++++++++++++++++++------- tests/test_application.py | 30 ++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/hobo/applications/models.py b/hobo/applications/models.py index 53b4895..2007071 100644 --- a/hobo/applications/models.py +++ b/hobo/applications/models.py @@ -241,7 +241,7 @@ class Version(models.Model): def __repr__(self): return '' % self.application.slug - def create_bundle(self): + def create_bundle(self, job=None): app = self.application elements = app.scandeps() tar_io = io.BytesIO() @@ -289,9 +289,23 @@ class Version(models.Model): self.bundle.save('%s.tar' % app.slug, content=ContentFile(tar_io.getvalue())) self.save() + bundle_content = self.bundle.read() + self.do_something_with_bundle(bundle_content, 'declare', job=job) + def deploy(self, job=None): bundle_content = self.bundle.read() self.deploy_roles(bundle_content) + self.do_something_with_bundle(bundle_content, 'deploy', job=job) + self.application.refresh_elements(cache_only=True) + + def do_something_with_bundle(self, bundle_content, action, job=None): + if action == 'deploy': + target_url = 'api/export-import/bundle-import/' + exception_message = _('Failed to deploy module %s (%s)') + elif action == 'declare': + target_url = 'api/export-import/bundle-declare/' + exception_message = _('Failed to declare elements for module %s (%s)') + for service_id, services in getattr(settings, 'KNOWN_SERVICES', {}).items(): if service_id not in Application.SUPPORTED_MODULES: continue @@ -301,12 +315,10 @@ class Version(models.Model): continue if service_objects[service['url']].secondary: continue - url = urllib.parse.urljoin(service['url'], 'api/export-import/bundle-import/') + url = urllib.parse.urljoin(service['url'], target_url) response = requests.put(url, data=bundle_content) if not response.ok: - raise DeploymentError( - _('Failed to deploy module %s (%s)') % (service_id, response.status_code) - ) + raise DeploymentError(exception_message % (service_id, response.status_code)) if not job: continue try: @@ -319,7 +331,6 @@ class Version(models.Model): job.progression_urls[service_id] = {} job.progression_urls[service_id][service['title']] = response_json['url'] job.save() - self.application.refresh_elements(cache_only=True) def get_authentic_service(self): for service_id, services in getattr(settings, 'KNOWN_SERVICES', {}).items(): @@ -396,7 +407,7 @@ class AsyncJob(models.Model): if self.action == 'scandeps': self.application.scandeps() elif self.action == 'create_bundle': - self.version.create_bundle() + self.version.create_bundle(self) elif self.action == 'deploy': self.version.deploy(self) except ApplicationError as e: diff --git a/tests/test_application.py b/tests/test_application.py index 43366e6..1267795 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -386,14 +386,28 @@ def test_create_application(app, admin_user, settings, analyze): assert versions.count('2.0') == 1 assert resp.text.count('Creating application bundle') == 4 - # non editable app - application.editable = False - application.save() - app.get('/applications/manifest/test/metadata/', status=404) - app.get('/applications/manifest/test/scandeps/', status=404) - app.get('/applications/manifest/test/generate/', status=404) - app.get('/applications/manifest/test/add/forms/', status=404) - app.get('/applications/manifest/test/delete/%s/' % application.relation_set.first().pk, status=404) + def response_content(url, request): + if url.path == '/api/export-import/bundle-declare/': + return {'status_code': 500} + return mocked_http(url, request) + + resp = app.get('/applications/manifest/test/generate/') + with HTTMock(response_content): + with pytest.raises(DeploymentError) as e: + resp.form.submit() + assert str(e.value) == 'Failed to declare elements for module wcs (500)' + job = AsyncJob.objects.latest('pk') + assert job.status == 'failed' + assert job.exception == 'Failed to declare elements for module wcs (500)' + + # non editable app + application.editable = False + application.save() + app.get('/applications/manifest/test/metadata/', status=404) + app.get('/applications/manifest/test/scandeps/', status=404) + app.get('/applications/manifest/test/generate/', status=404) + app.get('/applications/manifest/test/add/forms/', status=404) + app.get('/applications/manifest/test/delete/%s/' % application.relation_set.first().pk, status=404) def test_manifest_ordering(app, admin_user, settings):