From b2f9ce364995acd1072b116f5f11303927c441c9 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Tue, 16 Apr 2024 14:16:08 +0200 Subject: [PATCH] refresh: log snapd response text when v2/snaps/{snap} fails Signed-off-by: Olivier Gayot --- subiquity/server/controllers/refresh.py | 12 +++++-- .../server/controllers/tests/test_refresh.py | 32 ++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/subiquity/server/controllers/refresh.py b/subiquity/server/controllers/refresh.py index 283f28c2..03ada0ff 100644 --- a/subiquity/server/controllers/refresh.py +++ b/subiquity/server/controllers/refresh.py @@ -214,9 +214,15 @@ class RefreshController(SubiquityController): @with_context() async def start_update(self, context): - change_id = await self.app.snapdapi.v2.snaps[self.snap_name].POST( - SnapActionRequest(action=SnapAction.REFRESH, ignore_running=True) - ) + try: + change_id = await self.app.snapdapi.v2.snaps[self.snap_name].POST( + SnapActionRequest(action=SnapAction.REFRESH, ignore_running=True) + ) + except requests.exceptions.HTTPError as http_err: + log.warning( + "v2/snaps/%s returned %s", self.snap_name, http_err.response.text + ) + raise context.description = "change id: {}".format(change_id) return change_id diff --git a/subiquity/server/controllers/tests/test_refresh.py b/subiquity/server/controllers/tests/test_refresh.py index 22ca38cc..b553f6fa 100644 --- a/subiquity/server/controllers/tests/test_refresh.py +++ b/subiquity/server/controllers/tests/test_refresh.py @@ -16,12 +16,14 @@ from unittest import mock import jsonschema +import requests +import requests_mock from jsonschema.validators import validator_for from subiquity.server import snapdapi from subiquity.server.controllers import refresh as refresh_mod from subiquity.server.controllers.refresh import RefreshController, SnapChannelSource -from subiquitycore.snapd import AsyncSnapd, get_fake_connection +from subiquitycore.snapd import AsyncSnapd, SnapdConnection, get_fake_connection from subiquitycore.tests import SubiTestCase from subiquitycore.tests.mocks import make_app @@ -111,3 +113,31 @@ class TestRefreshController(SubiTestCase): ) JsonValidator.check_schema(RefreshController.autoinstall_schema) + + async def test_start_update_api_error_logged(self): + self.app.snapdapi = snapdapi.make_api_client( + AsyncSnapd(SnapdConnection(root="/inexistent", sock="snapd")) + ) + json_body = { + "type": "error", + "status-code": 409, + "status": "Conflict", + "result": { + "message": 'snap "subiquity" has "update" change in progress', + }, + } + requests_mocker = requests_mock.Mocker() + requests_mocker.post( + "http+unix://snapd/v2/snaps/subiquity", + json=json_body, + status_code=409, + ) + + with requests_mocker: + with self.assertRaises(requests.exceptions.HTTPError): + with self.assertLogs( + "subiquity.server.controllers.refresh", level="WARNING" + ) as logs: + await self.rc.start_update() + + self.assertIn('snap \\"subiquity\\" has \\"update\\"', logs.output[0])