From 785d8091ea30948ec380c6c0f228b9aeb489fba3 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 29 Oct 2020 12:39:11 +0100 Subject: [PATCH 1/4] adding new test for check the debug file --- tests/test_snapshot.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index 42792c2f..2ec4412b 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -502,6 +502,26 @@ def test_save_snapshot_in_file(app: Devicehub, user: UserClient): 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 """ + tmp_snapshots = app.config['TMP_SNAPSHOTS'] + snapshot_file = file('basic.snapshot.with_debug') + debug = snapshot_file['debug'] + user.post(res=Snapshot, data=snapshot_file) + + uuid = snapshot_file['uuid'] + files = [x for x in os.listdir(tmp_snapshots) if uuid in x] + + if files: + path_snapshot = os.path.join(tmp_snapshots, files[0]) + with open(path_snapshot) as file_snapshot: + snapshot = json.loads(file_snapshot.read()) + + os.remove(path_snapshot) + + assert snapshot['debug'] == debug + @pytest.mark.mvp def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient): From 5f68803d9f60c2e71081fa5d83bfc6811222d385 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 29 Oct 2020 16:09:02 +0100 Subject: [PATCH 2/4] adding the directory structure for save all jsons --- ereuse_devicehub/resources/action/views.py | 28 +++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index 689cb942..f16f8947 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.min 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): From ed32f691f729232d2322393e2ffb41515fc8de6d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 29 Oct 2020 16:09:38 +0100 Subject: [PATCH 3/4] fixing test for new structure and adding debug test --- tests/test_snapshot.py | 59 +++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index 2ec4412b..6b85d94d 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 @@ -483,20 +484,22 @@ 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]) 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'] @@ -505,20 +508,23 @@ def test_save_snapshot_in_file(app: Devicehub, user: UserClient): @pytest.mark.mvp def test_save_snapshot_with_debug(app: Devicehub, user: UserClient): """ This test check if works the function save_snapshot_in_file """ - tmp_snapshots = app.config['TMP_SNAPSHOTS'] snapshot_file = file('basic.snapshot.with_debug') debug = snapshot_file['debug'] user.post(res=Snapshot, data=snapshot_file) - uuid = snapshot_file['uuid'] - files = [x for x in os.listdir(tmp_snapshots) if uuid in x] + 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(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['debug'] == debug @@ -527,6 +533,7 @@ def test_save_snapshot_with_debug(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 """ 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'] @@ -534,13 +541,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'] @@ -551,6 +558,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'] @@ -558,13 +566,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'] @@ -575,6 +583,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'] @@ -582,13 +591,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'] @@ -599,6 +608,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'] @@ -606,13 +616,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'] @@ -623,6 +633,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'] @@ -630,13 +641,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'] From 2dbcd2e7e2490f84f215b9b77aa103f3e527116a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 29 Oct 2020 17:50:52 +0100 Subject: [PATCH 4/4] add the snapshot file for test of debug --- tests/files/basic.snapshot.with_debug.yaml | 126 +++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/files/basic.snapshot.with_debug.yaml 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 " +}