add snapshotLog in old api

This commit is contained in:
Cayo Puigdefabregas 2022-06-03 11:06:49 +02:00
parent 5fc3056907
commit ad7848edc6
3 changed files with 32 additions and 5 deletions

View File

@ -129,6 +129,8 @@ class SnapshotView(SnapshotMixin):
# snapshot, and we want to wait to flush snapshot at the end # snapshot, and we want to wait to flush snapshot at the end
def __init__(self, snapshot_json: dict, resource_def, schema): def __init__(self, snapshot_json: dict, resource_def, schema):
from ereuse_devicehub.parser.models import SnapshotsLog
self.schema = schema self.schema = schema
self.resource_def = resource_def self.resource_def = resource_def
self.tmp_snapshots = app.config['TMP_SNAPSHOTS'] self.tmp_snapshots = app.config['TMP_SNAPSHOTS']
@ -136,18 +138,29 @@ class SnapshotView(SnapshotMixin):
snapshot_json.pop('debug', None) snapshot_json.pop('debug', None)
try: try:
self.snapshot_json = resource_def.schema.load(snapshot_json) self.snapshot_json = resource_def.schema.load(snapshot_json)
except ValidationError as err: snapshot = self.build()
from ereuse_devicehub.parser.models import SnapshotsLog except Exception as err:
txt = "{}".format(err) txt = "{}".format(err)
uuid = snapshot_json.get('uuid') uuid = snapshot_json.get('uuid')
version = snapshot_json.get('version')
error = SnapshotsLog( 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) error.save(commit=True)
raise err raise err
snapshot = self.build()
db.session.add(snapshot) 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() db.session().final_flush()
self.response = self.schema.jsonify(snapshot) # transform it back self.response = self.schema.jsonify(snapshot) # transform it back
self.response.status_code = 201 self.response.status_code = 201

View File

@ -28,7 +28,7 @@
<th scope="col">SID</th> <th scope="col">SID</th>
<th scope="col">Snapshot id</th> <th scope="col">Snapshot id</th>
<th scope="col">Version</th> <th scope="col">Version</th>
<th scope="col">Device</th> <th scope="col">DHID</th>
<th scope="col">Status</th> <th scope="col">Status</th>
<th scope="col" data-type="date" data-format="DD-MM-YYYY">Time</th> <th scope="col" data-type="date" data-format="DD-MM-YYYY">Time</th>
</tr> </tr>

View File

@ -189,6 +189,12 @@ def test_snapshot_power_on_hours(user: UserClient):
== test_data_storage.power_on_hours == 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 @pytest.mark.mvp
def test_snapshot_component_add_remove(user: UserClient): 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.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient): def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient):
"""This test check if the file snapshot is create when some snapshot is wrong""" """This test check if the file snapshot is create when some snapshot is wrong"""
tmp_snapshots = app.config['TMP_SNAPSHOTS'] tmp_snapshots = app.config['TMP_SNAPSHOTS']
@ -776,6 +783,13 @@ def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient):
with pytest.raises(KeyError): with pytest.raises(KeyError):
response = user.post(res=Snapshot, data=json_encode(snapshot_no_hid)) 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] files = [x for x in os.listdir(path_dir_base) if uuid in x]
if files: if files:
path_snapshot = os.path.join(path_dir_base, files[0]) path_snapshot = os.path.join(path_dir_base, files[0])