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']