From ad7848edc635ca16bf8c2051d8a8189a2b52af55 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 3 Jun 2022 11:06:49 +0200 Subject: [PATCH] add snapshotLog in old api --- .../resources/action/views/snapshot.py | 21 +++++++++++++++---- .../templates/inventory/snapshots_list.html | 2 +- tests/test_snapshot.py | 14 +++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/ereuse_devicehub/resources/action/views/snapshot.py b/ereuse_devicehub/resources/action/views/snapshot.py index 55a6065e..a038318f 100644 --- a/ereuse_devicehub/resources/action/views/snapshot.py +++ b/ereuse_devicehub/resources/action/views/snapshot.py @@ -129,6 +129,8 @@ class SnapshotView(SnapshotMixin): # snapshot, and we want to wait to flush snapshot at the end def __init__(self, snapshot_json: dict, resource_def, schema): + from ereuse_devicehub.parser.models import SnapshotsLog + self.schema = schema self.resource_def = resource_def self.tmp_snapshots = app.config['TMP_SNAPSHOTS'] @@ -136,18 +138,29 @@ class SnapshotView(SnapshotMixin): snapshot_json.pop('debug', None) try: self.snapshot_json = resource_def.schema.load(snapshot_json) - except ValidationError as err: - from ereuse_devicehub.parser.models import SnapshotsLog + snapshot = self.build() + except Exception as err: txt = "{}".format(err) uuid = snapshot_json.get('uuid') + version = snapshot_json.get('version') error = SnapshotsLog( - description=txt, snapshot_uuid=uuid, severity=Severity.Error + description=txt, + snapshot_uuid=uuid, + severity=Severity.Error, + version=str(version), ) error.save(commit=True) raise err - snapshot = self.build() db.session.add(snapshot) + snap_log = SnapshotsLog( + description='Ok', + snapshot_uuid=snapshot.uuid, + severity=Severity.Info, + version=str(snapshot.version), + snapshot=snapshot, + ) + snap_log.save() db.session().final_flush() self.response = self.schema.jsonify(snapshot) # transform it back self.response.status_code = 201 diff --git a/ereuse_devicehub/templates/inventory/snapshots_list.html b/ereuse_devicehub/templates/inventory/snapshots_list.html index 0a6084d1..a52f76c2 100644 --- a/ereuse_devicehub/templates/inventory/snapshots_list.html +++ b/ereuse_devicehub/templates/inventory/snapshots_list.html @@ -28,7 +28,7 @@ SID Snapshot id Version - Device + DHID Status Time diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index c5c16599..299ca79d 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -189,6 +189,12 @@ def test_snapshot_power_on_hours(user: UserClient): == test_data_storage.power_on_hours ) + errors = SnapshotsLog.query.filter().all() + snap_log = errors[0] + assert str(snap_log.snapshot.uuid) == snap['uuid'] + assert len(errors) == 1 + assert errors[0].description == 'Ok' + @pytest.mark.mvp def test_snapshot_component_add_remove(user: UserClient): @@ -765,6 +771,7 @@ def test_save_snapshot_with_debug(app: Devicehub, user: UserClient): @pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient): """This test check if the file snapshot is create when some snapshot is wrong""" tmp_snapshots = app.config['TMP_SNAPSHOTS'] @@ -776,6 +783,13 @@ def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient): with pytest.raises(KeyError): response = user.post(res=Snapshot, data=json_encode(snapshot_no_hid)) + errors = SnapshotsLog.query.filter().all() + snap_log = errors[0] + assert snap_log.description == "'BenchmarkProcessorr'" + assert snap_log.version == "11.0b9" + assert str(snap_log.snapshot_uuid) == '9a3e7485-fdd0-47ce-bcc7-65c55226b598' + assert len(errors) == 1 + files = [x for x in os.listdir(path_dir_base) if uuid in x] if files: path_snapshot = os.path.join(path_dir_base, files[0])