diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index de99781c..2d71b4fd 100644 --- a/ereuse_devicehub/resources/action/views.py +++ b/ereuse_devicehub/resources/action/views.py @@ -2,6 +2,7 @@ import os import json +import shutil from datetime import datetime from distutils.version import StrictVersion from uuid import UUID @@ -35,10 +36,14 @@ def save_json(req_json, tmp_snapshots, user): minutes = now.minute name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{user}_{uuid}.json" - path_name = os.path.join(tmp_snapshots, name_file) + path_dir_base = os.path.join(tmp_snapshots, user) + path_errors = os.path.join(path_dir_base, 'errors') + path_fixeds = os.path.join(path_dir_base, 'fixeds') + path_name = os.path.join(path_errors, name_file) - if not os.path.isdir(tmp_snapshots): - os.system('mkdir -p {}'.format(tmp_snapshots)) + if not os.path.isdir(path_dir_base): + os.system(f'mkdir -p {path_errors}') + os.system(f'mkdir -p {path_fixeds}') with open(path_name, 'w') as snapshot_file: snapshot_file.write(json.dumps(req_json)) @@ -46,12 +51,23 @@ def save_json(req_json, tmp_snapshots, user): return path_name +def move_json(tmp_snapshots, path_name, user): + """ + This function move the json than it's correct + """ + path_dir_base = os.path.join(tmp_snapshots, user) + if os.path.isfile(path_name): + shutil.copy(path_name, path_dir_base) + os.remove(path_name) + + class ActionView(View): def post(self): """Posts an action.""" json = request.get_json(validate=False) tmp_snapshots = app.config['TMP_SNAPSHOTS'] path_snapshot = save_json(json, tmp_snapshots, g.user.email) + json.pop('debug', None) if not json or 'type' not in json: raise ValidationError('Resource needs a type.') # todo there should be a way to better get subclassess resource @@ -60,13 +76,13 @@ class ActionView(View): a = resource_def.schema.load(json) if json['type'] == Snapshot.t: response = self.snapshot(a, resource_def) - os.remove(path_snapshot) + move_json(tmp_snapshots, path_snapshot, g.user.email) return response if json['type'] == VisualTest.t: pass # TODO JN add compute rate with new visual test and old components device if json['type'] == InitTransfer.t: - os.remove(path_snapshot) + move_json(tmp_snapshots, path_snapshot, g.user.email) return self.transfer_ownership() Model = db.Model._decl_class_registry.data[json['type']]() action = Model(**a) @@ -75,7 +91,7 @@ class ActionView(View): ret = self.schema.jsonify(action) ret.status_code = 201 db.session.commit() - os.remove(path_snapshot) + move_json(tmp_snapshots, path_snapshot, g.user.email) return ret def one(self, id: UUID): diff --git a/tests/files/basic.snapshot.with_debug.yaml b/tests/files/basic.snapshot.with_debug.yaml new file mode 100644 index 00000000..0ccb912c --- /dev/null +++ b/tests/files/basic.snapshot.with_debug.yaml @@ -0,0 +1,126 @@ +{ + "elapsed": 4, + "type": "Snapshot", + "uuid": "0c822fb7-6e51-4781-86cf-994bd306212e", + "software": "Workbench", + "closed": false, + "endTime": "2018-07-05T11:57:17.284891+00:00", + "components": [ + { + "type": "NetworkAdapter", + "model": "82567LM-3 Gigabit Network Connection", + "speed": 1000, + "serialNumber": "00:23:24:0d:86:28", + "manufacturer": "Intel Corporation", + "wireless": false, + "actions": [] + }, + { + "type": "NetworkAdapter", + "model": "82541PI Gigabit Ethernet Controller", + "speed": 1000, + "serialNumber": "00:0e:0c:b6:f2:91", + "manufacturer": "Intel Corporation", + "wireless": false, + "actions": [] + }, + { + "cores": 4, + "type": "Processor", + "model": "Intel Core2 Quad CPU Q8400 @ 2.66GHz", + "speed": 1.9980000000000002, + "serialNumber": null, + "manufacturer": "Intel Corp.", + "actions": [ + { + "elapsed": 0, + "rate": 6665.7, + "type": "BenchmarkProcessor" + } + ], + "address": 64 + }, + { + "type": "GraphicCard", + "model": "4 Series Chipset Integrated Graphics Controller", + "serialNumber": null, + "manufacturer": "Intel Corporation", + "actions": [], + "memory": 256.0 + }, + { + "type": "SoundCard", + "model": "82801JD/DO HD Audio Controller", + "serialNumber": null, + "manufacturer": "Intel Corporation", + "actions": [] + }, + { + "size": 2048, + "interface": "DDR3", + "type": "RamModule", + "model": "16JTF25664AZ-1G4F", + "speed": 1333.0, + "serialNumber": "F8482E29", + "format": "DIMM", + "manufacturer": "JEDEC ID:80 2C", + "actions": [] + }, + { + "size": 2048, + "interface": "DDR3", + "type": "RamModule", + "model": "16JTF25664AZ-1G4F", + "speed": 1333.0, + "serialNumber": "62072F30", + "format": "DIMM", + "manufacturer": "JEDEC ID:80 2C", + "actions": [] + }, + { + "size": 238475, + "interface": "ATA", + "type": "HardDrive", + "model": "ST3250318AS", + "serialNumber": "9VY6HBKE", + "manufacturer": "Seagate", + "actions": [ + { + "elapsed": 0, + "type": "TestDataStorage", + "status": "Unspecified Error. Self-test not started.", + "severity": "Error", + "length": "Short" + }, + { + "type": "BenchmarkDataStorage", + "elapsed": 16, + "readSpeed": 66.2, + "writeSpeed": 21.8 + } + ] + }, + { + "slots": 0, + "pcmcia": 0, + "type": "Motherboard", + "model": "3646h", + "serialNumber": "CZC03217S7", + "firewire": 0, + "manufacturer": "Hewlett-Packard", + "actions": [], + "serial": 0, + "usb": 8 + } + ], + "version": "11.0a3", + "device": { + "type": "Desktop", + "model": "HP Compaq 8000 Elite SFF", + "chassis": "Tower", + "serialNumber": "CZC03217S7", + "manufacturer": "Hewlett-Packard", + "actions": [] + }, + "debug": "123456789 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 " +} diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index 28badb66..0c4bbf1d 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -1,5 +1,6 @@ import os import json +import shutil import pytest from datetime import datetime, timedelta, timezone @@ -518,31 +519,57 @@ def test_pc_2(user: UserClient): @pytest.mark.mvp def test_save_snapshot_in_file(app: Devicehub, user: UserClient): """ This test check if works the function save_snapshot_in_file """ - tmp_snapshots = app.config['TMP_SNAPSHOTS'] snapshot_no_hid = file('basic.snapshot.nohid') + tmp_snapshots = app.config['TMP_SNAPSHOTS'] + path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors') + save_json(snapshot_no_hid, tmp_snapshots, user.user['email']) uuid = snapshot_no_hid['uuid'] - files = [x for x in os.listdir(tmp_snapshots) if uuid in x] + files = [x for x in os.listdir(path_dir_base) if uuid in x] snapshot = {'software': '', 'version': '', 'uuid': ''} if files: - path_snapshot = os.path.join(tmp_snapshots, files[0]) + path_snapshot = os.path.join(path_dir_base, files[0]) assert not "0001-01-01 00:00:00" in path_snapshot with open(path_snapshot) as file_snapshot: snapshot = json.loads(file_snapshot.read()) - os.remove(path_snapshot) + shutil.rmtree(tmp_snapshots) assert snapshot['software'] == snapshot_no_hid['software'] assert snapshot['version'] == snapshot_no_hid['version'] assert snapshot['uuid'] == uuid +@pytest.mark.mvp +def test_save_snapshot_with_debug(app: Devicehub, user: UserClient): + """ This test check if works the function save_snapshot_in_file """ + snapshot_file = file('basic.snapshot.with_debug') + debug = snapshot_file['debug'] + user.post(res=Snapshot, data=snapshot_file) + + tmp_snapshots = app.config['TMP_SNAPSHOTS'] + path_dir_base = os.path.join(tmp_snapshots, user.user['email']) + + uuid = snapshot_file['uuid'] + files = [x for x in os.listdir(path_dir_base) if uuid in x] + + snapshot = {'debug': ''} + if files: + path_snapshot = os.path.join(path_dir_base, files[0]) + with open(path_snapshot) as file_snapshot: + snapshot = json.loads(file_snapshot.read()) + + shutil.rmtree(tmp_snapshots) + + assert snapshot['debug'] == debug + @pytest.mark.mvp 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'] + path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors') snapshot_no_hid = file('basic.snapshot.badly_formed') uuid = snapshot_no_hid['uuid'] @@ -550,13 +577,13 @@ def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient): with pytest.raises(KeyError): response = user.post(res=Snapshot, data=snapshot_no_hid) - files = [x for x in os.listdir(tmp_snapshots) if uuid in x] + files = [x for x in os.listdir(path_dir_base) if uuid in x] if files: - path_snapshot = os.path.join(tmp_snapshots, files[0]) + path_snapshot = os.path.join(path_dir_base, files[0]) with open(path_snapshot) as file_snapshot: snapshot = json.loads(file_snapshot.read()) - os.remove(path_snapshot) + shutil.rmtree(tmp_snapshots) assert snapshot['software'] == snapshot_no_hid['software'] assert snapshot['version'] == snapshot_no_hid['version'] @@ -567,6 +594,7 @@ def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient): def test_snapshot_failed_missing_cpu_benchmark(app: Devicehub, user: UserClient): """ This test check if the file snapshot is create when some snapshot is wrong """ tmp_snapshots = app.config['TMP_SNAPSHOTS'] + path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors') snapshot_error = file('failed.snapshot.500.missing-cpu-benchmark') uuid = snapshot_error['uuid'] @@ -574,13 +602,13 @@ def test_snapshot_failed_missing_cpu_benchmark(app: Devicehub, user: UserClient) with pytest.raises(TypeError): user.post(res=Snapshot, data=snapshot_error) - files = [x for x in os.listdir(tmp_snapshots) if uuid in x] + files = [x for x in os.listdir(path_dir_base) if uuid in x] if files: - path_snapshot = os.path.join(tmp_snapshots, files[0]) + path_snapshot = os.path.join(path_dir_base, files[0]) with open(path_snapshot) as file_snapshot: snapshot = json.loads(file_snapshot.read()) - os.remove(path_snapshot) + shutil.rmtree(tmp_snapshots) assert snapshot['software'] == snapshot_error['software'] assert snapshot['version'] == snapshot_error['version'] @@ -591,6 +619,7 @@ def test_snapshot_failed_missing_cpu_benchmark(app: Devicehub, user: UserClient) def test_snapshot_failed_missing_hdd_benchmark(app: Devicehub, user: UserClient): """ This test check if the file snapshot is create when some snapshot is wrong """ tmp_snapshots = app.config['TMP_SNAPSHOTS'] + path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors') snapshot_error = file('failed.snapshot.500.missing-hdd-benchmark') uuid = snapshot_error['uuid'] @@ -598,13 +627,13 @@ def test_snapshot_failed_missing_hdd_benchmark(app: Devicehub, user: UserClient) with pytest.raises(TypeError): user.post(res=Snapshot, data=snapshot_error) - files = [x for x in os.listdir(tmp_snapshots) if uuid in x] + files = [x for x in os.listdir(path_dir_base) if uuid in x] if files: - path_snapshot = os.path.join(tmp_snapshots, files[0]) + path_snapshot = os.path.join(path_dir_base, files[0]) with open(path_snapshot) as file_snapshot: snapshot = json.loads(file_snapshot.read()) - os.remove(path_snapshot) + shutil.rmtree(tmp_snapshots) assert snapshot['software'] == snapshot_error['software'] assert snapshot['version'] == snapshot_error['version'] @@ -615,6 +644,7 @@ def test_snapshot_failed_missing_hdd_benchmark(app: Devicehub, user: UserClient) def test_snapshot_failed_null_chassis(app: Devicehub, user: UserClient): """ This test check if the file snapshot is create when some snapshot is wrong """ tmp_snapshots = app.config['TMP_SNAPSHOTS'] + path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors') snapshot_error = file('failed.snapshot.422.null-chassis') uuid = snapshot_error['uuid'] @@ -622,13 +652,13 @@ def test_snapshot_failed_null_chassis(app: Devicehub, user: UserClient): with pytest.raises(TypeError): user.post(res=Snapshot, data=snapshot_error) - files = [x for x in os.listdir(tmp_snapshots) if uuid in x] + files = [x for x in os.listdir(path_dir_base) if uuid in x] if files: - path_snapshot = os.path.join(tmp_snapshots, files[0]) + path_snapshot = os.path.join(path_dir_base, files[0]) with open(path_snapshot) as file_snapshot: snapshot = json.loads(file_snapshot.read()) - os.remove(path_snapshot) + shutil.rmtree(tmp_snapshots) assert snapshot['software'] == snapshot_error['software'] assert snapshot['version'] == snapshot_error['version'] @@ -639,6 +669,7 @@ def test_snapshot_failed_null_chassis(app: Devicehub, user: UserClient): def test_snapshot_failed_missing_chassis(app: Devicehub, user: UserClient): """ This test check if the file snapshot is create when some snapshot is wrong """ tmp_snapshots = app.config['TMP_SNAPSHOTS'] + path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors') snapshot_error = file('failed.snapshot.422.missing-chassis') uuid = snapshot_error['uuid'] @@ -646,13 +677,13 @@ def test_snapshot_failed_missing_chassis(app: Devicehub, user: UserClient): with pytest.raises(TypeError): user.post(res=Snapshot, data=snapshot_error) - files = [x for x in os.listdir(tmp_snapshots) if uuid in x] + files = [x for x in os.listdir(path_dir_base) if uuid in x] if files: - path_snapshot = os.path.join(tmp_snapshots, files[0]) + path_snapshot = os.path.join(path_dir_base, files[0]) with open(path_snapshot) as file_snapshot: snapshot = json.loads(file_snapshot.read()) - os.remove(path_snapshot) + shutil.rmtree(tmp_snapshots) assert snapshot['software'] == snapshot_error['software'] assert snapshot['version'] == snapshot_error['version']